程式扎記: [ Java 常見問題 ] JFreeChart : Retrieve data from mouse click on graph

標籤

2013年1月9日 星期三

[ Java 常見問題 ] JFreeChart : Retrieve data from mouse click on graph

Preface: 
這邊要講解, 如何透過在 JFreeChart 產生的 Chart 圖表上, 點擊圖表並獲得特定區域的資料. 這邊會以 PieChart3DDemo3 當作範例. 

Sample Code: 
在 PieChart3DDemo3 的執行結果會得到下面的 PieChart 圖表, 而我們的加 code 讓你在Pie 圖表特定區塊上點擊滑鼠後, 跳出視窗顯示點擊區域的主題: 
 

其實只要在 ChartPanel 加上 ChartMouseListener 如下: 
  1. chartpanel.addChartMouseListener(new ChartMouseListener(){  
  2.     public void chartMouseClicked(ChartMouseEvent e){  
  3.         PieSectionEntity entity = (PieSectionEntity)e.getEntity();  
  4.         System.out.println(entity.getSectionKey());  
  5.         JOptionPane.showConfirmDialog(null, entity.getSectionKey(), "Info", JOptionPane.CLOSED_OPTION, JOptionPane.INFORMATION_MESSAGE);  
  6.     }  
  7.     public void chartMouseMoved(ChartMouseEvent e){  
  8.         //[...do something on move...]  
  9.     }  
  10. });  
如此你在 Java 區域點擊便會得到對應 Key 值的 Popup 如下: 
 

完整代碼如下: 
  1. package piecharts;  
  2.   
  3. import java.awt.Dimension;  
  4. import java.text.AttributedString;  
  5.   
  6. import javax.swing.JOptionPane;  
  7. import javax.swing.JPanel;  
  8.   
  9. import org.jfree.chart.ChartFactory;  
  10. import org.jfree.chart.ChartMouseEvent;  
  11. import org.jfree.chart.ChartMouseListener;  
  12. import org.jfree.chart.ChartPanel;  
  13. import org.jfree.chart.JFreeChart;  
  14. import org.jfree.chart.entity.PieSectionEntity;  
  15. import org.jfree.chart.labels.PieSectionLabelGenerator;  
  16. import org.jfree.chart.plot.PiePlot3D;  
  17. import org.jfree.data.general.DefaultPieDataset;  
  18. import org.jfree.data.general.PieDataset;  
  19. import org.jfree.ui.ApplicationFrame;  
  20. import org.jfree.ui.RefineryUtilities;  
  21. import org.jfree.util.Rotation;  
  22.   
  23. public class PieChart3DDemo3 extends ApplicationFrame {  
  24.     static class CustomLabelGenerator implements PieSectionLabelGenerator {  
  25.   
  26.         public String generateSectionLabel(PieDataset piedataset,  
  27.                 Comparable comparable) {  
  28.             String s = null;  
  29.             if (piedataset != null && !comparable.equals("PHP"))  
  30.                 s = comparable.toString();  
  31.             return s;  
  32.         }  
  33.   
  34.         public AttributedString generateAttributedSectionLabel(  
  35.                 PieDataset piedataset, Comparable comparable) {  
  36.             return null;  
  37.         }  
  38.   
  39.         CustomLabelGenerator() {  
  40.         }  
  41.     }  
  42.   
  43.     public PieChart3DDemo3(String s) {  
  44.         super(s);  
  45.         PieDataset piedataset = createDataset();  
  46.         JFreeChart jfreechart = createChart(piedataset);  
  47.         ChartPanel chartpanel = new ChartPanel(jfreechart);  
  48.         chartpanel.setPreferredSize(new Dimension(500270));  
  49.         chartpanel.addChartMouseListener(new ChartMouseListener(){  
  50.             public void chartMouseClicked(ChartMouseEvent e){  
  51.                 PieSectionEntity entity = (PieSectionEntity)e.getEntity();  
  52.                 System.out.println(entity.getSectionKey());  
  53.                 JOptionPane.showConfirmDialog(null, entity.getSectionKey(), "Info", JOptionPane.CLOSED_OPTION, JOptionPane.INFORMATION_MESSAGE);  
  54.             }  
  55.             public void chartMouseMoved(ChartMouseEvent e){  
  56.                 //[...do something on move...]  
  57.             }  
  58.         });  
  59.         setContentPane(chartpanel);  
  60.     }  
  61.   
  62.     private static PieDataset createDataset() {  
  63.         DefaultPieDataset defaultpiedataset = new DefaultPieDataset();  
  64.         defaultpiedataset.setValue("Java"new Double(43.200000000000003D));  
  65.         defaultpiedataset.setValue("Visual Basic"new Double(10D));  
  66.         defaultpiedataset.setValue("C/C++"new Double(17.5D));  
  67.         defaultpiedataset.setValue("PHP"new Double(32.5D));  
  68.         defaultpiedataset.setValue("Perl"new Double(1.0D));  
  69.         return defaultpiedataset;  
  70.     }  
  71.   
  72.     private static JFreeChart createChart(PieDataset piedataset) {  
  73.         JFreeChart jfreechart = ChartFactory.createPieChart3D(  
  74.                 "Pie Chart 3D Demo 3", piedataset, truetruefalse);  
  75.         PiePlot3D pieplot3d = (PiePlot3D) jfreechart.getPlot();  
  76.         pieplot3d.setStartAngle(290D);  
  77.         pieplot3d.setDirection(Rotation.CLOCKWISE);  
  78.         pieplot3d.setForegroundAlpha(0.5F);  
  79.         pieplot3d.setNoDataMessage("No data to display");  
  80.         pieplot3d.setLabelGenerator(new CustomLabelGenerator());  
  81.         return jfreechart;  
  82.     }  
  83.   
  84.     public static JPanel createDemoPanel() {  
  85.         JFreeChart jfreechart = createChart(createDataset());  
  86.         return new ChartPanel(jfreechart);  
  87.     }  
  88.   
  89.     public static void main(String args[]) {  
  90.         PieChart3DDemo3 piechart3ddemo3 = new PieChart3DDemo3(  
  91.                 "JFreeChart: PieChart3DDemo3.java");  
  92.         piechart3ddemo3.pack();  
  93.         RefineryUtilities.centerFrameOnScreen(piechart3ddemo3);  
  94.         piechart3ddemo3.setVisible(true);  
  95.     }  
  96. }  

Supplement: 
Stackoverflow : JFreeChart - obtain data source value on mouse click

沒有留言:

張貼留言

網誌存檔

關於我自己

我的相片
Where there is a will, there is a way!