1

I am developing a simple pie chart using JFreeChart in Swing application. Based on key event, I want to focus or highlight particular pie section of a pie chart. Any idea what api in JFreeChart provides such feature?

1 Answers1

3

You can use setExplodePercent() on your PiePlot, like they show here.

JFreeChart chart = ChartFactory.createPieChart(…);
PiePlot plot = (PiePlot) chart.getPlot();
plot.setExplodePercent(KEY, PERCENT);

I need some method to set a border or set a focus to the section based on some event, e.g. mouse hover on particular section.

I tried @trashgod's idea by adding a ChartMouseListener to createDemoPanel() in PieChartDemo1. Hover over each section to see the effect. Try different values for the percent to get the effect you want.

pie chart

panel.addChartMouseListener(new ChartMouseListener() {

    private Comparable lastKey;

    @Override
    public void chartMouseMoved(ChartMouseEvent e) {
        ChartEntity entity = e.getEntity();
        if (entity instanceof PieSectionEntity) {
            PieSectionEntity section = (PieSectionEntity) entity;
            PiePlot plot = (PiePlot) chart.getPlot();
            if (lastKey != null) {
                plot.setExplodePercent(lastKey, 0);
            }
            Comparable key = section.getSectionKey();
            plot.setExplodePercent(key, 0.10);
            lastKey = key;
        }
    }

    @Override
    public void chartMouseClicked(ChartMouseEvent e) {
    }
});
Community
  • 1
  • 1
Catalina Island
  • 7,027
  • 2
  • 23
  • 42
  • Thanks for your response. But I need some method to set a border or set a focus to the section based on some event (Eg: Mouse hover on particular section) rather than exploding that section. Is there any way? Thanks! – Chaya Shetty Feb 06 '17 at 10:59
  • @ChayaShetty: Add a `ChartMouseListener` to your `ChartPanel` and invoke `setExplodePercent()` in `chartMouseMoved()`. – trashgod Feb 06 '17 at 18:28
  • @ChayaShetty: I added an example to try. A smaller percent like `0.05` may be good. – Catalina Island Feb 07 '17 at 10:56