BusyBird-Input-Feed

 view release on metacpan or  search on metacpan

t/samples/stackoverflow.atom  view on Meta::CPAN

            <category scheme="http://stackoverflow.com/tags" term="swing" />
            <category scheme="http://stackoverflow.com/tags" term="jfreechart" />
            <category scheme="http://stackoverflow.com/tags" term="scatter-plot" />
        <author>
            <name>enjal</name>
            <uri>http://stackoverflow.com/users/2657277</uri>
        </author>
        <link rel="alternate" href="http://stackoverflow.com/questions/24591898/scatter-plot-in-jfreechart-from-database" />
        <published>2014-07-06T01:20:55Z</published>
        <updated>2014-07-06T05:30:13Z</updated>
        <summary type="html">
            

            &lt;p&gt;How can i draw scatter plot of datas in mysql database table using jfreechart in java. I have used swing library.
Any link would be helpful. I searched google but couldnot find a understanding solution.
If you have got code just provide me.
Actually i did do barchart and plot it using jfreechart.
The code i used for my barchart is here. Here display3 function displays the barchart. 
How can i modify it  to display scatter plot?&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;public void display3() throws SQLException, ClassNotFoundException{

    DefaultCategoryDataset dataset = new DefaultCategoryDataset();

    String JDBC_DRIVER=&quot;com.mysql.jdbc.Driver&quot;;
    String DB_URL=&quot;jdbc:mysql://localhost/data2&quot;;
    Connection conn;
    Statement stmt;
    String USER = &quot;root&quot;;
    String PASS = &quot;&quot;;
    try{
        Class.forName(JDBC_DRIVER);
        conn=DriverManager.getConnection(DB_URL,USER,PASS);
        System.out.println(&quot;Creating statement...&quot;);
        stmt = conn.createStatement();

        String sql;
        sql=&quot;SELECT * FROM `production` WHERE crop_id = 1 AND location_id = 1&quot;;
        ResultSet rs=stmt.executeQuery(sql);

        while (rs.next()){
            //String student = rs.getString(&quot;studentname&quot;);
            String yeartext = rs.getString(&quot;year_of_production&quot;);
            //double value = Double.parseDouble(text);
            String productiontext = rs.getString(&quot;production_amount&quot;);
            double production = Double.parseDouble(productiontext);
            Integer year = Integer.parseInt(yeartext);

            dataset.setValue(production, &quot;production&quot;, year);


        }



        JFreeChart chart = ChartFactory.createBarChart(&quot;Bar Graph&quot;,// Chart Title
                &quot;Year&quot;, //horizontal axis label
                &quot;Paddy Production&quot;, // vertical axis label
                dataset, //data
                PlotOrientation.VERTICAL, //orientation of chart
                true, //include legend
                false, // tool tips
                true);//urls
        CategoryPlot p = chart.getCategoryPlot();
        ChartPanel chartPanel = new ChartPanel(chart, false);
        jPanel9.setLayout(new BorderLayout());
        jPanel9.add(chartPanel, BorderLayout.EAST);
        jPanel9.add(chartPanel);
        SwingUtilities.updateComponentTreeUI(this);
        p.setRangeGridlinePaint(blue);

        System.out.println(&quot;Database created successfully...&quot;);

    } catch(SQLException se) {
        //Handle errors for JDBC
        System.out.println(&quot;Connect failed ! &quot;);
        se.printStackTrace();
    }

}       

  I finally solved my problem: 
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The refine code is below and it works: &lt;/p&gt;

&lt;pre&gt;&lt;code&gt;public void display3() throws SQLException, ClassNotFoundException{

        //DefaultCategoryDataset dataset = new DefaultCategoryDataset();
         XYSeriesCollection dataset = new XYSeriesCollection();
         XYSeries series = new XYSeries(&quot;production&quot;);
        String JDBC_DRIVER=&quot;com.mysql.jdbc.Driver&quot;;
    String DB_URL=&quot;jdbc:mysql://localhost/data2&quot;;
    Connection conn;
    Statement stmt;
    String USER = &quot;root&quot;;
    String PASS = &quot;&quot;;
        try{
            Class.forName(JDBC_DRIVER);
            conn=DriverManager.getConnection(DB_URL,USER,PASS);
            System.out.println(&quot;Creating statement...&quot;);
            stmt = conn.createStatement();
                        String sql;
                        sql=&quot;SELECT * FROM `production` WHERE crop_id = 1 AND location_id = 1&quot;;
                        ResultSet rs=stmt.executeQuery(sql);

                        while (rs.next()){
                            //String student = rs.getString(&quot;studentname&quot;);
                            String yeartext = rs.getString(&quot;year_of_production&quot;);
                            //double value = Double.parseDouble(text);
                            String productiontext = rs.getString(&quot;production_amount&quot;);
                            double production = Double.parseDouble(productiontext);
                            double year = Double.parseDouble(yeartext);
                            series.add(year,production) ;
                            //dataset.addSeries(series);




                        }
                        dataset.addSeries(series);



( run in 1.525 second using v1.01-cache-2.11-cpan-e1769b4cff6 )