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

落爺英雄遲暮 提交于 2019-12-02 03:28:32

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