Having trouble with MultiAxis Charts

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
Ajz
Posts: 6
Joined: Tue Apr 26, 2005 9:00 pm

Having trouble with MultiAxis Charts

Post by Ajz » Thu Sep 01, 2005 8:46 pm

I have two separate datasets and i am trying to just get it so that i can render both on separate axis and with a separate color. I took the example in the demo document and have changed it so that it generates and image in my C drive: Here is the code:

Code: Select all

public void testChart() {
        XYDataset dSet1 = createDataset1();
        XYDataset dSet2 = createDataset2();
        JFreeChart chart = createChart(dSet1, dSet2);

        File f = new File("C:/imageTemp.jpg");
        try {
            ChartUtilities.saveChartAsJPEG(f, chart, 700, 500);
        } catch (IOException e) {

        }
    }

    private static JFreeChart createChart(XYDataset dataset, XYDataset dataset2) {
        JFreeChart chart = ChartFactory.createTimeSeriesChart("Legal & General Unit Trust Prices", // title
                "Date", // x-axis label
                "Price Per Unit", // y-axis label
                dataset, // data
                true, // create legend?
                true, // generate tooltips?
                false // generate URLs?
        );

        chart.setBackgroundPaint(Color.white);
        XYPlot plot = (XYPlot) chart.getPlot();

        NumberAxis axis2 = new NumberAxis("Range Axis 2");
        axis2.setLabelPaint(Color.BLUE);
        axis2.setAxisLinePaint(Color.BLUE);
        axis2.setTickLabelPaint(Color.BLUE);
        plot.setRangeAxis(1, axis2);
        plot.setRangeAxisLocation(1, AxisLocation.BOTTOM_OR_RIGHT);
        plot.setDataset(1, dataset2);
        plot.mapDatasetToDomainAxis(1, 0);
        plot.mapDatasetToRangeAxis(1, 1);

        XYItemRenderer r2 = plot.getRenderer();
        r2.setPaint(Color.BLUE);
        plot.setRenderer(1, r2);

        XYItemRenderer r = plot.getRenderer();
        if (r instanceof XYLineAndShapeRenderer) {
            XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer) r;
            renderer.setDefaultShapesVisible(true);
            renderer.setDefaultShapesFilled(true);
        }
        plot.setRenderer(0, r);
        DateAxis axis = (DateAxis) plot.getDomainAxis();
        axis.setDateFormatOverride(new SimpleDateFormat("MMM-yyyy"));
        return chart;
    }
PLease advise me as in what i am doing wrong and how i can generate this graph with multi axis

Thank you.
Ajay

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 » Fri Sep 02, 2005 11:43 am

You need to post runnable code and describe what is wrong with the output you are getting.
David Gilbert
JFreeChart Project Leader

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

Ajz
Posts: 6
Joined: Tue Apr 26, 2005 9:00 pm

here is all the code

Post by Ajz » Fri Sep 02, 2005 2:36 pm

Code: Select all

public class graphTests{


    public void main(){
        testChart();
    }
    
    public void testChart() {
        XYDataset dSet1 = createDataset1();
        XYDataset dSet2 = createDataset2();
        JFreeChart chart = createChart(dSet1, dSet2);

        File f = new File("C:/imageTemp.jpg");
        try {
            ChartUtilities.saveChartAsJPEG(f, chart, 700, 500);
        } catch (IOException e) {

        }
    }

    private static JFreeChart createChart(XYDataset dataset, XYDataset dataset2) {
        JFreeChart chart = ChartFactory.createTimeSeriesChart("Legal & General Unit Trust Prices", // title
                "Date", // x-axis label
                "Price Per Unit", // y-axis label
                dataset, // data
                true, // create legend?
                true, // generate tooltips?
                false // generate URLs?
        );

        chart.setBackgroundPaint(Color.white);
        XYPlot plot = (XYPlot) chart.getPlot();

        NumberAxis axis2 = new NumberAxis("Range Axis 2");
        axis2.setLabelPaint(Color.BLUE);
        axis2.setAxisLinePaint(Color.BLUE);
        axis2.setTickLabelPaint(Color.BLUE);
        plot.setRangeAxis(1, axis2);
        plot.setRangeAxisLocation(1, AxisLocation.BOTTOM_OR_RIGHT);
        plot.setDataset(1, dataset2);
        plot.mapDatasetToDomainAxis(1, 0);
        plot.mapDatasetToRangeAxis(1, 1);

        XYItemRenderer r2 = plot.getRenderer();
        r2.setPaint(Color.BLUE);
        plot.setRenderer(1, r2);

        XYItemRenderer r = plot.getRenderer();
        if (r instanceof XYLineAndShapeRenderer) {
            XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer) r;
            renderer.setDefaultShapesVisible(true);
            renderer.setDefaultShapesFilled(true);
        }
        plot.setRenderer(0, r);
        DateAxis axis = (DateAxis) plot.getDomainAxis();
        axis.setDateFormatOverride(new SimpleDateFormat("MMM-yyyy"));
        return chart;
    }

    private static XYDataset createDataset1() {
        TimeSeries s1 = new TimeSeries("L&G European Index Trust", Month.class);
        s1.add(new Month(2, 2001), 181.8);
        s1.add(new Month(3, 2001), 167.3);
        s1.add(new Month(4, 2001), 153.8);
        s1.add(new Month(5, 2001), 167.6);
        s1.add(new Month(6, 2001), 158.8);
        s1.add(new Month(7, 2001), 148.3);
        s1.add(new Month(8, 2001), 153.9);
        s1.add(new Month(9, 2001), 142.7);
        s1.add(new Month(10, 2001), 123.2);
        s1.add(new Month(11, 2001), 131.8);
        s1.add(new Month(12, 2001), 139.6);
        s1.add(new Month(1, 2002), 142.9);
        s1.add(new Month(2, 2002), 138.7);
        s1.add(new Month(3, 2002), 137.3);
        s1.add(new Month(4, 2002), 143.9);
        s1.add(new Month(5, 2002), 139.8);
        s1.add(new Month(6, 2002), 137.0);
        s1.add(new Month(7, 2002), 132.8);
        TimeSeriesCollection dataset = new TimeSeriesCollection();
        dataset.addSeries(s1);
        dataset.setDomainIsPointsInTime(true);
        return dataset;
    }

    private static XYDataset createDataset2() {
        TimeSeries s2 = new TimeSeries("L&G UK Index Trust", Month.class);
        s2.add(new Month(2, 2001), 129.6);
        s2.add(new Month(3, 2001), 123.2);
        s2.add(new Month(4, 2001), 117.2);
        s2.add(new Month(5, 2001), 124.1);
        s2.add(new Month(6, 2001), 122.6);
        s2.add(new Month(7, 2001), 119.2);
        s2.add(new Month(8, 2001), 116.5);
        s2.add(new Month(9, 2001), 112.7);
        s2.add(new Month(10, 2001), 101.5);
        s2.add(new Month(11, 2001), 106.1);
        s2.add(new Month(12, 2001), 110.3);
        s2.add(new Month(1, 2002), 111.7);
        s2.add(new Month(2, 2002), 111.0);
        s2.add(new Month(3, 2002), 109.6);
        s2.add(new Month(4, 2002), 113.2);
        s2.add(new Month(5, 2002), 111.6);
        s2.add(new Month(6, 2002), 108.8);
        s2.add(new Month(7, 2002), 101.6);
        TimeSeriesCollection dataset = new TimeSeriesCollection();
        dataset.addSeries(s2);
        dataset.setDomainIsPointsInTime(true);
        return dataset;
    }
}
When i run this code, i get a graph where both the lines are colored blue. I know that i can simple add the second series to the timeseries chart but i am trying to learn how to use the multiaxis chart.

Please example to me what i am doing wrong or if you have a working sample that i can see and run - i would appreciate you letting me take a look at it so that i can learn how this is done.

Thanks
Ajay

christopher.loerken
Posts: 16
Joined: Wed Aug 31, 2005 4:25 pm

Post by christopher.loerken » Mon Sep 05, 2005 2:46 pm

Hi,
I think your only defining one renderer for two series...

You get the renderer by just calling

Code: Select all

plot.getRenderer()
This way, you get two times the reference to the same renderer.
Try using:

Code: Select all

r = plot.getRenderer(0);
r2 = plot.getRenderer(1);
This should do what you want.
But note, that the renderer might be null if you haven't created it. Then just construct one using "new XYItemRenderer" .

I hope it helps,
cheers,
Christopher

Ajz
Posts: 6
Joined: Tue Apr 26, 2005 9:00 pm

Thanks

Post by Ajz » Wed Sep 07, 2005 8:53 pm

- that helped me

i have got it to where it is working with different color.

However, there was one other thing - is there some way in which it would automatically detect what the others color is and not repeat it or do i ahve to explicitly specify the color each time?

Thank you for all your help

Ajay

Locked