1 package com.xxmassdeveloper.mpchartexample.fragments; 2 import android.graphics.Color; 3 import android.graphics.Typeface; 4 import android.os.Bundle; 5 import androidx.annotation.NonNull; 6 import androidx.fragment.app.Fragment; 7 import android.text.SpannableString; 8 import android.text.style.ForegroundColorSpan; 9 import android.text.style.RelativeSizeSpan; 10 import android.view.LayoutInflater; 11 import android.view.View; 12 import android.view.ViewGroup; 13 14 import com.github.mikephil.charting.charts.PieChart; 15 import com.github.mikephil.charting.components.Legend; 16 import com.xxmassdeveloper.mpchartexample.R; 17 18 19 public class PieChartFrag extends SimpleFragment { 20 21 @NonNull newInstance()22 public static Fragment newInstance() { 23 return new PieChartFrag(); 24 } 25 26 @SuppressWarnings("FieldCanBeLocal") 27 private PieChart chart; 28 29 @Override onCreateView(@onNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)30 public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 31 View v = inflater.inflate(R.layout.frag_simple_pie, container, false); 32 33 chart = v.findViewById(R.id.pieChart1); 34 chart.getDescription().setEnabled(false); 35 36 Typeface tf = Typeface.createFromAsset(context.getAssets(), "OpenSans-Light.ttf"); 37 38 chart.setCenterTextTypeface(tf); 39 chart.setCenterText(generateCenterText()); 40 chart.setCenterTextSize(10f); 41 chart.setCenterTextTypeface(tf); 42 43 // radius of the center hole in percent of maximum radius 44 chart.setHoleRadius(45f); 45 chart.setTransparentCircleRadius(50f); 46 47 Legend l = chart.getLegend(); 48 l.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP); 49 l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.RIGHT); 50 l.setOrientation(Legend.LegendOrientation.VERTICAL); 51 l.setDrawInside(false); 52 53 chart.setData(generatePieData()); 54 55 return v; 56 } 57 generateCenterText()58 private SpannableString generateCenterText() { 59 SpannableString s = new SpannableString("Revenues\nQuarters 2015"); 60 s.setSpan(new RelativeSizeSpan(2f), 0, 8, 0); 61 s.setSpan(new ForegroundColorSpan(Color.GRAY), 8, s.length(), 0); 62 return s; 63 } 64 } 65