| |
| package com.xxmassdeveloper.mpchartexample; |
| |
| import android.graphics.Typeface; |
| import android.os.Bundle; |
| import android.util.Log; |
| 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.charts.PieChart; |
| import com.github.mikephil.charting.data.Entry; |
| import com.github.mikephil.charting.data.PieData; |
| import com.github.mikephil.charting.data.PieDataSet; |
| import com.github.mikephil.charting.interfaces.OnChartValueSelectedListener; |
| import com.github.mikephil.charting.utils.ColorTemplate; |
| import com.github.mikephil.charting.utils.Highlight; |
| import com.github.mikephil.charting.utils.Legend; |
| import com.github.mikephil.charting.utils.Legend.LegendPosition; |
| import com.xxmassdeveloper.mpchartexample.notimportant.DemoBase; |
| |
| import java.util.ArrayList; |
| |
| public class PieChartActivity extends DemoBase implements OnSeekBarChangeListener, |
| OnChartValueSelectedListener { |
| |
| private PieChart 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_piechart); |
| |
| tvX = (TextView) findViewById(R.id.tvXMax); |
| tvY = (TextView) findViewById(R.id.tvYMax); |
| |
| mSeekBarX = (SeekBar) findViewById(R.id.seekBar1); |
| mSeekBarY = (SeekBar) findViewById(R.id.seekBar2); |
| |
| mSeekBarX.setOnSeekBarChangeListener(this); |
| mSeekBarY.setOnSeekBarChangeListener(this); |
| |
| mChart = (PieChart) findViewById(R.id.chart1); |
| |
| Typeface tf = Typeface.createFromAsset(getAssets(), "OpenSans-Regular.ttf"); |
| |
| mChart.setValueTypeface(tf); |
| mChart.setCenterTextTypeface(Typeface.createFromAsset(getAssets(), "OpenSans-Light.ttf")); |
| |
| mChart.setHoleRadius(60f); |
| |
| mChart.setDescription(""); |
| |
| mChart.setDrawYValues(true); |
| mChart.setDrawCenterText(true); |
| |
| mChart.setDrawHoleEnabled(true); |
| |
| mChart.setRotationAngle(0); |
| |
| // draws the corresponding description value into the slice |
| mChart.setDrawXValues(true); |
| |
| // enable rotation of the chart by touch |
| mChart.setRotationEnabled(true); |
| |
| // display percentage values |
| mChart.setUsePercentValues(true); |
| // mChart.setUnit(" €"); |
| // mChart.setDrawUnitsInChart(true); |
| |
| // add a selection listener |
| mChart.setOnChartValueSelectedListener(this); |
| // mChart.setTouchEnabled(false); |
| |
| mSeekBarX.setProgress(3); |
| mSeekBarY.setProgress(100); |
| |
| mChart.animateXY(1500, 1500); |
| |
| Legend l = mChart.getLegend(); |
| l.setPosition(LegendPosition.RIGHT_OF_CHART); |
| l.setXEntrySpace(7f); |
| l.setYEntrySpace(5f); |
| } |
| |
| @Override |
| public boolean onCreateOptionsMenu(Menu menu) { |
| getMenuInflater().inflate(R.menu.pie, menu); |
| return true; |
| } |
| |
| @Override |
| public boolean onOptionsItemSelected(MenuItem item) { |
| |
| switch (item.getItemId()) { |
| case R.id.actionToggleValues: { |
| if (mChart.isDrawYValuesEnabled()) |
| mChart.setDrawYValues(false); |
| else |
| mChart.setDrawYValues(true); |
| mChart.invalidate(); |
| break; |
| } |
| case R.id.actionTogglePercent: { |
| if (mChart.isUsePercentValuesEnabled()) |
| mChart.setUsePercentValues(false); |
| else |
| mChart.setUsePercentValues(true); |
| mChart.invalidate(); |
| break; |
| } |
| case R.id.actionToggleHole: { |
| if (mChart.isDrawHoleEnabled()) |
| mChart.setDrawHoleEnabled(false); |
| else |
| mChart.setDrawHoleEnabled(true); |
| mChart.invalidate(); |
| break; |
| } |
| case R.id.actionDrawCenter: { |
| if (mChart.isDrawCenterTextEnabled()) |
| mChart.setDrawCenterText(false); |
| else |
| mChart.setDrawCenterText(true); |
| mChart.invalidate(); |
| break; |
| } |
| case R.id.actionToggleXVals: { |
| if (mChart.isDrawXValuesEnabled()) |
| mChart.setDrawXValues(false); |
| else |
| mChart.setDrawXValues(true); |
| mChart.invalidate(); |
| break; |
| } |
| case R.id.actionSave: { |
| // mChart.saveToGallery("title"+System.currentTimeMillis()); |
| mChart.saveToPath("title" + System.currentTimeMillis(), ""); |
| break; |
| } |
| case R.id.animateX: { |
| mChart.animateX(1800); |
| break; |
| } |
| case R.id.animateY: { |
| mChart.animateY(1800); |
| break; |
| } |
| case R.id.animateXY: { |
| mChart.animateXY(1800, 1800); |
| break; |
| } |
| } |
| return true; |
| } |
| |
| private String[] mParties = new String[] { |
| "Party A", "Party B", "Party C", "Party D", "Party E" |
| }; |
| |
| @Override |
| public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { |
| |
| tvX.setText("" + (mSeekBarX.getProgress() + 1)); |
| tvY.setText("" + (mSeekBarY.getProgress())); |
| |
| float mult = (float) mSeekBarY.getProgress(); |
| |
| ArrayList<Entry> yVals1 = new ArrayList<Entry>(); |
| // ArrayList<Entry> yVals2 = new ArrayList<Entry>(); |
| |
| // IMPORTANT: In a PieChart, no values (Entry) should have the same |
| // xIndex (even if from different DataSets), since no values can be |
| // drawn above each other. |
| for (int i = 0; i < mSeekBarX.getProgress() + 1; i++) { |
| yVals1.add(new Entry((float) (Math.random() * mult) + mult / 5, i)); |
| } |
| |
| // for (int i = mSeekBarX.getProgress() / 2; i < |
| // mSeekBarX.getProgress(); i++) { |
| // yVals2.add(new Entry((float) (Math.random() * mult) + mult / 5, i)); |
| // } |
| |
| ArrayList<String> xVals = new ArrayList<String>(); |
| |
| for (int i = 0; i < mSeekBarX.getProgress() + 1; i++) |
| xVals.add(mParties[i % mParties.length]); |
| |
| PieDataSet set1 = new PieDataSet(yVals1, "Election Results"); |
| set1.setSliceSpace(3f); |
| set1.setColors(ColorTemplate.createColors(getApplicationContext(), |
| ColorTemplate.VORDIPLOM_COLORS)); |
| |
| PieData data = new PieData(xVals, set1); |
| mChart.setData(data); |
| |
| // undo all highlights |
| mChart.highlightValues(null); |
| |
| // set a text for the chart center |
| mChart.setCenterText("Total Value\n" + (int) mChart.getYValueSum() + "\n(all slices)"); |
| mChart.invalidate(); |
| } |
| |
| @Override |
| public void onValueSelected(Entry e, int dataSetIndex) { |
| Log.i("VAL SELECTED", |
| "Value: " + e.getVal() + ", xIndex: " + e.getXIndex() |
| + ", DataSet index: " + dataSetIndex); |
| } |
| |
| @Override |
| public void onNothingSelected() { |
| Log.i("PieChart", "nothing selected"); |
| } |
| |
| @Override |
| public void onStartTrackingTouch(SeekBar seekBar) { |
| // TODO Auto-generated method stub |
| |
| } |
| |
| @Override |
| public void onStopTrackingTouch(SeekBar seekBar) { |
| // TODO Auto-generated method stub |
| |
| } |
| } |