• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 
2 package com.xxmassdeveloper.mpchartexample;
3 
4 import android.content.Intent;
5 import android.graphics.Color;
6 import android.graphics.Typeface;
7 import android.net.Uri;
8 import android.os.Bundle;
9 import android.text.SpannableString;
10 import android.text.style.ForegroundColorSpan;
11 import android.text.style.RelativeSizeSpan;
12 import android.text.style.StyleSpan;
13 import android.util.DisplayMetrics;
14 import android.view.Menu;
15 import android.view.MenuItem;
16 import android.view.WindowManager;
17 import android.widget.RelativeLayout;
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.data.PieData;
23 import com.github.mikephil.charting.data.PieDataSet;
24 import com.github.mikephil.charting.data.PieEntry;
25 import com.github.mikephil.charting.formatter.PercentFormatter;
26 import com.github.mikephil.charting.utils.ColorTemplate;
27 import com.xxmassdeveloper.mpchartexample.notimportant.DemoBase;
28 
29 import java.util.ArrayList;
30 
31 @SuppressWarnings("SameParameterValue")
32 public class HalfPieChartActivity extends DemoBase {
33 
34     private PieChart chart;
35 
36     @Override
onCreate(Bundle savedInstanceState)37     protected void onCreate(Bundle savedInstanceState) {
38         super.onCreate(savedInstanceState);
39         getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
40                 WindowManager.LayoutParams.FLAG_FULLSCREEN);
41         setContentView(R.layout.activity_piechart_half);
42 
43         setTitle("HalfPieChartActivity");
44 
45         chart = findViewById(R.id.chart1);
46         chart.setBackgroundColor(Color.WHITE);
47 
48         moveOffScreen();
49 
50         chart.setUsePercentValues(true);
51         chart.getDescription().setEnabled(false);
52 
53         chart.setCenterTextTypeface(tfLight);
54         chart.setCenterText(generateCenterSpannableText());
55 
56         chart.setDrawHoleEnabled(true);
57         chart.setHoleColor(Color.WHITE);
58 
59         chart.setTransparentCircleColor(Color.WHITE);
60         chart.setTransparentCircleAlpha(110);
61 
62         chart.setHoleRadius(58f);
63         chart.setTransparentCircleRadius(61f);
64 
65         chart.setDrawCenterText(true);
66 
67         chart.setRotationEnabled(false);
68         chart.setHighlightPerTapEnabled(true);
69 
70         chart.setMaxAngle(180f); // HALF CHART
71         chart.setRotationAngle(180f);
72         chart.setCenterTextOffset(0, -20);
73 
74         setData(4, 100);
75 
76         chart.animateY(1400, Easing.EaseInOutQuad);
77 
78         Legend l = chart.getLegend();
79         l.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP);
80         l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.CENTER);
81         l.setOrientation(Legend.LegendOrientation.HORIZONTAL);
82         l.setDrawInside(false);
83         l.setXEntrySpace(7f);
84         l.setYEntrySpace(0f);
85         l.setYOffset(0f);
86 
87         // entry label styling
88         chart.setEntryLabelColor(Color.WHITE);
89         chart.setEntryLabelTypeface(tfRegular);
90         chart.setEntryLabelTextSize(12f);
91     }
92 
setData(int count, float range)93     private void setData(int count, float range) {
94 
95         ArrayList<PieEntry> values = new ArrayList<>();
96 
97         for (int i = 0; i < count; i++) {
98             values.add(new PieEntry((float) ((Math.random() * range) + range / 5), parties[i % parties.length]));
99         }
100 
101         PieDataSet dataSet = new PieDataSet(values, "Election Results");
102         dataSet.setSliceSpace(3f);
103         dataSet.setSelectionShift(5f);
104 
105         dataSet.setColors(ColorTemplate.MATERIAL_COLORS);
106         //dataSet.setSelectionShift(0f);
107 
108         PieData data = new PieData(dataSet);
109         data.setValueFormatter(new PercentFormatter());
110         data.setValueTextSize(11f);
111         data.setValueTextColor(Color.WHITE);
112         data.setValueTypeface(tfLight);
113         chart.setData(data);
114 
115         chart.invalidate();
116     }
117 
generateCenterSpannableText()118     private SpannableString generateCenterSpannableText() {
119 
120         SpannableString s = new SpannableString("MPAndroidChart\ndeveloped by Philipp Jahoda");
121         s.setSpan(new RelativeSizeSpan(1.7f), 0, 14, 0);
122         s.setSpan(new StyleSpan(Typeface.NORMAL), 14, s.length() - 15, 0);
123         s.setSpan(new ForegroundColorSpan(Color.GRAY), 14, s.length() - 15, 0);
124         s.setSpan(new RelativeSizeSpan(.8f), 14, s.length() - 15, 0);
125         s.setSpan(new StyleSpan(Typeface.ITALIC), s.length() - 14, s.length(), 0);
126         s.setSpan(new ForegroundColorSpan(ColorTemplate.getHoloBlue()), s.length() - 14, s.length(), 0);
127         return s;
128     }
129 
moveOffScreen()130     private void moveOffScreen() {
131 
132         DisplayMetrics displayMetrics = new DisplayMetrics();
133         getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
134 
135         int height = displayMetrics.heightPixels;
136 
137         int offset = (int)(height * 0.65); /* percent to move */
138 
139         RelativeLayout.LayoutParams rlParams =
140                 (RelativeLayout.LayoutParams) chart.getLayoutParams();
141         rlParams.setMargins(0, 0, 0, -offset);
142         chart.setLayoutParams(rlParams);
143     }
144 
145     @Override
onCreateOptionsMenu(Menu menu)146     public boolean onCreateOptionsMenu(Menu menu) {
147         getMenuInflater().inflate(R.menu.only_github, menu);
148         return true;
149     }
150 
151     @Override
onOptionsItemSelected(MenuItem item)152     public boolean onOptionsItemSelected(MenuItem item) {
153 
154         switch (item.getItemId()) {
155             case R.id.viewGithub: {
156                 Intent i = new Intent(Intent.ACTION_VIEW);
157                 i.setData(Uri.parse("https://github.com/PhilJay/MPAndroidChart/blob/master/MPChartExample/src/com/xxmassdeveloper/mpchartexample/HalfPieChartActivity.java"));
158                 startActivity(i);
159                 break;
160             }
161         }
162 
163         return true;
164     }
165 
166     @Override
saveToGallery()167     public void saveToGallery() { /* Intentionally left empty */ }
168 }
169