Jfreechart with scroller
Asked Answered
R

1

0

I wanted to add to the chart a scroll bar in order to view big datasets which did not fit on the screen, because to keep all xtick. However, I got the following error message:

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)

With the following code:

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

What did I do wrong?

Thank you in advance.

Repeater answered 14/9, 2012 at 3:30 Comment(0)
P
1

The error message is pretty obvious:

org.jfree.data.xy.XYSeriesCollection cannot be cast to org.jfree.data.category.CategoryDataset

You are casting a XYSeriesCollection object to a CategoryDataset object, which does not work. Looking at the lines in the stack trace, it is caused by the following line

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

The createDataset() methods returns a XYSeriesCollection and not a CategoryDataset, so you cannot do this cast. Either adjust the return type of that method, or get rid of the cast

Profession answered 14/9, 2012 at 6:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.