Search code examples
androidpie-chartmpandroidchart

PieDataSet cannot be applied to String in MPAndroidChart


i want make PieChart with library MPAndroidChart from github and i follow instruction on google or youtube, but i'm stuck in this code

private void addData(){

    ArrayList<Entry> yVals1 = new ArrayList<Entry>();

    for (int i = 0; i < yData.length; i++)
        yVals1.add(new Entry(yData[i], i));

    ArrayList<String> xVals = new ArrayList<String>();

    for (int i = 0; i < xData.length; i++)
        xVals.add(xData[i]);

    PieDataSet dataSet = new PieDataSet(yVals1, "Market Share");
    dataSet.setSliceSpace(3);
    dataSet.setSelectionShift(5);

}

in

PieDataSet dataSet = new PieDataSet(yVals1, "Market Share");

I got warn, PieDataSet Cannot Applied to String. I don't know whats wrong because in tutorial is same with me


Solution

  • for pie chart with MPCharts, I've used this snippet

     protected PieData generatePieData() {
    
        int count = 10;
    
        ArrayList<PieEntry> entries1 = new ArrayList<PieEntry>();
    
        for(int i = 0; i < count; i++) {
            entries1.add(new PieEntry( (float) salary[i], months[i]));
        }
    
        PieDataSet ds1 = new PieDataSet(entries1, "Monthly Salary 2017");
        ds1.setColors(ColorTemplate.JOYFUL_COLORS);
        ds1.setSliceSpace(2f);
        ds1.setValueTextColor(Color.WHITE);
        ds1.setValueTextSize(10f);
        ds1.setSliceSpace(5f);
    
    
        return new PieData(ds1);
    }
    

    where salary, and months are array