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);
}
}
Add a
ChartMouseListener
to theChartPanel
that contains your chart. You can get details about thePieSectionEntity
that was clicked as shown below. See the implementation oftoString()
inPieSectionEntity
for more. Optionally, you can highlight the entity as shown here.I've never done it before, but in principle
ChartPanel
extends fromJPanel
. So you could use thesetLayout()
method and add components, such as aJButton
to your chart.However, I think this is not the spirit of
ChartPanel
. The chart itself is not made ofJComponent
s that would be added toChartPanel
. In facts, it is drawn by thepaintComponent()
method fromChartPanel
(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()
andsetPopupMenu()
, orcreatePopupMenu()
, and the default menu from JFreeCharts is already quite neat.Alternatively, you could add
JButtons
next to or bellow the graph in yourpnl
container. Here's a example.