1
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/2012to 。18/4/2012如何删除没有价值的日期。为什么会这样?

任何人都可以帮助我吗?

4

2 回答 2

3

尝试这个:

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

在此处输入图像描述

而不是使用aTimeSeriesChart使用 CategoryDataSet 作为@Dirk sugested 然后切换到LineAndShapeRenderer.

于 2012-09-06T15:29:27.287 回答
2

连续显示,这TimeSeries意味着日期沿轴均匀分布,即使该日期没有实际值。如果您只想显示某些日期,您应该使用某种CategoryDataSet

于 2012-09-06T11:56:29.793 回答