I can't draw LineChart!

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
jannisery
Posts: 3
Joined: Thu Nov 09, 2006 7:55 pm

I can't draw LineChart!

Post by jannisery » Thu Nov 09, 2006 8:00 pm

Hi everyone,

I have a database and i want to draw a line chart which has it's data from the database... I've added the datas into the dataset but i have no idea how to draw it...

I bought developer's guide but i can't do it either... Please help me!

This is my Main class where i have a initDataSet() function to add the database values into the dataset but i stucked!

Any help?

Code: Select all

import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.data.category.DefaultCategoryDataset;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class Main extends JFrame {

   private File source;
   private JTextField dbName;
   private JLabel dbNameLabel;
   private JTextField userName;
   private JLabel userNameLabel;
   private JPasswordField password;
   private JLabel passwordLabel;
   private JButton openFileButton;
   private JButton connectDatabaseAndLoadButton;
   private JPanel panel1;
   MysqlDbConnection conn;
   DefaultCategoryDataset dataset;
   JFreeChart chart;
   ChartPanel chartPanel;


   public Main() {
      super("main");
      setUpGUI();
      openFileButton.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent e) {
            openFile();
         }
      });

      connectDatabaseAndLoadButton.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent e) {
            if (dbName.getText().equals("") || userName.getText().equals("") || password.getPassword().equals("")) {
               JOptionPane.showMessageDialog(null, "Please fill the necessary fields properly!!!");
               return;
            }
            connectDatabaseAndLoadButton.setEnabled(false);
            parseAndInsert();
         }
      });
   }//end of constructor

   private void openFile() {
      JFileChooser fileChooser = new JFileChooser(new File("."));
      fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
      //show file chooser
      int result = fileChooser.showOpenDialog(this);
      if (result == JFileChooser.CANCEL_OPTION) {
         return;
      }
      source = fileChooser.getSelectedFile();
      if (source.getName().equalsIgnoreCase("dataset.txt")) {
         connectDatabaseAndLoadButton.setEnabled(true);
         openFileButton.setEnabled(false);
      } else {
         JOptionPane.showMessageDialog(null, "Wrong File!!!");
      }
   }//end of method openFile

   private void parseAndInsert() {
      Parser parser = new Parser(source);
      conn = new MysqlDbConnection(dbName.getText(), userName.getText(), new String(password.getPassword()));
      int frames;
      float seconds;
      float xCoord;
      float yCoord;
      boolean isCamera1;
      Statement sqlStatement = conn.getStatement();
      String tables[] = new String[7];

      String tokens[] = parser.parseNextLine();

      for (int i = 2, j = 0; i < 23; i++) {
         if ((i - 2) % 3 == 0) {

            //JOptionPane.showMessageDialog(null, createTable(tokens[i]));
            tables[j++] = tokens[i];
            try {
               sqlStatement.executeUpdate(createTable(tokens[i]));
               //tables[ (int)((i-2)/7) ] = tokens[ i ];
            } catch (SQLException e) {
               e.printStackTrace();
            }
         }
      }
      parser.parseNextLine();

      while ((tokens = parser.parseNextLine()) != null) {
         frames = Integer.parseInt(tokens[0]);
         seconds = Float.parseFloat(tokens[1]);
         for (int i = 2; i < 23; i += 3) {
            xCoord = Float.parseFloat(tokens[i]);
            yCoord = Float.parseFloat(tokens[i + 1]);
            if (Integer.parseInt(tokens[i + 2]) == 1)
               isCamera1 = true;
            else
               isCamera1 = false;

            //JOptionPane.showMessageDialog( null, insertTable( frames, seconds, xCoord, yCoord,isCamera1,tables[(int)((i-2)/3)] ) );

            try {
               sqlStatement.executeUpdate(insertTable(frames, seconds, xCoord, yCoord, isCamera1, tables[(int) ((i - 2) / 3)]));
            } catch (SQLException e) {
               e.printStackTrace();
            }
         }
         //break;
      }
      //
      JOptionPane.showMessageDialog(null, "File parsed and inserted to database");
   }//end of method parseAndInsert

   private String createTable(String tableName) {
      String sqlSentence = "CREATE TABLE " + tableName + " ";
      sqlSentence += "( FRAMES INTEGER,";
      sqlSentence += " SECONDS FLOAT,";
      sqlSentence += " X_COORD FLOAT,";
      sqlSentence += " Y_COORD FLOAT,";
      sqlSentence += " IS_CAMERA_1 BOOLEAN )";
      return sqlSentence;
   }//end of method createTable

   private String insertTable(int fra, float sec, float x, float y, boolean cam, String table) {
      String sqlSentence = "INSERT INTO " + table + " VALUES(";
      sqlSentence += " " + fra + ", " + sec + ", " + x + ", " + y + ", " + cam + " )";
      return sqlSentence;
   }//end of method insertTable


   public void initDataSet() {
      // row keys...
      String series1 = "GREEN";

      dataset = new DefaultCategoryDataset();
      Statement sql = conn.getStatement();
      ResultSet rs;
      try {
         rs = sql.executeQuery("SELECT * from green");
         int i = 0;
         while (rs.next()) {
            dataset.addValue(rs.getDouble("X_COORD"), series1, new String("" + (i++)));
         }
      } catch (SQLException e) {
         e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
      }


   }


   public void setUpGUI() {
      panel1 = new JPanel();
      panel1.setLayout(new GridLayout(6, 2));
      panel1.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEmptyBorder(), null));
      dbName = new JTextField();
      dbNameLabel = new JLabel();
      dbNameLabel.setText("Database Name to Connect");
      panel1.add(dbNameLabel);
      panel1.add(dbName);
      userName = new JTextField();
      userNameLabel = new JLabel();
      userNameLabel.setText("Database User Name");
      panel1.add(userNameLabel);
      panel1.add(userName);
      password = new JPasswordField();
      passwordLabel = new JLabel();
      passwordLabel.setText("Current User Password");
      panel1.add(passwordLabel);
      panel1.add(password);
      connectDatabaseAndLoadButton = new JButton();
      connectDatabaseAndLoadButton.setEnabled(false);
      connectDatabaseAndLoadButton.setText("Connect Database and Load");
      connectDatabaseAndLoadButton.setMnemonic('C');
      connectDatabaseAndLoadButton.setDisplayedMnemonicIndex(0);
      openFileButton = new JButton();
      openFileButton.setEnabled(true);
      openFileButton.setText("Open File \"dataset.txt\"");
      openFileButton.setMnemonic('O');
      openFileButton.setDisplayedMnemonicIndex(0);
      panel1.add(openFileButton);
      panel1.add(connectDatabaseAndLoadButton);
      dbNameLabel.setLabelFor(dbName);
      userNameLabel.setLabelFor(userName);
      passwordLabel.setLabelFor(password);

   }


   public static void main(String[] args) {
      Main frame = new Main();
      frame.setContentPane(frame.panel1);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.pack();
      frame.setVisible(true);

   }

}//End Of Class Main


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

Post by david.gilbert » Fri Nov 10, 2006 10:48 am

If you have a populated dataset, then you can use one of the methods in the ChartFactory class to create a JFreeChart instance. To display that chart in your GUI, create a ChartPanel, and place the ChartPanel in your user interface in the same way you'd place any Swing component.
David Gilbert
JFreeChart Project Leader

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

Locked