显示不同的jfree图表

时间:2014-08-16 08:45:44

标签: java combobox jfreechart

我正在尝试使用jfree图表显示散点图和XYline图表。我能够一次显示它们。我有一个组合框,可让您选择要绘制的绘图类型。在此基础上,我必须使用jfree图绘制相应的图。

我有以下代码来绘制散点图并且它有效。

 JFreeChart chart = ChartFactory.createScatterPlot("Scatter Plot","Year","Production", dataset);

现在我刚刚添加了if语句,并且我有以下代码,我现在用它来根据用户选择通过组合框进行绘制。这是行不通的。它只是说没有使用变量图表,即它不会在if语句

之外识别变量图表
if(jComboBox6.getSelectedItem().equals("Scatter Plot"))
                        {
                            JFreeChart chart = ChartFactory.createScatterPlot("Scatter Plot","Year","Production", dataset);

                        }
                         if(jComboBox6.getSelectedItem().equals("Line Chart"))
                        {
                            JFreeChart chart = ChartFactory.createScatterPlot("Line Chart","Year","Production", dataset);
                        }

我显示情节的功能如下所示。

public void displayProduction() throws SQLException, ClassNotFoundException{

       XYSeriesCollection dataset = new XYSeriesCollection();
         XYSeries series = new XYSeries("Production");
        String JDBC_DRIVER="com.mysql.jdbc.Driver";
    String DB_URL="jdbc:mysql://localhost/data2";
    Connection conn;
    Statement stmt;
        PreparedStatement ps = null;
    String USER = "root";
    String PASS = "";
        Object cropname = CropCombo.getSelectedItem();
       String cropnames = cropname.toString();
       Object locationname = LocationCombo1.getSelectedItem();
       String locationnames = locationname.toString();   
       Object yearfrom = yearfromCombo1.getSelectedItem();
       String yearfromcombo = yearfrom.toString();
       Object yearto = yeartoCombo1.getSelectedItem();
       String yeartocombo = yearto.toString();
       String pd="paddy ";
            System.out.println(cropnames.length()+" "+pd.length());

            System.out.println(cropsList);
         String sql;
         String sql1;
          sql1="Select * from production AS cust INNER JOIN location AS comp ON cust.location_id=comp.location_id INNER JOIN crops AS crop ON cust.crop_id=crop.crop_id WHERE comp.name=? AND crop.name=? AND year_of_production BETWEEN "+yearfrom + " AND " + yearto;
        try{
            Class.forName(JDBC_DRIVER);
            conn=DriverManager.getConnection(DB_URL,USER,PASS);
            System.out.println("Creating statement...");
            //stmt = conn.createStatement(); 
                        ps = conn.prepareStatement(sql1);
                        ps.setString(1, locationnames);
                         ps.setString(2, cropnames);

                       System.out.println(sql1);    
                       ResultSet rs = ps.executeQuery();
                        // ResultSet rs=stmt.executeQuery(sql);                      
                        while (rs.next()){

                            String yeartext = rs.getString("year_of_production");

                            String productiontext = rs.getString("production_amount");

                            double production = Double.parseDouble(productiontext);
                            double years = Double.parseDouble(yeartext);
                            series.add(years,production) ;

                            //dataset.addSeries(series);              
             }
                        dataset.addSeries(series);

                        graph1.removeAll();
                        if(jComboBox6.getSelectedItem().equals("Scatter Plot"))
                        {
                            JFreeChart chart = ChartFactory.createScatterPlot("Scatter Plot","Year","Production", dataset);

                        }
                         if(jComboBox6.getSelectedItem().equals("Line Chart"))
                        {
                            JFreeChart chart = ChartFactory.createScatterPlot("Line Chart","Year","Production", dataset);
                        }

                        ChartPanel chartPanel = new ChartPanel(chart, false);
                        graph1.setLayout(new BorderLayout());
                        graph1.add(chartPanel, BorderLayout.EAST);
                        graph1.add(chartPanel);
                        SwingUtilities.updateComponentTreeUI(this);
//                        p.setRangeGridlinePaint(blue);
                        graph1.updateUI();
                        System.out.println("Database created successfully...");

                }
           catch(SQLException se)
                {
                    //Handle errors for JDBC
                    System.out.println("Connect failed ! ");
                    se.printStackTrace();
//                    JOptionPane.showMessageDialog(MajorUI.this, err.getMessage());
                    }

    }

1 个答案:

答案 0 :(得分:1)

局部变量(包括形式参数)仅在声明它们的方法,构造函数或块中可见。

  

所有局部变量实际上都是声明它们的块的私有变量。该块之外的程序的任何部分都不能看到它们。

您有以下代码:

if(jComboBox6.getSelectedItem().equals("Scatter Plot"))
   {
   JFreeChart chart = ChartFactory.createScatterPlot("Scatter Plot","Year","Production", dataset);
   }
if(jComboBox6.getSelectedItem().equals("Line Chart"))
   {
   JFreeChart chart = ChartFactory.createScatterPlot("Line Chart","Year","Production", dataset);
   }
ChartPanel chartPanel = new ChartPanel(chart, false);

您在chart块的范围内声明if,然后在if块之后超出范围时尝试使用它。

请尝试以下代码:

JFreeChart chart;
if(jComboBox6.getSelectedItem().equals("Scatter Plot"))
   {
   chart = ChartFactory.createScatterPlot("Scatter Plot","Year","Production", dataset);
   }
if(jComboBox6.getSelectedItem().equals("Line Chart"))
   {
   chart = ChartFactory.createScatterPlot("Line Chart","Year","Production", dataset);
   }
ChartPanel chartPanel = new ChartPanel(chart, false);

有关详细信息,请参阅Local/Instance/Class Variables