使用JFreeChart中的类将double转换为日期格式 [英] To convert the double into date format using a class in JFreeChart

查看:164
本文介绍了使用JFreeChart中的类将double转换为日期格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道,如果可以将两个参数设置为日期,在此示例中,它将第一个参数视为可比较,将第二个参数视为double。但我希望双重显示为日期。是否有可以使用的类。如果没有是否有其他方式显示为日期。例如,我需要x轴和y轴作为日期。

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.

对于 data.addValue(8/4/2012,7.0)

我想要这样(8/4/2012 20:06:02,8/5/2012) - >是否可以使用下图。

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

先谢谢你。

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


推荐答案

NumberAxis ,使用 DateAxis 。这将允许您在 DateFormat /DateAxis.html#setDateFormatOverride%28java.text.DateFormat%29\"rel =nofollow> setDateFormatOverride() method。

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

更新: org.jfree.chart.demo.TimeSeriesChartDemo1 中有一个完整的示例。您可能需要 createLineChart()。这是锄头,你可以让范围轴显示日期。

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

这篇关于使用JFreeChart中的类将double转换为日期格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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