Search in sources :

Example 1 with Legend

use of com.github.mikephil.charting.utils.Legend in project carat by amplab.

the class SummaryFragment method drawPieChart.

private void drawPieChart(final View inflatedView) {
    // This fixes a crash I got 2015-02-11:
    View v = inflatedView;
    if (v == null)
        v = getView();
    if (v == null)
        return;
    mChart = (PieChart) inflatedView.findViewById(R.id.chart1);
    mChart.setDescription("");
    // int orientation = getResources().getConfiguration().orientation;
    // switch (orientation) {
    // case (ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE):
    // mChart.setValueTextSize(9);
    // break;
    // case (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT):
    // mChart.setValueTextSize(15);
    // break;
    // }
    Typeface tf = Typeface.createFromAsset(getActivity().getAssets(), "fonts/OpenSans-Regular.ttf");
    mChart.setValueTypeface(tf);
    mChart.setCenterTextTypeface(Typeface.createFromAsset(getActivity().getAssets(), "fonts/OpenSans-Light.ttf"));
    mChart.setUsePercentValues(true);
    mChart.setCenterText(getString(R.string.summary_chart_center_text));
    mChart.setCenterTextSize(22f);
    // radius of the center hole in percent of maximum radius
    mChart.setHoleRadius(40f);
    mChart.setTransparentCircleRadius(50f);
    // disable click / touch / tap on the chart
    mChart.setTouchEnabled(false);
    // enable / disable drawing of x- and y-values
    // mChart.setDrawYValues(false);
    // mChart.setDrawXValues(false);
    mChart.setData(generatePieData());
    Legend l = mChart.getLegend();
    l.setPosition(LegendPosition.NONE);
}
Also used : Legend(com.github.mikephil.charting.utils.Legend) Typeface(android.graphics.Typeface) View(android.view.View) TextView(android.widget.TextView)

Example 2 with Legend

use of com.github.mikephil.charting.utils.Legend in project carat by amplab.

the class Chart method prepareLegend.

/**
 * Generates an automatically prepared legend depending on the DataSets in
 * the chart and their colors.
 */
public void prepareLegend() {
    ArrayList<String> labels = new ArrayList<String>();
    ArrayList<Integer> colors = new ArrayList<Integer>();
    // loop for building up the colors and labels used in the legend
    for (int i = 0; i < mData.getDataSetCount(); i++) {
        DataSet<? extends Entry> dataSet = mData.getDataSetByIndex(i);
        ArrayList<Integer> clrs = dataSet.getColors();
        int entryCount = dataSet.getEntryCount();
        // if we have a barchart with stacked bars
        if (dataSet instanceof BarDataSet && ((BarDataSet) dataSet).getStackSize() > 1) {
            BarDataSet bds = (BarDataSet) dataSet;
            String[] sLabels = bds.getStackLabels();
            for (int j = 0; j < clrs.size() && j < entryCount && j < bds.getStackSize(); j++) {
                labels.add(sLabels[j % sLabels.length]);
                colors.add(clrs.get(j));
            }
            // add the legend description label
            colors.add(-2);
            labels.add(bds.getLabel());
        } else if (dataSet instanceof PieDataSet) {
            ArrayList<String> xVals = mData.getXVals();
            PieDataSet pds = (PieDataSet) dataSet;
            for (int j = 0; j < clrs.size() && j < entryCount && j < xVals.size(); j++) {
                labels.add(xVals.get(j));
                colors.add(clrs.get(j));
            }
            // add the legend description label
            colors.add(-2);
            labels.add(pds.getLabel());
        } else {
            for (int j = 0; j < clrs.size() && j < entryCount; j++) {
                // if multiple colors are set for a DataSet, group them
                if (j < clrs.size() - 1 && j < entryCount - 1) {
                    labels.add(null);
                } else {
                    // add label to the last entry
                    String label = mData.getDataSetByIndex(i).getLabel();
                    labels.add(label);
                }
                colors.add(clrs.get(j));
            }
        }
    }
    Legend l = new Legend(colors, labels);
    if (mLegend != null) {
        // apply the old legend settings to a potential new legend
        l.apply(mLegend);
    }
    mLegend = l;
}
Also used : Legend(com.github.mikephil.charting.utils.Legend) BarDataSet(com.github.mikephil.charting.data.BarDataSet) PieDataSet(com.github.mikephil.charting.data.PieDataSet) ArrayList(java.util.ArrayList) Paint(android.graphics.Paint)

Aggregations

Legend (com.github.mikephil.charting.utils.Legend)2 Paint (android.graphics.Paint)1 Typeface (android.graphics.Typeface)1 View (android.view.View)1 TextView (android.widget.TextView)1 BarDataSet (com.github.mikephil.charting.data.BarDataSet)1 PieDataSet (com.github.mikephil.charting.data.PieDataSet)1 ArrayList (java.util.ArrayList)1