StackedXYBarChart + LineChart

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
baegmon
Posts: 5
Joined: Fri Oct 07, 2016 3:55 pm
antibot: No, of course not.

StackedXYBarChart + LineChart

Post by baegmon » Wed Feb 22, 2017 8:44 am

Hi all,
Trying to get StackedXYBarChart working with a LineChart but I can't seem to get it working.
Does anyone have an example of how I could get this working?

So far I've done a basic StackedXYBarChart but trying to add the LineChart datasets creates an error.
I appreciate any suggestions you might have for me.
Thanks!

david.gilbert
JFreeChart Project Leader
Posts: 11734
Joined: Fri Mar 14, 2003 10:29 am
antibot: No, of course not.
Contact:

Re: StackedXYBarChart + LineChart

Post by david.gilbert » Wed Feb 22, 2017 9:15 am

You should post the code that you've tried so far.

In general, to overlay a second dataset you need to specify (1) an additional dataset, (2) a renderer for that dataset (you probably want an XYLineAndShapeRenderer). There are examples of this in the demo collection provided with the JFreeChart Developer Guide...but if you have a dataset called data2A and a renderer called renderer2A then you'll only need the following change to your plot:

Code: Select all

plot.setDataset(1, data2A);
plot.setRenderer(1, renderer2A);
After that, there are attributes which can control the rendering order (which dataset appears "in front"), plus if you need to use additional axes you can map a dataset/renderer pair to different axes (if your plot has them).
David Gilbert
JFreeChart Project Leader

:idea: Read my blog
:idea: Support JFree via the Github sponsorship program

baegmon
Posts: 5
Joined: Fri Oct 07, 2016 3:55 pm
antibot: No, of course not.

Re: StackedXYBarChart + LineChart

Post by baegmon » Wed Feb 22, 2017 12:23 pm

david.gilbert wrote:You should post the code that you've tried so far.

In general, to overlay a second dataset you need to specify (1) an additional dataset, (2) a renderer for that dataset (you probably want an XYLineAndShapeRenderer). There are examples of this in the demo collection provided with the JFreeChart Developer Guide...but if you have a dataset called data2A and a renderer called renderer2A then you'll only need the following change to your plot:

Code: Select all

plot.setDataset(1, data2A);
plot.setRenderer(1, renderer2A);
After that, there are attributes which can control the rendering order (which dataset appears "in front"), plus if you need to use additional axes you can map a dataset/renderer pair to different axes (if your plot has them).

Hi David,

I forgot to post my code but I have the following so far:

Code: Select all

        TimeSeriesCollection data = new TimeSeriesCollection();
        TimeSeriesCollection data2 = new TimeSeriesCollection();

        TimeSeries time = new TimeSeries("A");

        time.add(new Month(1, new Year(2017)), 22);
        time.add(new Month(2, new Year(2017)), 24);
        time.add(new Month(3, new Year(2017)), 26);
        time.add(new Month(4, new Year(2017)), 28);
        time.add(new Month(5, new Year(2017)), 30);

        TimeSeries time2 = new TimeSeries("B");

        time2.add(new Month(1, new Year(2017)), 32);
        time2.add(new Month(2, new Year(2017)), 34);
        time2.add(new Month(3, new Year(2017)), 36);
        time2.add(new Month(4, new Year(2017)), 38);
        time2.add(new Month(5, new Year(2017)), 40);

        TimeSeries time3 = new TimeSeries("C");

        time3.add(new Month(1, new Year(2017)), 42);
        time3.add(new Month(2, new Year(2017)), 44);
        time3.add(new Month(3, new Year(2017)), 46);
        time3.add(new Month(4, new Year(2017)), 48);
        time3.add(new Month(5, new Year(2017)), 50);

        data.addSeries(time);
        data.addSeries(time2);

        data2.addSeries(time3);


        JFreeChart chart = org.jfree.chart.ChartFactory.createXYLineChart(
                "DEMO",
                "DATE",
                "VALUE",
                data

        );

        XYPlot plot = (XYPlot) chart.getPlot();

        PeriodAxis domainAxis = new PeriodAxis("Date");
        domainAxis.setAutoRangeTimePeriodClass(Month.class);

        plot.setDomainAxis(domainAxis);

        XYStepAreaRenderer renderer = new XYStepAreaRenderer(XYStepAreaRenderer.AREA);
        plot.setRenderer(0, renderer);
        plot.setDataset(2, data2);
        renderer.setOutline(true);


        XYLineAndShapeRenderer renderer1 = new XYLineAndShapeRenderer();
        renderer1.setBaseShapesVisible(true);
        renderer1.setBaseLinesVisible(false);
        plot.setRenderer(2, renderer1);

        plot.setDomainPannable(false);
        plot.setRangePannable(true);

        // display chart
        return new ChartViewer(chart);
This creates a chart like Image.

I have picked up on the tip you have given off a post on StackOverflow that I researched earlier however I am trying to make it into a stacked bar chart instead of an area chart.

I have tried using the StackedXYBarRenderer but I cannot cast TimeSeriesCollection into a TableXYDataset.
Is there any easier way to achieve the effect I'm after?

Thanks for your help.

EDIT: Solved it by using a TimeTableXYDataset instead. Thanks again!

Locked