In jfreechart TimeSeriesCollection how to show dates which has only values?

老子叫甜甜 提交于 2019-12-11 18:07:36

问题


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

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?

Can any one help me please?


回答1:


Try this:

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

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




回答2:


The TimeSeries is displayed continuously, which means that the dates are equally distributed along the axis, even if there is no actual value for this date. If you want only certain dates displayed, you should use some kind of CategoryDataSet.



来源:https://stackoverflow.com/questions/12297851/in-jfreechart-timeseriescollection-how-to-show-dates-which-has-only-values

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!