Search code examples
javachartsjfreechart

Combined bar and line chart JfreeChart. Trying to make a 3 point average with the line chart. The values do not line up correctly


I have a dataset for my barchart lets say:{1,2,3,4,5,6,7,8,9}. Then I have my line chart which is suppose to be the three point average, which is the value of the barchart at that index, and the two values before that, then divided by three.

The line chart should be nonexistent for the corresponding barchart values 1 and 2, and then begin at position 3. I tried using a try and catch block and a if statement so that it only put values in if it was possible, but when I did that it still started the line graph at position 1 and just stretched it out to fit the graph.

How can I do it so that the line chart starts at column 3 and then goes on from there?

Heres what I have right now:

public class mockTest extends JFrame{

public mockTest()
{
    //Mock data
    DefaultCategoryDataset dataset = new DefaultCategoryDataset();
    DefaultCategoryDataset dataset2 = new DefaultCategoryDataset();
    int[] times = new int[]{1,2,3,4,5,6,7,8,9};

    for ( int i = 0; i < times.length; i++ ){
        dataset.addValue(times[i], "Time", "Hour" + String.valueOf(i+1));;
        if(i>2)
        {
            dataset2.addValue((times[i] + times[i-1] + times[i-2])/3, "Time", "Hour" + String.valueOf(i+1));

        }

    }
    CategoryPlot plot = new CategoryPlot();

    //create the plot

    //add the first dataset, and render as bar values
    CategoryItemRenderer renderer = new BarRenderer();
    plot.setDataset(0,dataset);
    plot.setRenderer(0,renderer);  

    //add the second dataset, render as lines
    CategoryItemRenderer renderer2 = new LineAndShapeRenderer();
    plot.setDataset(1, dataset2);

    plot.setRenderer(1, renderer2);

    //set axis 
    CategoryAxis domainAxis = new CategoryAxis("Time");  
    NumberAxis rangeAxis = new NumberAxis("Value"); 

    plot.setDomainAxis(0,domainAxis);
    plot.setRangeAxis(rangeAxis);
    JFreeChart chart = new JFreeChart(plot);
    ChartPanel chartPanel = new ChartPanel( chart ); 

    this.setContentPane(chartPanel);
}   

** Figured it out, just where I put the if(i<2) statement, add an else statement and put in null as the values.


Solution

  • I ended up using a try and catch block, but an if and else also would have worked. Where you dont want any values just put null as the value.

        for(int i=0;i<ints1.size();i++)
        {
            dataset.addValue(ints1.get(i), "Good Product", "Hour" + String.valueOf(i+1));
            dataset.addValue(ints2.get(i), "Bad Product", "Hour" + String.valueOf(i+1));
            try{
                dataset2.addValue((ints1.get(i) + ints1.get(i-1) + ints1.get(i-2))/3, "Time", "Hour" + String.valueOf(i+1));
                }
                catch(ArrayIndexOutOfBoundsException e)
                {
                    dataset2.addValue(null, "Time", "Hour" + String.valueOf(i+1));
                }
        }