title: How to display time that is stored in database on Timseries Chart
body: <p>I am developing a Timeseries chart using Jfree library. I am storing time in database. I want to display that timings on chart. How can i do that.
  I am having one table commodity having columns like price,volume date and time. I am ploting chart of price and volume from database. I want time which is there in database that is also to be plotted on X-axis. Right now it gives me random timings. I want my database time.   <strong>i am having a table in which i am storing a time of event. So i want to display that time on X-axis  in time series chart.</strong></p>
  
  <p>Here is my code</p>
  
  <pre><code>/*
   * commoditychart.java
   */
  package com.das.dbmodule;
  
  import com.das.dbmodule.Dbconnection;
  import org.jfree.chart.ChartFactory;
  import org.jfree.chart.ChartRenderingInfo;
  import org.jfree.chart.ChartUtilities;
  import org.jfree.chart.JFreeChart;
  import org.jfree.chart.axis.NumberAxis;
  import org.jfree.chart.axis.ValueAxis;
  import org.jfree.chart.entity.StandardEntityCollection;
  import org.jfree.chart.plot.XYPlot;
  import org.jfree.chart.renderer.xy.XYAreaRenderer;
  import org.jfree.data.time.Hour;
  import org.jfree.data.time.RegularTimePeriod;
  import org.jfree.data.time.TimeSeries;
  import org.jfree.data.time.TimeSeriesCollection;
  import org.jfree.data.xy.XYDataset;
  import java.awt.*;
  import java.io.File;
  import java.io.IOException;
  import java.sql.*;
  import java.text.SimpleDateFormat;
  import java.util.Vector;
  import javax.swing.*;
  import org.jfree.chart.axis.DateAxis;
  import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
  
  public class commoditychart {
  
      private static final String CHARTDIR = "";
      String theResult = "";
  
      public commoditychart() {
      }
  
      public String produceAllCharts(String cmname, String today, String imagepath)
          throws ClassNotFoundException, SQLException {
          int count = 4,
              i = 0,
              id = 2,
              x = 0;
          String str = cmname;
          String tsym = null,
              commodity = cmname,
              path = "";
          Vector allSyms = null;
          ResultSet results = null,
              results1 = null;
          JFrame frame = null;
          File ifle = null;
          String chartFilename = null;
          Dbconnection dbobject = new Dbconnection();
          System.out.println("In commodity");
          dbobject.Dbconnect();
          System.out.println("call con");
  
          String startingtime = "00:00:00";
          String endingtime = "23:59:59";
  
          tsym = str;
  
          try {
              System.out.println(" inside commodity ");
              results1 = dbobject.execSQL(""
                  + "Select open_contract_vol, open_contract_price , Date, Time "
                  + "from commodity where com_name='" + cmname
                  + "' and Date='" + today
                  + "' and Time between '" + startingtime
                  + "'  and '" + endingtime + "'");
  
              // results1 = DbObject.execSQL(query);// here the query get executed and the Resultset is populated.
              int count1 = 0;
              while (results1.next()) {
                  count1++;// count1 is having total number of records that were feteched from the query.
              }
  
              results1.first();
              System.out.println("count1  --- =  " + count1);
              if (count1 &gt; 0) {
                  float[] dPrice = new float[count1];
                  float[] dVolume = new float[count1];
                  java.util.Date[] oDateTime = new java.util.Date[count1];
  
                  for (i = 0; i &lt; count1; i++) {
                      dVolume[i] = results1.getFloat("open_contract_vol");
                      dPrice[i] = results1.getFloat("open_contract_price");
                      oDateTime[i] = results1.getDate("Date");
                      System.out.println("price ::" + dPrice[i] + " vol :: "
                          + dVolume[i] + " date :: " + oDateTime[i]);
                      results1.next();
                  }
  
                  XYDataset xDataSet = createPriceDataset(dPrice);
                  XYDataset yDataSet = createVolumeDataset(dVolume);
                  JFreeChart chart = createChart(xDataSet, yDataSet, dPrice, dVolume, today);
                  ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection());
  
                  // ifle = new File("d:\\javapractice/marketpoint/web/"+tsym+".png");
                  File ifle2 = new File(imagepath + tsym + ".png");
  
                  // path = ifle.getAbsolutePath();
                  path = ifle2.getAbsolutePath();
                  System.out.println("new path " + path);
  
                  try {
                      ChartUtilities.saveChartAsPNG(ifle2, chart, 600, 500, info);
                  } catch (IOException ex) {
                      ex.printStackTrace();
                  }
  
                  chartFilename = this.CHARTDIR + tsym + ".png";
                  count++;
              }
          } catch (Exception oError) {
              System.out.println("Here is the error :" + oError.getMessage());
          }
          return path;
      }
  
      private XYDataset createPriceDataset(final float[] dPrice) {
          final TimeSeriesCollection dataset = new TimeSeriesCollection();
          final TimeSeries s1 = new TimeSeries("Price (US $)", Hour.class);
  
          // RegularTimePeriod start = new Minute();
          RegularTimePeriod start = new Hour();
  
          for (int i = 0; i &lt; dPrice.length; i++) {
              s1.add(start, dPrice[i]);
  
              // System.out.println("time start is "+ start);
              start = start.next();
              start = start.next();
              start = start.next();
              start = start.next();
              start = start.next();
              start = start.next();
          }
          dataset.addSeries(s1);
          return dataset;
      }
  
      private JFreeChart createChart(XYDataset xDataSet, XYDataset yDataSet,
          final float[] dPrice, final float[] dVolume,
          String date) {
  
          // final XYDataset direction = createPriceDataset(600);
          // Date on which the chart is created
          final JFreeChart chart = ChartFactory.createTimeSeriesChart("Time", date,
              "Price(US $)", xDataSet, true, true, false);
          final XYPlot plot = chart.getXYPlot();
  
          plot.getDomainAxis().setLowerMargin(0.0);
          plot.getDomainAxis().setUpperMargin(0.0);
  
          float maxprice = 0;
          float maxvolume = 0;
  
          for (int j = 0; j &lt; dPrice.length; j++) {
              if (dPrice[j] &gt; maxprice) {
                  maxprice = dPrice[j];
                  // System.out.println("maxprice is "+ maxprice);
              }
          }
  
          for (int j = 0; j &lt; dVolume.length; j++) {
              if (dVolume[j] &gt; maxvolume) {
                  maxvolume = dVolume[j];
  
                  XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer();
                  renderer.setSeriesLinesVisible(0, true);
                  plot.setRenderer(renderer);
              }
          }
  
          final ValueAxis axis1 = new NumberAxis("Price(US $)");
          axis1.setRange(0.0, maxprice + 40);
  
          // add the wind force with a secondary dataset/renderer/axis
          plot.setRangeAxis(axis1);
  
          final XYAreaRenderer renderer2 = new XYAreaRenderer();
          final ValueAxis axis2 = new NumberAxis("Volume(Carton)");
  
          axis2.setRange(0.0, maxvolume + 40);
          renderer2.setSeriesPaint(0, new Color(0, 0, 255, 128));
          plot.setDataset(2, yDataSet);
          plot.setRenderer(2, renderer2);
          plot.setRangeAxis(2, axis2);
          plot.mapDatasetToRangeAxis(2, 2);
          chart.setBorderVisible(true);
          final DateAxis axis = (DateAxis) plot.getDomainAxis();
          axis.setDateFormatOverride(new SimpleDateFormat("hh:mma"));
          return chart;
      }
  
      private XYDataset createVolumeDataset(float[] dVolume) {
          final TimeSeriesCollection dataset = new TimeSeriesCollection();
          final TimeSeries s1 = new TimeSeries("Volume(Carton) ", Hour.class);
          RegularTimePeriod start = new Hour();
          System.out.println("Strta:::" + start);
          for (int i = 0; i &lt; dVolume.length; i++) {
              s1.add(start, dVolume[i]);
              start = start.next();
              start = start.next();
              start = start.next();
              start = start.next();
              start = start.next();
              start = start.next();
          }
          dataset.addSeries(s1);
          return dataset;
      }
  
      public static void main(String[] args) throws ClassNotFoundException, SQLException {
          commoditychart chartAgent = new commoditychart();
          chartAgent.produceAllCharts("ac", "2012-04-02", "E:/images/");
          //System.exit(0);
      }
  }
  </code></pre>
  
  <p>I want my chart should reflect database timings as it reflect price and volume. Please help me. Thanks. I tried a lot. But didn't get how to do it. Please if anybody knows it help me. I am having my deadlines.</p>