Search code examples
javamysqljfreechart

Barchart using database doesn't show


I want to create a chart using mysql database with Jfreechart, this is the code i used :

try {
    String query = 
        "Select id,Prop_menages_Urbains_Proprietaires from fes";

    JDBCCategoryDataset dataset = 
        new JDBCCategoryDataset(
            DBConnection.DBConnection(),
            query
        );

    JFreeChart chart = 
            ChartFactory.createBarChart(
                "test", 
                "id", 
                "Prop_menages_Urbains_Proprietaires",
                dataset, 
                PlotOrientation.VERTICAL, 
                false, 
                true, 
                true
            );

    BarRenderer render = null;

    CategoryPlot plot = null;

    render = new BarRenderer();

    ChartFrame fram = 
        new ChartFrame("test", chart);

    fram.setVisible(true);
    fram.setSize(600,650);
}
catch(Exception ex) {
    System.out.println("Erreur: " + ex);
}

after the execution the barchart doesn't show up although there are no errors in the code syntax any help please ?

UPDATE: i tried the same code but this time i used other columns from the database and it worked , what's the issues with the other one knowing that they're all numbers


Solution

  • The JDBCCategoryDataset API specifies:

    The SQL query must return at least two columns. The first column will be the category name and remaining columns values (each column represents a series).

    This complete example, which you have successfully adapted, illustrates the effect. The question then becomes,

    What's the issues with the other one knowing that they're all numbers?

    The executeQuery() source shows how each of the java.sql.Types is mapped to a numeric value: a numeric type becomes a Number directly, a date type becomes a Number representing milliseconds from the Java epoch, and a character type is converted using Double.valueOf(); the default is null.

    In this case, verify that Prop_menages_Urbains_Proprietaires has a SQL type that is suitable for conversion to a numeric value.