Mpandroidchart: piechart display after click on piechart area !!!

Created on 10 Jun 2015  路  9Comments  路  Source: PhilJay/MPAndroidChart

i have filled all required data into dataset of graph(with x-axis values and y-axis values).
but on first time rendering chart it will not display.after click or touch on chart area it will display.

Most helpful comment

call invalidate() after all setup

All 9 comments

provide code

screenshot_1_empty
screenshot_2 _withchartpng

/**
* initialize pie chart
*/
private void initPieChart() {

    Log.e(TAG, "Init Pie Chart");
    mPieChartPortfolio.setDescription("");
    mPieChartPortfolio.setNoDataText("No chart data avaiable");
    mPieChartPortfolio
            .setNoDataTextDescription("You need to provide data for the chart.");
    mPieChartPortfolio.setHighlightEnabled(true);
    mPieChartPortfolio.setTouchEnabled(true);

    mPieChartPortfolio.setDrawHoleEnabled(true);
    mPieChartPortfolio.setHoleColorTransparent(true);
    mPieChartPortfolio.setTransparentCircleRadius(Color.WHITE);

    mPieChartPortfolio.setHoleRadius(0f);
    mPieChartPortfolio.setTransparentCircleRadius(0f);

    mPieChartPortfolio.setRotationAngle(360 - 135f);
    mPieChartPortfolio.setRotationEnabled(false);
    mPieChartPortfolio.setHorizontalScrollBarEnabled(true);
    Log.i("asset_length", arrAssetName.length + "");
    setChartData(arrAssetName.length, 100);

    Legend legend = mPieChartPortfolio.getLegend();
    legend.setEnabled(false);

}

private void setChartData(int count, int range) {

    Log.e(TAG, "Set Data");
    // temporary data
    arrValue = new float[] { perGold, perSilver, perPlatinum, perPalladium };
    // y value
    ArrayList<Entry> yValues = new ArrayList<Entry>();
    for (int i = 0; i < count; i++) {
        yValues.add(new Entry((float) (arrValue[i]), i));

    }

    // x value
    xValues = new ArrayList<String>();
    for (int i = 0; i < count; i++) {
        String str = arrValue[i] + "";
        if (str.toString().equals("0.0")) {
            arrAssetName[i] = "";
        }
        xValues.add(arrAssetName[i]);
    }

    PieDataSet dataSet = new PieDataSet(yValues, "Assets");
    dataSet.setSliceSpace(0f);
    dataSet.setSelectionShift(5f);

    // set colors
    ArrayList<Integer> colors = new ArrayList<Integer>();

    for (int c : ColorTemplate.createColors(new int[] { COLOR_GOLD,
            COLOR_SILVER, COLOR_PLATINUM, COLOR_PALLADIUM }))
        colors.add(c);

    colors.add(ColorTemplate.getHoloBlue());
    dataSet.setColors(colors);

    mPieDataPortfolio = new PieData(xValues, dataSet);
    mPieDataPortfolio.setValueFormatter(new PercentFormatter());
    mPieDataPortfolio.setValueTextSize(12f);
    mPieDataPortfolio.setValueTextColor(contextAct.getResources().getColor(
            R.color.white));
    mPieChartPortfolio.setData(mPieDataPortfolio);

    mPieChartPortfolio.setHighlightEnabled(true);
    Log.i("pie chart set===>", "pie chart set===>");
}

call invalidate() after all setup

after called invalidate() its worked.
thanks

I need to show data on click of particular portion on the pie chart..like a label..how to do it ?

Hii

Override onNothingSelected() and onValueSelected method.

public void onNothingSelected() {
Log.i("PieChart", "nothing selected");
try {
dataSet.getColors().set(intSelectedPos, Color.rgb(3, 3, 3));
} catch (Exception e) {
e.printStackTrace();
}
}


public void onValueSelected(Entry e, Highlight h) {

    if (e == null)
        return;
    intSelectedPos = (int) h.getX();
    if (intSelectedPos >= 0) {
        for (int i = 0; i < dataSet.getColors().size(); i++) {
            dataSet.getColors().set(i, Color.rgb(3, 3, 3));
        }
        if (intSelectedPos == (int) h.getX()) {
            dataSet.getColors().set(intSelectedPos, Color.rgb(1, 229, 68));
        }
    }


    Log.i("VAL SELECTED", "Value: " + e.getY() + ", index: " + h.getX() + ", DataSet index: " + h.getDataSetIndex());

}

call invalidate() after all setup

Perfect !

If dataset is changing,call notifyDataSetChanged() will help before calling invalidsate()

Was this page helpful?
0 / 5 - 0 ratings

Related issues

chathudan picture chathudan  路  3Comments

botondbutuza picture botondbutuza  路  3Comments

JungYongWook picture JungYongWook  路  3Comments

Giammaofwar picture Giammaofwar  路  3Comments

blotfi picture blotfi  路  3Comments