|
| 1 | + |
| 2 | +package com.xxmassdeveloper.mpchartexample; |
| 3 | + |
| 4 | +import android.graphics.Color; |
| 5 | +import android.graphics.Typeface; |
| 6 | +import android.os.Bundle; |
| 7 | +import android.text.SpannableString; |
| 8 | +import android.text.style.ForegroundColorSpan; |
| 9 | +import android.text.style.RelativeSizeSpan; |
| 10 | +import android.text.style.StyleSpan; |
| 11 | +import android.util.Log; |
| 12 | +import android.view.Menu; |
| 13 | +import android.view.MenuItem; |
| 14 | +import android.view.WindowManager; |
| 15 | +import android.widget.SeekBar; |
| 16 | +import android.widget.SeekBar.OnSeekBarChangeListener; |
| 17 | +import android.widget.TextView; |
| 18 | + |
| 19 | +import com.github.mikephil.charting.animation.Easing; |
| 20 | +import com.github.mikephil.charting.charts.PieChart; |
| 21 | +import com.github.mikephil.charting.components.Legend; |
| 22 | +import com.github.mikephil.charting.components.Legend.LegendPosition; |
| 23 | +import com.github.mikephil.charting.data.Entry; |
| 24 | +import com.github.mikephil.charting.data.PieData; |
| 25 | +import com.github.mikephil.charting.data.PieDataSet; |
| 26 | +import com.github.mikephil.charting.formatter.PercentFormatter; |
| 27 | +import com.github.mikephil.charting.highlight.Highlight; |
| 28 | +import com.github.mikephil.charting.interfaces.datasets.IDataSet; |
| 29 | +import com.github.mikephil.charting.listener.OnChartValueSelectedListener; |
| 30 | +import com.github.mikephil.charting.utils.ColorTemplate; |
| 31 | +import com.xxmassdeveloper.mpchartexample.notimportant.DemoBase; |
| 32 | + |
| 33 | +import java.util.ArrayList; |
| 34 | + |
| 35 | +public class PiePolylineChartActivity extends DemoBase implements OnSeekBarChangeListener, |
| 36 | + OnChartValueSelectedListener { |
| 37 | + |
| 38 | + private PieChart mChart; |
| 39 | + private SeekBar mSeekBarX, mSeekBarY; |
| 40 | + private TextView tvX, tvY; |
| 41 | + |
| 42 | + private Typeface tf; |
| 43 | + |
| 44 | + @Override |
| 45 | + protected void onCreate(Bundle savedInstanceState) { |
| 46 | + super.onCreate(savedInstanceState); |
| 47 | + getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, |
| 48 | + WindowManager.LayoutParams.FLAG_FULLSCREEN); |
| 49 | + setContentView(R.layout.activity_piechart); |
| 50 | + |
| 51 | + tvX = (TextView) findViewById(R.id.tvXMax); |
| 52 | + tvY = (TextView) findViewById(R.id.tvYMax); |
| 53 | + |
| 54 | + mSeekBarX = (SeekBar) findViewById(R.id.seekBar1); |
| 55 | + mSeekBarY = (SeekBar) findViewById(R.id.seekBar2); |
| 56 | + |
| 57 | + mSeekBarY.setProgress(10); |
| 58 | + |
| 59 | + mSeekBarX.setOnSeekBarChangeListener(this); |
| 60 | + mSeekBarY.setOnSeekBarChangeListener(this); |
| 61 | + |
| 62 | + mChart = (PieChart) findViewById(R.id.chart1); |
| 63 | + mChart.setUsePercentValues(true); |
| 64 | + mChart.setDescription(""); |
| 65 | + mChart.setExtraOffsets(5, 10, 5, 5); |
| 66 | + |
| 67 | + mChart.setDragDecelerationFrictionCoef(0.95f); |
| 68 | + |
| 69 | + tf = Typeface.createFromAsset(getAssets(), "OpenSans-Regular.ttf"); |
| 70 | + |
| 71 | + mChart.setCenterTextTypeface(Typeface.createFromAsset(getAssets(), "OpenSans-Light.ttf")); |
| 72 | + mChart.setCenterText(generateCenterSpannableText()); |
| 73 | + |
| 74 | + mChart.setExtraOffsets(0.f, 50.f, 0.f, 50.f); |
| 75 | + |
| 76 | + mChart.setDrawHoleEnabled(true); |
| 77 | + mChart.setHoleColor(Color.WHITE); |
| 78 | + |
| 79 | + mChart.setTransparentCircleColor(Color.WHITE); |
| 80 | + mChart.setTransparentCircleAlpha(110); |
| 81 | + |
| 82 | + mChart.setHoleRadius(58f); |
| 83 | + mChart.setTransparentCircleRadius(61f); |
| 84 | + |
| 85 | + mChart.setDrawCenterText(true); |
| 86 | + |
| 87 | + mChart.setRotationAngle(0); |
| 88 | + // enable rotation of the chart by touch |
| 89 | + mChart.setRotationEnabled(true); |
| 90 | + mChart.setHighlightPerTapEnabled(true); |
| 91 | + |
| 92 | + // mChart.setUnit(" €"); |
| 93 | + // mChart.setDrawUnitsInChart(true); |
| 94 | + |
| 95 | + // add a selection listener |
| 96 | + mChart.setOnChartValueSelectedListener(this); |
| 97 | + |
| 98 | + setData(3, 100); |
| 99 | + |
| 100 | + mChart.animateY(1400, Easing.EasingOption.EaseInOutQuad); |
| 101 | + // mChart.spin(2000, 0, 360); |
| 102 | + |
| 103 | + Legend l = mChart.getLegend(); |
| 104 | + l.setPosition(LegendPosition.RIGHT_OF_CHART); |
| 105 | + l.setEnabled(false); |
| 106 | + } |
| 107 | + |
| 108 | + @Override |
| 109 | + public boolean onCreateOptionsMenu(Menu menu) { |
| 110 | + getMenuInflater().inflate(R.menu.pie, menu); |
| 111 | + return true; |
| 112 | + } |
| 113 | + |
| 114 | + @Override |
| 115 | + public boolean onOptionsItemSelected(MenuItem item) { |
| 116 | + |
| 117 | + switch (item.getItemId()) { |
| 118 | + case R.id.actionToggleValues: { |
| 119 | + for (IDataSet<?> set : mChart.getData().getDataSets()) |
| 120 | + set.setDrawValues(!set.isDrawValuesEnabled()); |
| 121 | + |
| 122 | + mChart.invalidate(); |
| 123 | + break; |
| 124 | + } |
| 125 | + case R.id.actionToggleHole: { |
| 126 | + if (mChart.isDrawHoleEnabled()) |
| 127 | + mChart.setDrawHoleEnabled(false); |
| 128 | + else |
| 129 | + mChart.setDrawHoleEnabled(true); |
| 130 | + mChart.invalidate(); |
| 131 | + break; |
| 132 | + } |
| 133 | + case R.id.actionDrawCenter: { |
| 134 | + if (mChart.isDrawCenterTextEnabled()) |
| 135 | + mChart.setDrawCenterText(false); |
| 136 | + else |
| 137 | + mChart.setDrawCenterText(true); |
| 138 | + mChart.invalidate(); |
| 139 | + break; |
| 140 | + } |
| 141 | + case R.id.actionToggleXVals: { |
| 142 | + |
| 143 | + mChart.setDrawSliceText(!mChart.isDrawSliceTextEnabled()); |
| 144 | + mChart.invalidate(); |
| 145 | + break; |
| 146 | + } |
| 147 | + case R.id.actionSave: { |
| 148 | + // mChart.saveToGallery("title"+System.currentTimeMillis()); |
| 149 | + mChart.saveToPath("title" + System.currentTimeMillis(), ""); |
| 150 | + break; |
| 151 | + } |
| 152 | + case R.id.actionTogglePercent: |
| 153 | + mChart.setUsePercentValues(!mChart.isUsePercentValuesEnabled()); |
| 154 | + mChart.invalidate(); |
| 155 | + break; |
| 156 | + case R.id.animateX: { |
| 157 | + mChart.animateX(1400); |
| 158 | + break; |
| 159 | + } |
| 160 | + case R.id.animateY: { |
| 161 | + mChart.animateY(1400); |
| 162 | + break; |
| 163 | + } |
| 164 | + case R.id.animateXY: { |
| 165 | + mChart.animateXY(1400, 1400); |
| 166 | + break; |
| 167 | + } |
| 168 | + } |
| 169 | + return true; |
| 170 | + } |
| 171 | + |
| 172 | + @Override |
| 173 | + public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { |
| 174 | + |
| 175 | + tvX.setText("" + (mSeekBarX.getProgress() + 1)); |
| 176 | + tvY.setText("" + (mSeekBarY.getProgress())); |
| 177 | + |
| 178 | + setData(mSeekBarX.getProgress(), mSeekBarY.getProgress()); |
| 179 | + } |
| 180 | + |
| 181 | + private void setData(int count, float range) { |
| 182 | + |
| 183 | + float mult = range; |
| 184 | + |
| 185 | + ArrayList<Entry> yVals1 = new ArrayList<Entry>(); |
| 186 | + |
| 187 | + // IMPORTANT: In a PieChart, no values (Entry) should have the same |
| 188 | + // xIndex (even if from different DataSets), since no values can be |
| 189 | + // drawn above each other. |
| 190 | + for (int i = 0; i < count + 1; i++) { |
| 191 | + yVals1.add(new Entry((float) (Math.random() * mult) + mult / 5, i)); |
| 192 | + } |
| 193 | + |
| 194 | + ArrayList<String> xVals = new ArrayList<String>(); |
| 195 | + |
| 196 | + for (int i = 0; i < count + 1; i++) |
| 197 | + xVals.add(mParties[i % mParties.length]); |
| 198 | + |
| 199 | + PieDataSet dataSet = new PieDataSet(yVals1, "Election Results"); |
| 200 | + dataSet.setSliceSpace(3f); |
| 201 | + dataSet.setSelectionShift(5f); |
| 202 | + |
| 203 | + // add a lot of colors |
| 204 | + |
| 205 | + ArrayList<Integer> colors = new ArrayList<Integer>(); |
| 206 | + |
| 207 | + for (int c : ColorTemplate.VORDIPLOM_COLORS) |
| 208 | + colors.add(c); |
| 209 | + |
| 210 | + for (int c : ColorTemplate.JOYFUL_COLORS) |
| 211 | + colors.add(c); |
| 212 | + |
| 213 | + for (int c : ColorTemplate.COLORFUL_COLORS) |
| 214 | + colors.add(c); |
| 215 | + |
| 216 | + for (int c : ColorTemplate.LIBERTY_COLORS) |
| 217 | + colors.add(c); |
| 218 | + |
| 219 | + for (int c : ColorTemplate.PASTEL_COLORS) |
| 220 | + colors.add(c); |
| 221 | + |
| 222 | + colors.add(ColorTemplate.getHoloBlue()); |
| 223 | + |
| 224 | + dataSet.setColors(colors); |
| 225 | + //dataSet.setSelectionShift(0f); |
| 226 | + |
| 227 | + |
| 228 | + dataSet.setValueLinePart1OffsetPercentage(80.f); |
| 229 | + dataSet.setValueLinePart1Length(0.4f); |
| 230 | + dataSet.setValueLinePart2Length(0.5f); |
| 231 | + // dataSet.setXValuePosition(PieDataSet.ValuePosition.OUTSIDE_SLICE); |
| 232 | + dataSet.setYValuePosition(PieDataSet.ValuePosition.OUTSIDE_SLICE); |
| 233 | + |
| 234 | + PieData data = new PieData(xVals, dataSet); |
| 235 | + data.setValueFormatter(new PercentFormatter()); |
| 236 | + data.setValueTextSize(11f); |
| 237 | + data.setValueTextColor(Color.BLACK); |
| 238 | + data.setValueTypeface(tf); |
| 239 | + mChart.setData(data); |
| 240 | + |
| 241 | + // undo all highlights |
| 242 | + mChart.highlightValues(null); |
| 243 | + |
| 244 | + mChart.invalidate(); |
| 245 | + } |
| 246 | + |
| 247 | + private SpannableString generateCenterSpannableText() { |
| 248 | + |
| 249 | + SpannableString s = new SpannableString("MPAndroidChart\ndeveloped by Philipp Jahoda"); |
| 250 | + s.setSpan(new RelativeSizeSpan(1.7f), 0, 14, 0); |
| 251 | + s.setSpan(new StyleSpan(Typeface.NORMAL), 14, s.length() - 15, 0); |
| 252 | + s.setSpan(new ForegroundColorSpan(Color.GRAY), 14, s.length() - 15, 0); |
| 253 | + s.setSpan(new RelativeSizeSpan(.8f), 14, s.length() - 15, 0); |
| 254 | + s.setSpan(new StyleSpan(Typeface.ITALIC), s.length() - 14, s.length(), 0); |
| 255 | + s.setSpan(new ForegroundColorSpan(ColorTemplate.getHoloBlue()), s.length() - 14, s.length(), 0); |
| 256 | + return s; |
| 257 | + } |
| 258 | + |
| 259 | + @Override |
| 260 | + public void onValueSelected(Entry e, int dataSetIndex, Highlight h) { |
| 261 | + |
| 262 | + if (e == null) |
| 263 | + return; |
| 264 | + Log.i("VAL SELECTED", |
| 265 | + "Value: " + e.getVal() + ", xIndex: " + e.getXIndex() |
| 266 | + + ", DataSet index: " + dataSetIndex); |
| 267 | + } |
| 268 | + |
| 269 | + @Override |
| 270 | + public void onNothingSelected() { |
| 271 | + Log.i("PieChart", "nothing selected"); |
| 272 | + } |
| 273 | + |
| 274 | + @Override |
| 275 | + public void onStartTrackingTouch(SeekBar seekBar) { |
| 276 | + // TODO Auto-generated method stub |
| 277 | + |
| 278 | + } |
| 279 | + |
| 280 | + @Override |
| 281 | + public void onStopTrackingTouch(SeekBar seekBar) { |
| 282 | + // TODO Auto-generated method stub |
| 283 | + |
| 284 | + } |
| 285 | +} |
0 commit comments