Search code examples
jfreechart

JFreeChart ignoring lower values in Pie Chart


Method to create dataset:

private static DefaultPieDataset getPieDataset(List<ChartObject> list)
{
   DefaulPieDataset dataset = new DefaultPieDataset();
   for(ChartObject object : list)
       dataset.setValues(object.getKey(), object.getValueX());

   return dataset;
}

My list contains three object with following values:

Key: HIGH Value: 9

Key: MEDIUM Value: 30

Key: LOW Value: 46

But my pie chart show only two categories LOW & MEDIUM. HIGH is completely ignored.

Pie Chart

A quick Google Search showed me that we can set threshold aggregate the lower values so I gave it a shot and changed the return type of my function to PieDataSet and the return statement to:

return DatasetUtilities.createConsolidatedPieDataset(dataset, "Other", 0);

But still no luck.

How do I force JFreeChart to not to ignore the lower value.


Solution

  • I'm guessing your List<ChartObject> is wrong somehow, and your getPieDataset() doesn't compile. Here's an example that uses a Map<String, Integer>.

    enter image description here

    private Map<String, Integer> getData() {
        Map<String, Integer> map = new HashMap<>();
        map.put("High", 9);
        map.put("Medium", 30);
        map.put("Low", 46);
        return map;
    }
    
    private ChartPanel createPieChart() {
        DefaultPieDataset data = new DefaultPieDataset();
        for (Map.Entry<String, Integer> entry : getData().entrySet()) {
            data.setValue(entry.getKey(), entry.getValue());
        }
        return new ChartPanel(ChartFactory.createPieChart(
            "PieTest", data, true, true, false));
    }