JFreeChart:在图表中设置最小X-tick间隔

时间:2014-03-07 10:55:15

标签: java jfreechart

我需要在图表中设置最小X-tick间隔。我有一个包含1000个条目的数据集,即<0,12>,<5,22>,<10,23>,...,<1000,20>。在数据集中,X值之间的间隔等于5.在图表中,我想将X-tick间隔设置为100。

我使用此代码,但它不起作用:

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

这是我的代码段:

    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;    
    }

1 个答案:

答案 0 :(得分:6)

您已设置范围轴的刻度单位,即JFreeChart中的y轴。对于x轴,您需要域轴。

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

范围术语来自将一组域值映射到一组范围值的函数。事后看来,使用getXAxis()和getYAxis()作为方法名称对于大多数人来说会更清楚......对于JFreeChart 2我可能会改变它。