To convert the double into date format using a class in JFreeChart

喜夏-厌秋 提交于 2019-12-20 05:15:26

问题


I would like to know, if it is possible to set the both parameter as date, which in this example it take the first parameter as comparable and the second as double. But i want the double to be displayed as date. Is there a class that can be used. If not Is there other way to display both as date. For instance i need both the x-axis and y-axis as date.

For the data.addValue("8/4/2012" ,7.0)

I want it like this ("8/4/2012 20:06:02" ,"8/5/2012") --> Is it possible with the graph below.

Thank you in advanced.

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

回答1:


Instead of a NumberAxis, use a DateAxis. That will let you use a DateFormat in the setDateFormatOverride() method.

Update: There's a complete example in org.jfree.chart.demo.TimeSeriesChartDemo1. You might want createLineChart(). Here's hoe you'd make the range axis show dates.

public class Example1 {

    public static void main(String args[]) {
        DefaultKeyedValues data = new DefaultKeyedValues();
        data.addValue("8/4/2012", new Day(8, 4, 2012).getFirstMillisecond());
        data.addValue("19/04/2012", new Day(19, 4, 2012).getFirstMillisecond());
        CategoryDataset dataset = DatasetUtilities
            .createCategoryDataset("Population", data);

        JFreeChart chart = ChartFactory.createLineChart("Population", "Date",
            "Population", dataset, PlotOrientation.VERTICAL, true, true, false);
        CategoryPlot plot = (CategoryPlot) chart.getPlot();
        LineAndShapeRenderer renderer = (LineAndShapeRenderer) plot.getRenderer();
        renderer.setBaseShapesVisible(true);
        renderer.setBaseLinesVisible(false);

        DateAxis range = new DateAxis("Date");
        range.setDateFormatOverride(new SimpleDateFormat("dd/MM/yyyy"));
        plot.setRangeAxis(range);

        ChartFrame frame = new ChartFrame("Test", chart);
        frame.pack();
        frame.setVisible(true);
    }
}


来源:https://stackoverflow.com/questions/18492137/to-convert-the-double-into-date-format-using-a-class-in-jfreechart

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