Insert a button into a JFreeChart graph

2019-02-26 23:57发布

问题:

I used a code in order to display a graph. I want to insert a button in this graph (Show details) that i will used in order to present some details about the graph .It is realisable? thanks.

if(jCheckBox3.isSelected()) {
    try {
        con = getConnection("jdbc:mysql://localhost:3306/base_rapport","root","");
        Statement statement = con.createStatement(rs.TYPE_FORWARD_ONLY,rs.CONCUR_READ_ONLY);
        String sql3 = "Select Vendor, sum(Rate) as Rate from (select case Vendor when 'NSN' then 'Nokia' else Vendor end as Vendor, Rate from  (  Select vendor ,(count(1) )*100/(Select count(id_incident)from incident where open_time between '"+jTextField1.getText()+"' and'"+jTextField2.getText()+"' and vendor !='') as Rate  from incident   where open_time between '"+jTextField1.getText()+"'and'"+jTextField2.getText()+"' and vendor !='' group by upper(vendor) ) as x ) as y group by vendor";

        rs3 = statement.executeQuery(sql3);
        DefaultPieDataset pieDataset = new DefaultPieDataset(); 

        while(rs3.next()) {      
            pieDataset.setValue( rs3.getString("vendor"),rs3.getDouble(2));
        }

        JFreeChart chart = ChartFactory.createPieChart3D("Disfonctionnement par fournisseurs",  pieDataset, true, true, true); 
        PiePlot3D piePlot3d = (PiePlot3D) chart.getPlot();
        piePlot3d.setLabelGenerator(new StandardPieSectionLabelGenerator("{0}={1}"));
        fenetre fen3 = new fenetre();
        JPanel pnl = new JPanel(new BorderLayout()); 

        fen3.setContentPane(pnl); 
        fen3.setVisible(true);
        fen3.setSize(500, 500); 
        ChartPanel cPanel1 = new ChartPanel(chart);    
        pnl.add(cPanel1);
        File fichier = new File("C:\\Users\\alaeddine.zammeli.st\\Desktop\\résultat_application\\Répartition par fournisseur de '"+jTextField1.getText()+"' à '"+jTextField2.getText()+"'.png");

        try { 
            ChartUtilities.saveChartAsPNG(fichier, chart, 500, 500); 
        } catch (IOException e) { 
            e.printStackTrace(); 
        } 
    } catch (Exception e) {
        JOptionPane.showMessageDialog(this,e);
    }
}

回答1:

I've never done it before, but in principle ChartPanel extends from JPanel. So you could use the setLayout() method and add components, such as a JButton to your chart.

However, I think this is not the spirit of ChartPanel. The chart itself is not made of JComponents that would be added to ChartPanel. In facts, it is drawn by the paintComponent() method from ChartPanel (Using Java2D). So if you add buttons and components to the panel, you'll have no way of properly controlling their display with respect to the charts. In particular the graph will always be displayed in the background, covering the full space of the panel.

What I have done a few times with JFreeCharts, and works really well, is use popup menus. You can completely control what to put in the popup menu, using getPopupMenu() and setPopupMenu(), or createPopupMenu(), and the default menu from JFreeCharts is already quite neat.

Alternatively, you could add JButtons next to or bellow the graph in your pnl container. Here's a example.



回答2:

Add a ChartMouseListener to the ChartPanel that contains your chart. You can get details about the PieSectionEntity that was clicked as shown below. See the implementation of toString() in PieSectionEntity for more. Optionally, you can highlight the entity as shown here.

ChartPanel panel = new ChartPanel(chart);
panel.addChartMouseListener(new ChartMouseListener() {

    @Override
    public void chartMouseClicked(ChartMouseEvent e) {
        ChartEntity entity = e.getEntity();
        if (entity instanceof PieSectionEntity) {
            PieSectionEntity pse = (PieSectionEntity) entity;
            System.out.println(pse);
        }
    }

    @Override
    public void chartMouseMoved(ChartMouseEvent event) {
    }
});