0
votes

I am using a ListView to display a group of charts that get the data from a hosted server. I am using the MPAndroidChart library, and have successfully used all the charts except the CombinedChart and the PieChart. I tried looking for documentation on both, but still was unable to understand these chart implementations. Can anyone please explain to me the working of these two charts : 1) CombinedChart 2) PieChart. Also in combined chart we can pass line and bar data, but how do I add these two datasets to the chart. As per my understanding, I can do it by creating a CombinedChartData object. But I am not able to do it as the application is crashing causing a NullPointerException. I have shown my code below for reference :

    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_activity2);
    combinedChart=(CombinedChart)findViewById(R.id.combined_chart);

    ArrayList<String> labels = getLabels();
    ArrayList<Entry> entries = new ArrayList<>(getEntries());
    ArrayList<BarEntry> entries1 = new ArrayList<>(getEntries1());
    LineDataSet lineDataSet = new LineDataSet(entries,"Line Data");
    BarDataSet barDataSet = new BarDataSet(entries1,"Bar Data");
    barDataSet.setColor(0);

    CombinedData combinedData = new CombinedData(labels);
    combinedData.addDataSet(lineDataSet);
    combinedData.addDataSet(barDataSet);

    combinedChart.setData(combinedData);
    combinedChart.invalidate();
1

1 Answers

0
votes

Have a look at the example:

CombinedChart: https://github.com/PhilJay/MPAndroidChart/blob/master/MPChartExample/src/com/xxmassdeveloper/mpchartexample/CombinedChartActivity.java

What you do is create your LineData and BarData object and then set those to the CombinedData. It's all in the example. What you did cannot work since you never ever added the "combineddatasets" to some kind of data object that is then set for the chart.

CombinedData data = new CombinedData(yourLabels);
data.setData(generateLineData());
data.setData(generateBarData());

mChart.setData(data);

PieChart: https://github.com/PhilJay/MPAndroidChart/blob/master/MPChartExample/src/com/xxmassdeveloper/mpchartexample/PieChartActivity.java

You should find everything you need there.