11
22package com .xxmassdeveloper .mpchartexample ;
33
4+ import android .graphics .Color ;
45import android .graphics .Typeface ;
56import android .os .Bundle ;
67import android .util .Log ;
1314
1415import com .github .mikephil .charting .charts .PieChart ;
1516import com .github .mikephil .charting .data .Entry ;
17+ import com .github .mikephil .charting .data .LineData ;
18+ import com .github .mikephil .charting .data .LineDataSet ;
1619import com .github .mikephil .charting .data .PieData ;
1720import com .github .mikephil .charting .data .PieDataSet ;
1821import com .github .mikephil .charting .interfaces .OnChartValueSelectedListener ;
@@ -48,6 +51,9 @@ protected void onCreate(Bundle savedInstanceState) {
4851
4952 mChart = (PieChart ) findViewById (R .id .chart1 );
5053
54+ // change the color of the center-hole
55+ mChart .setHoleColor (Color .rgb (235 , 235 , 235 ));
56+
5157 Typeface tf = Typeface .createFromAsset (getAssets (), "OpenSans-Regular.ttf" );
5258
5359 mChart .setValueTypeface (tf );
@@ -78,9 +84,9 @@ protected void onCreate(Bundle savedInstanceState) {
7884 // add a selection listener
7985 mChart .setOnChartValueSelectedListener (this );
8086 // mChart.setTouchEnabled(false);
81-
87+
8288 setData (3 , 100 );
83-
89+
8490 mChart .animateXY (1500 , 1500 );
8591 // mChart.spin(2000, 0, 360);
8692
@@ -91,6 +97,47 @@ protected void onCreate(Bundle savedInstanceState) {
9197
9298 }
9399
100+ private void removeLastEntry () {
101+
102+ PieData data = mChart .getDataOriginal ();
103+
104+ if (data != null ) {
105+
106+ PieDataSet set = data .getDataSet ();
107+
108+ if (set != null ) {
109+
110+ Entry e = set .getEntryForXIndex (set .getEntryCount () - 1 );
111+
112+ data .removeEntry (e , 0 );
113+ // or remove by index
114+ // mData.removeEntry(xIndex, dataSetIndex);
115+
116+ mChart .notifyDataSetChanged ();
117+ mChart .invalidate ();
118+ }
119+ }
120+ }
121+
122+ private void addEntry () {
123+
124+ PieData data = mChart .getDataOriginal ();
125+
126+ if (data != null ) {
127+
128+ PieDataSet set = data .getDataSet ();
129+ // set.addEntry(...);
130+
131+ data .addEntry (new Entry ((float ) (Math .random () * 50 ) + 50f , set .getEntryCount ()), 0 );
132+
133+ // let the chart know it's data has changed
134+ mChart .notifyDataSetChanged ();
135+
136+ // redraw the chart
137+ mChart .invalidate ();
138+ }
139+ }
140+
94141 @ Override
95142 public boolean onCreateOptionsMenu (Menu menu ) {
96143 getMenuInflater ().inflate (R .menu .pie , menu );
@@ -102,19 +149,21 @@ public boolean onOptionsItemSelected(MenuItem item) {
102149
103150 switch (item .getItemId ()) {
104151 case R .id .actionToggleValues : {
105- if (mChart .isDrawYValuesEnabled ())
106- mChart .setDrawYValues (false );
107- else
108- mChart .setDrawYValues (true );
109- mChart .invalidate ();
152+ // if (mChart.isDrawYValuesEnabled())
153+ // mChart.setDrawYValues(false);
154+ // else
155+ // mChart.setDrawYValues(true);
156+ // mChart.invalidate();
157+ removeLastEntry ();
110158 break ;
111159 }
112160 case R .id .actionTogglePercent : {
113- if (mChart .isUsePercentValuesEnabled ())
114- mChart .setUsePercentValues (false );
115- else
116- mChart .setUsePercentValues (true );
117- mChart .invalidate ();
161+ // if (mChart.isUsePercentValuesEnabled())
162+ // mChart.setUsePercentValues(false);
163+ // else
164+ // mChart.setUsePercentValues(true);
165+ // mChart.invalidate();
166+ addEntry ();
118167 break ;
119168 }
120169 case R .id .actionToggleHole : {
@@ -163,7 +212,7 @@ public boolean onOptionsItemSelected(MenuItem item) {
163212 }
164213
165214 private String [] mParties = new String [] {
166- "Party A" , "Party B" , "Party C" , "Party D" , "Party E"
215+ "Party A" , "Party B" , "Party C" , "Party D" , "Party E" , "Party F" , "Party G"
167216 };
168217
169218 @ Override
@@ -174,9 +223,9 @@ public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
174223
175224 setData (mSeekBarX .getProgress (), mSeekBarY .getProgress ());
176225 }
177-
226+
178227 private void setData (int count , float range ) {
179-
228+
180229 float mult = range ;
181230
182231 ArrayList <Entry > yVals1 = new ArrayList <Entry >();
@@ -210,12 +259,16 @@ private void setData(int count, float range) {
210259 mChart .highlightValues (null );
211260
212261 // set a text for the chart center
213- mChart .setCenterText ("Total Value\n " + (int ) mChart .getYValueSum () + "\n (all slices)" );
262+ mChart .setCenterText ("MPAndroid\n Chart" );
263+
214264 mChart .invalidate ();
215265 }
216266
217267 @ Override
218268 public void onValueSelected (Entry e , int dataSetIndex ) {
269+
270+ if (e == null )
271+ return ;
219272 Log .i ("VAL SELECTED" ,
220273 "Value: " + e .getVal () + ", xIndex: " + e .getXIndex ()
221274 + ", DataSet index: " + dataSetIndex );
0 commit comments