带滚动条的Jfreechart



我想在图表中添加一个滚动条,以便查看不适合屏幕的大数据集,因为要保留所有的xtick。然而,我收到了以下错误消息:

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);
    }
}

我做错了什么?

提前谢谢。

错误消息非常明显:

org.free.data.xy.XYSeriesCollection不能强制转换为org.free.ddata.category.CategoryDataset

您正在将XYSeriesCollection对象强制转换为CategoryDataset对象,但这不起作用。查看堆栈跟踪中的行,这是由以下行引起的

dataset = new SlidingCategoryDataset((CategoryDataset) createDataset(), 0, 10);

createDataset()方法返回的是XYSeriesCollection,而不是CategoryDataset,因此不能执行此强制转换。要么调整该方法的返回类型,要么去掉强制转换

相关内容

  • 没有找到相关文章

最新更新