JfreeChart畫雙Y軸折線圖
/*jadclipse*/// Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov.
package com.tgysys.report;
import java.awt.Color;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.List;
import javax.servlet.http.HttpSession;
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.entity.StandardEntityCollection;
import org.jfree.chart.labels.StandardXYToolTipGenerator;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.StandardXYItemRenderer;
import org.jfree.chart.renderer.xy.XYItemRenderer;
import org.jfree.chart.servlet.ServletUtilities;
import org.jfree.data.time.Second;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.data.xy.IntervalXYDataset;
import org.jfree.data.xy.XYDataset;
import com.tgysys.model.javabean.DapfIstanceView;
public class Chart1 {
public static String generateLineChart(HttpSession session, PrintWriter pw,
int w, int h, List list, List dwplList, List hbaceList) {
String filename = null;
JFreeChart jfreechart = createChart(dwplList, hbaceList);
try {
ChartRenderingInfo info = new ChartRenderingInfo(
new StandardEntityCollection());
filename = ServletUtilities.saveChartAsPNG(jfreechart, 1000, 500,
info, session);
ChartUtilities.writeImageMap(pw, filename, info, true);
} catch (IOException e) {
e.printStackTrace();
}
return filename;
}
private static JFreeChart createChart(List dwplList, List hbaceList) {
XYDataset xydataset = createPriceDataset(hbaceList);
String s = "CHART1 TEST";
JFreeChart jfreechart = ChartFactory.createTimeSeriesChart(s, "Date",
"", xydataset, true, true, false);
XYPlot xyplot = (XYPlot) jfreechart.getPlot();
// NumberAxis numberaxis = (NumberAxis) xyplot.getRangeAxis();
// numberaxis.setLowerMargin(0.40000000000000002D);
// DecimalFormat decimalformat = new DecimalFormat("00.00");
// numberaxis.setNumberFormatOverride(decimalformat);
XYItemRenderer xyitemrenderer = xyplot.getRenderer();
xyitemrenderer.setBaseToolTipGenerator(new StandardXYToolTipGenerator(
"{0}: ({1}, {2})", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"),
new DecimalFormat("0,000.00000")));
NumberAxis numberaxis1 = new NumberAxis("");
numberaxis1.setUpperMargin(0.002);
numberaxis1.setLowerMargin(0.15);
numberaxis1.setLowerBound(49.90);
numberaxis1.setUpperBound(50.08);
DecimalFormat decimalformat2 = new DecimalFormat("00.00");
numberaxis1.setNumberFormatOverride(decimalformat2);
xyplot.setRangeAxis(1, numberaxis1);
xyplot.setDataset(1, createVolumeDataset(dwplList));
xyplot.setRangeAxis(1, numberaxis1);
xyplot.mapDatasetToRangeAxis(1, 1);
StandardXYItemRenderer standardxyitemrenderer1 = new StandardXYItemRenderer();
standardxyitemrenderer1.setSeriesPaint(0, Color.blue);
numberaxis1.setLabelPaint(Color.blue);
numberaxis1.setTickLabelPaint(Color.blue);
xyplot.setRenderer(1, standardxyitemrenderer1);
return jfreechart;
}
private static XYDataset createPriceDataset(List hbaceList) {
try {
final TimeSeriesCollection dataset = new TimeSeriesCollection();
dataset.setDomainIsPointsInTime(true);
TimeSeries timeseries = new TimeSeries("ACE",
org.jfree.data.time.Second.class);
for (int i = 0; i < hbaceList.size(); i++) {
DapfIstanceView df = (DapfIstanceView) hbaceList.get(i);
String[] strs = df.getDatetime().split("[- :]");
int a = Integer.parseInt(strs[0]);
int b = Integer.parseInt(strs[1]);
int c = Integer.parseInt(strs[2]);
int d = Integer.parseInt(strs[3]);
int e = Integer.parseInt(strs[4]);
int f = Integer.parseInt(strs[5].substring(0, 2));
timeseries.add(new Second(f, e, d, c, b, a), df.getAcekh());
}
TimeSeries timeseries2 = new TimeSeries("波動上限",
org.jfree.data.time.Second.class);
for (int i = 0; i < hbaceList.size(); i++) {
DapfIstanceView df = (DapfIstanceView) hbaceList.get(i);
String[] strs = df.getDatetime().split("[- :]");
int a = Integer.parseInt(strs[0]);
int b = Integer.parseInt(strs[1]);
int c = Integer.parseInt(strs[2]);
int d = Integer.parseInt(strs[3]);
int e = Integer.parseInt(strs[4]);
int f = Integer.parseInt(strs[5].substring(0, 2));
timeseries2.add(new Second(f, e, d, c, b, a), 200);
}
TimeSeries timeseries3 = new TimeSeries("波動下限",
org.jfree.data.time.Second.class);
for (int i = 0; i < hbaceList.size(); i++) {
DapfIstanceView df = (DapfIstanceView) hbaceList.get(i);
String[] strs = df.getDatetime().split("[- :]");
int a = Integer.parseInt(strs[0]);
int b = Integer.parseInt(strs[1]);
int c = Integer.parseInt(strs[2]);
int d = Integer.parseInt(strs[3]);
int e = Integer.parseInt(strs[4]);
int f = Integer.parseInt(strs[5].substring(0, 2));
timeseries3.add(new Second(f, e, d, c, b, a), -200);
}
dataset.addSeries(timeseries);
dataset.addSeries(timeseries2);
dataset.addSeries(timeseries3);
return dataset;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
private static IntervalXYDataset createVolumeDataset(List dwplList) {
try {
TimeSeries timeseries = new TimeSeries("頻率",
org.jfree.data.time.Second.class);
for (int i = 0; i < dwplList.size(); i++) {
DapfIstanceView df = (DapfIstanceView) dwplList.get(i);
String[] strs = df.getDatetime().split("[- :]");
int a = Integer.parseInt(strs[0]);
int b = Integer.parseInt(strs[1]);
int c = Integer.parseInt(strs[2]);
int d = Integer.parseInt(strs[3]);
int e = Integer.parseInt(strs[4]);
int f = Integer.parseInt(strs[5].substring(0, 2));
timeseries.add(new Second(f, e, d, c, b, a), df.getFvalue());
}
return new TimeSeriesCollection(timeseries);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}