【发布时间】:2012-04-17 01:38:34
【问题描述】:
我正在使用 Jfree 库开发时间序列图表。我将时间存储在数据库中。我想在图表上显示该时间。我怎样才能做到这一点。 我有一个表格商品,其中包含价格、数量日期和时间等列。我正在从数据库中绘制价格和数量图表。我想要数据库中的时间也将绘制在 X 轴上。现在它给了我随机的时间。我想要我的数据库时间。 我有一张桌子,我在其中存储事件时间。所以我想在时间序列图中的 X 轴上显示那个时间。
这是我的代码
/*
* 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);
}
}
我希望我的图表应该反映数据库时间,因为它反映了价格和交易量。请帮我。谢谢。我尝试了很多。但不知道怎么做。请如果有人知道它帮助我。我有我的最后期限。
【问题讨论】:
-
先生,我有一张表,我在其中存储事件时间。所以我想在时间序列图中的 X 轴上显示那个时间。
-
有没有人可以指导我解决这个问题
-
您需要使用sscce 更新您的问题。引用的示例显示了如何构造
TimeSeries。 -
在此处查看示例:dirtyhandsphp.blogspot.in/2012/07/… 我认为它可以解决您的问题。唯一的区别是您从数据库中获取日期,在此示例中,这些日期是手动分配的。
标签: java charts jfreechart time-series