Introduced the DataSet class. This class is used to wrap all data in the chart that belongs together. For example an individual line in the LineChart.
This commit is contained in:
parent
846f5cca2d
commit
2895a014b8
14 changed files with 1643 additions and 1375 deletions
|
@ -1,7 +1,5 @@
|
|||
package com.example.mpchartexample;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.os.Bundle;
|
||||
import android.view.Menu;
|
||||
|
@ -14,8 +12,11 @@ import android.widget.TextView;
|
|||
import com.github.mikephil.charting.BarChart;
|
||||
import com.github.mikephil.charting.ChartData;
|
||||
import com.github.mikephil.charting.ColorTemplate;
|
||||
import com.github.mikephil.charting.DataSet;
|
||||
import com.github.mikephil.charting.Series;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class BarChartActivity extends Activity implements OnSeekBarChangeListener {
|
||||
|
||||
private BarChart mChart;
|
||||
|
@ -151,7 +152,11 @@ public class BarChartActivity extends Activity implements OnSeekBarChangeListene
|
|||
tvX.setText("" + (mSeekBarX.getProgress() + 1));
|
||||
tvY.setText("" + (mSeekBarY.getProgress() / 10));
|
||||
|
||||
ChartData data = new ChartData(xVals, yVals);
|
||||
DataSet set = new DataSet(yVals, 0);
|
||||
ArrayList<DataSet> dataSets = new ArrayList<DataSet>();
|
||||
dataSets.add(set);
|
||||
|
||||
ChartData data = new ChartData(xVals, dataSets);
|
||||
|
||||
mChart.setData(data);
|
||||
mChart.invalidate();
|
||||
|
|
|
@ -1,7 +1,5 @@
|
|||
package com.example.mpchartexample;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.graphics.Color;
|
||||
import android.os.Bundle;
|
||||
|
@ -14,9 +12,12 @@ import android.widget.SeekBar.OnSeekBarChangeListener;
|
|||
import android.widget.TextView;
|
||||
|
||||
import com.github.mikephil.charting.ChartData;
|
||||
import com.github.mikephil.charting.DataSet;
|
||||
import com.github.mikephil.charting.LineChart;
|
||||
import com.github.mikephil.charting.Series;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class LineChartActivity extends Activity implements OnSeekBarChangeListener {
|
||||
|
||||
private LineChart mChart;
|
||||
|
@ -163,8 +164,12 @@ public class LineChartActivity extends Activity implements OnSeekBarChangeListen
|
|||
|
||||
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);
|
||||
|
||||
ChartData data = new ChartData(xVals, yVals);
|
||||
ChartData data = new ChartData(xVals, dataSets);
|
||||
|
||||
mChart.setData(data);
|
||||
mChart.invalidate();
|
||||
|
|
|
@ -15,6 +15,7 @@ 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.LineChart;
|
||||
import com.github.mikephil.charting.Point;
|
||||
import com.github.mikephil.charting.Series;
|
||||
|
@ -156,36 +157,36 @@ public class MultiLineChartActivity extends Activity implements OnSeekBarChangeL
|
|||
xVals.add((i) + "");
|
||||
}
|
||||
|
||||
ArrayList<Series> yVals = new ArrayList<Series>();
|
||||
ArrayList<Double[]> values = new ArrayList<Double[]>();
|
||||
|
||||
Approximator approximator = new Approximator(ApproximatorType.DOUGLAS_PEUCKER);
|
||||
|
||||
for (int z = 0; z < 1; z++) {
|
||||
for (int z = 0; z < 3; z++) {
|
||||
|
||||
Double[] vals = new Double[mSeekBarX.getProgress()];
|
||||
|
||||
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, z, i));
|
||||
double val = (Math.random() * mult * 0.1) + 3;// + (float) ((mult * 0.1) / 10);
|
||||
vals[i] = val;
|
||||
}
|
||||
|
||||
values.add(vals);
|
||||
}
|
||||
|
||||
ArrayList<Series> filtered = approximator.filter(yVals);
|
||||
|
||||
for (int i = 0; i < filtered.size(); i++) {
|
||||
filtered.get(i).setType(1);
|
||||
}
|
||||
// yVals.addAll(filtered);
|
||||
|
||||
// for (int z = 0; z < 5; z++) {
|
||||
// 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, z, i));
|
||||
// }
|
||||
// }
|
||||
// ArrayList<Series> filtered = approximator.filter(yVals1);
|
||||
//
|
||||
// for (int i = 0; i < filtered.size(); i++) {
|
||||
// filtered.get(i).setType(1);
|
||||
// }
|
||||
|
||||
tvX.setText("" + (mSeekBarX.getProgress() + 1));
|
||||
tvY.setText("" + (mSeekBarY.getProgress() / 10));
|
||||
|
||||
ArrayList<DataSet> dataSets = new ArrayList<DataSet>();
|
||||
dataSets.addAll(DataSet.makeDataSets(values));
|
||||
|
||||
ChartData data = new ChartData(xVals, filtered);
|
||||
ChartData data = new ChartData(xVals, dataSets);
|
||||
|
||||
mChart.setData(data);
|
||||
mChart.invalidate();
|
||||
|
|
|
@ -14,6 +14,7 @@ import android.widget.TextView;
|
|||
|
||||
import com.github.mikephil.charting.ChartData;
|
||||
import com.github.mikephil.charting.ColorTemplate;
|
||||
import com.github.mikephil.charting.DataSet;
|
||||
import com.github.mikephil.charting.OnChartValueSelectedListener;
|
||||
import com.github.mikephil.charting.PieChart;
|
||||
import com.github.mikephil.charting.Series;
|
||||
|
@ -139,7 +140,11 @@ public class PieChartActivity extends Activity implements OnSeekBarChangeListene
|
|||
for (int i = 0; i < yVals.size(); i++)
|
||||
xVals.add("Text" + (i + 1));
|
||||
|
||||
ChartData data = new ChartData(xVals, yVals);
|
||||
DataSet set = new DataSet(yVals, 0);
|
||||
ArrayList<DataSet> dataSets = new ArrayList<DataSet>();
|
||||
dataSets.add(set);
|
||||
|
||||
ChartData data = new ChartData(xVals, dataSets);
|
||||
mChart.setData(data);
|
||||
mChart.setCenterText("Total Value\n" + (int) mChart.getYValueSum() + "\n(all slices)");
|
||||
mChart.invalidate();
|
||||
|
|
|
@ -3,7 +3,7 @@ package com.github.mikephil.charting;
|
|||
import java.util.ArrayList;
|
||||
|
||||
/**
|
||||
* Implemented according to Wiki-Pseudocode {@link} http://en.wikipedia.org/wiki/RamerÐDouglasÐPeucker_algorithm
|
||||
* Implemented according to Wiki-Pseudocode {@link} http://en.wikipedia.org/wiki/Ramer<EFBFBD>Douglas<EFBFBD>Peucker_algorithm
|
||||
*
|
||||
* @author Philipp Baldauf & Phliipp Jahoda
|
||||
*/
|
||||
|
@ -118,25 +118,4 @@ public class Approximator {
|
|||
* (endSeries.getXIndex() - startSeries.getXIndex()))
|
||||
/ normalLength;
|
||||
}
|
||||
|
||||
// /**
|
||||
// * calculate the distance between a line between two series and a series (point)
|
||||
// *
|
||||
// * @param seriesToInspect
|
||||
// * @param startSeries
|
||||
// * @param endSeries
|
||||
// * @return
|
||||
// */
|
||||
// private static double shortestDistToSegment(Series seriesToInspect, Series startSeries, Series endSeries) {
|
||||
// double area = Math.abs((startSeries.getVal() * endSeries.getXIndex() + endSeries.getVal()
|
||||
// * seriesToInspect.getXIndex() + seriesToInspect.getVal() * startSeries.getXIndex() - endSeries.getVal()
|
||||
// * startSeries.getXIndex() - seriesToInspect.getVal() * endSeries.getXIndex() - startSeries.getVal()
|
||||
// * seriesToInspect.getXIndex()) / 2.0);
|
||||
//
|
||||
// double bottom = Math.hypot(startSeries.getVal() - endSeries.getVal(),
|
||||
// startSeries.getXIndex() - endSeries.getXIndex());
|
||||
//
|
||||
// return (area / bottom * 2.0);
|
||||
// }
|
||||
|
||||
}
|
||||
|
|
|
@ -130,11 +130,11 @@ public class BarChart extends BarLineChartBase {
|
|||
int index = mIndicesToHightlight[i];
|
||||
|
||||
// check outofbounds
|
||||
if (index < mData.getYValSize() && index >= 0) {
|
||||
if (index < mData.getYValCount() && index >= 0) {
|
||||
|
||||
mHighlightPaint.setAlpha(120);
|
||||
|
||||
float y = mData.getYVals().get(index).getVal();
|
||||
float y = getYValue(index);
|
||||
float left = index + mBarSpace / 2f;
|
||||
float right = index + 1f - mBarSpace / 2f;
|
||||
float top = y >= 0 ? y : 0;
|
||||
|
@ -205,9 +205,9 @@ public class BarChart extends BarLineChartBase {
|
|||
|
||||
float depth = Math.abs(pts[3] - pts[1]) * mDepth;
|
||||
|
||||
for (int i = 0; i < mData.getYValSize(); i++) {
|
||||
for (int i = 0; i < mData.getYValCount(); i++) {
|
||||
|
||||
float y = mData.getYVals().get(i).getVal();
|
||||
float y = getYValue(i);
|
||||
float left = i + mBarSpace / 2f;
|
||||
float right = i + 1f - mBarSpace / 2f;
|
||||
float top = y >= 0 ? y : 0;
|
||||
|
@ -235,11 +235,11 @@ public class BarChart extends BarLineChartBase {
|
|||
}
|
||||
|
||||
// do the drawing
|
||||
for (int i = 0; i < mData.getYValSize(); i++) {
|
||||
for (int i = 0; i < mData.getYValCount(); i++) {
|
||||
|
||||
Paint paint = mDrawPaints[i % mDrawPaints.length];
|
||||
|
||||
float y = mData.getYVals().get(i).getVal();
|
||||
float y = getYValue(i);
|
||||
float left = i + mBarSpace / 2f;
|
||||
float right = i + 1f - mBarSpace / 2f;
|
||||
float top = y >= 0 ? y : 0;
|
||||
|
@ -270,14 +270,14 @@ public class BarChart extends BarLineChartBase {
|
|||
protected void drawValues() {
|
||||
|
||||
// if values are drawn
|
||||
if (mDrawYValues && mData.getYValSize() < mMaxVisibleCount * mScaleX) {
|
||||
if (mDrawYValues && mData.getYValCount() < mMaxVisibleCount * mScaleX) {
|
||||
|
||||
float[] valuePoints = new float[mData.getYValSize() * 2];
|
||||
float[] valuePoints = new float[mData.getYValCount() * 2];
|
||||
|
||||
for (int i = 0; i < valuePoints.length; i += 2) {
|
||||
valuePoints[i] = i / 2 + 0.5f; // add 0.5f too keep the values
|
||||
// centered on top of the bars
|
||||
valuePoints[i + 1] = mData.getYVals().get(i / 2).getVal();
|
||||
valuePoints[i + 1] = getYValue(i / 2);
|
||||
}
|
||||
|
||||
transformPointArray(valuePoints);
|
||||
|
@ -286,11 +286,11 @@ public class BarChart extends BarLineChartBase {
|
|||
|
||||
if (mDrawUnitInChart) {
|
||||
|
||||
mDrawCanvas.drawText(mFormatValue.format(mData.getYVals().get(i / 2).getVal()) + mUnit,
|
||||
mDrawCanvas.drawText(mFormatValue.format(getYValue(i / 2)) + mUnit,
|
||||
valuePoints[i], valuePoints[i + 1] - 12, mValuePaint);
|
||||
} else {
|
||||
|
||||
mDrawCanvas.drawText(mFormatValue.format(mData.getYVals().get(i / 2).getVal()), valuePoints[i],
|
||||
mDrawCanvas.drawText(mFormatValue.format(getYValue(i / 2)), valuePoints[i],
|
||||
valuePoints[i + 1] - 12, mValuePaint);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -231,7 +231,7 @@ public abstract class BarLineChartBase extends Chart {
|
|||
float[] values = new float[9];
|
||||
mMatrixTouch.getValues(values);
|
||||
|
||||
mLegendGridModulus = (int) Math.ceil((mData.getXValSize() * mXLegendWidth)
|
||||
mLegendGridModulus = (int) Math.ceil((mData.getXValCount() * mXLegendWidth)
|
||||
/ (mContentRect.width() * values[Matrix.MSCALE_X]));
|
||||
}
|
||||
|
||||
|
@ -314,7 +314,7 @@ public abstract class BarLineChartBase extends Chart {
|
|||
|
||||
StringBuffer a = new StringBuffer();
|
||||
|
||||
int length = (int) (((float) (mData.getXVals().get(0).length() + mData.getXVals().get(mData.getXValSize() - 1)
|
||||
int length = (int) (((float) (mData.getXVals().get(0).length() + mData.getXVals().get(mData.getXValCount() - 1)
|
||||
.length())));
|
||||
|
||||
if (mData.getXVals().get(0).length() <= 3)
|
||||
|
@ -414,7 +414,7 @@ public abstract class BarLineChartBase extends Chart {
|
|||
// pre allocate to save performance (dont allocate in loop)
|
||||
float[] position = new float[] { 0f, 0f };
|
||||
|
||||
for (int i = 0; i < mData.getXValSize(); i++) {
|
||||
for (int i = 0; i < mData.getXValCount(); i++) {
|
||||
|
||||
if (i % mLegendGridModulus == 0) {
|
||||
|
||||
|
@ -549,7 +549,7 @@ public abstract class BarLineChartBase extends Chart {
|
|||
|
||||
float[] position = new float[] { 0f, 0f };
|
||||
|
||||
for (int i = 0; i < mData.getXValSize(); i++) {
|
||||
for (int i = 0; i < mData.getXValCount(); i++) {
|
||||
|
||||
if (i % mLegendGridModulus == 0) {
|
||||
|
||||
|
@ -869,7 +869,7 @@ public abstract class BarLineChartBase extends Chart {
|
|||
* @return
|
||||
*/
|
||||
public Series getSeriesByTouchPoint(float x, float y) {
|
||||
return mData.getYVals().get(getIndexByTouchPoint(x, y));
|
||||
return getSeries(getIndexByTouchPoint(x, y));
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -36,11 +36,11 @@ public class BarLineChartTouchListener extends SimpleOnGestureListener implement
|
|||
|
||||
private GestureDetector mGestureDetector;
|
||||
|
||||
public BarLineChartTouchListener(BarLineChartBase ctx, Matrix start) {
|
||||
this.mChart = ctx;
|
||||
public BarLineChartTouchListener(BarLineChartBase chart, Matrix start) {
|
||||
this.mChart = chart;
|
||||
this.matrix = start;
|
||||
|
||||
mGestureDetector = new GestureDetector(this);
|
||||
mGestureDetector = new GestureDetector(chart.getContext(), this);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
File diff suppressed because it is too large
Load diff
|
@ -1,3 +1,4 @@
|
|||
|
||||
package com.github.mikephil.charting;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
@ -6,172 +7,217 @@ import java.util.ArrayList;
|
|||
* Class that holds all relevant data that represents the chart
|
||||
*
|
||||
* @author Philipp
|
||||
*
|
||||
*/
|
||||
public class ChartData {
|
||||
|
||||
/** maximum y-value in the y-value array */
|
||||
private float mYMax = 0.0f;
|
||||
/** maximum y-value in the y-value array */
|
||||
private float mYMax = 0.0f;
|
||||
|
||||
/** the minimum y-value in the y-value array */
|
||||
private float mYMin = 0.0f;
|
||||
/** the minimum y-value in the y-value array */
|
||||
private float mYMin = 0.0f;
|
||||
|
||||
/** the total sum of all y-values */
|
||||
private float mYValueSum = 0f;
|
||||
/** the total sum of all y-values */
|
||||
private float mYValueSum = 0f;
|
||||
|
||||
private ArrayList<String> mXVals;
|
||||
private ArrayList<Series> mYVals;
|
||||
/** holds all x-values the chart represents */
|
||||
private ArrayList<String> mXVals;
|
||||
|
||||
/** array that holds all the different type ids that are in the series array */
|
||||
private ArrayList<Integer> mDiffTypes;
|
||||
/** holds all the datasets (e.g. different lines) the chart represents */
|
||||
private ArrayList<DataSet> mDataSets;
|
||||
|
||||
/** field that holds all the series split into their different types */
|
||||
private ArrayList<ArrayList<Series>> typeSeries;
|
||||
/** array that holds all the different type ids that are in the series array */
|
||||
private ArrayList<Integer> mDiffTypes;
|
||||
|
||||
/**
|
||||
* constructor for chart data
|
||||
*
|
||||
* @param xVals
|
||||
* must be at least as long as the longest series array of one type
|
||||
* @param yVals
|
||||
* all y series values
|
||||
*/
|
||||
public ChartData(ArrayList<String> xVals, ArrayList<Series> yVals) {
|
||||
this.mXVals = xVals;
|
||||
this.mYVals = yVals;
|
||||
/**
|
||||
* constructor for chart data
|
||||
*
|
||||
* @param xVals must be at least as long as the longest series array of one
|
||||
* type
|
||||
* @param dataSets all DataSet objects the chart needs to represent
|
||||
*/
|
||||
public ChartData(ArrayList<String> xVals, ArrayList<DataSet> dataSets) {
|
||||
this.mXVals = xVals;
|
||||
this.mDataSets = dataSets;
|
||||
|
||||
calcTypes();
|
||||
calcMinMax();
|
||||
calcYValueSum();
|
||||
calcTypeSeries();
|
||||
calcTypes();
|
||||
calcMinMax();
|
||||
calcYValueSum();
|
||||
|
||||
for (int i = 0; i < typeSeries.size(); i++) {
|
||||
if (typeSeries.get(i).size() > xVals.size()) {
|
||||
throw new IllegalArgumentException("x values are smaller than the largest y series array of one type");
|
||||
}
|
||||
}
|
||||
}
|
||||
for (int i = 0; i < mDataSets.size(); i++) {
|
||||
if (mDataSets.get(i).getYVals().size() > xVals.size()) {
|
||||
throw new IllegalArgumentException(
|
||||
"x values are smaller than the largest y series array of one type");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* calculates all different types that occur in the series and stores them for fast access
|
||||
*/
|
||||
private void calcTypes() {
|
||||
mDiffTypes = new ArrayList<Integer>();
|
||||
/**
|
||||
* calculates all different types that occur in the datasets and stores them
|
||||
* for fast access
|
||||
*/
|
||||
private void calcTypes() {
|
||||
mDiffTypes = new ArrayList<Integer>();
|
||||
|
||||
for (int i = 0; i < mYVals.size(); i++) {
|
||||
for (int i = 0; i < mDataSets.size(); i++) {
|
||||
|
||||
int type = mYVals.get(i).getType();
|
||||
int type = mDataSets.get(i).getType();
|
||||
|
||||
if (!alreadyCounted(mDiffTypes, type)) {
|
||||
mDiffTypes.add(type);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!alreadyCounted(mDiffTypes, type)) {
|
||||
mDiffTypes.add(type);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* calc minimum and maximum y value
|
||||
*/
|
||||
private void calcMinMax() {
|
||||
/**
|
||||
* calc minimum and maximum y value over all datasets
|
||||
*/
|
||||
private void calcMinMax() {
|
||||
|
||||
mYMin = mYVals.get(0).getVal();
|
||||
mYMax = mYVals.get(0).getVal();
|
||||
mYMin = mDataSets.get(0).getYMin();
|
||||
mYMax = mDataSets.get(0).getYMax();
|
||||
|
||||
for (int i = 0; i < mYVals.size(); i++) {
|
||||
if (mYVals.get(i).getVal() < mYMin)
|
||||
mYMin = mYVals.get(i).getVal();
|
||||
for (int i = 0; i < mDataSets.size(); i++) {
|
||||
if (mDataSets.get(i).getYMin() < mYMin)
|
||||
mYMin = mDataSets.get(i).getYMin();
|
||||
|
||||
if (mYVals.get(i).getVal() > mYMax)
|
||||
mYMax = mYVals.get(i).getVal();
|
||||
}
|
||||
}
|
||||
if (mDataSets.get(i).getYMax() > mYMax)
|
||||
mYMax = mDataSets.get(i).getYMax();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* calculates the sum of all y-values
|
||||
*/
|
||||
private void calcYValueSum() {
|
||||
/**
|
||||
* calculates the sum of all y-values in all datasets
|
||||
*/
|
||||
private void calcYValueSum() {
|
||||
|
||||
mYValueSum = 0;
|
||||
mYValueSum = 0;
|
||||
|
||||
for (int i = 0; i < getYValSize(); i++) {
|
||||
mYValueSum += Math.abs(getYVals().get(i).getVal());
|
||||
}
|
||||
}
|
||||
for (int i = 0; i < mDataSets.size(); i++) {
|
||||
mYValueSum += Math.abs(mDataSets.get(i).getYValueSum());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* extract all different types of series and store them in seperate arrays
|
||||
*/
|
||||
private void calcTypeSeries() {
|
||||
typeSeries = new ArrayList<ArrayList<Series>>();
|
||||
private boolean alreadyCounted(ArrayList<Integer> countedTypes, int type) {
|
||||
for (int i = 0; i < countedTypes.size(); i++) {
|
||||
if (countedTypes.get(i) == type)
|
||||
return true;
|
||||
}
|
||||
|
||||
for (int i = 0; i < getTypeCount(); i++) {
|
||||
ArrayList<Series> series = new ArrayList<Series>();
|
||||
return false;
|
||||
}
|
||||
|
||||
for (int j = 0; j < mYVals.size(); j++) {
|
||||
Series s = mYVals.get(j);
|
||||
if (s.getType() == mDiffTypes.get(i)) {
|
||||
series.add(s);
|
||||
}
|
||||
}
|
||||
public int getDataSetCount() {
|
||||
return mDataSets.size();
|
||||
}
|
||||
|
||||
typeSeries.add(series);
|
||||
}
|
||||
}
|
||||
public float getYMin() {
|
||||
return mYMin;
|
||||
}
|
||||
|
||||
private boolean alreadyCounted(ArrayList<Integer> countedTypes, int type) {
|
||||
for (int i = 0; i < countedTypes.size(); i++) {
|
||||
if (countedTypes.get(i) == type)
|
||||
return true;
|
||||
}
|
||||
public float getYMax() {
|
||||
return mYMax;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
public float getYValueSum() {
|
||||
return mYValueSum;
|
||||
}
|
||||
|
||||
public int getTypeCount() {
|
||||
return mDiffTypes.size();
|
||||
}
|
||||
/**
|
||||
* Checks if the ChartData object contains valid data
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public boolean isValid() {
|
||||
|
||||
public float getYMin() {
|
||||
return mYMin;
|
||||
}
|
||||
if (mXVals == null || mXVals.size() <= 1 || mDataSets == null || mDataSets.size() < 1
|
||||
|| mDataSets.get(0).getYVals().size() <= 1)
|
||||
return false;
|
||||
else
|
||||
return true;
|
||||
}
|
||||
|
||||
public float getYMax() {
|
||||
return mYMax;
|
||||
}
|
||||
/**
|
||||
* returns the x-values the chart represents
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public ArrayList<String> getXVals() {
|
||||
return mXVals;
|
||||
}
|
||||
|
||||
public float getYValueSum() {
|
||||
return mYValueSum;
|
||||
}
|
||||
/**
|
||||
* returns the series object at the given dataset index
|
||||
*
|
||||
* @param index
|
||||
* @return
|
||||
*/
|
||||
public ArrayList<Series> getYVals(int index) {
|
||||
return mDataSets.get(index).getYVals();
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the ChartData object contains valid data
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public boolean isValid() {
|
||||
/**
|
||||
* returns the dataset at the given index
|
||||
*
|
||||
* @param index
|
||||
* @return
|
||||
*/
|
||||
public DataSet getDataSetByIndex(int index) {
|
||||
return mDataSets.get(index);
|
||||
}
|
||||
|
||||
if (mXVals == null || mXVals.size() <= 1 || mYVals == null || mYVals.size() <= 1)
|
||||
return false;
|
||||
else
|
||||
return true;
|
||||
}
|
||||
/**
|
||||
* retrieve a dataset with a specific type from the chartdata
|
||||
*
|
||||
* @param type
|
||||
* @return
|
||||
*/
|
||||
public DataSet getDataSetByType(int type) {
|
||||
for (int i = 0; i < mDataSets.size(); i++)
|
||||
if (type == mDataSets.get(i).getType())
|
||||
return mDataSets.get(i);
|
||||
|
||||
public ArrayList<String> getXVals() {
|
||||
return mXVals;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* returns all DataSet objects the ChartData represents.
|
||||
* @return
|
||||
*/
|
||||
public ArrayList<DataSet> getDataSets() {
|
||||
return mDataSets;
|
||||
}
|
||||
|
||||
public ArrayList<Series> getYVals() {
|
||||
return mYVals;
|
||||
}
|
||||
/**
|
||||
* returns all the different DataSet types the chartdata represents
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public ArrayList<Integer> getTypes() {
|
||||
return mDiffTypes;
|
||||
}
|
||||
|
||||
public ArrayList<ArrayList<Series>> getTypeSeries() {
|
||||
return typeSeries;
|
||||
}
|
||||
/**
|
||||
* returns the total number of x-values this chartdata represents (the size
|
||||
* of the xvals array)
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public int getXValCount() {
|
||||
return mXVals.size();
|
||||
}
|
||||
|
||||
public int getXValSize() {
|
||||
return mXVals.size();
|
||||
}
|
||||
/**
|
||||
* returns the total number of y-values across all DataSets the chartdata
|
||||
* represents
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public int getYValCount() {
|
||||
int count = 0;
|
||||
for (int i = 0; i < mDataSets.size(); i++) {
|
||||
count += mDataSets.get(i).getYValCount();
|
||||
}
|
||||
|
||||
public int getYValSize() {
|
||||
return mYVals.size();
|
||||
}
|
||||
return count;
|
||||
}
|
||||
}
|
||||
|
|
132
MPChartLib/src/com/github/mikephil/charting/DataSet.java
Normal file
132
MPChartLib/src/com/github/mikephil/charting/DataSet.java
Normal file
|
@ -0,0 +1,132 @@
|
|||
|
||||
package com.github.mikephil.charting;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
/**
|
||||
* The DataSet class represents one group or type of entries (Series) in the
|
||||
* Chart that belong together.
|
||||
*
|
||||
* @author Philipp Jahoda
|
||||
*/
|
||||
public class DataSet {
|
||||
|
||||
/** the series that this dataset represents / holds together */
|
||||
private ArrayList<Series> mYVals;
|
||||
|
||||
/** maximum y-value in the y-value array */
|
||||
private float mYMax = 0.0f;
|
||||
|
||||
/** the minimum y-value in the y-value array */
|
||||
private float mYMin = 0.0f;
|
||||
|
||||
/** the total sum of all y-values */
|
||||
private float mYValueSum = 0f;
|
||||
|
||||
/** type, used for identification amongst other DataSets */
|
||||
private int mType = 0;
|
||||
|
||||
/**
|
||||
* Creates a new DataSet object with the given values it represents and a
|
||||
* type for indentification amongst other DataSet objects (the type can be
|
||||
* chosen freely and must not be equal to another type in the ChartData
|
||||
* object).
|
||||
*
|
||||
* @param yVals
|
||||
* @param type
|
||||
*/
|
||||
public DataSet(ArrayList<Series> yVals, int type) {
|
||||
this.mType = type;
|
||||
this.mYVals = yVals;
|
||||
|
||||
calcMinMax();
|
||||
calcYValueSum();
|
||||
}
|
||||
|
||||
/**
|
||||
* calc minimum and maximum y value
|
||||
*/
|
||||
private void calcMinMax() {
|
||||
|
||||
mYMin = mYVals.get(0).getVal();
|
||||
mYMax = mYVals.get(0).getVal();
|
||||
|
||||
for (int i = 0; i < mYVals.size(); i++) {
|
||||
if (mYVals.get(i).getVal() < mYMin)
|
||||
mYMin = mYVals.get(i).getVal();
|
||||
|
||||
if (mYVals.get(i).getVal() > mYMax)
|
||||
mYMax = mYVals.get(i).getVal();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* calculates the sum of all y-values
|
||||
*/
|
||||
private void calcYValueSum() {
|
||||
|
||||
mYValueSum = 0;
|
||||
|
||||
for (int i = 0; i < mYVals.size(); i++) {
|
||||
mYValueSum += Math.abs(mYVals.get(i).getVal());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* returns the number of y-values this DataSet represents
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public int getYValCount() {
|
||||
return mYVals.size();
|
||||
}
|
||||
|
||||
public ArrayList<Series> getYVals() {
|
||||
return mYVals;
|
||||
}
|
||||
|
||||
public float getYValueSum() {
|
||||
return mYValueSum;
|
||||
}
|
||||
|
||||
public float getYMin() {
|
||||
return mYMin;
|
||||
}
|
||||
|
||||
public float getYMax() {
|
||||
return mYMax;
|
||||
}
|
||||
|
||||
public int getType() {
|
||||
return mType;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convenience method to create multiple DataSets of different types with
|
||||
* various double value arrays. Each double array represents the data of one
|
||||
* DataSet with a type created by this method, starting at 0 (and
|
||||
* incremented).
|
||||
*
|
||||
* @param yValues
|
||||
* @return
|
||||
*/
|
||||
public static ArrayList<DataSet> makeDataSets(ArrayList<Double[]> yValues) {
|
||||
|
||||
ArrayList<DataSet> dataSets = new ArrayList<DataSet>();
|
||||
|
||||
for (int i = 0; i < yValues.size(); i++) {
|
||||
|
||||
Double[] curValues = yValues.get(i);
|
||||
|
||||
ArrayList<Series> series = new ArrayList<Series>();
|
||||
|
||||
for (int j = 0; j < curValues.length; j++) {
|
||||
series.add(new Series(curValues[j].floatValue(), i, j));
|
||||
}
|
||||
|
||||
dataSets.add(new DataSet(series, i));
|
||||
}
|
||||
|
||||
return dataSets;
|
||||
}
|
||||
}
|
|
@ -107,10 +107,10 @@ public class LineChart extends BarLineChartBase {
|
|||
int index = mIndicesToHightlight[i];
|
||||
|
||||
// check outofbounds
|
||||
if (index < mData.getYValSize() && index >= 0) {
|
||||
if (index < mData.getYValCount() && index >= 0) {
|
||||
|
||||
float[] pts = new float[] { index, mYChartMax, index, mYChartMin, 0,
|
||||
mData.getYVals().get(index).getVal(), mDeltaX, mData.getYVals().get(index).getVal() };
|
||||
getYValue(index), mDeltaX, getYValue(index) };
|
||||
|
||||
transformPointArray(pts);
|
||||
// draw the highlight lines
|
||||
|
@ -126,11 +126,12 @@ public class LineChart extends BarLineChartBase {
|
|||
@Override
|
||||
protected void drawData() {
|
||||
|
||||
ArrayList<ArrayList<Series>> typeSeries = mData.getTypeSeries();
|
||||
ArrayList<DataSet> dataSets = mData.getDataSets();
|
||||
|
||||
for (int i = 0; i < mData.getTypeCount(); i++) {
|
||||
for (int i = 0; i < mData.getDataSetCount(); i++) {
|
||||
|
||||
ArrayList<Series> series = typeSeries.get(i);
|
||||
DataSet dataSet = dataSets.get(i);
|
||||
ArrayList<Series> series = dataSet.getYVals();
|
||||
|
||||
float[] valuePoints = new float[series.size() * 2];
|
||||
|
||||
|
@ -159,16 +160,16 @@ public class LineChart extends BarLineChartBase {
|
|||
if (mDrawFilled) {
|
||||
|
||||
Path filled = new Path();
|
||||
filled.moveTo(0, mData.getYVals().get(0).getVal());
|
||||
filled.moveTo(0, getYValue(0));
|
||||
|
||||
// create a new path
|
||||
for (int x = 1; x < mData.getYValSize(); x++) {
|
||||
for (int x = 1; x < mData.getYValCount(); x++) {
|
||||
|
||||
filled.lineTo(x, mData.getYVals().get(x).getVal());
|
||||
filled.lineTo(x, getYValue(x));
|
||||
}
|
||||
|
||||
// close up
|
||||
filled.lineTo(mData.getXValSize() - 1, mYChartMin);
|
||||
filled.lineTo(mData.getXValCount() - 1, mYChartMin);
|
||||
filled.lineTo(0f, mYChartMin);
|
||||
filled.close();
|
||||
|
||||
|
@ -182,26 +183,28 @@ public class LineChart extends BarLineChartBase {
|
|||
protected void drawValues() {
|
||||
|
||||
// if values are drawn
|
||||
if (mDrawYValues && mData.getYValSize() < mMaxVisibleCount * mScaleX) {
|
||||
if (mDrawYValues && mData.getYValCount() < mMaxVisibleCount * mScaleX) {
|
||||
|
||||
float[] valuePoints = new float[mData.getYValSize() * 2];
|
||||
float[] valuePoints = new float[mData.getYValCount() * 2];
|
||||
|
||||
for (int i = 0; i < valuePoints.length; i += 2) {
|
||||
valuePoints[i] = i / 2;
|
||||
valuePoints[i + 1] = mData.getYVals().get(i / 2).getVal();
|
||||
valuePoints[i + 1] = getYValue(i / 2);
|
||||
}
|
||||
|
||||
transformPointArray(valuePoints);
|
||||
|
||||
for (int i = 0; i < valuePoints.length; i += 2) {
|
||||
|
||||
float val = getYValue(i / 2);
|
||||
|
||||
if (mDrawUnitInChart) {
|
||||
|
||||
mDrawCanvas.drawText(mFormatValue.format(mData.getYVals().get(i / 2).getVal()) + mUnit,
|
||||
mDrawCanvas.drawText(mFormatValue.format(val) + mUnit,
|
||||
valuePoints[i], valuePoints[i + 1] - 12, mValuePaint);
|
||||
} else {
|
||||
|
||||
mDrawCanvas.drawText(mFormatValue.format(mData.getYVals().get(i / 2).getVal()), valuePoints[i],
|
||||
mDrawCanvas.drawText(mFormatValue.format(val), valuePoints[i],
|
||||
valuePoints[i + 1] - 12, mValuePaint);
|
||||
}
|
||||
}
|
||||
|
@ -216,13 +219,14 @@ public class LineChart extends BarLineChartBase {
|
|||
// if drawing circles is enabled
|
||||
if (mDrawCircles) {
|
||||
|
||||
ArrayList<ArrayList<Series>> typeSeries = mData.getTypeSeries();
|
||||
ArrayList<DataSet> dataSets = mData.getDataSets();
|
||||
|
||||
for (int i = 0; i < mData.getTypeCount(); i++) {
|
||||
for (int i = 0; i < mData.getDataSetCount(); i++) {
|
||||
|
||||
ArrayList<Series> series = typeSeries.get(i);
|
||||
DataSet dataSet = dataSets.get(i);
|
||||
ArrayList<Series> series = dataSet.getYVals();
|
||||
|
||||
float[] positions = new float[mData.getYValSize() * 2];
|
||||
float[] positions = new float[dataSet.getYValCount() * 2];
|
||||
|
||||
for (int j = 0; j < positions.length; j += 2) {
|
||||
positions[j] = series.get(j / 2).getXIndex();
|
||||
|
|
|
@ -265,11 +265,11 @@ public class PieChart extends Chart {
|
|||
*/
|
||||
private void calcAngles() {
|
||||
|
||||
mDrawAngles = new float[mData.getYValSize()];
|
||||
mAbsoluteAngles = new float[mData.getYValSize()];
|
||||
mDrawAngles = new float[mData.getYValCount()];
|
||||
mAbsoluteAngles = new float[mData.getYValCount()];
|
||||
|
||||
for (int i = 0; i < mData.getYValSize(); i++) {
|
||||
mDrawAngles[i] = calcAngle(mData.getYVals().get(i).getVal());
|
||||
for (int i = 0; i < mData.getYValCount(); i++) {
|
||||
mDrawAngles[i] = calcAngle(getYValue(i));
|
||||
|
||||
if (i > 0)
|
||||
mAbsoluteAngles[i] = mAbsoluteAngles[i - 1] + mDrawAngles[i];
|
||||
|
@ -318,7 +318,7 @@ public class PieChart extends Chart {
|
|||
|
||||
float angle = mChartAngle;
|
||||
|
||||
for (int i = 0; i < mData.getYValSize(); i++) {
|
||||
for (int i = 0; i < mData.getYValCount(); i++) {
|
||||
|
||||
float newangle = mDrawAngles[i];
|
||||
|
||||
|
@ -386,7 +386,7 @@ public class PieChart extends Chart {
|
|||
float r = mCircleBox.width() / 2 - off; // offset to keep things inside
|
||||
// the chart
|
||||
|
||||
for (int i = 0; i < mData.getYValSize(); i++) {
|
||||
for (int i = 0; i < mData.getYValCount(); i++) {
|
||||
|
||||
// offset needed to center the drawn text in the slice
|
||||
float offset = mDrawAngles[i] / 2;
|
||||
|
@ -401,11 +401,12 @@ public class PieChart extends Chart {
|
|||
// }
|
||||
|
||||
String val = "";
|
||||
float value = getYValue(i);
|
||||
|
||||
if (mUsePercentValues)
|
||||
val = mFormatValue.format(getPercentOfTotal(mData.getYVals().get(i).getVal())) + " %";
|
||||
val = mFormatValue.format(getPercentOfTotal(value)) + " %";
|
||||
else
|
||||
val = mFormatValue.format(mData.getYVals().get(i).getVal());
|
||||
val = mFormatValue.format(value);
|
||||
|
||||
// draw everything, depending on settings
|
||||
if (mDrawXVals && mDrawYValues) {
|
||||
|
|
|
@ -36,59 +36,59 @@ public class Series {
|
|||
public int getXIndex() {
|
||||
return mXIndex;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convenience method to create a series of double values
|
||||
*
|
||||
* @param yValues
|
||||
* @return
|
||||
*/
|
||||
public static ArrayList<Series> makeSeries(double[] yValues) {
|
||||
ArrayList<Series> series = new ArrayList<Series>();
|
||||
for (int i = 0; i < yValues.length; i++) {
|
||||
series.add(new Series((float) yValues[i], 0, i));
|
||||
}
|
||||
return series;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convenience method to create multiple series of different types of various double value arrays. Each double array
|
||||
* represents one type starting at 0.
|
||||
*
|
||||
* @param yValues
|
||||
* @return
|
||||
*/
|
||||
public static ArrayList<Series> makeMultipleSeries(ArrayList<Double[]> yValues) {
|
||||
ArrayList<Series> series = new ArrayList<Series>();
|
||||
|
||||
int sizeOfFirst = yValues.get(0).length;
|
||||
|
||||
for (int i = 0; i < yValues.size(); i++) {
|
||||
Double[] curValues = yValues.get(i);
|
||||
if (curValues.length != sizeOfFirst) {
|
||||
throw new IllegalArgumentException("Array sizes do not match");
|
||||
}
|
||||
for (int j = 0; j < curValues.length; j++) {
|
||||
series.add(new Series(curValues[j].floatValue(), i, j));
|
||||
}
|
||||
}
|
||||
|
||||
return series;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convenience method to add a series. The new series has to be the same size as the old. If you want to create a
|
||||
* different sized series, please add manually.
|
||||
*
|
||||
* @param series
|
||||
* @param yValues
|
||||
* @param type
|
||||
*/
|
||||
public static void addSeries(ArrayList<Series> series, double[] yValues, int type) {
|
||||
for (int i = 0; i < yValues.length; i++) {
|
||||
series.add(new Series((float) yValues[i], type, i));
|
||||
}
|
||||
}
|
||||
//
|
||||
// /**
|
||||
// * Convenience method to create a series of double values
|
||||
// *
|
||||
// * @param yValues
|
||||
// * @return
|
||||
// */
|
||||
// public static ArrayList<Series> makeSeries(double[] yValues) {
|
||||
// ArrayList<Series> series = new ArrayList<Series>();
|
||||
// for (int i = 0; i < yValues.length; i++) {
|
||||
// series.add(new Series((float) yValues[i], 0, i));
|
||||
// }
|
||||
// return series;
|
||||
// }
|
||||
//
|
||||
// /**
|
||||
// * Convenience method to create multiple series of different types of various double value arrays. Each double array
|
||||
// * represents one type starting at 0.
|
||||
// *
|
||||
// * @param yValues
|
||||
// * @return
|
||||
// */
|
||||
// public static ArrayList<Series> makeMultipleSeries(ArrayList<Double[]> yValues) {
|
||||
// ArrayList<Series> series = new ArrayList<Series>();
|
||||
//
|
||||
// int sizeOfFirst = yValues.get(0).length;
|
||||
//
|
||||
// for (int i = 0; i < yValues.size(); i++) {
|
||||
// Double[] curValues = yValues.get(i);
|
||||
// if (curValues.length != sizeOfFirst) {
|
||||
// throw new IllegalArgumentException("Array sizes do not match");
|
||||
// }
|
||||
// for (int j = 0; j < curValues.length; j++) {
|
||||
// series.add(new Series(curValues[j].floatValue(), i, j));
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// return series;
|
||||
// }
|
||||
//
|
||||
// /**
|
||||
// * Convenience method to add a series. The new series has to be the same size as the old. If you want to create a
|
||||
// * different sized series, please add manually.
|
||||
// *
|
||||
// * @param series
|
||||
// * @param yValues
|
||||
// * @param type
|
||||
// */
|
||||
// public static void addSeries(ArrayList<Series> series, double[] yValues, int type) {
|
||||
// for (int i = 0; i < yValues.length; i++) {
|
||||
// series.add(new Series((float) yValues[i], type, i));
|
||||
// }
|
||||
// }
|
||||
|
||||
public float getVal() {
|
||||
return mVal;
|
||||
|
|
Loading…
Add table
Reference in a new issue