Bugfixes concerning selecting values in the LineChart.

This commit is contained in:
Philipp Jahoda 2014-05-28 13:26:25 +02:00
parent 5e16518614
commit 1732d2d376
9 changed files with 236 additions and 183 deletions

View file

@ -1,8 +1,10 @@
package com.example.mpchartexample;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
@ -11,179 +13,208 @@ import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
import com.github.mikephil.charting.Approximator;
import com.github.mikephil.charting.ChartData;
import com.github.mikephil.charting.DataSet;
import com.github.mikephil.charting.Highlight;
import com.github.mikephil.charting.LineChart;
import com.github.mikephil.charting.OnChartValueSelectedListener;
import com.github.mikephil.charting.Series;
import com.github.mikephil.charting.Approximator.ApproximatorType;
import java.util.ArrayList;
public class LineChartActivity extends Activity implements OnSeekBarChangeListener {
public class LineChartActivity extends Activity implements OnSeekBarChangeListener,
OnChartValueSelectedListener {
private LineChart mChart;
private SeekBar mSeekBarX, mSeekBarY;
private TextView tvX, tvY;
private LineChart mChart;
private SeekBar mSeekBarX, mSeekBarY;
private TextView tvX, tvY;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_linechart);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_linechart);
tvX = (TextView) findViewById(R.id.tvXMax);
tvY = (TextView) findViewById(R.id.tvYMax);
tvX = (TextView) findViewById(R.id.tvXMax);
tvY = (TextView) findViewById(R.id.tvYMax);
mSeekBarX = (SeekBar) findViewById(R.id.seekBar1);
mSeekBarX.setOnSeekBarChangeListener(this);
mSeekBarX = (SeekBar) findViewById(R.id.seekBar1);
mSeekBarX.setOnSeekBarChangeListener(this);
mSeekBarY = (SeekBar) findViewById(R.id.seekBar2);
mSeekBarY.setOnSeekBarChangeListener(this);
mSeekBarY = (SeekBar) findViewById(R.id.seekBar2);
mSeekBarY.setOnSeekBarChangeListener(this);
mChart = (LineChart) findViewById(R.id.chart1);
// mChart.setColorTemplate(new ColorTemplate(ColorTemplate.getColors(this, ColorTemplate.LIBERTY_COLORS)));
mChart = (LineChart) findViewById(R.id.chart1);
mChart.setOnChartValueSelectedListener(this);
// mChart.setColorTemplate(new
// ColorTemplate(ColorTemplate.getColors(this,
// ColorTemplate.LIBERTY_COLORS)));
// mChart.setDrawFilled(true);
// mChart.setRoundedYLegend(false);
// mChart.setStartAtZero(true);
mChart.setDrawYValues(false);
mChart.setLineWidth(5f);
mChart.setCircleSize(5f);
// mChart.setSpacePercent(20, 10);
mChart.setYLegendCount(6);
mChart.setTouchEnabled(true);
mChart.setHighlightEnabled(true);
// mChart.highlightValues(new int[] {2, 6});
mChart.setDragEnabled(true);
mChart.setTouchEnabled(true);
// mChart.setDrawFilled(true);
// mChart.setRoundedYLegend(false);
// mChart.setStartAtZero(true);
mChart.setDrawYValues(false);
mChart.setLineWidth(5f);
mChart.setCircleSize(5f);
// mChart.setSpacePercent(20, 10);
mChart.setYLegendCount(6);
mChart.setTouchEnabled(true);
mChart.setHighlightEnabled(true);
// mChart.highlightValues(new int[] {2, 6});
mChart.setDragEnabled(true);
mChart.setTouchEnabled(true);
TextView textView = new TextView(this);
textView.setVisibility(View.VISIBLE);
textView.setBackgroundColor(Color.WHITE);
textView.setPadding(15, 15, 15, 15);
textView.setText("Marker View");
TextView textView = new TextView(this);
textView.setVisibility(View.VISIBLE);
textView.setBackgroundColor(Color.WHITE);
textView.setPadding(15, 15, 15, 15);
textView.setText("Marker View");
mChart.setDrawMarkerView(true);
mChart.setMarkerView(textView);
mChart.setDrawMarkerView(true);
mChart.setMarkerView(textView);
mSeekBarX.setProgress(45);
mSeekBarY.setProgress(100);
}
mSeekBarX.setProgress(45);
mSeekBarY.setProgress(100);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.line, menu);
return true;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.line, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.actionToggleRound: {
if (mChart.isYLegendRounded())
mChart.setRoundedYLegend(false);
else
mChart.setRoundedYLegend(true);
mChart.invalidate();
break;
}
case R.id.actionToggleValues: {
if (mChart.isDrawYValuesEnabled())
mChart.setDrawYValues(false);
else
mChart.setDrawYValues(true);
mChart.invalidate();
break;
}
case R.id.actionToggleHighlight: {
if (mChart.isHighlightEnabled())
mChart.setHighlightEnabled(false);
else
mChart.setHighlightEnabled(true);
mChart.invalidate();
break;
}
case R.id.actionToggleFilled: {
if (mChart.isDrawFilledEnabled())
mChart.setDrawFilled(false);
else
mChart.setDrawFilled(true);
mChart.invalidate();
break;
}
case R.id.actionToggleCircles: {
if (mChart.isDrawCirclesEnabled())
mChart.setDrawCircles(false);
else
mChart.setDrawCircles(true);
mChart.invalidate();
break;
}
case R.id.actionToggleStartzero: {
if (mChart.isStartAtZeroEnabled())
mChart.setStartAtZero(false);
else
mChart.setStartAtZero(true);
switch (item.getItemId()) {
case R.id.actionToggleRound: {
if (mChart.isYLegendRounded())
mChart.setRoundedYLegend(false);
else
mChart.setRoundedYLegend(true);
mChart.invalidate();
break;
}
case R.id.actionToggleValues: {
if (mChart.isDrawYValuesEnabled())
mChart.setDrawYValues(false);
else
mChart.setDrawYValues(true);
mChart.invalidate();
break;
}
case R.id.actionToggleHighlight: {
if (mChart.isHighlightEnabled())
mChart.setHighlightEnabled(false);
else
mChart.setHighlightEnabled(true);
mChart.invalidate();
break;
}
case R.id.actionToggleFilled: {
if (mChart.isDrawFilledEnabled())
mChart.setDrawFilled(false);
else
mChart.setDrawFilled(true);
mChart.invalidate();
break;
}
case R.id.actionToggleCircles: {
if (mChart.isDrawCirclesEnabled())
mChart.setDrawCircles(false);
else
mChart.setDrawCircles(true);
mChart.invalidate();
break;
}
case R.id.actionToggleStartzero: {
if (mChart.isStartAtZeroEnabled())
mChart.setStartAtZero(false);
else
mChart.setStartAtZero(true);
mChart.invalidate();
break;
}
case R.id.actionToggleAdjustXLegend: {
if (mChart.isAdjustXLegendEnabled())
mChart.setAdjustXLegend(false);
else
mChart.setAdjustXLegend(true);
mChart.invalidate();
break;
}
case R.id.actionToggleAdjustXLegend: {
if (mChart.isAdjustXLegendEnabled())
mChart.setAdjustXLegend(false);
else
mChart.setAdjustXLegend(true);
mChart.invalidate();
break;
}
case R.id.actionSave: {
// mChart.saveToGallery("title"+System.currentTimeMillis());
mChart.saveToPath("title" + System.currentTimeMillis(), "");
break;
}
}
return true;
}
mChart.invalidate();
break;
}
case R.id.actionSave: {
// mChart.saveToGallery("title"+System.currentTimeMillis());
mChart.saveToPath("title" + System.currentTimeMillis(), "");
break;
}
}
return true;
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
ArrayList<String> xVals = new ArrayList<String>();
for (int i = 0; i < mSeekBarX.getProgress(); i++) {
xVals.add((i) + "");
}
ArrayList<String> xVals = new ArrayList<String>();
for (int i = 0; i < mSeekBarX.getProgress(); i++) {
xVals.add((i) + "");
}
ArrayList<Series> yVals = new ArrayList<Series>();
ArrayList<Series> yVals = new ArrayList<Series>();
for (int i = 0; i < mSeekBarX.getProgress(); i++) {
float mult = (mSeekBarY.getProgress() + 1);
float val = (float) (Math.random() * mult * 0.1) + 3;// + (float) ((mult * 0.1) / 10);
yVals.add(new Series(val, i));
}
for (int i = 0; i < mSeekBarX.getProgress(); i++) {
float mult = (mSeekBarY.getProgress() + 1);
float val = (float) (Math.random() * mult * 0.1) + 3;// + (float)
// ((mult *
// 0.1) / 10);
yVals.add(new Series(val, i));
}
tvX.setText("" + (mSeekBarX.getProgress() + 1));
tvY.setText("" + (mSeekBarY.getProgress() / 10));
DataSet set = new DataSet(yVals, 0);
ArrayList<DataSet> dataSets = new ArrayList<DataSet>();
dataSets.add(set);
tvX.setText("" + (mSeekBarX.getProgress() + 1));
tvY.setText("" + (mSeekBarY.getProgress() / 10));
ChartData data = new ChartData(xVals, dataSets);
// Approximator a = new Approximator(ApproximatorType.DOUGLAS_PEUCKER);
// ArrayList<Series> filtered = a.filter(yVals, 1.0);
mChart.setData(data);
mChart.invalidate();
}
DataSet set1 = new DataSet(yVals, 0);
// DataSet set2 = new DataSet(filtered, 1);
ArrayList<DataSet> dataSets = new ArrayList<DataSet>();
dataSets.add(set1);
// dataSets.add(set2);
ChartData data = new ChartData(xVals, dataSets);
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
mChart.setData(data);
mChart.invalidate();
}
}
@Override
public void onValuesSelected(Series[] values, Highlight[] highlights) {
Log.i("VALS SELECTED",
"Value: " + values[0].getVal() + ", xIndex: " + highlights[0].getXIndex()
+ ", DataSet index: " + highlights[0].getDataSetIndex());
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
@Override
public void onNothingSelected() {
// TODO Auto-generated method stub
}
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
}

View file

@ -12,14 +12,13 @@ import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
import com.github.mikephil.charting.Approximator;
import com.github.mikephil.charting.Approximator.ApproximatorType;
import com.github.mikephil.charting.ChartData;
import com.github.mikephil.charting.ColorTemplate;
import com.github.mikephil.charting.DataSet;
import com.github.mikephil.charting.Highlight;
import com.github.mikephil.charting.LineChart;
import com.github.mikephil.charting.OnChartValueSelectedListener;
import com.github.mikephil.charting.Series;
import java.util.ArrayList;
@ -195,8 +194,8 @@ public class MultiLineChartActivity extends Activity implements OnSeekBarChangeL
}
@Override
public void onValuesSelected(float[] values, Highlight[] highlights) {
Log.i("VALS SELECTED", "Value: " + values[0] + ", xIndex: " + highlights[0].getXIndex() + ", DataSet index: " + highlights[0].getDataSetIndex());
public void onValuesSelected(Series[] values, Highlight[] highlights) {
Log.i("VALS SELECTED", "Value: " + values[0].getVal() + ", xIndex: " + highlights[0].getXIndex() + ", DataSet index: " + highlights[0].getDataSetIndex());
}
@Override

View file

@ -10,6 +10,8 @@ import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
import com.github.mikephil.charting.Approximator;
import com.github.mikephil.charting.Approximator.ApproximatorType;
import com.github.mikephil.charting.ChartData;
import com.github.mikephil.charting.ColorTemplate;
import com.github.mikephil.charting.DataSet;
@ -140,9 +142,11 @@ public class PieChartActivity extends Activity implements OnSeekBarChangeListene
for (int i = 0; i < yVals.size(); i++)
xVals.add("Text" + (i + 1));
DataSet set = new DataSet(yVals, 0);
ArrayList<DataSet> dataSets = new ArrayList<DataSet>();
dataSets.add(set);
ChartData data = new ChartData(xVals, dataSets);
@ -152,12 +156,12 @@ public class PieChartActivity extends Activity implements OnSeekBarChangeListene
}
@Override
public void onValuesSelected(float[] values, Highlight[] highs) {
public void onValuesSelected(Series[] values, Highlight[] highs) {
StringBuffer a = new StringBuffer();
for (int i = 0; i < values.length; i++)
a.append("val: " + values[i] + ", x-ind: " + highs[i].getXIndex() + ", dataset-ind: " + highs[i].getDataSetIndex() + "\n");
a.append("val: " + values[i].getVal() + ", x-ind: " + highs[i].getXIndex() + ", dataset-ind: " + highs[i].getDataSetIndex() + "\n");
Log.i("PieChart", "Selected: " + a.toString());
}

View file

@ -21,12 +21,12 @@ public class Approximator {
this.type = type;
}
public ArrayList<Series> filter(ArrayList<Series> points) {
public ArrayList<Series> filter(ArrayList<Series> points, double tolerance) {
keep = new boolean[points.size()];
switch (type) {
case DOUGLAS_PEUCKER:
return reduceWithDouglasPeuker(points, 1);
return reduceWithDouglasPeuker(points, tolerance);
case NONE:
return null;
default:

View file

@ -903,9 +903,9 @@ public abstract class BarLineChartBase extends Chart {
// touch out of chart
if (this instanceof LineChart
&& (xTouchVal < 0 || xTouchVal > getValueCount() - 1))
&& (xTouchVal < 0 || xTouchVal > mDeltaX))
return null;
if (this instanceof BarChart && (xTouchVal < 0 || xTouchVal > getValueCount()))
if (this instanceof BarChart && (xTouchVal < 0 || xTouchVal > mDeltaX+1))
return null;
int xIndex = (int) base;
@ -922,6 +922,8 @@ public abstract class BarLineChartBase extends Chart {
ArrayList<Float> valsAtIndex = getYValsAtIndex(xIndex);
yIndex = getClosestDataSetIndex(valsAtIndex, (float) yTouchVal);
if(yIndex == -1) return null;
return new Highlight(xIndex, yIndex);
}
@ -934,7 +936,7 @@ public abstract class BarLineChartBase extends Chart {
*/
private int getClosestDataSetIndex(ArrayList<Float> valsAtIndex, float val) {
int index = 0;
int index = -1;
float distance = Float.MAX_VALUE;
for (int c = 0; c < valsAtIndex.size(); c++) {
@ -945,6 +947,8 @@ public abstract class BarLineChartBase extends Chart {
distance = cdistance;
}
}
Log.i(LOG_TAG, "Closest select index: " + index);
return index;
}

View file

@ -634,7 +634,7 @@ public abstract class Chart extends View {
* @return
*/
public boolean valuesToHighlight() {
return mIndicesToHightlight == null || mIndicesToHightlight.length == 0
return mIndicesToHightlight == null || mIndicesToHightlight.length <= 0
|| mIndicesToHightlight[0] == null ? false
: true;
}
@ -658,10 +658,10 @@ public abstract class Chart extends View {
mSelectionListener.onNothingSelected();
else {
float[] values = new float[highs.length];
Series[] values = new Series[highs.length];
for (int i = 0; i < values.length; i++)
values[i] = getYValueByDataSetIndex(highs[i].getXIndex(),
values[i] = getSeriesByDataSetIndex(highs[i].getXIndex(),
highs[i].getDataSetIndex());
// notify the listener
@ -1189,7 +1189,8 @@ public abstract class Chart extends View {
/**
* returns the Series object from the first DataSet stored in the ChartData
* object. If multiple DataSets are used, use getSeries(index, type);
* object. If multiple DataSets are used, use getSeries(index, type) or
* getSeriesByDataSetIndex(xIndex, dataSetIndex);
*
* @param index
* @return
@ -1211,8 +1212,22 @@ public abstract class Chart extends View {
}
/**
* get the y-values from the Series object at the given index across all
* DataSets
* Returns the corresponding Seires object at the given xIndex from the
* given DataSet. INFORMATION: This method does calculations at runtime. Do
* not over-use in performance critical situations.
*
* @param xIndex
* @param dataSetIndex
* @return
*/
public Series getSeriesByDataSetIndex(int xIndex, int dataSetIndex) {
return mData.getDataSetByIndex(dataSetIndex).getSeriesForXIndex(xIndex);
}
/**
* Get the y-values from the Series object at the given index across all
* DataSets. INFORMATION: This method does calculations at runtime. Do not
* over-use in performance critical situations.
*
* @param xIndex
* @return

View file

@ -82,7 +82,8 @@ public class DataSet {
}
/**
* returns the value of the Series object at the given xIndex
* Returns the value of the Series object at the given xIndex. Returns
* Float.NaN if no value is at the given x-index. INFORMATION: This method does calculations at runtime. Do not over-use in performance critical situations.
*
* @param xIndex
* @return
@ -90,13 +91,15 @@ public class DataSet {
public float getYValForXIndex(int xIndex) {
Series s = getSeriesForXIndex(xIndex);
if(s != null) return s.getVal();
else return Float.NaN;
if (s != null)
return s.getVal();
else
return Float.NaN;
}
/**
* returns the Series object at the given xIndex
* Returns the Series object at the given xIndex. INFORMATION: This method does calculations at runtime. Do not over-use in performance critical situations.
*
* @param xIndex
* @return

View file

@ -1,16 +1,15 @@
package com.github.mikephil.charting;
import java.util.ArrayList;
import android.content.Context;
import android.graphics.Canvas.VertexMode;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Path;
import android.util.AttributeSet;
import java.util.ArrayList;
public class LineChart extends BarLineChartBase {
/** the radius of the circle-shaped value indicators */
@ -90,22 +89,20 @@ public class LineChart extends BarLineChartBase {
if (mHighlightEnabled && valuesToHighlight()) {
for (int i = 0; i < mIndicesToHightlight.length; i++) {
int xIndex = mIndicesToHightlight[i].getXIndex();
DataSet set = getDataSetByIndex(mIndicesToHightlight[i].getDataSetIndex());
int xIndex = mIndicesToHightlight[i].getXIndex(); // get the x-position
float y = set.getYValForXIndex(xIndex); // get the y-position
// check outofbounds
if (xIndex < set.getSeriesCount() && xIndex >= 0) {
float[] pts = new float[] {
xIndex, mYChartMax, xIndex, mYChartMin, 0,
y, mDeltaX, y
};
float[] pts = new float[] {
xIndex, mYChartMax, xIndex, mYChartMin, 0,
set.getYValForXIndex(xIndex), mDeltaX, set.getYValForXIndex(xIndex)
};
transformPointArray(pts);
// draw the highlight lines
mDrawCanvas.drawLines(pts, mHighlightPaint);
}
transformPointArray(pts);
// draw the highlight lines
mDrawCanvas.drawLines(pts, mHighlightPaint);
}
}
}

View file

@ -2,6 +2,6 @@ package com.github.mikephil.charting;
public interface OnChartValueSelectedListener {
public void onValuesSelected(float[] values, Highlight[] highlights);
public void onValuesSelected(Series[] values, Highlight[] highlights);
public void onNothingSelected();
}