Chaya Shetty
Chaya Shetty

Reputation: 41

How to highlight or focus particular pie section in a pie chart using JFreeChart

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?

Upvotes: 1

Views: 766

Answers (1)

Catalina Island
Catalina Island

Reputation: 7126

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) {
    }
});

Upvotes: 3

Related Questions