Skip to content

Commit a3075e9

Browse files
committed
Improved the piechart, added percentage and legend. Added selectionlistener. Improved example.
1 parent e16e0da commit a3075e9

9 files changed

Lines changed: 248 additions & 34 deletions

File tree

‎MPChartExample/res/menu/pie.xml‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
33
<item android:id="@+id/actionToggleValues" android:title="Toggle Values"></item>
4+
<item android:id="@+id/actionToggleXVals" android:title="Toggle X-Values"></item>
5+
<item android:id="@+id/actionTogglePercent" android:title="Toggle Percent"></item>
46
<item android:id="@+id/actionToggleHole" android:title="Toggle Hole"></item>
57
<item android:id="@+id/actionDrawCenter" android:title="Draw CenterText"></item>
68
<item android:id="@+id/actionSave" android:title="Save to Gallery"></item>
9+
710
</menu>

‎MPChartExample/src/com/example/mpchartexample/MultipleChartsActivity.java‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,14 @@ protected void onCreate(Bundle savedInstanceState) {
4141
small.add(6f);
4242

4343
for(int i = 0; i < small.size(); i++) {
44-
xvalsSmall.add(""+i);
44+
xvalsSmall.add("Val"+i);
4545
}
4646

4747
ArrayList<Float> large = new ArrayList<Float>();
4848

4949
for(int i = 0; i < 1000; i++) {
5050
large.add((float) (Math.random() * 50));
51-
xvalsLarge.add(""+i);
51+
xvalsLarge.add("Val"+i);
5252
}
5353

5454
mLineChart.setData(xvalsLarge, large);
@@ -69,6 +69,7 @@ protected void onCreate(Bundle savedInstanceState) {
6969
mPieChart.setColorTemplate(new ColorTemplate(ColorTemplate.getColors(this, ColorTemplate.FRESH_COLORS)));
7070
// mChart3.highlightValues(new int[] {0, 1, 2, 3, 4} );
7171
mPieChart.setDrawValues(true);
72+
mPieChart.setDrawXVals(false);
7273

7374
mBarChart3D.setData(xvalsLarge, large);
7475
mBarChart3D.setColorTemplate(new ColorTemplate(ColorTemplate.getColors(this, ColorTemplate.LIBERTY_COLORS)));

‎MPChartExample/src/com/example/mpchartexample/PieChartActivity.java‎

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import android.app.Activity;
44
import android.os.Bundle;
5-
import android.os.Handler;
65
import android.util.Log;
76
import android.view.Menu;
87
import android.view.MenuItem;
@@ -12,11 +11,12 @@
1211
import android.widget.TextView;
1312

1413
import com.github.mikephil.charting.ColorTemplate;
14+
import com.github.mikephil.charting.OnChartValueSelectedListener;
1515
import com.github.mikephil.charting.PieChart;
1616

1717
import java.util.ArrayList;
1818

19-
public class PieChartActivity extends Activity implements OnSeekBarChangeListener {
19+
public class PieChartActivity extends Activity implements OnSeekBarChangeListener, OnChartValueSelectedListener {
2020

2121
private PieChart mChart;
2222
private SeekBar mSeekBarX, mSeekBarY;
@@ -41,15 +41,17 @@ protected void onCreate(Bundle savedInstanceState) {
4141
mChart = (PieChart) findViewById(R.id.chart1);
4242
mChart.setColorTemplate(new ColorTemplate(ColorTemplate.getColors(this, ColorTemplate.COLORFUL_COLORS)));
4343

44-
mChart.setDrawValues(false);
44+
mChart.setDrawValues(true);
4545
mChart.setDrawCenterText(true);
4646

4747
mChart.setDescription("This is a description.");
4848
mChart.setDrawHoleEnabled(true);
49-
49+
mChart.setDrawXVals(true);
5050
mChart.setTouchEnabled(true);
51+
mChart.setUsePercentValues(false);
52+
mChart.setOnChartValueSelectedListener(this);
5153

52-
mSeekBarX.setProgress(10);
54+
mSeekBarX.setProgress(5);
5355
mSeekBarY.setProgress(100);
5456

5557
// float diameter = mChart.getDiameter();
@@ -76,6 +78,14 @@ public boolean onOptionsItemSelected(MenuItem item) {
7678
mChart.invalidate();
7779
break;
7880
}
81+
case R.id.actionTogglePercent: {
82+
if (mChart.isUsePercentValuesEnabled())
83+
mChart.setUsePercentValues(false);
84+
else
85+
mChart.setUsePercentValues(true);
86+
mChart.invalidate();
87+
break;
88+
}
7989
case R.id.actionToggleHole: {
8090
if (mChart.isDrawHoleEnabled())
8191
mChart.setDrawHoleEnabled(false);
@@ -92,6 +102,14 @@ public boolean onOptionsItemSelected(MenuItem item) {
92102
mChart.invalidate();
93103
break;
94104
}
105+
case R.id.actionToggleXVals: {
106+
if (mChart.isDrawXValsEnabled())
107+
mChart.setDrawXVals(false);
108+
else
109+
mChart.setDrawXVals(true);
110+
mChart.invalidate();
111+
break;
112+
}
95113
case R.id.actionSave: {
96114
// mChart.saveToGallery("title"+System.currentTimeMillis());
97115
mChart.saveToPath("title"+System.currentTimeMillis(), "");
@@ -117,12 +135,27 @@ public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
117135

118136
ArrayList<String> xVals = new ArrayList<String>();
119137

120-
for(int i = 0; i < yVals.size(); i++) xVals.add(""+i);
138+
for(int i = 0; i < yVals.size(); i++) xVals.add("Text"+(i+1));
121139

122140
mChart.setData(xVals, yVals);
141+
mChart.setCenterText("Total Value\n" + (int) mChart.getYValueSum() + "\n(all slices)");
123142
mChart.invalidate();
124143
}
144+
145+
@Override
146+
public void onValuesSelected(float[] values, int[] indices) {
147+
StringBuffer a = new StringBuffer();
148+
149+
for(int i = 0; i < values.length; i++) a.append("val: " + values[i] + ", ind: " + indices[i] + "\n");
150+
151+
Log.i("PieChart", "Selected: " + a.toString());
152+
}
125153

154+
@Override
155+
public void onNothingSelected() {
156+
Log.i("PieChart", "nothing selected");
157+
}
158+
126159
@Override
127160
public void onStartTrackingTouch(SeekBar seekBar) {
128161
// TODO Auto-generated method stub

‎MPChartLib/src/com/github/mikephil/charting/BarChart.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ protected void drawData() {
202202

203203
@Override
204204
public void highlightValues(int[] indices) {
205-
205+
super.highlightValues(indices);
206206
}
207207

208208
/**

‎MPChartLib/src/com/github/mikephil/charting/BarLineChartBase.java‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -694,4 +694,9 @@ public void setGridWidth(float width) {
694694
width = 3.0f;
695695
mGridWidth = width;
696696
}
697+
698+
@Override
699+
protected void highlightValues(int[] indices) {
700+
super.highlightValues(indices);
701+
}
697702
}

‎MPChartLib/src/com/github/mikephil/charting/Chart.java‎

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,9 @@ public abstract class Chart extends View {
122122
/** this rectangle defines the area in which graph values can be drawn */
123123
protected Rect mContentRect;
124124

125+
/** listener that is called when a value on the chart is selected */
126+
protected OnChartValueSelectedListener mSelectionListener;
127+
125128
/** default constructor for initialization in code */
126129
public Chart(Context context) {
127130
super(context);
@@ -482,7 +485,22 @@ protected int calcTextWidth(Paint paint, String demoText) {
482485
*
483486
* @param indices
484487
*/
485-
public abstract void highlightValues(int[] indices);
488+
protected void highlightValues(int[] indices) {
489+
490+
if(mSelectionListener != null) {
491+
492+
if(indices[0] == -1) mSelectionListener.onNothingSelected();
493+
else {
494+
495+
float[] values = new float[indices.length];
496+
497+
for(int i = 0; i < values.length; i++) values[i] = getYValue(indices[i]);
498+
499+
// notify the listener
500+
mSelectionListener.onValuesSelected(values, indices);
501+
}
502+
}
503+
}
486504

487505
/**
488506
* ################ ################ ################ ################
@@ -867,6 +885,47 @@ public boolean isDrawValuesEnabled() {
867885
return mDrawValues;
868886
}
869887

888+
/**
889+
* returns the y-value at the given index
890+
*
891+
* @param index
892+
* @return
893+
*/
894+
public float getYValue(int index) {
895+
return mYVals.get(index);
896+
}
897+
898+
/**
899+
* returns the x-value at the given index
900+
*
901+
* @param index
902+
* @return
903+
*/
904+
public String getXValue(int index) {
905+
if (mXVals == null || mXVals.size() >= index)
906+
return null;
907+
else
908+
return mXVals.get(index);
909+
}
910+
911+
/**
912+
* returns the percentage the given value has of the total y-values
913+
*
914+
* @param val
915+
* @return
916+
*/
917+
public float getPercentOfTotal(float val) {
918+
return val / mYValueSum * 100f;
919+
}
920+
921+
/**
922+
* set a selection listener for the chart
923+
* @param l
924+
*/
925+
public void setOnChartValueSelectedListener(OnChartValueSelectedListener l) {
926+
this.mSelectionListener = l;
927+
}
928+
870929
/**
871930
* saves the current chart state to a bitmap in the gallery NOTE: Needs
872931
* permission WRITE_EXTERNAL_STORAGE

‎MPChartLib/src/com/github/mikephil/charting/LineChart.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ protected void drawAdditional() {
148148

149149
@Override
150150
public void highlightValues(int[] indices) {
151-
151+
super.highlightValues(indices);
152152
}
153153

154154
/**
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.github.mikephil.charting;
2+
3+
public interface OnChartValueSelectedListener {
4+
5+
public void onValuesSelected(float[] values, int[] indices);
6+
public void onNothingSelected();
7+
}

0 commit comments

Comments
 (0)