JFreeChart:鼠标单击时获取栏的ID

时间:2014-03-12 06:53:20

标签: java swing jfreechart mouselistener

我想将三个值绑定到数据集,如Value(y轴值),Column Name(X轴值),Column ID(点击鼠标)以制作下钻图表。

以下是我创建数据集的代码:

DefaultCategoryDataset categoryDataset = new DefaultCategoryDataset();
categoryDataset.setValue(80, "", "Chap 1");
categoryDataset.setValue(52, "", "Chap 2");
categoryDataset.setValue(95, "", "Chap 3");
categoryDataset.setValue(25, "", "Chap 4");

图表:

JFreeChart chapterChart = createChart(chapterChartDataset, "Average Result");
ChartPanel chapterChartPanel = new ChartPanel(chapterChart);
chapterChartPanel.addChartMouseListener(new ChartMouseListener() {
   @Override
   public void chartMouseMoved(ChartMouseEvent arg0) {
   }
   @Override
   public void chartMouseClicked(ChartMouseEvent event){
      System.out.println(event.getSource().getClass());
      ChartEntity chartentity = event.getEntity();
      if (chartentity != null && chartentity.getToolTipText() != null && chartentity.getToolTipText().trim().length() > 0){
         System.out.println("Mouse clicked: " + chartentity.toString());
         String chapName = chartentity.getToolTipText().trim();
      }else{
         System.out.println("Mouse clicked: null entity.");
      }
   }
});

在此代码中,我还需要通过鼠标单击存储和检索章节ID。

0 个答案:

没有答案