Object to cast problem

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
diegohap
Posts: 1
Joined: Tue Nov 29, 2016 6:02 pm
antibot: No, of course not.

Object to cast problem

Post by diegohap » Tue Nov 29, 2016 6:12 pm

Hi people. I have some trouble to declare some object. The point is when I try to change the axis scale using an "XYPlot" object, but when I tyr, an error trigger and then I can't draw.
When I use:
XYPlot plot = (XYPlot)chart.getXYPlot();
I have this error:
Exception in thread "main" java.lang.ClassCastException: org.jfree.chart.plot.CategoryPlot cannot be cast to org.jfree.chart.plot.XYPlot

It's look like "plot" or "chart.getXYPlot()" is a "CategoryPlot" object type and not "XYPlot" one.
I try casting type with "(XYPlot)" and without it, and using "getPlot()" instated and still happening the same problem.

This is de code:
import javax.swing.JFrame;
import javax.swing.JPanel;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.data.category.DefaultCategoryDataset;



public class Ventana extends JFrame{
JPanel panel;
public Ventana(){
setTitle("Como Hacer Graficos con Java");
setSize(800,600);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
init();
}

private void init() {
panel = new JPanel();
getContentPane().add(panel);
float var=0;
// Fuente de Datos
DefaultCategoryDataset line_chart_dataset = new DefaultCategoryDataset();
for(Integer i=1; i<=50; i++){
var += 2;
line_chart_dataset.addValue(var*var+4, "visitas", i);
}

// Creando el Grafico
JFreeChart chart=ChartFactory.createLineChart("Trafico en el Blog",
"Mes","Visitas",line_chart_dataset,PlotOrientation.VERTICAL,
true,true,false);

XYPlot plot = (XYPlot)chart.getXYPlot();


// Mostrar Grafico
ChartPanel chartPanel = new ChartPanel(chart);
panel.add(chartPanel);
}

public static void main(String args[]){
new Ventana().setVisible(true);
}
}

John Matthews
Posts: 513
Joined: Wed Sep 12, 2007 3:18 pm

Re: Object to cast problem

Post by John Matthews » Wed Nov 30, 2016 5:21 am

Your chosen ChartFactory creates a CategoryAxis for the domain, which you can access as shown below. Also consider a time series chart, shown here.

Image

Code: Select all

import java.awt.EventQueue;
import javax.swing.JFrame;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.CategoryLabelPositions;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset;

public class Ventana extends JFrame {

    public Ventana() {
        setTitle("Como Hacer Graficos con Java");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        init();
        pack();
        setLocationRelativeTo(null);
        setVisible(true);
    }

    private void init() {
        float var = 0;
        DefaultCategoryDataset dataset = new DefaultCategoryDataset();
        for (Integer i = 1; i <= 50; i++) {
            var += 2;
            dataset.addValue(var * var + 4, "visitas", i);
        }
        JFreeChart chart = ChartFactory.createLineChart("Trafico en el Blog",
            "Mes", "Visitas", dataset, PlotOrientation.VERTICAL,
            true, true, false);
        CategoryPlot plot = chart.getCategoryPlot();
        CategoryAxis domain = plot.getDomainAxis();
        domain.setCategoryLabelPositions(CategoryLabelPositions.UP_90);
        add(new ChartPanel(chart));
    }

    public static void main(String args[]) {
        EventQueue.invokeLater(() -> {
            new Ventana().setVisible(true);
        });
    }
}
Last edited by John Matthews on Thu Dec 01, 2016 2:28 am, edited 1 time in total.

david.gilbert
JFreeChart Project Leader
Posts: 11734
Joined: Fri Mar 14, 2003 10:29 am
antibot: No, of course not.
Contact:

Re: Object to cast problem

Post by david.gilbert » Wed Nov 30, 2016 9:05 pm

When you use the ChartFactory to create a chart, you should read the API docs or look in the source code to see what type of plot it is using. JFreeChart uses two separate plot types for charts with 2 axes...the CategoryPlot and the XYPlot. In the former, the x-axis displays categories rather than a numerical scale (which you get with an XYPlot). There are two separate dataset interfaces for these plots as well (CategoryDataset and XYDataset).
David Gilbert
JFreeChart Project Leader

:idea: Read my blog
:idea: Support JFree via the Github sponsorship program

Locked