22
33import android .app .Activity ;
44import android .os .Bundle ;
5- import android .os .Handler ;
65import android .util .Log ;
76import android .view .Menu ;
87import android .view .MenuItem ;
1211import android .widget .TextView ;
1312
1413import com .github .mikephil .charting .ColorTemplate ;
14+ import com .github .mikephil .charting .OnChartValueSelectedListener ;
1515import com .github .mikephil .charting .PieChart ;
1616
1717import 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
0 commit comments