|
| 1 | + |
| 2 | +package com.xxmassdeveloper.mpchartexample; |
| 3 | + |
| 4 | +import android.graphics.Color; |
| 5 | +import android.graphics.Point; |
| 6 | +import android.graphics.Typeface; |
| 7 | +import android.os.Bundle; |
| 8 | +import android.text.SpannableString; |
| 9 | +import android.text.style.ForegroundColorSpan; |
| 10 | +import android.text.style.RelativeSizeSpan; |
| 11 | +import android.text.style.StyleSpan; |
| 12 | +import android.view.Display; |
| 13 | +import android.view.WindowManager; |
| 14 | +import android.widget.RelativeLayout; |
| 15 | + |
| 16 | +import com.github.mikephil.charting.animation.Easing; |
| 17 | +import com.github.mikephil.charting.charts.PieChart; |
| 18 | +import com.github.mikephil.charting.components.Legend; |
| 19 | +import com.github.mikephil.charting.components.Legend.LegendPosition; |
| 20 | +import com.github.mikephil.charting.data.PieData; |
| 21 | +import com.github.mikephil.charting.data.PieDataSet; |
| 22 | +import com.github.mikephil.charting.data.PieEntry; |
| 23 | +import com.github.mikephil.charting.formatter.PercentFormatter; |
| 24 | +import com.github.mikephil.charting.utils.ColorTemplate; |
| 25 | +import com.xxmassdeveloper.mpchartexample.notimportant.DemoBase; |
| 26 | + |
| 27 | +import java.util.ArrayList; |
| 28 | + |
| 29 | +public class HalfPieChartActivity extends DemoBase { |
| 30 | + |
| 31 | + private PieChart mChart; |
| 32 | + |
| 33 | + @Override |
| 34 | + protected void onCreate(Bundle savedInstanceState) { |
| 35 | + super.onCreate(savedInstanceState); |
| 36 | + getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, |
| 37 | + WindowManager.LayoutParams.FLAG_FULLSCREEN); |
| 38 | + setContentView(R.layout.activity_piechart_half); |
| 39 | + |
| 40 | + mChart = (PieChart) findViewById(R.id.chart1); |
| 41 | + mChart.setBackgroundColor(Color.WHITE); |
| 42 | + |
| 43 | + moveOffScreen(); |
| 44 | + |
| 45 | + mChart.setUsePercentValues(true); |
| 46 | + mChart.setDescription(""); |
| 47 | + |
| 48 | + mChart.setCenterTextTypeface(mTfLight); |
| 49 | + mChart.setCenterText(generateCenterSpannableText()); |
| 50 | + |
| 51 | + mChart.setDrawHoleEnabled(true); |
| 52 | + mChart.setHoleColor(Color.WHITE); |
| 53 | + |
| 54 | + mChart.setTransparentCircleColor(Color.WHITE); |
| 55 | + mChart.setTransparentCircleAlpha(110); |
| 56 | + |
| 57 | + mChart.setHoleRadius(58f); |
| 58 | + mChart.setTransparentCircleRadius(61f); |
| 59 | + |
| 60 | + mChart.setDrawCenterText(true); |
| 61 | + |
| 62 | + mChart.setRotationEnabled(false); |
| 63 | + mChart.setHighlightPerTapEnabled(true); |
| 64 | + |
| 65 | + mChart.setMaxAngle(180f); // HALF CHART |
| 66 | + mChart.setRotationAngle(180f); |
| 67 | + mChart.setCenterTextOffset(0, -20); |
| 68 | + |
| 69 | + setData(4, 100); |
| 70 | + |
| 71 | + mChart.animateY(1400, Easing.EasingOption.EaseInOutQuad); |
| 72 | + |
| 73 | + Legend l = mChart.getLegend(); |
| 74 | + l.setPosition(LegendPosition.ABOVE_CHART_CENTER); |
| 75 | + l.setXEntrySpace(7f); |
| 76 | + l.setYEntrySpace(0f); |
| 77 | + l.setYOffset(0f); |
| 78 | + |
| 79 | + // entry label styling |
| 80 | + mChart.setEntryLabelColor(Color.WHITE); |
| 81 | + mChart.setEntryLabelTypeface(mTfRegular); |
| 82 | + mChart.setEntryLabelTextSize(12f); |
| 83 | + } |
| 84 | + |
| 85 | + private void setData(int count, float range) { |
| 86 | + |
| 87 | + ArrayList<PieEntry> values = new ArrayList<PieEntry>(); |
| 88 | + |
| 89 | + for (int i = 0; i < count; i++) { |
| 90 | + values.add(new PieEntry((float) ((Math.random() * range) + range / 5), mParties[i % mParties.length])); |
| 91 | + } |
| 92 | + |
| 93 | + PieDataSet dataSet = new PieDataSet(values, "Election Results"); |
| 94 | + dataSet.setSliceSpace(3f); |
| 95 | + dataSet.setSelectionShift(5f); |
| 96 | + |
| 97 | + dataSet.setColors(ColorTemplate.MATERIAL_COLORS); |
| 98 | + //dataSet.setSelectionShift(0f); |
| 99 | + |
| 100 | + PieData data = new PieData(dataSet); |
| 101 | + data.setValueFormatter(new PercentFormatter()); |
| 102 | + data.setValueTextSize(11f); |
| 103 | + data.setValueTextColor(Color.WHITE); |
| 104 | + data.setValueTypeface(mTfLight); |
| 105 | + mChart.setData(data); |
| 106 | + |
| 107 | + mChart.invalidate(); |
| 108 | + } |
| 109 | + |
| 110 | + private SpannableString generateCenterSpannableText() { |
| 111 | + |
| 112 | + SpannableString s = new SpannableString("MPAndroidChart\ndeveloped by Philipp Jahoda"); |
| 113 | + s.setSpan(new RelativeSizeSpan(1.7f), 0, 14, 0); |
| 114 | + s.setSpan(new StyleSpan(Typeface.NORMAL), 14, s.length() - 15, 0); |
| 115 | + s.setSpan(new ForegroundColorSpan(Color.GRAY), 14, s.length() - 15, 0); |
| 116 | + s.setSpan(new RelativeSizeSpan(.8f), 14, s.length() - 15, 0); |
| 117 | + s.setSpan(new StyleSpan(Typeface.ITALIC), s.length() - 14, s.length(), 0); |
| 118 | + s.setSpan(new ForegroundColorSpan(ColorTemplate.getHoloBlue()), s.length() - 14, s.length(), 0); |
| 119 | + return s; |
| 120 | + } |
| 121 | + |
| 122 | + private void moveOffScreen() { |
| 123 | + |
| 124 | + Display display = getWindowManager().getDefaultDisplay(); |
| 125 | + int height = display.getHeight(); // deprecated |
| 126 | + |
| 127 | + int offset = (int)(height * 0.65); /* or whatever */ |
| 128 | + |
| 129 | + RelativeLayout.LayoutParams rlParams = |
| 130 | + (RelativeLayout.LayoutParams)mChart.getLayoutParams(); |
| 131 | + rlParams.setMargins(0, 0, 0, -offset); |
| 132 | + mChart.setLayoutParams(rlParams); |
| 133 | + } |
| 134 | +} |
0 commit comments