Search in sources :

Example 11 with PieData

use of com.github.mikephil.charting.data.PieData in project MPAndroidChart by PhilJay.

the class PieChartRenderer method drawValues.

@Override
public void drawValues(Canvas c) {
    MPPointF center = mChart.getCenterCircleBox();
    // get whole the radius
    float radius = mChart.getRadius();
    float rotationAngle = mChart.getRotationAngle();
    float[] drawAngles = mChart.getDrawAngles();
    float[] absoluteAngles = mChart.getAbsoluteAngles();
    float phaseX = mAnimator.getPhaseX();
    float phaseY = mAnimator.getPhaseY();
    final float roundedRadius = (radius - (radius * mChart.getHoleRadius() / 100f)) / 2f;
    final float holeRadiusPercent = mChart.getHoleRadius() / 100.f;
    float labelRadiusOffset = radius / 10f * 3.6f;
    if (mChart.isDrawHoleEnabled()) {
        labelRadiusOffset = (radius - (radius * holeRadiusPercent)) / 2f;
        if (!mChart.isDrawSlicesUnderHoleEnabled() && mChart.isDrawRoundedSlicesEnabled()) {
            // Add curved circle slice and spacing to rotation angle, so that it sits nicely inside
            rotationAngle += roundedRadius * 360 / (Math.PI * 2 * radius);
        }
    }
    final float labelRadius = radius - labelRadiusOffset;
    PieData data = mChart.getData();
    List<IPieDataSet> dataSets = data.getDataSets();
    float yValueSum = data.getYValueSum();
    boolean drawEntryLabels = mChart.isDrawEntryLabelsEnabled();
    float angle;
    int xIndex = 0;
    c.save();
    float offset = Utils.convertDpToPixel(5.f);
    for (int i = 0; i < dataSets.size(); i++) {
        IPieDataSet dataSet = dataSets.get(i);
        final boolean drawValues = dataSet.isDrawValuesEnabled();
        if (!drawValues && !drawEntryLabels)
            continue;
        final PieDataSet.ValuePosition xValuePosition = dataSet.getXValuePosition();
        final PieDataSet.ValuePosition yValuePosition = dataSet.getYValuePosition();
        // apply the text-styling defined by the DataSet
        applyValueTextStyle(dataSet);
        float lineHeight = Utils.calcTextHeight(mValuePaint, "Q") + Utils.convertDpToPixel(4f);
        IValueFormatter formatter = dataSet.getValueFormatter();
        int entryCount = dataSet.getEntryCount();
        boolean isUseValueColorForLineEnabled = dataSet.isUseValueColorForLineEnabled();
        int valueLineColor = dataSet.getValueLineColor();
        mValueLinePaint.setStrokeWidth(Utils.convertDpToPixel(dataSet.getValueLineWidth()));
        final float sliceSpace = getSliceSpace(dataSet);
        MPPointF iconsOffset = MPPointF.getInstance(dataSet.getIconsOffset());
        iconsOffset.x = Utils.convertDpToPixel(iconsOffset.x);
        iconsOffset.y = Utils.convertDpToPixel(iconsOffset.y);
        for (int j = 0; j < entryCount; j++) {
            PieEntry entry = dataSet.getEntryForIndex(j);
            if (xIndex == 0)
                angle = 0.f;
            else
                angle = absoluteAngles[xIndex - 1] * phaseX;
            final float sliceAngle = drawAngles[xIndex];
            final float sliceSpaceMiddleAngle = sliceSpace / (Utils.FDEG2RAD * labelRadius);
            // offset needed to center the drawn text in the slice
            final float angleOffset = (sliceAngle - sliceSpaceMiddleAngle / 2.f) / 2.f;
            angle = angle + angleOffset;
            final float transformedAngle = rotationAngle + angle * phaseY;
            float value = mChart.isUsePercentValuesEnabled() ? entry.getY() / yValueSum * 100f : entry.getY();
            String entryLabel = entry.getLabel();
            final float sliceXBase = (float) Math.cos(transformedAngle * Utils.FDEG2RAD);
            final float sliceYBase = (float) Math.sin(transformedAngle * Utils.FDEG2RAD);
            final boolean drawXOutside = drawEntryLabels && xValuePosition == PieDataSet.ValuePosition.OUTSIDE_SLICE;
            final boolean drawYOutside = drawValues && yValuePosition == PieDataSet.ValuePosition.OUTSIDE_SLICE;
            final boolean drawXInside = drawEntryLabels && xValuePosition == PieDataSet.ValuePosition.INSIDE_SLICE;
            final boolean drawYInside = drawValues && yValuePosition == PieDataSet.ValuePosition.INSIDE_SLICE;
            if (drawXOutside || drawYOutside) {
                final float valueLineLength1 = dataSet.getValueLinePart1Length();
                final float valueLineLength2 = dataSet.getValueLinePart2Length();
                final float valueLinePart1OffsetPercentage = dataSet.getValueLinePart1OffsetPercentage() / 100.f;
                float pt2x, pt2y;
                float labelPtx, labelPty;
                float line1Radius;
                if (mChart.isDrawHoleEnabled())
                    line1Radius = (radius - (radius * holeRadiusPercent)) * valueLinePart1OffsetPercentage + (radius * holeRadiusPercent);
                else
                    line1Radius = radius * valueLinePart1OffsetPercentage;
                final float polyline2Width = dataSet.isValueLineVariableLength() ? labelRadius * valueLineLength2 * (float) Math.abs(Math.sin(transformedAngle * Utils.FDEG2RAD)) : labelRadius * valueLineLength2;
                final float pt0x = line1Radius * sliceXBase + center.x;
                final float pt0y = line1Radius * sliceYBase + center.y;
                final float pt1x = labelRadius * (1 + valueLineLength1) * sliceXBase + center.x;
                final float pt1y = labelRadius * (1 + valueLineLength1) * sliceYBase + center.y;
                if (transformedAngle % 360.0 >= 90.0 && transformedAngle % 360.0 <= 270.0) {
                    pt2x = pt1x - polyline2Width;
                    pt2y = pt1y;
                    mValuePaint.setTextAlign(Align.RIGHT);
                    if (drawXOutside)
                        mEntryLabelsPaint.setTextAlign(Align.RIGHT);
                    labelPtx = pt2x - offset;
                    labelPty = pt2y;
                } else {
                    pt2x = pt1x + polyline2Width;
                    pt2y = pt1y;
                    mValuePaint.setTextAlign(Align.LEFT);
                    if (drawXOutside)
                        mEntryLabelsPaint.setTextAlign(Align.LEFT);
                    labelPtx = pt2x + offset;
                    labelPty = pt2y;
                }
                int lineColor = ColorTemplate.COLOR_NONE;
                if (isUseValueColorForLineEnabled)
                    lineColor = dataSet.getColor(j);
                else if (valueLineColor != ColorTemplate.COLOR_NONE)
                    lineColor = valueLineColor;
                if (lineColor != ColorTemplate.COLOR_NONE) {
                    mValueLinePaint.setColor(lineColor);
                    c.drawLine(pt0x, pt0y, pt1x, pt1y, mValueLinePaint);
                    c.drawLine(pt1x, pt1y, pt2x, pt2y, mValueLinePaint);
                }
                // draw everything, depending on settings
                if (drawXOutside && drawYOutside) {
                    drawValue(c, formatter, value, entry, 0, labelPtx, labelPty, dataSet.getValueTextColor(j));
                    if (j < data.getEntryCount() && entryLabel != null) {
                        drawEntryLabel(c, entryLabel, labelPtx, labelPty + lineHeight);
                    }
                } else if (drawXOutside) {
                    if (j < data.getEntryCount() && entryLabel != null) {
                        drawEntryLabel(c, entryLabel, labelPtx, labelPty + lineHeight / 2.f);
                    }
                } else if (drawYOutside) {
                    drawValue(c, formatter, value, entry, 0, labelPtx, labelPty + lineHeight / 2.f, dataSet.getValueTextColor(j));
                }
            }
            if (drawXInside || drawYInside) {
                // calculate the text position
                float x = labelRadius * sliceXBase + center.x;
                float y = labelRadius * sliceYBase + center.y;
                mValuePaint.setTextAlign(Align.CENTER);
                // draw everything, depending on settings
                if (drawXInside && drawYInside) {
                    drawValue(c, formatter, value, entry, 0, x, y, dataSet.getValueTextColor(j));
                    if (j < data.getEntryCount() && entryLabel != null) {
                        drawEntryLabel(c, entryLabel, x, y + lineHeight);
                    }
                } else if (drawXInside) {
                    if (j < data.getEntryCount() && entryLabel != null) {
                        drawEntryLabel(c, entryLabel, x, y + lineHeight / 2f);
                    }
                } else if (drawYInside) {
                    drawValue(c, formatter, value, entry, 0, x, y + lineHeight / 2f, dataSet.getValueTextColor(j));
                }
            }
            if (entry.getIcon() != null && dataSet.isDrawIconsEnabled()) {
                Drawable icon = entry.getIcon();
                float x = (labelRadius + iconsOffset.y) * sliceXBase + center.x;
                float y = (labelRadius + iconsOffset.y) * sliceYBase + center.y;
                y += iconsOffset.x;
                Utils.drawImage(c, icon, (int) x, (int) y, icon.getIntrinsicWidth(), icon.getIntrinsicHeight());
            }
            xIndex++;
        }
        MPPointF.recycleInstance(iconsOffset);
    }
    MPPointF.recycleInstance(center);
    c.restore();
}
Also used : PieEntry(com.github.mikephil.charting.data.PieEntry) IPieDataSet(com.github.mikephil.charting.interfaces.datasets.IPieDataSet) MPPointF(com.github.mikephil.charting.utils.MPPointF) Drawable(android.graphics.drawable.Drawable) TextPaint(android.text.TextPaint) Paint(android.graphics.Paint) IValueFormatter(com.github.mikephil.charting.formatter.IValueFormatter) IPieDataSet(com.github.mikephil.charting.interfaces.datasets.IPieDataSet) PieDataSet(com.github.mikephil.charting.data.PieDataSet) PieData(com.github.mikephil.charting.data.PieData)

Example 12 with PieData

use of com.github.mikephil.charting.data.PieData in project Gadgetbridge by Freeyourgadget.

the class ActivityListingAdapter method setChartsData.

private void setChartsData(PieChart pieChart, float value, float target, String label, Context context) {
    ArrayList<PieEntry> entries = new ArrayList<>();
    entries.add(new PieEntry((float) value, context.getResources().getDrawable(R.drawable.ic_star_gold)));
    Easing.EasingFunction animationEffect = Easing.EaseInOutSine;
    if (value < target) {
        entries.add(new PieEntry((float) (target - value)));
    }
    pieChart.setCenterText(String.format("%d%%\n%s", (int) (value * 100 / target), label));
    float colorValue = Math.max(0, Math.min(1, value / target));
    int chartColor = interpolateColor(Color.parseColor(CHART_COLOR_START), Color.parseColor(CHART_COLOR_END), colorValue);
    PieDataSet dataSet = new PieDataSet(entries, "");
    dataSet.setDrawIcons(false);
    dataSet.setIconsOffset(new MPPointF(0, -66));
    if (colorValue == 1) {
        dataSet.setDrawIcons(true);
    }
    dataSet.setSliceSpace(0f);
    dataSet.setSelectionShift(5f);
    dataSet.setColors(chartColor, Color.LTGRAY);
    PieData data = new PieData(dataSet);
    data.setValueTextSize(0f);
    data.setValueTextColor(Color.WHITE);
    pieChart.setData(data);
    pieChart.invalidate();
    pieChart.animateY(ANIM_TIME, animationEffect);
}
Also used : Easing(com.github.mikephil.charting.animation.Easing) PieEntry(com.github.mikephil.charting.data.PieEntry) PieDataSet(com.github.mikephil.charting.data.PieDataSet) MPPointF(com.github.mikephil.charting.utils.MPPointF) ArrayList(java.util.ArrayList) PieData(com.github.mikephil.charting.data.PieData)

Example 13 with PieData

use of com.github.mikephil.charting.data.PieData in project TumCampusApp by TCA-Team.

the class GradesActivity method showPieChart.

private void showPieChart(List<Exam> exams) {
    barChart.setVisibility(View.GONE);
    pieChart.setVisibility(View.VISIBLE);
    Map<String, Integer> gradeCount = calculateGradeDistribution(exams);
    List<PieEntry> entries = new ArrayList<>();
    for (String GRADE : GRADES) {
        entries.add(new PieEntry(gradeCount.get(GRADE), GRADE));
    }
    PieDataSet set = new PieDataSet(entries, getString(R.string.grades_without_weight));
    set.setColors(GRADE_COLORS, this);
    set.setDrawValues(false);
    pieChart.setData(new PieData(set));
    pieChart.setDrawEntryLabels(false);
    pieChart.getLegend().setWordWrapEnabled(true);
    pieChart.setDescription(null);
    pieChart.invalidate();
}
Also used : PieEntry(com.github.mikephil.charting.data.PieEntry) PieDataSet(com.github.mikephil.charting.data.PieDataSet) ArrayList(java.util.ArrayList) PieData(com.github.mikephil.charting.data.PieData)

Example 14 with PieData

use of com.github.mikephil.charting.data.PieData in project AdMoney by ErnestoGonAr.

the class Reporte_Egresos method addData.

private void addData() {
    ArrayList<PieEntry> yEntrys = new ArrayList<>();
    for (int i = 0; i < yData.length; i++) {
        yEntrys.add(new PieEntry(yData[i], "$ " + yData[i] + " " + xData[i]));
    }
    PieDataSet pieDataSet = new PieDataSet(yEntrys, "");
    pieDataSet.setSliceSpace(3);
    pieDataSet.setValueTextSize(24);
    ArrayList<Integer> colors = new ArrayList<Integer>();
    for (int c : ColorTemplate.MATERIAL_COLORS) colors.add(c);
    for (int c : ColorTemplate.VORDIPLOM_COLORS) colors.add(c);
    for (int c : ColorTemplate.JOYFUL_COLORS) colors.add(c);
    for (int c : ColorTemplate.COLORFUL_COLORS) colors.add(c);
    for (int c : ColorTemplate.LIBERTY_COLORS) colors.add(c);
    for (int c : ColorTemplate.PASTEL_COLORS) colors.add(c);
    pieDataSet.setColors(colors);
    PieData pieData = new PieData(pieDataSet);
    pieChart.setDrawEntryLabels(false);
    pieChart.setCenterText("%");
    pieChart.setCenterTextSize(30);
    pieChart.setUsePercentValues(true);
    pieChart.setData(pieData);
    pieChart.highlightValue(null);
    pieChart.invalidate();
    pieChart.animateXY(2000, 2000);
}
Also used : PieEntry(com.github.mikephil.charting.data.PieEntry) PieDataSet(com.github.mikephil.charting.data.PieDataSet) ArrayList(java.util.ArrayList) PieData(com.github.mikephil.charting.data.PieData)

Example 15 with PieData

use of com.github.mikephil.charting.data.PieData in project AdMoney by ErnestoGonAr.

the class Reporte_Ingresos method addData.

private void addData() {
    ArrayList<PieEntry> yEntrys = new ArrayList<>();
    for (int i = 0; i < yData.length; i++) {
        yEntrys.add(new PieEntry(yData[i], "$ " + yData[i] + " " + xData[i]));
    }
    PieDataSet pieDataset = new PieDataSet(yEntrys, "");
    pieDataset.setSliceSpace(3);
    pieDataset.setValueTextSize(24);
    ArrayList<Integer> colors = new ArrayList<Integer>();
    for (int c : ColorTemplate.MATERIAL_COLORS) colors.add(c);
    for (int c : ColorTemplate.VORDIPLOM_COLORS) colors.add(c);
    for (int c : ColorTemplate.JOYFUL_COLORS) colors.add(c);
    for (int c : ColorTemplate.COLORFUL_COLORS) colors.add(c);
    for (int c : ColorTemplate.LIBERTY_COLORS) colors.add(c);
    for (int c : ColorTemplate.PASTEL_COLORS) colors.add(c);
    pieDataset.setColors(colors);
    PieData pieData = new PieData(pieDataset);
    pieChart.setDrawEntryLabels(false);
    pieChart.setUsePercentValues(true);
    pieChart.setCenterText("%");
    pieChart.setCenterTextSize(30);
    pieChart.setData(pieData);
    pieChart.highlightValue(null);
    pieChart.invalidate();
    pieChart.animateXY(2000, 2000);
}
Also used : PieEntry(com.github.mikephil.charting.data.PieEntry) PieDataSet(com.github.mikephil.charting.data.PieDataSet) ArrayList(java.util.ArrayList) PieData(com.github.mikephil.charting.data.PieData)

Aggregations

PieData (com.github.mikephil.charting.data.PieData)28 PieDataSet (com.github.mikephil.charting.data.PieDataSet)24 PieEntry (com.github.mikephil.charting.data.PieEntry)20 ArrayList (java.util.ArrayList)20 PercentFormatter (com.github.mikephil.charting.formatter.PercentFormatter)7 MPPointF (com.github.mikephil.charting.utils.MPPointF)5 Entry (com.github.mikephil.charting.data.Entry)4 View (android.view.View)3 Intent (android.content.Intent)2 Paint (android.graphics.Paint)2 SpannableString (android.text.SpannableString)2 TextPaint (android.text.TextPaint)2 DisplayMetrics (android.util.DisplayMetrics)2 TextView (android.widget.TextView)2 PieChart (com.github.mikephil.charting.charts.PieChart)2 IPieDataSet (com.github.mikephil.charting.interfaces.datasets.IPieDataSet)2 Context (android.content.Context)1 SharedPreferences (android.content.SharedPreferences)1 Bitmap (android.graphics.Bitmap)1 Canvas (android.graphics.Canvas)1