在jfreechart中,TimeSeriesCollection如何显示只有值的日期? [英] In jfreechart TimeSeriesCollection how to show dates which has only values?

查看:20
本文介绍了在jfreechart中,TimeSeriesCollection如何显示只有值的日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public class Example {
    public static void main(String args[]){
        /*creating timeSeriesCollection*/
        TimeSeriesCollection dataset = new TimeSeriesCollection();
        TimeSeries timeSeries1 = new TimeSeries("Sample 1");
        timeSeries1.add(new Day(8,4, 2012), 7.0);
        timeSeries1.add(new Day(19,4, 2012), 5.0);
        dataset.addSeries(timeSeries1);
        /*Creating the chart*/
        JFreeChart chart = ChartFactory.createTimeSeriesChart("Population","Date","Population",dataset,true,true,false);
        /*Altering the graph */
        XYPlot plot = (XYPlot) chart.getPlot(); 
    plot.setAxisOffset(new RectangleInsets(5.0, 10.0, 10.0, 5.0));  
    plot.setDomainCrosshairVisible(true);
    plot.setRangeCrosshairVisible(true);
    XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer)plot.getRenderer();
    renderer.setBaseShapesVisible(true);
    renderer.setBaseShapesFilled(true);
    NumberAxis numberAxis = (NumberAxis)plot.getRangeAxis();        
    numberAxis.setRange(new Range(0,10));   
    DateAxis axis = (DateAxis) plot.getDomainAxis();
    axis.setDateFormatOverride(new SimpleDateFormat("dd-MM-yyyy"));
    axis.setAutoTickUnitSelection(false);
    axis.setVerticalTickLabels(true);
        /* Displaying the chart*/           
        ChartFrame frame = new ChartFrame("Test", chart);
        frame.pack();
        frame.setVisible(true);
    }   
}

我已经编写了上面的代码,该代码工作得很好,但是在渲染图形时,它显示没有值的日期.它在y轴上显示日期,例如 9/4/12 10/4/2012 18/4/2012 一个值.如何删除没有价值的日期.为什么会这样呢?

I have written the above code which works perfectly fine but when rendering the graph it displays date which does not have values. It displays dates like 9/4/12,10/4/2012 to 18/4/2012 in y-axis which does not have a value. How can I remove the date which does not have value. And why it is behaving so?

有人可以帮助我吗?

推荐答案

尝试一下:

  public class Example1 {
    public static void main(String args[]){

        DefaultKeyedValues data = new DefaultKeyedValues();
        data.addValue("8/4/2012" ,7.0);
        data.addValue("19/04/2012",5.0);

        CategoryDataset dataset = DatasetUtilities.createCategoryDataset("Population", data);
        JFreeChart chart = ChartFactory.createBarChart("Population","Date","Population",dataset,PlotOrientation.VERTICAL,true,true,false);
        ChartFrame frame = new ChartFrame("Test", chart);

        //Switch from a Bar Rendered to a LineAndShapeRenderer so the chart looks like an XYChart
        LineAndShapeRenderer renderer = new LineAndShapeRenderer();
        renderer.setBaseLinesVisible(false); //TUrn of the lines
        CategoryPlot plot = (CategoryPlot) chart.getPlot();
        plot.setRenderer(0, renderer);

        NumberAxis numberAxis = (NumberAxis)plot.getRangeAxis();        
        numberAxis.setRange(new Range(0,10));   

        frame.pack();
        frame.setVisible(true);
    }   
}

而不是使用 TimeSeriesChart ,而是使用CategoryDataSet作为@Dirk的摘要,然后切换到 LineAndShapeRenderer .

Rather than using a TimeSeriesChart use a CategoryDataSet as @Dirk sugested and then switch to a LineAndShapeRenderer.

这篇关于在jfreechart中,TimeSeriesCollection如何显示只有值的日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆