Search in sources :

Example 21 with PieDataSet

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

the class HalfPieChartActivity method setData.

private void setData(int count, float range) {
    ArrayList<PieEntry> values = new ArrayList<>();
    for (int i = 0; i < count; i++) {
        values.add(new PieEntry((float) ((Math.random() * range) + range / 5), parties[i % parties.length]));
    }
    PieDataSet dataSet = new PieDataSet(values, "Election Results");
    dataSet.setSliceSpace(3f);
    dataSet.setSelectionShift(5f);
    dataSet.setColors(ColorTemplate.MATERIAL_COLORS);
    // dataSet.setSelectionShift(0f);
    PieData data = new PieData(dataSet);
    data.setValueFormatter(new PercentFormatter());
    data.setValueTextSize(11f);
    data.setValueTextColor(Color.WHITE);
    data.setValueTypeface(tfLight);
    chart.setData(data);
    chart.invalidate();
}
Also used : PercentFormatter(com.github.mikephil.charting.formatter.PercentFormatter) 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 22 with PieDataSet

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

the class PieChartActivity method setData.

private void setData(int count, float range) {
    ArrayList<PieEntry> entries = new ArrayList<>();
    // the chart.
    for (int i = 0; i < count; i++) {
        entries.add(new PieEntry((float) ((Math.random() * range) + range / 5), parties[i % parties.length], getResources().getDrawable(R.drawable.star)));
    }
    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<>();
    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);
    colors.add(ColorTemplate.getHoloBlue());
    dataSet.setColors(colors);
    // dataSet.setSelectionShift(0f);
    PieData data = new PieData(dataSet);
    data.setValueFormatter(new PercentFormatter());
    data.setValueTextSize(11f);
    data.setValueTextColor(Color.WHITE);
    data.setValueTypeface(tfLight);
    chart.setData(data);
    // undo all highlights
    chart.highlightValues(null);
    chart.invalidate();
}
Also used : PercentFormatter(com.github.mikephil.charting.formatter.PercentFormatter) 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 23 with PieDataSet

use of com.github.mikephil.charting.data.PieDataSet in project carat by amplab.

the class SummaryFragment method generatePieData.

protected PieData generatePieData() {
    ArrayList<Entry> entries = new ArrayList<Entry>();
    ArrayList<String> xVals = new ArrayList<String>();
    xVals.add(getString(R.string.chart_wellbehaved));
    xVals.add(getString(R.string.chart_hogs));
    xVals.add(getString(R.string.chart_bugs));
    int wellbehaved = mMainActivity.mWellbehaved;
    int hogs = mMainActivity.mHogs;
    int bugs = mMainActivity.mBugs;
    entries.add(new Entry((float) (wellbehaved), 1));
    entries.add(new Entry((float) (hogs), 2));
    entries.add(new Entry((float) (bugs), 3));
    PieDataSet ds1 = new PieDataSet(entries, getString(R.string.summary_chart_center_text));
    ds1.setColors(Constants.CARAT_COLORS);
    ds1.setSliceSpace(2f);
    PieData d = new PieData(xVals, ds1);
    return d;
}
Also used : Entry(com.github.mikephil.charting.data.Entry) PieDataSet(com.github.mikephil.charting.data.PieDataSet) ArrayList(java.util.ArrayList) PieData(com.github.mikephil.charting.data.PieData)

Example 24 with PieDataSet

use of com.github.mikephil.charting.data.PieDataSet in project carat by amplab.

the class PieChart method drawData.

@Override
protected void drawData() {
    float angle = mRotationAngle;
    ArrayList<PieDataSet> dataSets = mData.getDataSets();
    int cnt = 0;
    for (int i = 0; i < mData.getDataSetCount(); i++) {
        PieDataSet dataSet = dataSets.get(i);
        ArrayList<Entry> entries = dataSet.getYVals();
        for (int j = 0; j < entries.size(); j++) {
            float newangle = mDrawAngles[cnt];
            float sliceSpace = dataSet.getSliceSpace();
            Entry e = entries.get(j);
            // draw only if the value is greater than zero
            if ((Math.abs(e.getVal()) > 0.000001)) {
                if (!needsHighlight(e.getXIndex(), i)) {
                    mRenderPaint.setColor(dataSet.getColor(j));
                    mDrawCanvas.drawArc(mCircleBox, angle + sliceSpace / 2f, newangle * mPhaseY - sliceSpace / 2f, true, mRenderPaint);
                }
            // if(sliceSpace > 0f) {
            // 
            // PointF outer = getPosition(c, radius, angle);
            // PointF inner = getPosition(c, radius * mHoleRadiusPercent
            // / 100f, angle);
            // }
            }
            angle += newangle * mPhaseX;
            cnt++;
        }
    }
}
Also used : Entry(com.github.mikephil.charting.data.Entry) PieDataSet(com.github.mikephil.charting.data.PieDataSet) Paint(android.graphics.Paint)

Example 25 with PieDataSet

use of com.github.mikephil.charting.data.PieDataSet in project carat by amplab.

the class PieChart method drawValues.

@Override
protected void drawValues() {
    // if neither xvals nor yvals are drawn, return
    if (!mDrawXVals && !mDrawYValues)
        return;
    PointF center = getCenterCircleBox();
    // get whole the radius
    float r = getRadius();
    float off = r / 2f;
    if (mDrawHole) {
        off = (r - (r / 100f * mHoleRadiusPercent)) / 2f;
    }
    // offset to keep things inside the chart
    r -= off;
    ArrayList<PieDataSet> dataSets = mData.getDataSets();
    int cnt = 0;
    for (int i = 0; i < mData.getDataSetCount(); i++) {
        PieDataSet dataSet = dataSets.get(i);
        ArrayList<Entry> entries = dataSet.getYVals();
        for (int j = 0; j < entries.size() * mPhaseX; j++) {
            // offset needed to center the drawn text in the slice
            float offset = mDrawAngles[cnt] / 2;
            // calculate the text position
            float x = (float) (r * Math.cos(Math.toRadians((mRotationAngle + mAbsoluteAngles[cnt] - offset) * mPhaseY)) + center.x);
            float y = (float) (r * Math.sin(Math.toRadians((mRotationAngle + mAbsoluteAngles[cnt] - offset) * mPhaseY)) + center.y);
            String val = "";
            float value = entries.get(j).getVal();
            if (mUsePercentValues)
                val = mValueFormatter.getFormattedValue(Math.abs(getPercentOfTotal(value))) + " %";
            else
                val = mValueFormatter.getFormattedValue(value);
            if (mDrawUnitInChart)
                val = val + mUnit;
            // draw everything, depending on settings
            if (mDrawXVals && mDrawYValues) {
                // use ascent and descent to calculate the new line
                // position,
                // 1.6f is the line spacing
                float lineHeight = (mValuePaint.ascent() + mValuePaint.descent()) * 1.6f;
                y -= lineHeight / 2;
                mDrawCanvas.drawText(val, x, y, mValuePaint);
                if (j < mData.getXValCount())
                    mDrawCanvas.drawText(mData.getXVals().get(j), x, y + lineHeight, mValuePaint);
            } else if (mDrawXVals && !mDrawYValues) {
                if (j < mData.getXValCount())
                    mDrawCanvas.drawText(mData.getXVals().get(j), x, y, mValuePaint);
            } else if (!mDrawXVals && mDrawYValues) {
                mDrawCanvas.drawText(val, x, y, mValuePaint);
            }
            cnt++;
        }
    }
}
Also used : Entry(com.github.mikephil.charting.data.Entry) PieDataSet(com.github.mikephil.charting.data.PieDataSet) PointF(android.graphics.PointF) Paint(android.graphics.Paint)

Aggregations

PieDataSet (com.github.mikephil.charting.data.PieDataSet)28 PieData (com.github.mikephil.charting.data.PieData)23 ArrayList (java.util.ArrayList)21 PieEntry (com.github.mikephil.charting.data.PieEntry)19 Entry (com.github.mikephil.charting.data.Entry)7 PercentFormatter (com.github.mikephil.charting.formatter.PercentFormatter)7 Paint (android.graphics.Paint)5 MPPointF (com.github.mikephil.charting.utils.MPPointF)4 SpannableString (android.text.SpannableString)2 DisplayMetrics (android.util.DisplayMetrics)2 View (android.view.View)2 TextView (android.widget.TextView)2 PieChart (com.github.mikephil.charting.charts.PieChart)2 Context (android.content.Context)1 Intent (android.content.Intent)1 SharedPreferences (android.content.SharedPreferences)1 Color (android.graphics.Color)1 PointF (android.graphics.PointF)1 RectF (android.graphics.RectF)1 Typeface (android.graphics.Typeface)1