BUG? CombineDomainXYPlot; XY scale is not working.

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
m3stevep
Posts: 13
Joined: Sun Oct 22, 2006 2:42 am
Location: UTAH, USA
Contact:

BUG? CombineDomainXYPlot; XY scale is not working.

Post by m3stevep » Mon Jun 04, 2007 10:10 am

I'm trying to create a CombinedDomainXYPlot, but am having troubles getting it to scale correctly.

It seems like the scale of the last XYPlot added wins and all others are ignored.

Here is a simplied version of my code. Any help is much appreciated.

Code: Select all


import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.plot.CombinedDomainXYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.DefaultTableXYDataset;

import javax.swing.*;
import java.awt.*;

/**
 * User: Steve Peterson
 * This class illustrates the limited functionality of the CombinedDomainXYPlot
 * Date: Jun 4, 2007
 * Time: 2:44:44 AM
 */
public class CombineDomainXYPlotDemo extends ApplicationFrame {

    public CombineDomainXYPlotDemo(String title) {
        super(title);
        JPanel chartPanel = createDemoPanel();
        chartPanel.setPreferredSize(new Dimension(500, 270));
        setContentPane(chartPanel);
    }

        /**
     *
     * @return new JFreeChart
     */
    private static JFreeChart createMultiChart()
    {
        XYSeries series;
        DefaultTableXYDataset dataset;
        XYLineAndShapeRenderer renderer;
        XYPlot plot;

        ValueAxis rangeAxis = new NumberAxis("Range");
        ValueAxis domainAxis = new NumberAxis("Domain");

        CombinedDomainXYPlot combined = new CombinedDomainXYPlot(domainAxis);
        combined.setRangeAxis(rangeAxis);

        for(int i=0; i < 2;i++)
        {
            series = getXYSeries(i);

            dataset = new DefaultTableXYDataset();
            dataset.addSeries(series);

            renderer = new XYLineAndShapeRenderer(true, false);
            plot = new XYPlot(dataset, domainAxis,  rangeAxis, renderer);
            combined.add(plot,4);
        }

        return new JFreeChart( "BUG; CombineDomainXYPlot doesn't scale with data", combined);

    }

    private static XYSeries getXYSeries(int label) {
        XYSeries series = new XYSeries(Integer.valueOf(label).toString(), true, false);

        series.add(10.0, 12353.3);
        series.add(20.0, 13734.4);
        series.add(30.0, 14525.3);
        series.add(40.0, 13984.3);
        series.add(50.0, 12999.4);
        series.add(60.0, 14274.3);
        series.add(70.0, 15943.5);
        series.add(80.0, 14845.3);
        series.add(90.0, 14645.4);
        series.add(100.0, 16234.6);
        series.add(110.0, 17232.3);
        series.add(120.0, 14232.2);
        series.add(130.0, 13102.2);
        series.add(140.0, 14230.2);
        series.add(150.0, 11235.2);

        // 2nd Graph doesn't auto scale to this Y value
        // Add a couple of points to illustrate
        if(label == 0) // using 0 because CombinedDomainXYPlot uses last series first
        {
            series.add(160.0, 21235.2);  // Notice X & Y value are larger than 1 series Range
            series.add(170.0, 31235.2);
        }

        return series;
    }

    /**
     * Creates a panel for the demo (used by SuperDemo.java).
     *
     * @return A panel.
     */
    public static JPanel createDemoPanel() {
        JFreeChart chart = createMultiChart();
        return new ChartPanel(chart);
    }

    /**
     * Starting point for the demonstration application.
     *
     * @param args  ignored.
     */
    public static void main(String[] args) {
        CombineDomainXYPlotDemo demo = new CombineDomainXYPlotDemo("CombineDomainXYPlot Demo");
        demo.pack();
        RefineryUtilities.centerFrameOnScreen(demo);
        demo.setVisible(true);
    }
}
This is the result. Notice that the graph in red goes off into nowhere. (not sure how to get the image to show, right-click 'view image' works)

Image
Last edited by m3stevep on Mon Jun 04, 2007 9:28 pm, edited 1 time in total.

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 » Mon Jun 04, 2007 3:32 pm

You've used the same NumberAxis instance as the y-axis on both subplots. You should create a new instance for each subplot.
David Gilbert
JFreeChart Project Leader

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

m3stevep
Posts: 13
Joined: Sun Oct 22, 2006 2:42 am
Location: UTAH, USA
Contact:

Post by m3stevep » Mon Jun 04, 2007 9:23 pm

Doing the above, as you suggested causes each plot to have its own range. That is not acceptable. The magnitude of change is skewed when you allow each plot to have a different scale.
Last edited by m3stevep on Thu Jun 07, 2007 8:23 am, edited 4 times in total.

m3stevep
Posts: 13
Joined: Sun Oct 22, 2006 2:42 am
Location: UTAH, USA
Contact:

Post by m3stevep » Tue Jun 05, 2007 6:22 am

After further review I was a bit premature to claim victory.

Each graph is in it's own range. I want them to be the same range, so the change is visible. The auto-scaling that is calculated per graph should be applied to all graphs in the combined.

Exampe: The scale for both graphs should be 0 - 31235.2 on both graphs.

If I do the following, the I get the desired outcome, but it seems kinda hokey to have to do it this way.

Code: Select all

        /**
     *
     * @return new JFreeChart
     */
    private static JFreeChart createMultiChart()
    {
        XYSeries series;
        DefaultTableXYDataset dataset;
        XYLineAndShapeRenderer renderer;
        XYPlot plot;

        ValueAxis rangeAxis = new NumberAxis("Range");
        ValueAxis domainAxis = new NumberAxis("Domain");
        domainAxis.setRangeWithMargins(0,170.0);
        rangeAxis.setRangeWithMargins(0,31235.2);

        CombinedDomainXYPlot combined = new CombinedDomainXYPlot(domainAxis);
        combined.setRangeAxis(rangeAxis);

        for(int i=0; i < 2;i++)
        {
            series = getXYSeries(i);

            dataset = new DefaultTableXYDataset();
            dataset.addSeries(series);

            renderer = new XYLineAndShapeRenderer(true, false);

           // plot = new XYPlot(dataset, new NumberAxis("Domain"),  new NumberAxis("Range"), renderer);
           plot = new XYPlot(dataset, domainAxis,  rangeAxis, renderer);
            
            combined.add(plot,4);
        }

        
        combined.setDomainAxis(domainAxis);
        combined.setRangeAxis(rangeAxis);

        return new JFreeChart( "BUG; CombineDomainXYPlot doesn't scale with data", combined);

    }

Locked