setSubtitles giving Classcast Exception

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
Murali Mohan

setSubtitles giving Classcast Exception

Post by Murali Mohan » Wed Jul 16, 2003 6:17 am

Hi all following code is working fine if I comment setsubtitles line. If I try to use it , it is giving Classcast Exception. Pls help me in this issue.

XYSeries series1 = new XYSeries("1 mm");
series1.add(20, 50);
series1.add(120, 100);
XYSeriesCollection xySeriesCollection = new XYSeriesCollection(series1);

JFreeChart chart = ChartFactory.createLineXYChart("", "Gate Pressure (Mpa)", "Flow Length (mm)",xySeriesCollection, true , true , true );

chart.setTitle("Test ");
java.util.Vector vTitles = new java.util.Vector();
vTitles.addElement("Melt Temperature : 250c");
vTitles.addElement("Mold Temperature : 60c");

chart.setSubtitles((java.util.List)vTitles);
ChartUtilities.saveChartAsJPEG(new File("chart.jpg"), chart, 300, 300);

//The following is the exception

java.lang.ClassCastException
at org.jfree.chart.JFreeChart.draw(Unknown Source)
at org.jfree.chart.JFreeChart.createBufferedImage(Unknown Source)
at org.jfree.chart.ChartUtilities.writeChartAsJPEG(Unknown Source)
at org.jfree.chart.ChartUtilities.saveChartAsJPEG(Unknown Source)
at org.jfree.chart.ChartUtilities.saveChartAsJPEG(Unknown Source)
at com.graph.services.GenerateGraph.main(GenerateGraph.java:74)

Thanks,
Murali

heijho01
Posts: 12
Joined: Thu Jul 17, 2003 2:16 pm

Post by heijho01 » Thu Jul 17, 2003 2:24 pm

I first had the same problem but now solved it.
The solution is very simple

The only thing is: Don't use ordinary Strings (in the List you use in setSubtitles).

package org.jfree.chart.* has 3 classes you can use for this:

ImageTitle
LegendTitle
TextTitle

these are all derived from org.jfree.chart.AbstractTitle

I hope i helped you with this,

Jeroen

Locked