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;
}

}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值