title: How to create a BarChart or a LineChart in Javafx Using ObservableLists
body: <p>I'm trying to generate statistical graphs (BarCharts and LineCharts) from the data saved in an sql database using Java. 
  The Data is expected to change from time to time.</p>
  
  <p>I have tried using ObservableLists but am getting error messages.</p>
  
  <p>Below is what I have tried so far;
  NOTE: This resides in the DatabaseHandler class</p>
  
  <pre><code>public ObservableList&lt;BarChart.Data&gt; getItemGraphStatistics(){
          ObservableList&lt;BarChart.Data&gt; data = FXCollections.observableArrayList();
  
          String sql = "SELECT itemID, COUNT(*) FROM (SELECT itemID FROM SALES UNION ALL SELECT itemID FROM STOCK) t GROUP BY itemID";
          System.out.println(sql );
          ResultSet rs = handler.execQuery(sql);
          try{
  
              while(rs.next()){
                  String item= rs.getString("itemID");
                  int count = rs.getInt(2);
  
                  data.add(new BarChart.Data(item+ "(" + count + ")", count));
  
              }
          } catch (SQLException ex) {
              Logger.getLogger(DatabaseHandler.class.getName()).log(Level.SEVERE, null, ex);
          }
          return data;
  
      }
  
  </code></pre>
  
  <p>On running the above am getting </p>
  
  <pre><code>java.lang.ClassCastException: javafx.scene.chart.XYChart$Data cannot be cast to javafx.scene.chart.XYChart$Series
  </code></pre>
  
  <p>When i try to change BarChart.Data to BarChart.Series, I get unable to convert int to observableList error message.</p>
  
  <p>Below is an example of the trial of the above;</p>
  
  <pre><code>import com.jfoenix.controls.JFXButton;
  import com.jfoenix.controls.JFXRadioButton;
  import com.jfoenix.controls.JFXTextField;
  import java.net.URL;
  import java.util.Calendar;
  import java.util.Dictionary;
  import java.util.Hashtable;
  import java.util.ResourceBundle;
  import java.util.concurrent.TimeUnit;
  import javafx.event.ActionEvent;
  import javafx.event.Event;
  import javafx.fxml.FXML;
  import javafx.fxml.Initializable;
  import javafx.scene.chart.BarChart;
  import javafx.scene.chart.CategoryAxis;
  import javafx.scene.chart.LineChart;
  import javafx.scene.chart.NumberAxis;
  import javafx.scene.chart.PieChart;
  import javafx.scene.control.ChoiceBox;
  import javafx.scene.control.DatePicker;
  import javafx.scene.control.ToggleGroup;
  import javafx.scene.input.MouseEvent;
  import javafx.scene.layout.AnchorPane;
  import javafx.scene.layout.HBox;
  import sales.manager.database.DatabaseHandler;
  
  /**
   * FXML Controller class
   *
   * @author Garande
   */
  public class PerformanceAnalyzerController implements Initializable {
  
      @FXML
      private AnchorPane analysisHandler;
      @FXML
      private AnchorPane graphHandler;
      @FXML
      private PieChart piechart;
      @FXML
      private BarChart&lt;?, ?&gt; barchart;
      @FXML
      private LineChart&lt;?, ?&gt; linechart;
  
      @FXML
      private NumberAxis yBarGraph;
      @FXML
      private CategoryAxis xBarGraph;
      @FXML
      private NumberAxis yLineGraph;
      @FXML
      private CategoryAxis xLineGraph;
  
      DatabaseHandler handler = DatabaseHandler.getInstance();
  
      /**
       * Initializes the controller class.
       */
      @Override
      public void initialize(URL url, ResourceBundle rb) {
  
  
      }    
  
      @FXML
      private void performAnalysisOperation(ActionEvent event) {
          //Performing analysis operation
  
          barchart.setData(handler.getItemGraphStatistics());
          graphHandler.getChildren().add(barchart);
      }
  }
  </code></pre>