javalabeljfreechart

Icons in sector labels with JFreeChart


In order to convey additional information, I am trying to replicate a specific functionality: embedding icons within the sector labels when using JFreeChart.

It should look something like this illustrated example I created with ChartDirector:

SampleChart

How do I achieve that?


Solution

  • One approach is to use the desired Unicode glyph, for example. Simply add the code point to the label string and use that string in the PieDataset<String>, as shown in this complete example. The string will appear in the label. Because the PieSectionLabelGenerator returns an AttributedString, you can alter the size and location as shown here and here.

    private static final String KEY1 = " šŸ˜€ Datum 1";
    ā€¦
    var dataset = new DefaultPieDataset<String>();
    dataset.setValue(KEY1, 99);
    ā€¦
    var chart = ChartFactory.createPieChart(
        "Header", dataset, true, true, false);
    

    PieChart