我知道如何设置突出显示pieplot.setExplodePercent
但不知道怎么判设置鼠标所在部分突出显示还是根本就没有这种设置,需要自己监听事件
如图:
当鼠标移至“管理人员”时,“管理人员”部分突出显示,当鼠标移至“市场人员”时,“市场人员”部分突出显示

解决方案 »

  1.   


    package xxx.yyy.zzz;import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Dimension;import javax.swing.JPanel;import org.jfree.chart.ChartFactory;
    import org.jfree.chart.ChartMouseEvent;
    import org.jfree.chart.ChartMouseListener;
    import org.jfree.chart.ChartPanel;
    import org.jfree.chart.JFreeChart;
    import org.jfree.chart.entity.ChartEntity;
    import org.jfree.chart.entity.PieSectionEntity;
    import org.jfree.chart.plot.PiePlot;
    import org.jfree.data.general.DefaultPieDataset;
    import org.jfree.data.general.PieDataset;
    import org.jfree.ui.ApplicationFrame;
    import org.jfree.ui.RefineryUtilities;/**
     * 一个简单的Mouse Over Pie Chart例子
     * 
     * @author 王飞
     */
    public class MouseOverDemo extends ApplicationFrame { private static final long serialVersionUID = -2034000038323204379L; private static final double EXPLODE_PERCENT = 0.2;
    private static final String DEFAULT_EXPLODE_SECTION = "One"; public MouseOverDemo(String title) {
    super(title); final JPanel chartPanel = createChartPanel();
    chartPanel.setPreferredSize(new Dimension(500, 270));
    this.setContentPane(chartPanel);
    } private static PieDataset createDataset() {
    final DefaultPieDataset dataset = new DefaultPieDataset(); dataset.setValue("One", 10.5);
    dataset.setValue("Two", 24.5);
    dataset.setValue("Three", 35);
    dataset.setValue("Four", 30); return dataset;
    } private static JFreeChart createChart(PieDataset dataset) {
    JFreeChart chart = ChartFactory.createPieChart("Mouse Over Pie Chart", // 标题
    dataset, // 数据集
    true, // 显示 legend
    true, // 显示tooltips
    false // 不显示URLs
    ); final PiePlot plot = (PiePlot) chart.getPlot();
    plot.setSectionPaint("One", Color.BLUE);
    plot.setSectionPaint("Two", Color.CYAN);
    plot.setSectionPaint("Three", Color.YELLOW);
    plot.setSectionPaint("Four", Color.RED); // 默认突出显示的SECTION
    plot.setExplodePercent(DEFAULT_EXPLODE_SECTION, EXPLODE_PERCENT); return chart;
    } private static JPanel createChartPanel() {
    final JFreeChart chart = createChart(createDataset());
    final PiePlot plot = (PiePlot) chart.getPlot(); final MouseListenerPanel panel = new MouseListenerPanel(plot); ChartPanel chartPanel = new ChartPanel(chart);
    chartPanel.addChartMouseListener(panel);
    panel.add(chartPanel); return panel;
    } public static void main(String[] args) {
    final MouseOverDemo demo = new MouseOverDemo("图随鼠动");
    demo.pack();
    demo.setVisible(true); RefineryUtilities.centerFrameOnScreen(demo);
    } /** 一个实现了ChartMouseListener接口的Jpanel, 响应鼠标事件 */
    public static class MouseListenerPanel extends JPanel implements
    ChartMouseListener { private static final long serialVersionUID = 4345617895170507940L; /** 一个 PiePlot对象,用来更新此对象的section */
    private PiePlot plot; /** 前一个 突出显示 section的key */
    private Comparable<?> previousSection; public MouseListenerPanel(PiePlot plot) {
    super(new BorderLayout()); this.plot = plot;
    this.previousSection = DEFAULT_EXPLODE_SECTION;
    } /**
     * 相应一个mouse move 事件,如果鼠标经过的是一个data item,
     * 则突出显示这个data item并将前一个置位.
     */
    public void chartMouseMoved(ChartMouseEvent event) {
    final ChartEntity entity = event.getEntity(); if (entity instanceof PieSectionEntity) {
    // 重置前一个突出显示的Section
    this.resetPreviousSection(); // 突出显示当前鼠标指向的Section
    final PieSectionEntity pieEntity = (PieSectionEntity) entity;
    this.plot.setExplodePercent(pieEntity.getSectionKey(),
    EXPLODE_PERCENT); // 记住当前鼠标指向的Section的key
    this.previousSection = pieEntity.getSectionKey(); return;
    }
    } public void chartMouseClicked(
    @SuppressWarnings("unused") ChartMouseEvent event) {
    // 在此例中先不关心鼠标点击事件的响应
    }

    private void resetPreviousSection() {
    this.plot.setExplodePercent(this.previousSection, 0);
    }
    }
    }写了个简单的例子,代码有注释,这里就不罗嗦了。