Fixed a highlighting bug for Line and BarChart. Barchart now supports multiple DataSets.

This commit is contained in:
Philipp Jahoda 2014-05-28 16:08:17 +02:00
parent 1732d2d376
commit 256c028a3e
6 changed files with 115 additions and 24 deletions

View file

@ -143,13 +143,20 @@ public class BarChartActivity extends Activity implements OnSeekBarChangeListene
xVals.add((i) + "");
}
ArrayList<Series> yVals = new ArrayList<Series>();
ArrayList<Series> yVals1 = new ArrayList<Series>();
// ArrayList<Series> yVals2 = 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));
yVals1.add(new Series(val, i));
}
// for (int i = mSeekBarX.getProgress() / 2; i < mSeekBarX.getProgress(); i++) {
// float mult = (mSeekBarY.getProgress() + 1);
// float val = (float) (Math.random() * mult * 0.1) + 3;// + (float) ((mult * 0.1) / 10);
// yVals2.add(new Series(val, i));
// }
tvX.setText("" + (mSeekBarX.getProgress() + 1));
tvY.setText("" + (mSeekBarY.getProgress() / 10));
@ -157,9 +164,11 @@ public class BarChartActivity extends Activity implements OnSeekBarChangeListene
// Approximator approximator = new Approximator(ApproximatorType.DOUGLAS_PEUCKER);
// ArrayList<Series> filtered = approximator.filter(yVals);
DataSet set = new DataSet(yVals, 0);
DataSet set1 = new DataSet(yVals1, 0);
// DataSet set2 = new DataSet(yVals2, 1);
ArrayList<DataSet> dataSets = new ArrayList<DataSet>();
dataSets.add(set);
dataSets.add(set1);
// dataSets.add(set2);
ChartData data = new ChartData(xVals, dataSets);

View file

@ -135,7 +135,7 @@ public class BarChart extends BarLineChartBase {
mHighlightPaint.setAlpha(120);
float y = getYValue(index);
float y = getYValueByDataSetIndex(index, mIndicesToHightlight[i].getDataSetIndex());
float left = index + mBarSpace / 2f;
float right = index + 1f - mBarSpace / 2f;
float top = y >= 0 ? y : 0;
@ -243,11 +243,15 @@ public class BarChart extends BarLineChartBase {
DataSet dataSet = dataSets.get(i);
ArrayList<Series> series = dataSet.getYVals();
// get the color for the dataset
Paint paint = mDrawPaints[i % mDrawPaints.length];
// do the drawing
for (int j = 0; j < dataSet.getSeriesCount(); j++) {
Paint paint = mDrawPaints[j % mDrawPaints.length];
// if only one DataSet exists, switch colors inside dataset
if(mData.getDataSetCount() == 1) paint = mDrawPaints[j % mDrawPaints.length];
int x = series.get(j).getXIndex();
float y = series.get(j).getVal();

View file

@ -919,7 +919,7 @@ public abstract class BarLineChartBase extends Chart {
}
}
ArrayList<Float> valsAtIndex = getYValsAtIndex(xIndex);
ArrayList<SelInfo> valsAtIndex = getYValsAtIndex(xIndex);
yIndex = getClosestDataSetIndex(valsAtIndex, (float) yTouchVal);
@ -934,21 +934,21 @@ public abstract class BarLineChartBase extends Chart {
* @param valsAtIndex
* @return
*/
private int getClosestDataSetIndex(ArrayList<Float> valsAtIndex, float val) {
private int getClosestDataSetIndex(ArrayList<SelInfo> valsAtIndex, float val) {
int index = -1;
float distance = Float.MAX_VALUE;
for (int c = 0; c < valsAtIndex.size(); c++) {
for (int i = 0; i < valsAtIndex.size(); i++) {
float cdistance = Math.abs((float) valsAtIndex.get(c) - val);
float cdistance = Math.abs((float) valsAtIndex.get(i).val - val);
if (cdistance < distance) {
index = c;
index = valsAtIndex.get(i).dataSetIndex;
distance = cdistance;
}
}
Log.i(LOG_TAG, "Closest select index: " + index);
Log.i(LOG_TAG, "Closest DataSet index: " + index);
return index;
}

View file

@ -1160,9 +1160,9 @@ public abstract class Chart extends View {
* @param dataSet
* @return
*/
public float getYValueByDataSetIndex(int index, int dataSet) {
public float getYValueByDataSetIndex(int xIndex, int dataSet) {
DataSet set = mData.getDataSetByIndex(dataSet);
return set.getYVals().get(index).getVal();
return set.getYValForXIndex(xIndex);
}
/**
@ -1225,24 +1225,52 @@ public abstract class Chart extends View {
}
/**
* 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.
* Returns an array of SelInfo objects for the given x-index. The SelInfo
* objects give information about the value at the selected index and the
* DataSet it belongs to. INFORMATION: This method does calculations at
* runtime. Do not over-use in performance critical situations.
*
* @param xIndex
* @return
*/
public ArrayList<Float> getYValsAtIndex(int xIndex) {
protected ArrayList<SelInfo> getYValsAtIndex(int xIndex) {
ArrayList<Float> vals = new ArrayList<Float>();
ArrayList<SelInfo> vals = new ArrayList<SelInfo>();
for (int i = 0; i < mData.getDataSetCount(); i++) {
// extract all y-values from all DataSets at the given x-index
float yVal = mData.getDataSetByIndex(i).getYValForXIndex(xIndex);
if (!Float.isNaN(yVal))
vals.add(yVal);
if (!Float.isNaN(yVal)) {
vals.add(new SelInfo(yVal, i));
}
}
return vals;
}
/**
* Get all Series objects 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
*/
public ArrayList<Series> getSeriesAtIndex(int xIndex) {
ArrayList<Series> vals = new ArrayList<Series>();
for (int i = 0; i < mData.getDataSetCount(); i++) {
DataSet set = mData.getDataSetByIndex(i);
Series s = set.getSeriesForXIndex(xIndex);
if (s != null) {
vals.add(s);
}
}
return vals;

View file

@ -83,7 +83,9 @@ public class DataSet {
/**
* 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.
* 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
@ -99,7 +101,9 @@ public class DataSet {
}
/**
* Returns the Series object at the given xIndex. INFORMATION: This method does calculations at runtime. Do not over-use in performance critical situations.
* Returns the Series object at the given xIndex. Returns null if no Series
* object at that index. INFORMATION: This method does calculations at
* runtime. Do not over-use in performance critical situations.
*
* @param xIndex
* @return
@ -114,22 +118,47 @@ public class DataSet {
return null;
}
/**
* returns the DataSets Series array
*
* @return
*/
public ArrayList<Series> getYVals() {
return mYVals;
}
/**
* gets the sum of all y-values
*
* @return
*/
public float getYValueSum() {
return mYValueSum;
}
/**
* returns the minimum y-value this DataSet holds
*
* @return
*/
public float getYMin() {
return mYMin;
}
/**
* returns the maximum y-value this DataSet holds
*
* @return
*/
public float getYMax() {
return mYMax;
}
/**
* returns the type of the DataSet, specified via constructor
*
* @return
*/
public int getType() {
return mType;
}

View file

@ -0,0 +1,21 @@
package com.github.mikephil.charting;
/**
* Class that encapsulates information of a value that has been
* selected/highlighted and its DataSet index. The SelInfo objects give
* information about the value at the selected index and the DataSet it belongs
* to. Needed only for highlighting onTouch().
*
* @author Philipp Jahoda
*/
public class SelInfo {
public float val;
public int dataSetIndex;
public SelInfo(float val, int dataSetIndex) {
this.val = val;
this.dataSetIndex = dataSetIndex;
}
}