public class xyArea{
public static void main(String arg[]){
  XYSeries series = new XYSeries("Average Weight");
  series.add(20.0, 20.0);
  series.add(40.0, 25.0);
  series.add(55.0, 50.0);
  series.add(70.0, 65.0);
  XYDataset xyDataset = new XYSeriesCollection(series);
  JFreeChart chart = ChartFactory.createAreaChart
  ("XY Chart using JFreeChart", "Age", "Weight",
   (CategoryDataset) xyDataset, PlotOrientation.VERTICAL, true,
  true, false);
  ChartFrame frame1=new ChartFrame("XYArea Chart",chart);
  frame1.setVisible(true);
  frame1.setSize(300,300);
  }
}


我写了这段代码,但出现以下错误,请指导我:

线程“主”中的异常java.lang.ClassCastException:org.jfree.data.xy.XYSeriesCollection无法转换为org.jfree.data.CategoryDataset
    在xyArea.main(xyArea.java:21)

最佳答案

好吧,您不能转换为CategoryDataset,因为XYSeriesCollection不是CategoryDataset

看来您需要.createXYAreaChart(..)(而不是.createAreaChart(..)

07-24 12:28