Avi Z Nash
Avi Z Nash

Reputation: 13

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

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

Upvotes: 1

Views: 2301

Answers (1)

Catalina Island
Catalina Island

Reputation: 7126

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

Upvotes: 1

Related Questions