RGonza
RGonza

Reputation: 107

JFreeChart How create an areachart with an TimeLine axis

I want to make the example of jfreechart: XYAreaChartDemo2.java, an xyareachart but the X axis must be of timeSeries. i have tried this:

TimeSeriesCollection dataset1 = new TimeSeriesCollection(timeSeries);//my timeseries 
    XYSeriesCollection dataset2 = new XYSeriesCollection();  

    JFreeChart chart = ChartFactory.createXYAreaChart("titulo","Eje x","eje Y", dataset2, PlotOrientation.VERTICAL,true, false,false);
    chart.setBackgroundPaint(Color.white);
    XYPlot xyplot= chart.getXYPlot();
    //fondo
    xyplot.setBackgroundPaint(Color.white);

    //pone la serie de time en el formato area (pero pierde el formato tiempo)
    xyplot.setDataset(dataset1);

but my chart is returned like a areachart with x-axis from 0 to 1.285......

Upvotes: 1

Views: 3337

Answers (2)

Ovilia
Ovilia

Reputation: 7256

I have a recommended solution here.

Don't limit your mind on making XYAreaChart with TimeSeries X axis. Why not make a TimeSeries chart and render it into XYAreaChart?

Here is how it can be done.

// Create TimeSeriesChart
JFreeChart localJFreeChart = createChart(createDataset()); 
// Set to be XYAreaChart
XYItemRenderer render = new XYAreaRenderer(); 
XYPlot plot = localJFreeChart.getXYPlot();
plot.setRenderer(render);

in which

private static JFreeChart createChart(XYDataset paramXYDataset) {
    JFreeChart localJFreeChart = ChartFactory.createTimeSeriesChart("Legal & General Unit Trust Prices", "Date", "Price Per Unit", paramXYDataset, true, true, false);
    // ...
    return (JFreeChart) localJFreeChart;
}

Full code can be seen here.

Most of the code is from TimeSeriesDemo1 in JFreeChart-1.0.14-demo.jar, and modified by myself to meet your requirements.

Hope this could help.

Upvotes: 4

trashgod
trashgod

Reputation: 205785

I'm assuming you're using TimeSeries and adding instances of RegularTimePeriod to the data set. By default, the method ChartFactory. createXYAreaChart() uses a NumberAxis for the domain. Instead, use a DateAxis.

XYPlot plot = chart.getXYPlot();
DateAxis domain = new DateAxis("Tiempo");
plot.setDomainAxis(domain);

Upvotes: 1

Related Questions