Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pie Chart Alignment issue using MPAndroidChart

In my application am using piechart. Using the following library
api 'com.github.PhilJay:MPAndroidChart:v3.0.3'.

But am facing an issue the alignment problem.Attaching the screenshots,Please have a look on it:

graph_1 graph_2

Code:

public static void chartDetails(PieChart mChart, Typeface tf) {
    mChart.animateXY(1400, 1400);
    mChart.getDescription().setEnabled(false);
    mChart.setCenterTextTypeface(tf);
    mChart.setCenterText("");
    mChart.setCenterTextSize(10f);
    mChart.setCenterTextTypeface(tf);
    // radius of the center hole in percent of maximum radius
    mChart.setHoleRadius(45f);
    mChart.setTransparentCircleRadius(50f);
    Legend l = mChart.getLegend();
    l.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP);
    l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.RIGHT);
    l.setOrientation(Legend.LegendOrientation.VERTICAL);
    l.setDrawInside(false);
    mChart.setTouchEnabled(true);
}
like image 259
Sunisha Sindhu Avatar asked Jun 18 '18 10:06

Sunisha Sindhu


1 Answers

You could use outside values in order to keep clear your dataset names for small pieces of data.

For example, you could achieve something like that:

Outside values

Here is the code. This code is supposed to use when overriding a PieChart. If you are not overriding it, you should adapt this code to work with your PieChart directly, such as mChart.setData(); instead of setData();.

PieDataSet dataSet = new PieDataSet(entries, null);
dataSet.setSliceSpace(3f);
dataSet.setIconsOffset(new MPPointF(0, 40));
dataSet.setSelectionShift(5f);

// Outside values
dataSet.setXValuePosition(PieDataSet.ValuePosition.OUTSIDE_SLICE);
dataSet.setYValuePosition(PieDataSet.ValuePosition.OUTSIDE_SLICE);
dataSet.setValueLinePart1OffsetPercentage(100f); /** When valuePosition is OutsideSlice, indicates offset as percentage out of the slice size */
dataSet.setValueLinePart1Length(0.6f); /** When valuePosition is OutsideSlice, indicates length of first half of the line */
dataSet.setValueLinePart2Length(0.6f); /** When valuePosition is OutsideSlice, indicates length of second half of the line */
setExtraOffsets(0.f, 5.f, 0.f, 5.f); // Ofsets of the view chart to prevent outside values being cropped /** Sets extra offsets (around the chart view) to be appended to the auto-calculated offsets.*/

PieData data = new PieData(dataSet);
data.setValueTextSize(10f);
data.setValueTextColor(Color.BLACK);
setData(data);
like image 133
Marc Estrada Avatar answered Sep 28 '22 09:09

Marc Estrada