Combining a line chart with a twin-bar chart

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
Laurence Moroney

Combining a line chart with a twin-bar chart

Post by Laurence Moroney » Fri Sep 20, 2002 4:53 pm

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

David Gilbert

Re: Combining a line chart with a twin-bar chart

Post by David Gilbert » Fri Sep 20, 2002 9:38 pm

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);

}

}

Laurence Moroney

Re: Combining a line chart with a twin-bar chart

Post by Laurence Moroney » Tue Sep 24, 2002 3:12 pm

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

David Gilbert

Re: Combining a line chart with a twin-bar chart

Post by David Gilbert » Wed Sep 25, 2002 8:36 am

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.

Raldy

Re: Combining a line chart with a twin-bar chart

Post by Raldy » Fri Sep 27, 2002 9:48 am

Hi David,

I tried running the code you posted above but there is no graph displayed when I run it.

Thanks

Andreas Schroeder

Re: Combining a line chart with a twin-bar chart

Post by Andreas Schroeder » Fri Sep 27, 2002 9:58 am

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

Locked