Scatter plot with date axis

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
jcd
Posts: 4
Joined: Thu Jan 21, 2010 12:04 pm
antibot: No, of course not.

Scatter plot with date axis

Post by jcd » Thu Jan 21, 2010 12:18 pm

I need to draw a scatter plot with date on x-axis and values on the y-axis. I'm having this weird problem....i'm unable to convert the numberaxis to dateaxis in a scatter plot....but the same works fine with a timeseries plot....
here is the code:

Code: Select all

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.StandardXYItemRenderer;
import org.jfree.chart.renderer.xy.XYItemRenderer;
import org.jfree.data.time.Day;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;

public class Demo {	
	public static void main(String[] args) {
		TimeSeries pop = new TimeSeries("BG Data", Day.class);
		pop.add(new Day(10, 1, 2004), 450);
		pop.add(new Day(11, 1, 2004), 250);
		pop.add(new Day(12, 1, 2004), 750);
		pop.add(new Day(13, 1, 2004), 225);
		pop.add(new Day(14, 1, 2004), 525);
		pop.add(new Day(15, 1, 2004), 25);
		TimeSeriesCollection dataset = new TimeSeriesCollection();
		dataset.addSeries(pop);
		JFreeChart chart = ChartFactory.createScatterPlot(
			"BG Data",
			"Date",
			"BG Values",
			dataset,
			PlotOrientation.VERTICAL,			
			true,
			true,
			false);
//		final JFreeChart chart = ChartFactory.createTimeSeriesChart(
//	            "Legal & General Unit Trust Prices",
//	            "Date", "Price Per Unit",
//	            dataset,
//	            true,
//	            true,
//	            false
//	    );
		final XYPlot plot = chart.getXYPlot();				
		final DateAxis domainAxis = (DateAxis) plot.getDomainAxis();
		domainAxis.setDateFormatOverride(new SimpleDateFormat("MM/dd"));
		try {
			ChartUtilities.saveChartAsJPEG(new File("C:chart.jpg"), chart, 500, 300);
		}
		catch (IOException e) {
			System.err.println("Problem occurred creating chart.");
		}
	}
}

this code gives the exception:
Exception in thread "main" java.lang.ClassCastException: org.jfree.chart.axis.NumberAxis incompatible with org.jfree.chart.axis.DateAxis
at Demo.main(Demo.java:47)

but the same runs fine with timeseries...what seems to be the problem?

jcd
Posts: 4
Joined: Thu Jan 21, 2010 12:04 pm
antibot: No, of course not.

Re: Scatter plot with date axis

Post by jcd » Fri Jan 22, 2010 6:18 am

any help is much appreciated!!

paradoxoff
Posts: 1634
Joined: Sat Feb 17, 2007 1:51 pm

Re: Scatter plot with date axis

Post by paradoxoff » Sat Jan 23, 2010 1:02 pm

The error message says it all.
Both ChartFactory.createTimeSeriesChart and ChartFactory.createScatterPlot return a JFreeChart with an XYPlot. The return value of XYPlot.getDomainAxis() is declared as a ValueAxis, which is the abstract superclass of e. g. DateAxis and NumberAxis. In case of ChartFactory.createTimeSeriesChart, the XYPlot has a DateAxis as domain axis, and the cast to DateAxis is successful. In case of ChartFactory.createScatterPlot, the XYPlot has a NumberAxis as domain axis, and the cast to DateAxis fails.

jcd
Posts: 4
Joined: Thu Jan 21, 2010 12:04 pm
antibot: No, of course not.

Re: Scatter plot with date axis

Post by jcd » Tue Jan 26, 2010 6:17 am

but how do you convert the number axis in a scatter plot to a date axis?

jcd
Posts: 4
Joined: Thu Jan 21, 2010 12:04 pm
antibot: No, of course not.

Re: Scatter plot with date axis

Post by jcd » Fri Jan 29, 2010 6:05 am

i've searched everywhere...i couldn't find anything on this....pls help!!

paradoxoff
Posts: 1634
Joined: Sat Feb 17, 2007 1:51 pm

Re: Scatter plot with date axis

Post by paradoxoff » Mon Feb 01, 2010 9:12 pm

jcd wrote:i've searched everywhere...
Not in the API documentation of the XYPlot class, otherwise you would have probably found that the method

Code: Select all

plot.getDomainAxis()
has a corresponding setter

Code: Select all

plot.setDomainAxis(ValueAxis axis) 
which will accept any subclass of ValueAxis, including a DateAxis.

RTJava
Posts: 4
Joined: Tue Mar 02, 2010 2:19 pm
antibot: No, of course not.

Re: Scatter plot with date axis

Post by RTJava » Sat Mar 06, 2010 3:47 pm

As well as "tooltips" must be changed as Date format. To do this, use as following XYToolTipGenerator :

XYToolTipGenerator toolTipGenerator = new StandardXYToolTipGenerator(DEFAULT_TOOL_TIP_FORMAT, new SimpleDateFormat("dd/MM/yyyy"), NumberFormat.getNumberInstance() );
renderer.setBaseToolTipGenerator(toolTipGenerator);

Locked