The examples presented so far populate data passing an array of values
to a series method and an array of labels to a constructor/method of a chart
context like the Graph class. Date labels can be automatically calculated,
yet a starting date still have to be informed.
JetChart implements methods to specify a JDBC driver and execute queries against a database,
retrieving chart labels and series values. The method GenericGraph.setDriver(String jdbcDriver)
sets the JDBC driver to be used to connect to a database. The connection is provided with
the method GenericGraph.setConnection(String jdbcURL,String user,String password).
The methods GenericGraph.setLabelsQuery(String labelsQuery) and
AbstractSerie.setValuesQuery(String valuesQuery) receive SQL statements to retrieve
chart labels and series values from a database.
The method AbstractSerie.setValuesQuery(String valuesQuery) is overriden by subclasses of
AbstractSerie that have multiple values associated with a data point, like OHLC series and
scatter series.
The example below establish a connection to a MySql database(http://www.mysql.com), getting the values of two bar
series and the chart labels after clicking a button located inside the top panel.
For detailed information regarding Java database connectivity API, please refer to the documentation found
at the Java official website(http://www.java.sun.com).
import java.awt.*; import java.awt.event.*; import javax.swing.*; import com.jinsight.jetchart.*; import java.util.Locale; import java.sql.*; public class Main extends JFrame implements ActionListener { Graph graph; BarSerie bs1,bs2; public Main() { graph=new Graph(); // Sets the MySql JDBC driver. graph.setDriver("org.gjt.mm.mysql.Driver"); // We are supposing that the MySql database name is 'chartdb' and the user // is authenticated using the name 'user' and password 'password'. // A table named 'chart' will be queried to get labels and values, and the // table structure is the following: // ==================================== // Field name | Field type // ==================================== // labels | varchar(20) // values1 | float // values2 | float // ==================================== graph.setConnection("jdbc:mysql://localhost:3306/chartdb","user","password"); graph.setTitle(new String[]{"The JetChart Library","Reading data from a database"}); graph.setTitleForeground(Color.white); graph.set3DEnabled(true); graph.set3DSeriesInLineEnabled(false); graph.setGradientColors(Color.blue,Color.white); bs1=new BarSerie(); bs1.setTitle("Bar series 1"); bs1.setColor(new Color(00,99,00)); bs2=new BarSerie(); bs2.setTitle("Bar series2"); bs2.setColor(Color.yellow); Grid grid=graph.getGraphSet(0).getGrid(); grid.setEnabled(true); grid.setThickness(2); graph.addSerie(bs1); graph.addSerie(bs2); JPanel topPanel=new JPanel(new FlowLayout(FlowLayout.LEFT)); JButton button=new JButton("Query database"); button.addActionListener(this); topPanel.add(button); Container ct=getContentPane(); ct.add(graph); ct.add("North",topPanel); setSize(500,400); setVisible(true); } public void actionPerformed(ActionEvent evt) { graph.setLabelsQuery("SELECT labels FROM chart"); bs1.setValuesQuery("SELECT values1 FROM chart"); bs2.setValuesQuery("SELECT values2 FROM chart"); graph.refresh(); } public static void main(String[] args) { new Main(); } }