I have a bar chart containing 2 series of bars in a categorydataset.
I want to add a line chart containing other data to this as a combined chart with the line on top, and the bars on the bottom.
All of the examples I have seen only allow me a single bar series on the bottom. can anybody help???
Thanks,
:L
Combining a line chart with a twin-bar chart
Re: Combining a line chart with a twin-bar chart
Hi Laurence,
I think this can be done using OverlaidVerticalCategoryPlot (a contributed class that I haven't used until just now). Here's my code (sorry, the forum software will strip all the leading white space and I'm not sure how to get around that, but you can use something like jEdit to reformat it easily).
Regards,
DG.
/* ===============
* JFreeChart Demo
* ===============
*
* Project Info: http://www.object-refinery.com/jfreechart/index.html
* Project Lead: David Gilbert (david.gilbert@object-refinery.com);
*
* (C) Copyright 2000-2002, by Simba Management Limited and Contributors.
*
* This library is free software; you can redistribute it and/or modify it under the terms
* of the GNU Lesser General Public License as published by the Free Software Foundation;
* either version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with this
* library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307, USA.
*
* -------------------------
* OverlaidBarChartDemo.java
* -------------------------
* (C) Copyright 2002, by Simba Management Limited.
*
* Original Author: David Gilbert (for Simba Management Limited);
* Contributor(s): -;
*
* $Id$
*
* Changes
* -------
* 20-Sep-2002 : Version 1 (DG);
*
*/
package com.jrefinery.chart.demo;
import com.jrefinery.data.CategoryDataset;
import com.jrefinery.data.DefaultCategoryDataset;
import com.jrefinery.chart.JFreeChart;
import com.jrefinery.chart.ChartPanel;
import com.jrefinery.chart.VerticalCategoryPlot;
import com.jrefinery.chart.OverlaidVerticalCategoryPlot;
import com.jrefinery.chart.CategoryItemRenderer;
import com.jrefinery.chart.LineAndShapeRenderer;
import com.jrefinery.chart.VerticalBarRenderer;
import com.jrefinery.ui.ApplicationFrame;
/**
* A simple demonstration application showing how to create a vertical bar chart.
*
* @author DG
*/
public class OverlaidBarChartDemo extends ApplicationFrame {
/** The first dataset. */
private CategoryDataset data1;
/** The second dataset. */
private CategoryDataset data2;
/**
* Default constructor.
*
* @param title the frame title.
*/
public OverlaidBarChartDemo(String title) {
super(title);
// create the first dataset...
double[][] data1 = 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 }
};
DefaultCategoryDataset dataset1 = new DefaultCategoryDataset(data1);
dataset1.setSeriesName(0, "First");
dataset1.setSeriesName(1, "Second");
String[] categories1 = new String[] { "Category 1", "Category 2", "Category 3",
"Category 4", "Category 5", "Category 6",
"Category 7", "Category 8" };
dataset1.setCategories(categories1);
// create the first plot...
CategoryItemRenderer renderer = new VerticalBarRenderer();
VerticalCategoryPlot plot1 = new VerticalCategoryPlot(dataset1, null, null, renderer);
// create the second dataset...
double[][] data2 = new double[][] {
{ 9.0, 7.0, 2.0, 6.0, 6.0, 9.0, 5.0, 4.0 }
};
DefaultCategoryDataset dataset2 = new DefaultCategoryDataset(data2);
dataset2.setSeriesName(0, "Level");
String[] categories2 = new String[] { "Category 1", "Category 2", "Category 3",
"Category 4", "Category 5", "Category 6",
"Category 7", "Category 8" };
dataset2.setCategories(categories2);
// create the second plot...
CategoryItemRenderer renderer2 = new LineAndShapeRenderer();
VerticalCategoryPlot plot2 = new VerticalCategoryPlot(dataset2, null, null, renderer2);
// create the overlaid plot...
OverlaidVerticalCategoryPlot plot = new OverlaidVerticalCategoryPlot("Category", "Value",
categories1);
plot.add(plot1);
plot.add(plot2);
JFreeChart chart = new JFreeChart(plot);
// add the chart to a panel...
ChartPanel chartPanel = new ChartPanel(chart);
this.setContentPane(chartPanel);
}
/**
* Starting point for the demonstration application.
*
* @param args ignored.
*/
public static void main(String[] args) {
OverlaidBarChartDemo demo = new OverlaidBarChartDemo("Overlaid Bar Chart Demo");
demo.pack();
demo.setVisible(true);
}
}
I think this can be done using OverlaidVerticalCategoryPlot (a contributed class that I haven't used until just now). Here's my code (sorry, the forum software will strip all the leading white space and I'm not sure how to get around that, but you can use something like jEdit to reformat it easily).
Regards,
DG.
/* ===============
* JFreeChart Demo
* ===============
*
* Project Info: http://www.object-refinery.com/jfreechart/index.html
* Project Lead: David Gilbert (david.gilbert@object-refinery.com);
*
* (C) Copyright 2000-2002, by Simba Management Limited and Contributors.
*
* This library is free software; you can redistribute it and/or modify it under the terms
* of the GNU Lesser General Public License as published by the Free Software Foundation;
* either version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with this
* library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307, USA.
*
* -------------------------
* OverlaidBarChartDemo.java
* -------------------------
* (C) Copyright 2002, by Simba Management Limited.
*
* Original Author: David Gilbert (for Simba Management Limited);
* Contributor(s): -;
*
* $Id$
*
* Changes
* -------
* 20-Sep-2002 : Version 1 (DG);
*
*/
package com.jrefinery.chart.demo;
import com.jrefinery.data.CategoryDataset;
import com.jrefinery.data.DefaultCategoryDataset;
import com.jrefinery.chart.JFreeChart;
import com.jrefinery.chart.ChartPanel;
import com.jrefinery.chart.VerticalCategoryPlot;
import com.jrefinery.chart.OverlaidVerticalCategoryPlot;
import com.jrefinery.chart.CategoryItemRenderer;
import com.jrefinery.chart.LineAndShapeRenderer;
import com.jrefinery.chart.VerticalBarRenderer;
import com.jrefinery.ui.ApplicationFrame;
/**
* A simple demonstration application showing how to create a vertical bar chart.
*
* @author DG
*/
public class OverlaidBarChartDemo extends ApplicationFrame {
/** The first dataset. */
private CategoryDataset data1;
/** The second dataset. */
private CategoryDataset data2;
/**
* Default constructor.
*
* @param title the frame title.
*/
public OverlaidBarChartDemo(String title) {
super(title);
// create the first dataset...
double[][] data1 = 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 }
};
DefaultCategoryDataset dataset1 = new DefaultCategoryDataset(data1);
dataset1.setSeriesName(0, "First");
dataset1.setSeriesName(1, "Second");
String[] categories1 = new String[] { "Category 1", "Category 2", "Category 3",
"Category 4", "Category 5", "Category 6",
"Category 7", "Category 8" };
dataset1.setCategories(categories1);
// create the first plot...
CategoryItemRenderer renderer = new VerticalBarRenderer();
VerticalCategoryPlot plot1 = new VerticalCategoryPlot(dataset1, null, null, renderer);
// create the second dataset...
double[][] data2 = new double[][] {
{ 9.0, 7.0, 2.0, 6.0, 6.0, 9.0, 5.0, 4.0 }
};
DefaultCategoryDataset dataset2 = new DefaultCategoryDataset(data2);
dataset2.setSeriesName(0, "Level");
String[] categories2 = new String[] { "Category 1", "Category 2", "Category 3",
"Category 4", "Category 5", "Category 6",
"Category 7", "Category 8" };
dataset2.setCategories(categories2);
// create the second plot...
CategoryItemRenderer renderer2 = new LineAndShapeRenderer();
VerticalCategoryPlot plot2 = new VerticalCategoryPlot(dataset2, null, null, renderer2);
// create the overlaid plot...
OverlaidVerticalCategoryPlot plot = new OverlaidVerticalCategoryPlot("Category", "Value",
categories1);
plot.add(plot1);
plot.add(plot2);
JFreeChart chart = new JFreeChart(plot);
// add the chart to a panel...
ChartPanel chartPanel = new ChartPanel(chart);
this.setContentPane(chartPanel);
}
/**
* Starting point for the demonstration application.
*
* @param args ignored.
*/
public static void main(String[] args) {
OverlaidBarChartDemo demo = new OverlaidBarChartDemo("Overlaid Bar Chart Demo");
demo.pack();
demo.setVisible(true);
}
}
Re: Combining a line chart with a twin-bar chart
Thanks David --
But this is an overlaid chart -- I am interested in a *combined* chart, with a line in the top 'frame' and the category based bar at the bottom...
Any ideas?
:L
But this is an overlaid chart -- I am interested in a *combined* chart, with a line in the top 'frame' and the category based bar at the bottom...
Any ideas?
:L
Re: Combining a line chart with a twin-bar chart
Hi Laurence,
There is no CombinedVerticalCategoryPlot class yet. It is something that needs to be done before JFreeChart gets to 1.0.0.
Regards,
DG.
There is no CombinedVerticalCategoryPlot class yet. It is something that needs to be done before JFreeChart gets to 1.0.0.
Regards,
DG.
Re: Combining a line chart with a twin-bar chart
Hi David,
I tried running the code you posted above but there is no graph displayed when I run it.
Thanks
I tried running the code you posted above but there is no graph displayed when I run it.
Thanks
Re: Combining a line chart with a twin-bar chart
Hi Radly,
i think the problem you have is the one i allready had. This is a bug in the 0.9.3 version that is fixed in the cvs. The modifiaction to make is simple, David explained at
http://www.object-refinery.com/phorum-3 ... 570&t=4570
what you have to do.
Regards,
Andreas Schroeder
i think the problem you have is the one i allready had. This is a bug in the 0.9.3 version that is fixed in the cvs. The modifiaction to make is simple, David explained at
http://www.object-refinery.com/phorum-3 ... 570&t=4570
what you have to do.
Regards,
Andreas Schroeder