【问题标题】:How to highlight or focus particular pie section in a pie chart using JFreeChart如何使用 JFreeChart 突出显示或聚焦饼图中的特定饼图部分
【发布时间】:2017-02-06 10:32:32
【问题描述】:

我正在 Swing 应用程序中使用 JFreeChart 开发一个简单的饼图。根据关键事件,我想聚焦或突出饼图的特定饼图部分。 知道 JFreeChart 中的什么 api 提供了这样的功能吗?

【问题讨论】:

  • 交叉发布here

标签: java swing jfreechart


【解决方案1】:

您可以在您的PiePlot 上使用setExplodePercent(),就像他们显示here 一样。

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

我需要一些方法来设置边框或根据某些事件设置部分的焦点,例如鼠标悬停在特定部分。

我尝试了@trashgod 的想法,在PieChartDemo1 中将ChartMouseListener 添加到createDemoPanel()。将鼠标悬停在每个部分上以查看效果。为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) {
    }
});

【讨论】:

  • 感谢您的回复。但是我需要一些方法来设置边框或基于某些事件(例如:鼠标悬停在特定部分上)而不是爆炸该部分。有什么办法吗?谢谢!
  • @ChayaShetty:将ChartMouseListener 添加到您的ChartPanel 并在chartMouseMoved() 中调用setExplodePercent()
  • @ChayaShetty:我添加了一个示例来尝试。像0.05 这样的较小百分比可能会很好。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-04
  • 1970-01-01
相关资源
最近更新 更多