adding a chart to an already existing panel
adding a chart to an already existing panel
I am trying to add a chart to my existing panel. I have 3 tables at the top and want the chart at the bottom. I am just using a demo for now, but once I get this to work, I will be creating my own charts. Here is the way I am calling the LineChartFrame:
LineChartFrame demo = new LineChartFrame();
demo.setBounds(new Rectangle(3, 518, 826, 212));
demo.pack();
RefineryUtilities.centerFrameOnScreen(demo);
demo.setVisible(true);
It brings up another window with the chart in it. Can I add it to my panel like I did with my tables (with scrolling bars so they are listpanes)?:
jPanel1.add(listPane1, null);
jPanel1.add(listPane2, null);
jPanel1.add(listPane3, null);
I am hoping to do this, but whenever I try this:
jPanel1.add(demo, null);
I get an error:
java.lang.IllegalArgumentException: adding a window to a container
Can anyone help me out? Thanks.
Allyson
LineChartFrame demo = new LineChartFrame();
demo.setBounds(new Rectangle(3, 518, 826, 212));
demo.pack();
RefineryUtilities.centerFrameOnScreen(demo);
demo.setVisible(true);
It brings up another window with the chart in it. Can I add it to my panel like I did with my tables (with scrolling bars so they are listpanes)?:
jPanel1.add(listPane1, null);
jPanel1.add(listPane2, null);
jPanel1.add(listPane3, null);
I am hoping to do this, but whenever I try this:
jPanel1.add(demo, null);
I get an error:
java.lang.IllegalArgumentException: adding a window to a container
Can anyone help me out? Thanks.
Allyson
Re: adding a chart to an already existing panel
Hi Allyson,
You are trying to add a (subclass of) JFrame to a JPanel...that won't work, of course, and Java tells you so.
You need to create a ChartPanel to display your chart. This is a subclass of JComponent, which you can happily add to a JPanel (or any other container).
Regards,
Dave Gilbert
You are trying to add a (subclass of) JFrame to a JPanel...that won't work, of course, and Java tells you so.
You need to create a ChartPanel to display your chart. This is a subclass of JComponent, which you can happily add to a JPanel (or any other container).
Regards,
Dave Gilbert
Re: adding a chart to an already existing panel
Thanks for the help. I created the LineChartFrame method and this is what it looks like:
private void LineChartFrame() {
double[][] data = new double[][] {
{ 1.0, 4.0, 3.0, 5.0, 5.0, 7.0, 7.0, 8.0 },
{ 5.0, 7.0, 6.0, 8.0, 4.0, 4.0, 2.0, 1.0 },
{ 4.0, 3.0, 2.0, 3.0, 6.0, 3.0, 4.0, 3.0 }
};
DefaultCategoryDataset dataset = new DefaultCategoryDataset(data);
// set the series names...
String[] seriesNames = new String[] { "First", "Second", "Third" };
dataset.setSeriesNames(seriesNames);
// set the category names...
String[] categories = new String[] { "Type 1", "Type 2", "Type 3", "Type 4", "Type 5", "Type 6", "Type 7", "Type 8" };
dataset.setCategories(categories);
// create the chart...
chart = ChartFactory.createLineChart(
"Line Chart Demo 1", // chart title
"Category", // domain axis label
"Value", // range axis label
dataset, // data
true, // include legend
true, // tooltips
false); // urls
chartPanel = new ChartPanel(chart);
chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
setContentPane(chartPanel);
}
Now, how do I call it so that the chart appears on the bottom on my panel? I know you said to create a chartpanel to display the chart. Isn't that what chartPanel does? So, do I just add it to the panel I want it in?
I tried this:
jPanel1.add(chartPanel, null);
But I get this error:
java.lang.NullPointerException
ChartPanel is defined globally in this file.
jPanel1 is my main panel that I want to add the chart to. Can you help me out? Thanks.
Allyson
private void LineChartFrame() {
double[][] data = new double[][] {
{ 1.0, 4.0, 3.0, 5.0, 5.0, 7.0, 7.0, 8.0 },
{ 5.0, 7.0, 6.0, 8.0, 4.0, 4.0, 2.0, 1.0 },
{ 4.0, 3.0, 2.0, 3.0, 6.0, 3.0, 4.0, 3.0 }
};
DefaultCategoryDataset dataset = new DefaultCategoryDataset(data);
// set the series names...
String[] seriesNames = new String[] { "First", "Second", "Third" };
dataset.setSeriesNames(seriesNames);
// set the category names...
String[] categories = new String[] { "Type 1", "Type 2", "Type 3", "Type 4", "Type 5", "Type 6", "Type 7", "Type 8" };
dataset.setCategories(categories);
// create the chart...
chart = ChartFactory.createLineChart(
"Line Chart Demo 1", // chart title
"Category", // domain axis label
"Value", // range axis label
dataset, // data
true, // include legend
true, // tooltips
false); // urls
chartPanel = new ChartPanel(chart);
chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
setContentPane(chartPanel);
}
Now, how do I call it so that the chart appears on the bottom on my panel? I know you said to create a chartpanel to display the chart. Isn't that what chartPanel does? So, do I just add it to the panel I want it in?
I tried this:
jPanel1.add(chartPanel, null);
But I get this error:
java.lang.NullPointerException
ChartPanel is defined globally in this file.
jPanel1 is my main panel that I want to add the chart to. Can you help me out? Thanks.
Allyson
Re: adding a chart to an already existing panel
I still can't get this to work. Does anyone have any ideas? Am I adding this chartpanel to jPanel1 wrong? Any ideas? Thanks.
Allyson
Allyson
Re: adding a chart to an already existing panel
Ok, I know someone must be able to figure this one out. I know that Dave Gilbert answered the original question, but I still can't figure this out. Anyone? Please?! Thanks!
Allyson
Allyson
Re: adding a chart to an already existing panel
I finally figured this out all by myself. Dave - if you want to respond on the java forum under java programming (just look for ajgwin), I will give you my one duke dollar. It was my last one. Thanks.
Allyson
Allyson
Re: adding a chart to an already existing panel
hi Allyson
today i m facing the similar problem that u faced earlier. if you have sorted out this problem, will you be kind enough to tell me what went wrong or whats the right way to display the chart in an existing panel. thanx.
Bilal.
today i m facing the similar problem that u faced earlier. if you have sorted out this problem, will you be kind enough to tell me what went wrong or whats the right way to display the chart in an existing panel. thanx.
Bilal.
Re: adding a chart to an already existing panel
hi Allyson
today i m facing the similar problem that u faced earlier. if you have sorted out this problem, will you be kind enough to tell me what went wrong or whats the right way to display the chart in an existing panel. thanx.
Bilal.
today i m facing the similar problem that u faced earlier. if you have sorted out this problem, will you be kind enough to tell me what went wrong or whats the right way to display the chart in an existing panel. thanx.
Bilal.
Re: adding a chart to an already existing panel
Bilal -
This is what I did:
private void LineChartFrame() {
double[][] data = new double[][] {
{ 1.0, 4.0, 3.0, 5.0, 5.0, 7.0, 7.0, 8.0 },
{ 5.0, 7.0, 6.0, 8.0, 4.0, 4.0, 2.0, 1.0 },
{ 4.0, 3.0, 2.0, 3.0, 6.0, 3.0, 4.0, 3.0 }
};
DefaultCategoryDataset dataset = new DefaultCategoryDataset(data);
// set the series names...
String[] setups = new String[SetupCount];
for (int i = 0; i < SetupCount; i++) {
String stringi = "Setup " + "#" + new Integer(i+1).toString();
setups = stringi;
}
dataset.setSeriesNames(setups);
// set the category names...
// String[] categories = new String[] { "0", "20", "40", "60", "80", "100", "120",
// "140", "160", "180", "200", "220", "240", "260", "280", "300", "320", "340", "360" };
String[] categories = new String[] { "0", "20", "40", "60", "80", "100", "120", "140" };
dataset.setCategories(categories);
// create the chart...
chart = ChartFactory.createLineChart(
"", // chart title
"Site--Azimuth (Surveyed)", // domain axis label
"Vertical Angle", // range axis label
dataset, // data
true, // include legend
true, // tooltips
false); // urls
chartPanel = new ChartPanel(chart);
chartPanel.setBounds(new Rectangle(3, 518, 826, 212));
jPanel1.add(chartPanel, null);
I called it below just like this:
LineChartFrame();
I removed the setContentPane(chartPanel) and added my own panel (jPanel1) in there. I was adding it to the panel outside the method, not inside it.
I hope this helps you out.
Allyson
This is what I did:
private void LineChartFrame() {
double[][] data = new double[][] {
{ 1.0, 4.0, 3.0, 5.0, 5.0, 7.0, 7.0, 8.0 },
{ 5.0, 7.0, 6.0, 8.0, 4.0, 4.0, 2.0, 1.0 },
{ 4.0, 3.0, 2.0, 3.0, 6.0, 3.0, 4.0, 3.0 }
};
DefaultCategoryDataset dataset = new DefaultCategoryDataset(data);
// set the series names...
String[] setups = new String[SetupCount];
for (int i = 0; i < SetupCount; i++) {
String stringi = "Setup " + "#" + new Integer(i+1).toString();
setups = stringi;
}
dataset.setSeriesNames(setups);
// set the category names...
// String[] categories = new String[] { "0", "20", "40", "60", "80", "100", "120",
// "140", "160", "180", "200", "220", "240", "260", "280", "300", "320", "340", "360" };
String[] categories = new String[] { "0", "20", "40", "60", "80", "100", "120", "140" };
dataset.setCategories(categories);
// create the chart...
chart = ChartFactory.createLineChart(
"", // chart title
"Site--Azimuth (Surveyed)", // domain axis label
"Vertical Angle", // range axis label
dataset, // data
true, // include legend
true, // tooltips
false); // urls
chartPanel = new ChartPanel(chart);
chartPanel.setBounds(new Rectangle(3, 518, 826, 212));
jPanel1.add(chartPanel, null);
I called it below just like this:
LineChartFrame();
I removed the setContentPane(chartPanel) and added my own panel (jPanel1) in there. I was adding it to the panel outside the method, not inside it.
I hope this helps you out.
Allyson
Re: adding a chart to an already existing panel
The important thing to remember is that the ChartPanel class extends JPanel, so you can add it anywhere in your Swing user interface that you would add a regular panel. And let the layout manager take care of the size...
Regards,
Dave Gilbert
Regards,
Dave Gilbert