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.");
}
}
}
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?