[JFreeChart] Problème intégration graphique dans application Java
Problème intégration graphique JFreeChart dans appli. Java
Bonjour,
J'ai un problème d'intégration de graphique JFreeChart à mon application java. Je m'explique :
J'ai recupéré le code d'un line chart, que j'ai un peu modifié. Lorsque je lance le graphique de manière independante, pas de problème, tout marche.
Voila le code du graphique, quand je le lance independamment de mon programme.
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
|
public class LineChartDemo6 extends ApplicationFrame {
private XYDataset dataset;
private JFreeChart chart;
private ChartPanel chartPanel;
/**
* Creates a new demo.
*
* @param title the frame title.
*/
public LineChartDemo6() {
super("test");
this.dataset = createDataset();
this.chart = createChart(dataset);
this.chartPanel = new ChartPanel(chart);
chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
setContentPane(chartPanel);
}
private XYDataset createDataset() {
final XYSeriesCollection dataset = new XYSeriesCollection();
XYSeries series1 = new XYSeries("First");
series1.add(1.0, 1.0);
series1.add(2.0, 4.0);
dataset.addSeries(series1);
series1 = new XYSeries ("Second");
series1.add(1.0, 2.0);
series1.add(2.0, 1.0);
dataset.addSeries(series1);
series1 = new XYSeries ("third");
series1.add(1.0, 1.0);
series1.add(2.0, 1.0);
dataset.addSeries(series1);
return dataset;
}
/**
* Creates a chart.
*
* @param dataset the data for the chart.
*
* @return a chart.
*/
private JFreeChart createChart(final XYDataset dataset) {
// create the chart...
final JFreeChart chart = ChartFactory.createXYLineChart(
"Line Chart Demo 6", // chart title
"X", // x axis label
"Y", // y axis label
dataset, // data
PlotOrientation.VERTICAL,
true, // include legend
true, // tooltips
false // urls
);
final XYPlot plot = chart.getXYPlot();
plot.setDomainGridlinePaint(Color.white);
plot.setRangeGridlinePaint(Color.white);
final XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer();
renderer.setSeriesLinesVisible(0, false);
renderer.setSeriesShapesVisible(1, false);
plot.setRenderer(renderer);
// change the auto tick unit selection to integer units only...
final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
// OPTIONAL CUSTOMISATION COMPLETED.
return chart;
}
public void setTitle(String title) {
chart.setTitle(title);
}
public static void main(final String[] args) {
final LineChartDemo6 demo = new LineChartDemo6();
demo.pack();
RefineryUtilities.centerFrameOnScreen(demo);
demo.setVisible(true);
}
} |
Pour l'intégrer à mon application, j'ai enlevé l'heritage a ApplicationFrame et tout ce qui s'en suit, et j'ai ajouté la methode suivante :
Code:
1 2 3 4 5
|
public ChartPanel getChartPanel() {
return chartPanel;
} |
Dans mon application, je fais quelque chose comme ca :
Code:
1 2
|
jContentPane.add(chart.getChartPanel(), BorderLayout.CENTER); |
Le problème que je rencontre dans mon application c'est qu'en faisant un clique droit sur le graphique (qui s'affiche ) le menu permettant d'imprimer, d'exporter en image etc s'ouvre, mais en meme temps, un zoom s'effectue automatiquement sur le graphique. Ce problème ne survient pas quand je lance le graphique de manière indépendante. Je présume qu'un problème quelconque de Listener se cacher derriere tout ca, mais je ne vois absolument pas comment regler ca. Quelqu'un aurait une idée ?
Merci d'avance