Fixed all known bugs concerning DataSets. LineChart, BarChart and PieChart now support single and multiple DataSets.

This commit is contained in:
Philipp Jahoda 2014-05-28 23:07:18 +02:00
parent 256c028a3e
commit 80afe1483c
17 changed files with 1199 additions and 755 deletions

View file

@ -28,6 +28,7 @@
<activity android:name="BarChartActivity"></activity>
<activity android:name="PieChartActivity"></activity>
<activity android:name="MultiLineChartActivity"></activity>
<activity android:name="BarChartActivityMultiDataset"></activity>
</application>
</manifest>

View file

@ -42,20 +42,27 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:text="MultilineChart" />
android:text="LineChart (more datasets)" />
<Button
android:id="@+id/button5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:text="MultipleCharts" />
android:text="BarChart (more datasets)" />
<Button
android:id="@+id/button6"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:text="MultipleCharts" />
<Button
android:id="@+id/button7"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:text="View on GitHub" />
</LinearLayout>

View file

@ -41,7 +41,13 @@ public class BarChartActivity extends Activity implements OnSeekBarChangeListene
mSeekBarY.setOnSeekBarChangeListener(this);
mChart = (BarChart) findViewById(R.id.chart1);
mChart.setColorTemplate(new ColorTemplate(ColorTemplate.getColors(this, ColorTemplate.FRESH_COLORS)));
ColorTemplate ct = new ColorTemplate();
// add colors for one dataset
ct.addDataSetColors(ColorTemplate.FRESH_COLORS, this);
mChart.setColorTemplate(ct);
// mChart.setLegendDigits(2);
// mChart.setValueDigits(2);
@ -144,31 +150,19 @@ public class BarChartActivity extends Activity implements OnSeekBarChangeListene
}
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);
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));
// Approximator approximator = new Approximator(ApproximatorType.DOUGLAS_PEUCKER);
// ArrayList<Series> filtered = approximator.filter(yVals);
DataSet set1 = new DataSet(yVals1, 0);
// DataSet set2 = new DataSet(yVals2, 1);
ArrayList<DataSet> dataSets = new ArrayList<DataSet>();
dataSets.add(set1);
// dataSets.add(set2);
ChartData data = new ChartData(xVals, dataSets);

View file

@ -0,0 +1,205 @@
package com.example.mpchartexample;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.WindowManager;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
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 BarChartActivityMultiDataset extends Activity implements OnSeekBarChangeListener {
private BarChart 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_barchart);
tvX = (TextView) findViewById(R.id.tvXMax);
tvY = (TextView) findViewById(R.id.tvYMax);
mSeekBarX = (SeekBar) findViewById(R.id.seekBar1);
mSeekBarX.setOnSeekBarChangeListener(this);
mSeekBarY = (SeekBar) findViewById(R.id.seekBar2);
mSeekBarY.setOnSeekBarChangeListener(this);
mChart = (BarChart) findViewById(R.id.chart1);
ColorTemplate ct = new ColorTemplate();
// add colors for the datasets
ct.addDataSetColors(ColorTemplate.FRESH_COLORS, this);
// the second dataset only has one color
ct.addDataSetColors(new int[] {R.color.liberty_2}, this);
ct.addDataSetColors(ColorTemplate.COLORFUL_COLORS, this);
mChart.setColorTemplate(ct);
// mChart.setLegendDigits(2);
// mChart.setValueDigits(2);
// mChart.setDrawFilled(true);
// mChart.setRoundedYLegend(false);
// mChart.setStartAtZero(true);
mChart.setDrawYValues(false);
mChart.set3DEnabled(false);
// mChart.setSpacePercent(20, 10);
mChart.setYLegendCount(5);
mChart.setTouchEnabled(true);
mSeekBarX.setProgress(45);
mSeekBarY.setProgress(100);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.bar, menu);
return true;
}
@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.actionToggle3D: {
if (mChart.is3DEnabled())
mChart.set3DEnabled(false);
else
mChart.set3DEnabled(true);
mChart.invalidate();
break;
}
case R.id.actionToggleHighlight: {
if (mChart.isHighlightEnabled())
mChart.setHighlightEnabled(false);
else
mChart.setHighlightEnabled(true);
mChart.invalidate();
break;
}
case R.id.actionToggleHighlightArrow: {
if (mChart.isDrawHighlightArrowEnabled())
mChart.setDrawHighlightArrow(false);
else
mChart.setDrawHighlightArrow(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.actionSave: {
// mChart.saveToGallery("title"+System.currentTimeMillis());
mChart.saveToPath("title" + System.currentTimeMillis(), "");
break;
}
}
return true;
}
@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<Series> yVals1 = new ArrayList<Series>();
ArrayList<Series> yVals2 = new ArrayList<Series>();
ArrayList<Series> yVals3 = new ArrayList<Series>();
for (int i = 0; i < mSeekBarX.getProgress() / 3; i++) {
float mult = (mSeekBarY.getProgress() + 1);
float val = (float) (Math.random() * mult * 0.1) + 3;// + (float) ((mult * 0.1) / 10);
yVals1.add(new Series(val, i));
}
for (int i = mSeekBarX.getProgress() / 3; i < mSeekBarX.getProgress() / 3 * 2; 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));
}
for (int i = mSeekBarX.getProgress() / 3 * 2; i < mSeekBarX.getProgress(); i++) {
float mult = (mSeekBarY.getProgress() + 1);
float val = (float) (Math.random() * mult * 0.1) + 3;// + (float) ((mult * 0.1) / 10);
yVals3.add(new Series(val, i));
}
tvX.setText("" + (mSeekBarX.getProgress() + 1));
tvY.setText("" + (mSeekBarY.getProgress() / 10));
// create 3 datasets with different types
DataSet set1 = new DataSet(yVals1, 0);
DataSet set2 = new DataSet(yVals2, 1);
DataSet set3 = new DataSet(yVals3, 2);
ArrayList<DataSet> dataSets = new ArrayList<DataSet>();
dataSets.add(set1);
dataSets.add(set2);
dataSets.add(set3);
ChartData data = new ChartData(xVals, dataSets);
mChart.setData(data);
mChart.invalidate();
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
}

View file

@ -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.ColorTemplate;
import com.github.mikephil.charting.DataSet;
import com.github.mikephil.charting.Highlight;
import com.github.mikephil.charting.LineChart;
@ -47,11 +48,15 @@ public class LineChartActivity extends Activity implements OnSeekBarChangeListen
mSeekBarY = (SeekBar) findViewById(R.id.seekBar2);
mSeekBarY.setOnSeekBarChangeListener(this);
// create a color template for one dataset with only one color
ColorTemplate ct = new ColorTemplate();
ct.addColorsForDataSets(new int[] {
R.color.colorful_1
}, this);
mChart = (LineChart) findViewById(R.id.chart1);
mChart.setOnChartValueSelectedListener(this);
// mChart.setColorTemplate(new
// ColorTemplate(ColorTemplate.getColors(this,
// ColorTemplate.LIBERTY_COLORS)));
mChart.setColorTemplate(ct);
// mChart.setDrawFilled(true);
// mChart.setRoundedYLegend(false);
@ -63,7 +68,10 @@ public class LineChartActivity extends Activity implements OnSeekBarChangeListen
mChart.setYLegendCount(6);
mChart.setTouchEnabled(true);
mChart.setHighlightEnabled(true);
// mChart.highlightValues(new int[] {2, 6});
// highlight index 2 and 6 in dataset 0
// mChart.highlightValues(new Highlight[] {new Highlight(2, 0), new
// Highlight(6, 0)});
mChart.setDragEnabled(true);
mChart.setTouchEnabled(true);
@ -178,15 +186,13 @@ public class LineChartActivity extends Activity implements OnSeekBarChangeListen
tvX.setText("" + (mSeekBarX.getProgress() + 1));
tvY.setText("" + (mSeekBarY.getProgress() / 10));
// Approximator a = new Approximator(ApproximatorType.DOUGLAS_PEUCKER);
// ArrayList<Series> filtered = a.filter(yVals, 1.0);
// create a dataset and give it a type (0)
DataSet set1 = new DataSet(yVals, 0);
// DataSet set2 = new DataSet(filtered, 1);
ArrayList<DataSet> dataSets = new ArrayList<DataSet>();
dataSets.add(set1);
// dataSets.add(set2);
dataSets.add(set1); // add the datasets
// create a data object with the datasets
ChartData data = new ChartData(xVals, dataSets);
mChart.setData(data);

View file

@ -25,12 +25,14 @@ public class MainActivity extends Activity implements OnClickListener {
Button btn4 = (Button) findViewById(R.id.button4);
Button btn5 = (Button) findViewById(R.id.button5);
Button btn6 = (Button) findViewById(R.id.button6);
Button btn7 = (Button) findViewById(R.id.button7);
btn1.setOnClickListener(this);
btn2.setOnClickListener(this);
btn3.setOnClickListener(this);
btn4.setOnClickListener(this);
btn5.setOnClickListener(this);
btn6.setOnClickListener(this);
btn7.setOnClickListener(this);
}
@Override
@ -56,10 +58,14 @@ public class MainActivity extends Activity implements OnClickListener {
startActivity(i);
break;
case R.id.button5:
i = new Intent(this, MultipleChartsActivity.class);
i = new Intent(this, BarChartActivityMultiDataset.class);
startActivity(i);
break;
case R.id.button6:
i = new Intent(this, MultipleChartsActivity.class);
startActivity(i);
break;
case R.id.button7:
i = new Intent(Intent.ACTION_VIEW, Uri.parse("https://github.com/PhilJay/MPAndroidChart"));
startActivity(i);
break;

View file

@ -44,7 +44,11 @@ public class MultiLineChartActivity extends Activity implements OnSeekBarChangeL
mSeekBarY.setOnSeekBarChangeListener(this);
mChart = (LineChart) findViewById(R.id.chart1);
mChart.setColorTemplate(new ColorTemplate(ColorTemplate.getColors(this, ColorTemplate.JOYFUL_COLORS)));
ColorTemplate ct = new ColorTemplate();
ct.addColorsForDataSets(ColorTemplate.COLORFUL_COLORS, this);
mChart.setColorTemplate(ct);
mChart.setDrawTopYLegendEntry(false);
mChart.setOnChartValueSelectedListener(this);

View file

@ -44,7 +44,11 @@ public class PieChartActivity extends Activity implements OnSeekBarChangeListene
mSeekBarY.setOnSeekBarChangeListener(this);
mChart = (PieChart) findViewById(R.id.chart1);
mChart.setColorTemplate(new ColorTemplate(ColorTemplate.getColors(this, ColorTemplate.COLORFUL_COLORS)));
ColorTemplate ct = new ColorTemplate();
ct.addDataSetColors(ColorTemplate.COLORFUL_COLORS, this);
ct.addDataSetColors(ColorTemplate.LIBERTY_COLORS, this);
mChart.setColorTemplate(ct);
mChart.setDrawYValues(true);
mChart.setDrawCenterText(true);
@ -56,7 +60,7 @@ public class PieChartActivity extends Activity implements OnSeekBarChangeListene
mChart.setUsePercentValues(false);
mChart.setOnChartValueSelectedListener(this);
mSeekBarX.setProgress(5);
mSeekBarX.setProgress(10);
mSeekBarY.setProgress(100);
// float diameter = mChart.getDiameter();
@ -127,30 +131,40 @@ public class PieChartActivity extends Activity implements OnSeekBarChangeListene
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
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++) {
// IMPORTANT: In a PieChart, no values (Series) should have the same xIndex, since no values can be drawn above each other.
for (int i = 0; i < mSeekBarX.getProgress() / 2; i++) {
float mult = (mSeekBarY.getProgress());
float val = (float) (Math.random() * mult) + mult / 5;// + (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());
float val = (float) (Math.random() * mult) + mult / 5;// + (float) ((mult * 0.1) / 10);
yVals2.add(new Series(val, i));
}
tvX.setText("" + (mSeekBarX.getProgress()));
tvY.setText("" + (mSeekBarY.getProgress()));
ArrayList<String> xVals = new ArrayList<String>();
for (int i = 0; i < yVals.size(); i++)
for (int i = 0; i < mSeekBarX.getProgress(); i++)
xVals.add("Text" + (i + 1));
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);
mChart.setData(data);
mChart.highlightValues(null);
mChart.setCenterText("Total Value\n" + (int) mChart.getYValueSum() + "\n(all slices)");
mChart.invalidate();
}

View file

@ -61,52 +61,52 @@ public class BarChart extends BarLineChartBase {
@Override
protected void prepareDataPaints(ColorTemplate ct) {
// prepare the paints
mDrawPaints = new Paint[ct.getColors().size()];
// // prepare the paints
// mDrawPaints = new Paint[ct.getColors().size()];
//
// for (int i = 0; i < ct.getColors().size(); i++) {
// mDrawPaints[i] = new Paint(Paint.ANTI_ALIAS_FLAG);
// mDrawPaints[i].setStyle(Style.FILL);
// mDrawPaints[i].setColor(Color.BLACK);
// }
for (int i = 0; i < ct.getColors().size(); i++) {
mDrawPaints[i] = new Paint(Paint.ANTI_ALIAS_FLAG);
mDrawPaints[i].setStyle(Style.FILL);
mDrawPaints[i].setColor(ct.getColors().get(i));
}
// generate the colors for the 3D effect
mTopColors = new int[mDrawPaints.length];
mSideColors = new int[mDrawPaints.length];
float[] hsv = new float[3];
for (int i = 0; i < mSideColors.length; i++) {
// extract the color
int c = mDrawPaints[i].getColor();
Color.colorToHSV(c, hsv); // convert to hsv
// make brighter
hsv[1] = hsv[1] - 0.1f; // less saturation
hsv[2] = hsv[2] + 0.1f; // more brightness
// convert back
c = Color.HSVToColor(hsv);
// assign
mTopColors[i] = c;
// get color again
c = mDrawPaints[i].getColor();
// convert
Color.colorToHSV(c, hsv);
// make darker
hsv[1] = hsv[1] + 0.1f; // more saturation
hsv[2] = hsv[2] - 0.1f; // less brightness
// reassing
c = Color.HSVToColor(hsv);
mSideColors[i] = c;
}
// // generate the colors for the 3D effect
// mTopColors = new int[mDrawPaints.length];
// mSideColors = new int[mDrawPaints.length];
//
// float[] hsv = new float[3];
//
// for (int i = 0; i < mSideColors.length; i++) {
//
// // extract the color
// int c = mDrawPaints[i].getColor();
// Color.colorToHSV(c, hsv); // convert to hsv
//
// // make brighter
// hsv[1] = hsv[1] - 0.1f; // less saturation
// hsv[2] = hsv[2] + 0.1f; // more brightness
//
// // convert back
// c = Color.HSVToColor(hsv);
//
// // assign
// mTopColors[i] = c;
//
// // get color again
// c = mDrawPaints[i].getColor();
//
// // convert
// Color.colorToHSV(c, hsv);
//
// // make darker
// hsv[1] = hsv[1] + 0.1f; // more saturation
// hsv[2] = hsv[2] - 0.1f; // less brightness
//
// // reassing
// c = Color.HSVToColor(hsv);
//
// mSideColors[i] = c;
// }
}
@Override
@ -135,7 +135,8 @@ public class BarChart extends BarLineChartBase {
mHighlightPaint.setAlpha(120);
float y = getYValueByDataSetIndex(index, mIndicesToHightlight[i].getDataSetIndex());
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,15 +244,17 @@ 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];
// Get the colors for the DataSet at the current index. If the index
// is out of bounds, reuse DataSet colors.
ArrayList<Integer> colors = mCt.getDataSetColors(i % mCt.getColors().size());
// do the drawing
for (int j = 0; j < dataSet.getSeriesCount(); j++) {
// if only one DataSet exists, switch colors inside dataset
if(mData.getDataSetCount() == 1) paint = mDrawPaints[j % mDrawPaints.length];
// Set the color for the currently drawn value. If the index is
// out of bounds, reuse colors.
mRenderPaint.setColor(colors.get(j % colors.size()));
int x = series.get(j).getXIndex();
float y = series.get(j).getVal();
@ -264,19 +267,19 @@ public class BarChart extends BarLineChartBase {
transformRect(mBarRect);
mDrawCanvas.drawRect(mBarRect, paint);
mDrawCanvas.drawRect(mBarRect, mRenderPaint);
if (m3DEnabled) {
int c = paint.getColor();
int c = mRenderPaint.getColor();
paint.setColor(mTopColors[j % mTopColors.length]);
mDrawCanvas.drawPath(topPaths.get(j), paint);
mRenderPaint.setColor(mTopColors[j % mTopColors.length]);
mDrawCanvas.drawPath(topPaths.get(j), mRenderPaint);
paint.setColor(mSideColors[j % mSideColors.length]);
mDrawCanvas.drawPath(sidePaths.get(j), paint);
mRenderPaint.setColor(mSideColors[j % mSideColors.length]);
mDrawCanvas.drawPath(sidePaths.get(j), mRenderPaint);
paint.setColor(c);
mRenderPaint.setColor(c);
}
}
}

View file

@ -929,9 +929,9 @@ public abstract class BarLineChartBase extends Chart {
}
/**
* returns the index DataSet that contains the closest value
* returns the index of the DataSet that contains the closest value
*
* @param valsAtIndex
* @param valsAtIndex all the values at a specific index
* @return
*/
private int getClosestDataSetIndex(ArrayList<SelInfo> valsAtIndex, float val) {

View file

@ -9,6 +9,7 @@ import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Paint.Align;
import android.graphics.Paint.Style;
import android.graphics.Path;
import android.graphics.PointF;
import android.graphics.Rect;
@ -77,11 +78,6 @@ public abstract class Chart extends View {
/** the highest value the chart can display */
protected float mYChartMax = 0.0f;
/**
* colortemplate containing all the colors used by the chart
*/
protected ColorTemplate mColorTemplate;
/**
* paint object used for darwing the bitmap to the screen
*/
@ -105,11 +101,11 @@ public abstract class Chart extends View {
*/
protected Paint mValuePaint;
/**
* paint objects used for drawing (e.g. the bars of a chart) usually, each
* paint object has a different color
*/
protected Paint[] mDrawPaints;
/** this is the paint object used for drawing the data onto the chart */
protected Paint mRenderPaint;
/** the colortemplate the chart uses */
protected ColorTemplate mCt;
/** description text that appears in the bottom right corner of the chart */
protected String mDescription = "Description.";
@ -186,6 +182,9 @@ public abstract class Chart extends View {
mOffsetLeft = (int) Utils.convertDpToPixel(mOffsetLeft);
mOffsetRight = (int) Utils.convertDpToPixel(mOffsetRight);
mOffsetTop = (int) Utils.convertDpToPixel(mOffsetTop);
mRenderPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mRenderPaint.setStyle(Style.FILL);
mDrawPaint = new Paint();
@ -204,7 +203,8 @@ public abstract class Chart extends View {
mValuePaint.setTextAlign(Align.CENTER);
mValuePaint.setTextSize(Utils.convertDpToPixel(9f));
setColorTemplate(new ColorTemplate(ColorTemplate.COLORFUL_COLORS, getContext()));
mCt = new ColorTemplate();
mCt.addDataSetColors(ColorTemplate.LIBERTY_COLORS, getContext());
}
/**
@ -607,10 +607,11 @@ public abstract class Chart extends View {
/**
* checks if the given index is set for highlighting or not
*
* @param index
* @param xIndex
* @param dataSetIndex
* @return
*/
public boolean needsHighlight(int index, int dataSet) {
public boolean needsHighlight(int xIndex, int dataSetIndex) {
// no highlight
if (!valuesToHighlight())
@ -619,8 +620,8 @@ public abstract class Chart extends View {
for (int i = 0; i < mIndicesToHightlight.length; i++)
// check if the xvalue for the given dataset needs highlight
if (mIndicesToHightlight[i].getXIndex() == index
&& mIndicesToHightlight[i].getDataSetIndex() == dataSet)
if (mIndicesToHightlight[i].getXIndex() == xIndex
&& mIndicesToHightlight[i].getDataSetIndex() == dataSetIndex && xIndex <= mDeltaX)
return true;
return false;
@ -963,10 +964,18 @@ public abstract class Chart extends View {
* @param ct
*/
public void setColorTemplate(ColorTemplate ct) {
this.mColorTemplate = ct;
this.mCt = ct;
prepareDataPaints(ct);
}
/**
* returns the colortemplate used by the chart
* @return
*/
public ColorTemplate getColorTemplate() {
return mCt;
}
/**
* sets the view that is displayed when a value is clicked on the chart

View file

@ -31,8 +31,8 @@ public class ChartData {
/**
* constructor for chart data
*
* @param xVals must be at least as long as the longest series array of one
* type
* @param xVals must be at least as long as the highest xIndex in the Series
* objects across all DataSets
* @param dataSets all DataSet objects the chart needs to represent
*/
public ChartData(ArrayList<String> xVals, ArrayList<DataSet> dataSets) {
@ -178,9 +178,10 @@ public class ChartData {
return null;
}
/**
* returns all DataSet objects the ChartData represents.
*
* @return
*/
public ArrayList<DataSet> getDataSets() {

View file

@ -8,8 +8,8 @@ import java.util.ArrayList;
public class ColorTemplate {
/**
* THE COLOR THEMES BELOW ARE PREDEFINED, FEEL FREE TO CREATE YOUR OWN WITH
* AS MANY DIFFERENT COLORS AS YOU WANT
* THE COLOR THEMES ARE PREDEFINED, FEEL FREE TO CREATE YOUR OWN WITH AS
* MANY DIFFERENT COLORS AS YOU WANT
*/
public static final int[] FRESH_COLORS = {
@ -35,33 +35,116 @@ public class ColorTemplate {
R.color.joyful_5
};
/** the array of colors the template represents */
private ArrayList<Integer> mColors;
/** an arraylist of color arrays (one color array per dataset) */
private ArrayList<ArrayList<Integer>> mDataSetColors;
/** the total amount of different colors in the template */
private int mColorCount = 0;
/**
* constructor
* @param colors
*/
public ColorTemplate(ArrayList<Integer> colors) {
this.mColors = colors;
public ColorTemplate() {
mDataSetColors = new ArrayList<ArrayList<Integer>>();
}
/**
* constructor
* @param colors
* @param c
*/
public ColorTemplate(int[] colors, Context c) {
this.mColors = getColors(c, colors);
}
/**
* returns an array of colors this template represents
* returns the total amount of different colors in the template
*
* @return
*/
public ArrayList<Integer> getColors() {
return mColors;
public int getColorCount() {
return mColorCount;
}
/**
* Adds a new array of colors for one DataSet to the template. Make sure to
* use getResources().getColor(R.color.yourcolor) for the colors. Use
* ColorTemplate.createColors(...) to make a color arraylist.
*
* @param colors
*/
public void addDataSetColors(ArrayList<Integer> colors) {
mDataSetColors.add(colors);
mColorCount += colors.size();
}
/**
* Adds a new array of colors for one DataSet to the template. You can use
* R.color.yourcolor for the integer values. Conversion is done internally.
*
* @param colors
* @param c
*/
public void addDataSetColors(int[] colors, Context c) {
mDataSetColors.add(createColors(c, colors));
mColorCount += colors.length;
}
/**
* Adds colors to the ColorTemplate. Each of the colors will create a new
* dataset color array in the template with just one color. This is
* especially useful when you want each of your DataSets only to be
* represented by one color and not multiple.
*
* @param colors
* @param c
*/
public void addColorsForDataSets(ArrayList<Integer> colors) {
for (int i = 0; i < colors.size(); i++) {
ArrayList<Integer> clrs = new ArrayList<Integer>();
clrs.add(colors.get(i));
addDataSetColors(clrs);
}
}
/**
* Adds colors to the ColorTemplate. Each of the colors will create a new
* dataset color array in the template with just one color. This is
* especially useful when you want each of your DataSets only to be
* represented by one color and not multiple.
*
* @param colors
* @param c
*/
public void addColorsForDataSets(int[] colors, Context c) {
for (int i = 0; i < colors.length; i++) {
addDataSetColors(new int[] {
colors[i]
}, c);
}
}
/**
* Returns all color arrays the template represents.
*
* @return
*/
public ArrayList<ArrayList<Integer>> getColors() {
return mDataSetColors;
}
/**
* returns the dataset color array at the given index
*
* @param dataSetIndex
* @return
*/
public ArrayList<Integer> getDataSetColors(int dataSetIndex) {
return mDataSetColors.get(dataSetIndex);
}
/**
* returns the color value at the given index from the DataSet at the given
* index
*
* @param dataSetIndex
* @param colorIndex
* @return
*/
public int getDataSetColor(int dataSetIndex, int colorIndex) {
return mDataSetColors.get(dataSetIndex).get(colorIndex);
}
/**
@ -71,7 +154,7 @@ public class ColorTemplate {
* @param colors e.g. ColorTemplate.MONO_COLORS
* @return
*/
public static ArrayList<Integer> getColors(Context c, int[] colors) {
public static ArrayList<Integer> createColors(Context c, int[] colors) {
ArrayList<Integer> result = new ArrayList<Integer>();

View file

@ -39,6 +39,9 @@ public class DataSet {
this.mType = type;
this.mYVals = yVals;
if (yVals == null || yVals.size() <= 0)
return;
calcMinMax();
calcYValueSum();
}

View file

@ -4,7 +4,6 @@ package com.github.mikephil.charting;
import android.content.Context;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Path;
import android.util.AttributeSet;
@ -68,18 +67,18 @@ public class LineChart extends BarLineChartBase {
@Override
protected void prepareDataPaints(ColorTemplate ct) {
if (ct == null)
return;
mDrawPaints = new Paint[ct.getColors().size()];
for (int i = 0; i < ct.getColors().size(); i++) {
mDrawPaints[i] = new Paint(Paint.ANTI_ALIAS_FLAG);
mDrawPaints[i].setStrokeWidth(mLineWidth);
mDrawPaints[i].setStyle(Style.FILL);
mDrawPaints[i].setColor(ct.getColors().get(i));
}
//
// if (ct == null)
// return;
//
// mDrawPaints = new Paint[ct.getColors().size()];
//
// for (int i = 0; i < ct.getColors().size(); i++) {
// mDrawPaints[i] = new Paint(Paint.ANTI_ALIAS_FLAG);
// mDrawPaints[i].setStrokeWidth(mLineWidth);
// mDrawPaints[i].setStyle(Style.FILL);
// // mDrawPaints[i].setColor(ct.getColors().get(i));
// }
}
@Override
@ -89,10 +88,11 @@ public class LineChart extends BarLineChartBase {
if (mHighlightEnabled && valuesToHighlight()) {
for (int i = 0; i < mIndicesToHightlight.length; i++) {
DataSet set = getDataSetByIndex(mIndicesToHightlight[i].getDataSetIndex());
int xIndex = mIndicesToHightlight[i].getXIndex(); // get the x-position
int xIndex = mIndicesToHightlight[i].getXIndex(); // get the
// x-position
float y = set.getYValForXIndex(xIndex); // get the y-position
float[] pts = new float[] {
@ -122,10 +122,18 @@ public class LineChart extends BarLineChartBase {
float[] valuePoints = generateTransformedValues(series, 0f);
Paint paint = mDrawPaints[i % mDrawPaints.length];
// Get the colors for the DataSet at the current index. If the index
// is out of bounds, reuse DataSet colors.
ArrayList<Integer> colors = mCt.getDataSetColors(i % mCt.getColors().size());
Paint paint = mRenderPaint;
for (int j = 0; j < valuePoints.length - 2; j += 2) {
// Set the color for the currently drawn value. If the index is
// out of bounds, reuse colors.
paint.setColor(colors.get(j % colors.size()));
if (isOffContentRight(valuePoints[j]))
break;
@ -137,13 +145,12 @@ public class LineChart extends BarLineChartBase {
valuePoints[j + 3], paint);
}
// if drawing filled is enabled
if (mDrawFilled) {
// mDrawCanvas.drawVertices(VertexMode.TRIANGLE_STRIP,
// valuePoints.length, valuePoints, 0,
// null, 0, null, 0, null, 0, 0, paint);
// filled is drawn with less alpha
paint.setAlpha(85);
@ -164,7 +171,7 @@ public class LineChart extends BarLineChartBase {
transformPath(filled);
mDrawCanvas.drawPath(filled, paint);
// restore alpha
paint.setAlpha(255);
}
@ -230,10 +237,19 @@ public class LineChart extends BarLineChartBase {
DataSet dataSet = dataSets.get(i);
ArrayList<Series> series = dataSet.getYVals();
// Get the colors for the DataSet at the current index. If the
// index
// is out of bounds, reuse DataSet colors.
ArrayList<Integer> colors = mCt.getDataSetColors(i % mCt.getColors().size());
float[] positions = generateTransformedValues(series, 0f);
for (int j = 0; j < positions.length; j += 2) {
// Set the color for the currently drawn value. If the index is
// out of bounds, reuse colors.
mRenderPaint.setColor(colors.get(j % colors.size()));
if (isOffContentRight(positions[j]))
break;
@ -244,7 +260,7 @@ public class LineChart extends BarLineChartBase {
continue;
mDrawCanvas.drawCircle(positions[j], positions[j + 1], mCircleSize,
mDrawPaints[i % mDrawPaints.length]);
mRenderPaint);
mDrawCanvas.drawCircle(positions[j], positions[j + 1], mCircleSize / 2,
mCirclePaintInner);
}
@ -323,9 +339,7 @@ public class LineChart extends BarLineChartBase {
width = 10.0f;
mLineWidth = width;
for (int i = 0; i < mDrawPaints.length; i++) {
mDrawPaints[i].setStrokeWidth(mLineWidth);
}
mRenderPaint.setStrokeWidth(width);
}
/**

File diff suppressed because it is too large Load diff

View file

@ -131,8 +131,11 @@ public class PieChartTouchListener extends SimpleOnGestureListener implements On
} else {
int index = mChart.getIndexForAngle(mChart.getAngleForPoint(e.getX(), e.getY()));
int dataSetIndex = mChart.getDataSetIndexForIndex(index);
if(dataSetIndex == -1) mChart.highlightValues(null);
mChart.highlightValues(new Highlight[] { new Highlight(index, 0) });
mChart.highlightValues(new Highlight[] { new Highlight(index, dataSetIndex) });
}
return true;