Last active
October 16, 2019 05:57
-
-
Save Jaosrikate/be55d10f3e0cfb6cf8eb89fd746d95b2 to your computer and use it in GitHub Desktop.
MPAndroidChart Pie chart highlighter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.myapplication; | |
import android.graphics.Color; | |
import android.graphics.Typeface; | |
import android.os.Bundle; | |
import android.support.v7.app.AppCompatActivity; | |
import android.text.SpannableString; | |
import android.text.style.ForegroundColorSpan; | |
import android.text.style.RelativeSizeSpan; | |
import android.text.style.StyleSpan; | |
import android.util.Log; | |
import android.view.WindowManager; | |
import com.github.mikephil.charting.animation.Easing; | |
import com.github.mikephil.charting.charts.PieChart; | |
import com.github.mikephil.charting.components.Legend; | |
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.data.PieEntry; | |
import com.github.mikephil.charting.formatter.PercentFormatter; | |
import com.github.mikephil.charting.highlight.Highlight; | |
import com.github.mikephil.charting.listener.OnChartValueSelectedListener; | |
import com.github.mikephil.charting.utils.ColorTemplate; | |
import com.github.mikephil.charting.utils.MPPointF; | |
import java.util.ArrayList; | |
public class MainActivity extends AppCompatActivity implements OnChartValueSelectedListener { | |
private PieChart chart; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, | |
WindowManager.LayoutParams.FLAG_FULLSCREEN); | |
setContentView(R.layout.activity_main); | |
setTitle("PieChartActivity"); | |
chart = findViewById(R.id.pieChart); | |
chart.setUsePercentValues(true); | |
chart.getDescription().setEnabled(false); | |
chart.setExtraOffsets(5, 10, 5, 5); | |
chart.setDragDecelerationFrictionCoef(0.95f); | |
// chart.setCenterTextTypeface(tfLight); | |
chart.setCenterText(generateCenterSpannableText()); | |
chart.setDrawHoleEnabled(true); | |
chart.setHoleColor(Color.WHITE); | |
chart.setTransparentCircleColor(Color.WHITE); | |
chart.setTransparentCircleAlpha(110); | |
chart.setHoleRadius(80f); | |
chart.setTransparentCircleRadius(61f); | |
chart.setDrawCenterText(true); | |
chart.setRotationAngle(0); | |
// enable rotation of the chart by touch | |
chart.setRotationEnabled(true); | |
chart.setHighlightPerTapEnabled(true); | |
// chart.setUnit(" €"); | |
// chart.setDrawUnitsInChart(true); | |
// add a selection listener | |
chart.setOnChartValueSelectedListener(this); | |
chart.animateY(1400, Easing.EaseInOutQuad); | |
// chart.spin(2000, 0, 360); | |
Legend l = chart.getLegend(); | |
l.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP); | |
l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.RIGHT); | |
l.setOrientation(Legend.LegendOrientation.VERTICAL); | |
l.setDrawInside(false); | |
l.setXEntrySpace(7f); | |
l.setYEntrySpace(0f); | |
l.setYOffset(0f); | |
// entry label styling | |
chart.setEntryLabelColor(Color.WHITE); | |
// chart.setEntryLabelTypeface(tfRegular); | |
chart.setEntryLabelTextSize(12f); | |
setData(5,5); | |
} | |
private void setData(int count, float range) { | |
ArrayList<PieEntry> entries = new ArrayList<>(); | |
// NOTE: The order of the entries when being added to the entries array determines their position around the center of | |
// the chart. | |
for (int i = 0; i < count ; i++) { | |
entries.add(new PieEntry((float) ((Math.random() * range) + range / 5))); | |
} | |
PieDataSet dataSet = new PieDataSet(entries, "Election Results"); | |
dataSet.setDrawIcons(false); | |
dataSet.setSliceSpace(3f); | |
dataSet.setIconsOffset(new MPPointF(0, 40)); | |
dataSet.setSelectionShift(5f); | |
// add a lot of colors | |
ArrayList<Integer> colors = new ArrayList<>(); | |
colors.add(ColorTemplate.getHoloBlue()); | |
dataSet.setColors(colors); | |
//dataSet.setSelectionShift(0f); | |
PieData data = new PieData(dataSet); | |
data.setValueFormatter(new PercentFormatter(chart)); | |
data.setValueTextSize(11f); | |
data.setValueTextColor(Color.WHITE); | |
// data.setValueTypeface(tfLight); | |
dataSet.setHighlightEnabled(true); | |
chart.setData(data); | |
// undo all highlights | |
chart.highlightValues(null); | |
Highlight[] h = new Highlight[1]; | |
h[0] = new Highlight(0,0,0); | |
chart.highlightValue(h[0]); | |
chart.invalidate(); | |
} | |
private SpannableString generateCenterSpannableText() { | |
SpannableString s = new SpannableString("MPAndroidChart\ndeveloped by Philipp Jahoda"); | |
s.setSpan(new RelativeSizeSpan(1.7f), 0, 14, 0); | |
s.setSpan(new StyleSpan(Typeface.NORMAL), 14, s.length() - 15, 0); | |
s.setSpan(new ForegroundColorSpan(Color.GRAY), 14, s.length() - 15, 0); | |
s.setSpan(new RelativeSizeSpan(.8f), 14, s.length() - 15, 0); | |
s.setSpan(new StyleSpan(Typeface.ITALIC), s.length() - 14, s.length(), 0); | |
s.setSpan(new ForegroundColorSpan(ColorTemplate.getHoloBlue()), s.length() - 14, s.length(), 0); | |
return s; | |
} | |
@Override | |
public void onValueSelected(Entry e, Highlight h) { | |
if (e == null) | |
return; | |
Log.i("VAL SELECTED", | |
"Value: " + e.getY() + ", index: " + h.getX() | |
+ ", DataSet index: " + h.getDataSetIndex()); | |
} | |
@Override | |
public void onNothingSelected() { | |
Log.i("PieChart", "nothing selected"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment