XYStepRenderer only works for first dataset?

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
forsey85
Posts: 17
Joined: Mon Jul 03, 2006 2:55 pm

XYStepRenderer only works for first dataset?

Post by forsey85 » Mon Jul 17, 2006 4:21 pm

Hi,

I have a chart with two datasets on two different axes.

If I try to use the XYStepRenderer on the plot it only affects the first dataset. If I use the method

Code: Select all

plot.setRenderer(index, new XYStepRenderer())
none of the datasets are affected.

Any ideas how to fix this?[/code]

angel
Posts: 899
Joined: Thu Jan 15, 2004 12:07 am
Location: Germany - Palatinate

Post by angel » Tue Jul 18, 2006 7:09 am

Have you mapped the datasource to the appropriate renderer?

forsey85
Posts: 17
Joined: Mon Jul 03, 2006 2:55 pm

Post by forsey85 » Tue Jul 18, 2006 8:19 am

is there a mapping method?

I've used

Code: Select all

plot.mapDatasetToRangeAxis(i, i)
to get the data on the second axis, but I can't see a similar method for mapping datasources to renderers.

I thought

Code: Select all

plot.setRenderer(i, renderer)
would map the specified renderer to the datasource of index i

angel
Posts: 899
Joined: Thu Jan 15, 2004 12:07 am
Location: Germany - Palatinate

Post by angel » Tue Jul 18, 2006 9:03 am

This is the mapping I asked about and it seems correct.

Maybe posting your sourcecode will help.

forsey85
Posts: 17
Joined: Mon Jul 03, 2006 2:55 pm

Post by forsey85 » Tue Jul 18, 2006 9:36 am

My source code isnt easy to post as it isn't very self contained, but I made a demo instead:

Code: Select all

import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.WindowConstants;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYStepRenderer;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

public class StepChartExample extends JFrame
{
    /** Creates a new instance of StepChartExample */
    public StepChartExample() 
    {
        //Create Data Sets, each with one series of random data.
        XYSeriesCollection dataSetOne = new XYSeriesCollection();
        dataSetOne.addSeries(createRandomData("DS1, S1", 100));
        
        XYSeriesCollection dataSetTwo = new XYSeriesCollection();
        dataSetOne.addSeries(createRandomData("DS2, S1", 100));
        
        //Create the chart
        JFreeChart chart = ChartFactory.createXYLineChart(  "Title",
                                                            "X Axis",
                                                            "Y Axis",
                                                            new XYSeriesCollection(),
                                                            PlotOrientation.VERTICAL,
                                                            true,
                                                            true,
                                                            true);
        //Get the charts plot
        XYPlot plot = chart.getXYPlot();
        
        //Add a second range axis
        plot.setRangeAxis(1, new NumberAxis("Y2 Axis"));
        
        //Set the data sets
        plot.setDataset(0, dataSetOne);
        plot.setDataset(1, dataSetTwo);
        
        //Map the data sets to the axes
        plot.mapDatasetToRangeAxis(0,0);
        plot.mapDatasetToRangeAxis(1,1);
        
        //THE PROBLEM:
            //Either of these two lines will set the
            //renderer for both data sets       
                //plot.setRenderer(new XYStepRenderer());
                //plot.setRenderer(0, new XYStepRenderer());
            //Whereas this line seems to have no effect on either 
            //data sets
                plot.setRenderer(1, new XYStepRenderer());
        
        //Create the chart panel
        ChartPanel chartPanel = new ChartPanel(chart);
        
        //Do the Swing stuff
        this.setLayout(new BorderLayout());
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        this.add(chartPanel);
        this.pack();
        this.setVisible(true);
    }
    
    /**Creates a series of random data*/
    public XYSeries createRandomData(String name, int amount)
    {
        XYSeries series = new XYSeries(name);
        
        for(int i=0; i<amount; i++)
            series.add(i, Math.random());
        
        return series;
    }
    
    public static void main(String[] args)
    {
        new StepChartExample();
    }
}

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

Post by david.gilbert » Tue Jul 18, 2006 10:20 am

forsey85 wrote:

Code: Select all

XYSeriesCollection dataSetTwo = new XYSeriesCollection();
dataSetOne.addSeries(createRandomData("DS2, S1", 100));
You have a bug in your code. You create a second dataset, but then you add the series data to the first dataset. So you end up with dataset 1 containing two series (both drawn by renderer1) and dataset 2 containing no data (so renderer2 has nothing to draw).
David Gilbert
JFreeChart Project Leader

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

forsey85
Posts: 17
Joined: Mon Jul 03, 2006 2:55 pm

Post by forsey85 » Tue Jul 18, 2006 12:38 pm

Oh yeh, I do have a bug in that code :lol:

When I fix the bug however i get a slightly different problem.

plot.setRenderer(1, new XYStepRenderer()); works correctly now, but plot.setRenderer(0, new XYStepRenderer()); sets the renderer for both datasets, when I only one it to set the renderer for the first dataset

Code: Select all

import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.WindowConstants;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYStepRenderer;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

public class StepChartExample extends JFrame
{
    /** Creates a new instance of StepChartExample */
    public StepChartExample() 
    {
        //Create Data Sets, each with one series of random data.
        XYSeriesCollection dataSetOne = new XYSeriesCollection();
        dataSetOne.addSeries(createRandomData("DS1, S1", 100));
        
        XYSeriesCollection dataSetTwo = new XYSeriesCollection();
        dataSetTwo.addSeries(createRandomData("DS2, S1", 100));
        
        //Create the chart
        JFreeChart chart = ChartFactory.createXYLineChart(  "Title",
                                                            "X Axis",
                                                            "Y Axis",
                                                            new XYSeriesCollection(),
                                                            PlotOrientation.VERTICAL,
                                                            true,
                                                            true,
                                                            true);
        //Get the charts plot
        XYPlot plot = chart.getXYPlot();
        
        //Add a second range axis
        plot.setRangeAxis(1, new NumberAxis("Y2 Axis"));
        
        //Set the data sets
        plot.setDataset(0, dataSetOne);
        plot.setDataset(1, dataSetTwo);
        
        //Map the data sets to the axes
        plot.mapDatasetToRangeAxis(0,0);
        plot.mapDatasetToRangeAxis(1,1);
        
        //THE PROBLEM:
            //Either of these two lines will set the
            //renderer for both data sets and makes both lines
            //red
                //plot.setRenderer(new XYStepRenderer());
                plot.setRenderer(0, new XYStepRenderer());
            //This line works
                //plot.setRenderer(1, new XYStepRenderer());
        
        //Create the chart panel
        ChartPanel chartPanel = new ChartPanel(chart);
        
        //Do the Swing stuff
        this.setLayout(new BorderLayout());
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        this.add(chartPanel);
        this.pack();
        this.setVisible(true);
    }
    
    //Creates a series of random data
    public XYSeries createRandomData(String name, int amount)
    {
        XYSeries series = new XYSeries(name);
        
        for(int i=0; i<amount; i++)
            series.add(i, Math.random());
        
        return series;
    }
    
    public static void main(String[] args)
    {
        new StepChartExample();
    }
}

forsey85
Posts: 17
Joined: Mon Jul 03, 2006 2:55 pm

Post by forsey85 » Tue Jul 18, 2006 12:56 pm

I've realised that it works fine so long as you don't use 0 for any of the dataset indicies. If you start from an index of 1 then its fine.

Locked