0
我想添加到圖表滾動條,以查看大屏幕上的數據集,因爲保持所有xtick。但是,我得到了以下錯誤消息:Jfreechart與滾動
Exception in thread "main" java.lang.ClassCastException: org.jfree.data.xy.XYSeriesCollection cannot be cast to org.jfree.data.category.CategoryDataset
at MySlidingCategoryDatasetDemo3$DemoPanel.<init>(MySlidingCategoryDatasetDemo3.java:86)
at MySlidingCategoryDatasetDemo3.createDemoPanel(MySlidingCategoryDatasetDemo3.java:112)
at MySlidingCategoryDatasetDemo3.<init>(MySlidingCategoryDatasetDemo3.java:107)
at MySlidingCategoryDatasetDemo3.main(MySlidingCategoryDatasetDemo3.java:117)
用下面的代碼:
import java.awt.*;
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import org.jfree.data.category.*;
import org.jfree.chart.*;
import org.jfree.chart.axis.NumberAxis;
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.XYLineAndShapeRenderer;
import org.jfree.data.category.*;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;
public class MySlidingCategoryDatasetDemo3 extends ApplicationFrame
{
static class DemoPanel extends JPanel
implements ChangeListener
{
private static XYDataset createDataset()
{
XYSeries xyseries = new XYSeries("First");
xyseries.add(1.0D, 1.0D);
xyseries.add(2D, 4D);
xyseries.add(3D, 3D);
xyseries.add(4D, 5D);
xyseries.add(5D, 5D);
xyseries.add(6D, 7D);
xyseries.add(7D, 7D);
xyseries.add(8D, 8D);
xyseries.add(9D, 8D);
xyseries.add(10D, 8D);
xyseries.add(11D, 8D);
xyseries.add(12D, 8D);
xyseries.add(13D, 8D);
xyseries.add(14D, 8D);
xyseries.add(15D, 8D);
xyseries.add(16D, 25D);
xyseries.add(17D, 8D);
xyseries.add(18D, 5D);
xyseries.add(19D, 7D);
xyseries.add(20D, 6D);
xyseries.add(21D, 8D);
xyseries.add(22D, 4D);
xyseries.add(23D, 4D);
xyseries.add(24D, 2D);
xyseries.add(25D, 1.0D);
XYSeriesCollection xyseriescollection = new XYSeriesCollection();
xyseriescollection.addSeries(xyseries);
return xyseriescollection;
}
private static JFreeChart createChart(XYDataset xydataset)
{
JFreeChart jfreechart = ChartFactory.createXYLineChart("Line Chart Demo 2", "X", "Y", xydataset, PlotOrientation.VERTICAL, true, true, false);
XYPlot xyplot = (XYPlot)jfreechart.getPlot();
xyplot.setDomainPannable(true);
xyplot.setRangePannable(true);
StandardXYItemRenderer xylineandshaperenderer = (StandardXYItemRenderer)xyplot.getRenderer();
xylineandshaperenderer.setBaseShapesVisible(true);
xylineandshaperenderer.setBaseShapesFilled(true);
NumberAxis numberaxis = (NumberAxis)xyplot.getRangeAxis();
numberaxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
numberaxis.setRange(0.0D, 100D);
return jfreechart;
}
public void stateChanged(ChangeEvent changeevent)
{
dataset.setFirstCategoryIndex(scroller.getValue());
}
JScrollBar scroller;
SlidingCategoryDataset dataset;
public DemoPanel()
{
super(new BorderLayout());
dataset = new SlidingCategoryDataset((CategoryDataset) createDataset(), 0, 10);
JFreeChart jfreechart = createChart((XYDataset) dataset);
//JFreeChart jfreechart = createChart(createDataset());
ChartPanel chartpanel = new ChartPanel(jfreechart);
chartpanel.setPreferredSize(new Dimension(400, 400));
scroller = new JScrollBar(0, 0, 10, 0, 50);
add(chartpanel);
scroller.getModel().addChangeListener(this);
JPanel jpanel = new JPanel(new BorderLayout());
jpanel.add(scroller);
jpanel.setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2));
jpanel.setBackground(Color.white);
add(jpanel, "South");
}
}
public MySlidingCategoryDatasetDemo3(String s)
{
super(s);
setDefaultCloseOperation(3);
setContentPane(createDemoPanel());
}
public static JPanel createDemoPanel()
{
return new DemoPanel();
}
public static void main(String args[])
{
MySlidingCategoryDatasetDemo3 slidingcategorydatasetdemo2 = new MySlidingCategoryDatasetDemo3("JFreeChart: SlidingCategoryDatasetDemo2.java");
slidingcategorydatasetdemo2.pack();
RefineryUtilities.centerFrameOnScreen(slidingcategorydatasetdemo2);
slidingcategorydatasetdemo2.setVisible(true);
}
}
我做了什麼錯?
預先感謝您。