Question

I need to set minimum X-tick interval in the chart. I have a dataset that contains 1000 entries, i.e. <0,12>,<5,22>,<10,23>,...,<1000,20>. In dataset, the interval between X-values is equal to 5. In the chart I want to set X-tick interval equal to 100.

I use this code, but it does not work:

    NumberAxis range = (NumberAxis)plot.getRangeAxis();  
    range.setTickUnit(new NumberTickUnit(100)); 

Here is my code snippet:

    private JFreeChart createChart(CategoryDataset dataset) 
    {

        final JFreeChart chart = ChartFactory.createAreaChart(
            this.title,
            "Time", 
            "Net demand", 
            dataset,
            PlotOrientation.VERTICAL,
            true,
            true,
            false
        );

        Title t = new TextTitle(this.subtitle);
        chart.addSubtitle(t);

        int transparency = 95;
        Color c = new Color(1, 158, 115, transparency);
        final CategoryPlot plot = chart.getCategoryPlot();
        CategoryItemRenderer renderer = new CustomRenderer(c);
        renderer.setSeriesOutlinePaint(0, Color.black);
        renderer.setSeriesOutlineStroke(0, new BasicStroke(0.5f));
        renderer.setSeriesPaint(0,c);
        plot.setRenderer(renderer);

        NumberAxis range = (NumberAxis)plot.getRangeAxis();  
        range.setTickUnit(new NumberTickUnit(60));  

        return chart;    
    }
Was it helpful?

Solution

You've set the tick unit for the range axis, which is the y-axis in JFreeChart. For the x-axis, you want the domain axis.

NumberAxis xAxis = (NumberAxis) plot.getDomainAxis();  
xAxis.setTickUnit(new NumberTickUnit(60));

The domain and range terms come from a function mapping a set of domain values to a set of range values. With hindsight, using getXAxis() and getYAxis() for the method names would have been clearer for most people...for JFreeChart 2 I'll probably change it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top