J'ai passé la majeure partie de la journée à lire "Le Guide du développeur de la bibliothèque de classes JFreeChart v1.0.13" et des exemples associés. Je crois que je sais comment créer le graphique, je ne suis pas clair sur la façon de l'afficher dans un JLabel JFrame. Quand j'ai essayé ceComment afficher un JFreeChart dans un JLabel JFrame
LineChartDemo1 chart = new LineChartDemo1("Chart title");
jLabelChart.add(chart);
chart.setVisible(true);
Je suis
Exception dans le thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: ajoutant une fenêtre à un conteneur
Je vois que ce que j'essaie est illégal, mais quelle approche dois-je utiliser pour afficher le graphique? Ne devrais-je pas utiliser un JLabel?
/**
* A simple demonstration application showing how to create a line chart using
* data from a {@link CategoryDataset}.
*/
public class LineChartDemo1 extends ApplicationFrame {
/**
* Creates a new demo.
*
* @param title the frame title.
*/
public LineChartDemo1(String title) {
super(title);
JPanel chartPanel = createDemoPanel();
chartPanel.setPreferredSize(new Dimension(500, 270));
setContentPane(chartPanel);
}
/**
* Creates a sample dataset.
*
* @return The dataset.
*/
private static CategoryDataset createDataset() {
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
dataset.addValue(212, "Classes", "JDK 1.0");
dataset.addValue(504, "Classes", "JDK 1.1");
dataset.addValue(1520, "Classes", "JDK 1.2");
dataset.addValue(1842, "Classes", "JDK 1.3");
dataset.addValue(2991, "Classes", "JDK 1.4");
dataset.addValue(3500, "Classes", "JDK 1.5");
return dataset;
}
/**
* Creates a sample chart.
*
* @param dataset a dataset.
*
* @return The chart.
*/
private static JFreeChart createChart(CategoryDataset dataset) {
// create the chart...
JFreeChart chart = ChartFactory.createLineChart(
"Java Standard Class Library", // chart title
null, // domain axis label
"Class Count", // range axis label
dataset, // data
PlotOrientation.VERTICAL, // orientation
false, // include legend
true, // tooltips
false // urls
);
chart.addSubtitle(new TextTitle("Number of Classes By Release"));
TextTitle source = new TextTitle(
"Source: Java In A Nutshell (5th Edition) "
+ "by David Flanagan (O'Reilly)");
source.setFont(new Font("SansSerif", Font.PLAIN, 10));
source.setPosition(RectangleEdge.BOTTOM);
source.setHorizontalAlignment(HorizontalAlignment.RIGHT);
chart.addSubtitle(source);
CategoryPlot plot = (CategoryPlot) chart.getPlot();
plot.setRangePannable(true);
plot.setRangeGridlinesVisible(false);
URL imageURL = LineChartDemo1.class.getClassLoader().getResource(
"OnBridge11small.png");
if (imageURL != null) {
ImageIcon temp = new ImageIcon(imageURL);
// use ImageIcon because it waits for the image to load...
chart.setBackgroundImage(temp.getImage());
plot.setBackgroundPaint(null);
}
// customise the range axis...
NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
ChartUtilities.applyCurrentTheme(chart);
// customise the renderer...
LineAndShapeRenderer renderer
= (LineAndShapeRenderer) plot.getRenderer();
renderer.setBaseShapesVisible(true);
renderer.setDrawOutlines(true);
renderer.setUseFillPaint(true);
renderer.setBaseFillPaint(Color.white);
renderer.setSeriesStroke(0, new BasicStroke(3.0f));
renderer.setSeriesOutlineStroke(0, new BasicStroke(2.0f));
renderer.setSeriesShape(0, new Ellipse2D.Double(-5.0, -5.0, 10.0, 10.0));
return chart;
}
/**
* Creates a panel for the demo (used by SuperDemo.java).
*
* @return A panel.
*/
public static JPanel createDemoPanel() {
JFreeChart chart = createChart(createDataset());
ChartPanel panel = new ChartPanel(chart);
panel.setMouseWheelEnabled(true);
return panel;
}
/**
* Starting point for the demonstration application.
*
* @param args ignored.
*/
public static void main(String[] args) {
LineChartDemo1 demo = new LineChartDemo1(
"JFreeChart: LineChartDemo1.java");
demo.pack();
RefineryUtilities.centerFrameOnScreen(demo);
demo.setVisible(true);
}
}