如何使用 JFreeChart 突出显示或聚焦饼图中的特定饼图部分

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

我正在 Swing 应用程序中使用 JFreeChart 开发一个简单的饼图。基于关键事件,我想重点关注或突出显示饼图的特定饼图部分。 知道 JFreeChart 中的 api 提供了这样的功能吗?

您可以使用 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.

我通过添加 ChartMouseListener to createDemoPanel() in PieChartDemo1 来尝试@trashgod 的想法。将鼠标悬停在每个部分上以查看效果。为 percent 尝试不同的值以获得您想要的效果。

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