combined charts-help required!!

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
ambika4
Posts: 9
Joined: Tue Jul 20, 2010 12:54 am
antibot: No, of course not.

combined charts-help required!!

Post by ambika4 » Wed Jul 28, 2010 8:28 pm

Hi,

I am working on combine charts which consists of a bar chart and a line chart. The domain axis is common. I have to include multiple axis. The image should look something like this.

http://ambika-project.blogspot.com/2010 ... mages.html



My code is:-

Code: Select all

DefaultCategoryDataset dataset1 = new DefaultCategoryDataset();
option1=209;option2=4058;option4=3490;option3=5245;


       dataset1.addValue(option1, "Option", "Year 1");
        dataset1.addValue(option2, "Option", "Year 2");
        dataset1.addValue(option3, "Option", "Year 3");
        dataset1.addValue(option4, "Option", "Year 4");
        dataset1.addValue(option5, "Option", "Year 5");
    

        // create the first renderer...
        CategoryItemLabelGenerator generator
                = new StandardCategoryItemLabelGenerator();
        CategoryItemRenderer renderer = new BarRenderer();
        renderer.setBaseItemLabelGenerator(generator);
        renderer.setBaseItemLabelsVisible(true);

        CategoryPlot plot = new CategoryPlot();
        plot.setDataset(dataset1);
        plot.setRenderer(renderer);

        plot.setDomainAxis(new CategoryAxis("Year"));
        plot.setRangeAxis(new NumberAxis("Options"));

        plot.setOrientation(PlotOrientation.VERTICAL);
        plot.setRangeGridlinesVisible(true);
        plot.setDomainGridlinesVisible(true);

        // now create the second dataset and renderer...
        DefaultCategoryDataset dataset2 = new DefaultCategoryDataset();
        dataset2.addValue(1.01, "Tag", "Year 1");
        dataset2.addValue(2.09, "Tag", "Year 2");
        dataset2.addValue(3.08, "Tag", "Year 3");
        dataset2.addValue(4.07, "Tag", "Year 4");
        dataset2.addValue(7.06, "Tag", "Year 5");

       
        CategoryItemRenderer renderer2 = new LineAndShapeRenderer();
        plot.setDataset(1, dataset2);
        plot.setRenderer(1, renderer2);

       NumberAxis axis2 = new NumberAxis("Tag");
       plot.setRangeAxis(1, axis2);
        


       plot.setRangeAxisLocation(1, AxisLocation.BOTTOM_OR_RIGHT);

       plot.setDataset(1, dataset2);
 plot.setDatasetRenderingOrder(DatasetRenderingOrder.FORWARD);

        plot.getDomainAxis().setCategoryLabelPositions(
                CategoryLabelPositions.UP_45);
        JFreeChart chart = new JFreeChart(plot);
        chart.setTitle("Overlaid Bar Chart");
        ChartUtilities.applyCurrentTheme(chart);
        return chart;

But i am not able to generate chart as i need. the image i get is something like this:


http://ambika-project.blogspot.com/2010 ... ution.html


Could anyone help??

thanks in advance

paradoxoff
Posts: 1634
Joined: Sat Feb 17, 2007 1:51 pm

Re: combined charts-help required!!

Post by paradoxoff » Wed Jul 28, 2010 8:57 pm

Code: Select all

plot.mapDatasetToRangeAxis(1,1) 

ambika4
Posts: 9
Joined: Tue Jul 20, 2010 12:54 am
antibot: No, of course not.

Re: combined charts-help required!!

Post by ambika4 » Wed Jul 28, 2010 9:05 pm

hi paradoxoff,

Its working fine now :) thank u!!!

Locked