1
我正在開發使用Jfree庫中的時間序列圖時間。我在數據庫中存儲時間。我想在圖表上顯示這些時間。我怎樣才能做到這一點。 我有具有類似於價格,成交量的日期和時間列一個表的商品。我正在繪製數據庫中的價格和交易量圖表。我想要在X軸上繪製數據庫的時間。現在它給了我隨機時間。我想要我的數據庫時間。 我有其中我正在存儲事件的時間的表。所以我想在時間序列圖上的X軸上顯示那個時間。如何顯示存儲在數據庫中Timseries圖
這裏是我的代碼
/*
* 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 > 0) {
float[] dPrice = new float[count1];
float[] dVolume = new float[count1];
java.util.Date[] oDateTime = new java.util.Date[count1];
for (i = 0; i < 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 < 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 < dPrice.length; j++) {
if (dPrice[j] > maxprice) {
maxprice = dPrice[j];
// System.out.println("maxprice is "+ maxprice);
}
}
for (int j = 0; j < dVolume.length; j++) {
if (dVolume[j] > 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 < 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);
}
}
我希望我的圖表應反映數據庫時機,因爲它反映了價格和交易量。請幫幫我。謝謝。我嘗試了很多。但沒有得到如何去做。請有人知道它幫助我。我有我的最後期限。
這是_方式_過多的調試,以及不完整的。考慮編輯您的問題包括[SSCCE(http://sscce.org/),專注於特定的問題。這[示例](http://stackoverflow.com/a/6210074/230513)可能會提供一些指導。 – trashgod 2012-04-03 21:32:26
主席先生,我具有其中我正在存儲事件的時間的表。所以我想在時間序列圖上的X軸上顯示那個時間。 – kanchan 2012-04-04 03:29:56
有沒有人可以指導我解決這個問題 – kanchan 2012-04-06 04:11:12