JfreeChart折线图




截图


复制代码
package com.tq365.chart;

import java.awt.Color;
import java.awt.Dimension;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;

import javax.servlet.http.HttpSession;
import javax.swing.JPanel;
import org.jfree.chart.*;
import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.entity.StandardEntityCollection;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.chart.servlet.ServletUtilities;
import org.jfree.data.time.*;
import org.jfree.data.xy.XYDataset;
import org.jfree.ui.*;

public class TimeSeriesDemo extends ApplicationFrame {

private static final long serialVersionUID = -4172191391806537567L;

public TimeSeriesDemo(String s) {
super(s);
XYDataset xydataset
= createDataset();
JFreeChart jfreechart
= createChart(xydataset);
ChartPanel chartpanel
= new ChartPanel(jfreechart, false);
chartpanel.setPreferredSize(
new Dimension(500, 270));
chartpanel.setMouseZoomable(
true, false);
setContentPane(chartpanel);
}

private static JFreeChart createChart(XYDataset xydataset) {
JFreeChart jfreechart
= ChartFactory.createTimeSeriesChart(
"Legal & General Unit Trust Prices", "Date", "Price Per Unit",
xydataset,
true, true, true);
jfreechart.setBackgroundPaint(Color.white);
XYPlot xyplot
= (XYPlot) jfreechart.getPlot();
xyplot.setBackgroundPaint(Color.lightGray);
xyplot.setDomainGridlinePaint(Color.white);
xyplot.setRangeGridlinePaint(Color.white);
xyplot.setAxisOffset(
new RectangleInsets(5D, 5D, 5D, 5D));
xyplot.setDomainCrosshairVisible(
true);
xyplot.setRangeCrosshairVisible(
true);
org.jfree.chart.renderer.xy.XYItemRenderer xyitemrenderer
= xyplot
.getRenderer();
if (xyitemrenderer instanceof XYLineAndShapeRenderer) {
XYLineAndShapeRenderer xylineandshaperenderer
= (XYLineAndShapeRenderer) xyitemrenderer;
xylineandshaperenderer.setBaseShapesVisible(
true);
xylineandshaperenderer.setBaseShapesFilled(
true);
}
DateAxis dateaxis
= (DateAxis) xyplot.getDomainAxis();
dateaxis.setDateFormatOverride(
new SimpleDateFormat("MMM-yyyy"));
return jfreechart;
}

private static XYDataset createDataset() {
TimeSeries timeseries
= new TimeSeries("L&G European Index Trust",
org.jfree.data.time.Month.
class);
timeseries.add(
new Month(2, 2001), 181.80000000000001D);
timeseries.add(
new Month(3, 2001), 167.30000000000001D);
timeseries.add(
new Month(4, 2001), 153.80000000000001D);
timeseries.add(
new Month(5, 2001), 167.59999999999999D);
timeseries.add(
new Month(6, 2001), 158.80000000000001D);
timeseries.add(
new Month(7, 2001), 148.30000000000001D);
timeseries.add(
new Month(8, 2001), 153.90000000000001D);
timeseries.add(
new Month(9, 2001), 142.69999999999999D);
timeseries.add(
new Month(10, 2001), 123.2D);
timeseries.add(
new Month(11, 2001), 131.80000000000001D);
timeseries.add(
new Month(12, 2001), 139.59999999999999D);
timeseries.add(
new Month(1, 2002), 142.90000000000001D);
timeseries.add(
new Month(2, 2002), 138.69999999999999D);
timeseries.add(
new Month(3, 2002), 137.30000000000001D);
timeseries.add(
new Month(4, 2002), 143.90000000000001D);
timeseries.add(
new Month(5, 2002), 139.80000000000001D);
timeseries.add(
new Month(6, 2002), 137D);
timeseries.add(
new Month(7, 2002), 132.80000000000001D);
TimeSeries timeseries1
= new TimeSeries("L&G UK Index Trust",
org.jfree.data.time.Month.
class);
timeseries1.add(
new Month(2, 2001), 129.59999999999999D);
timeseries1.add(
new Month(3, 2001), 123.2D);
timeseries1.add(
new Month(4, 2001), 117.2D);
timeseries1.add(
new Month(5, 2001), 124.09999999999999D);
timeseries1.add(
new Month(6, 2001), 122.59999999999999D);
timeseries1.add(
new Month(7, 2001), 119.2D);
timeseries1.add(
new Month(8, 2001), 116.5D);
timeseries1.add(
new Month(9, 2001), 112.7D);
timeseries1.add(
new Month(10, 2001), 101.5D);
timeseries1.add(
new Month(11, 2001), 106.09999999999999D);
timeseries1.add(
new Month(12, 2001), 110.3D);
timeseries1.add(
new Month(1, 2002), 111.7D);
timeseries1.add(
new Month(2, 2002), 111D);
timeseries1.add(
new Month(3, 2002), 109.59999999999999D);
timeseries1.add(
new Month(4, 2002), 113.2D);
timeseries1.add(
new Month(5, 2002), 111.59999999999999D);
timeseries1.add(
new Month(6, 2002), 108.8D);
timeseries1.add(
new Month(7, 2002), 101.59999999999999D);
TimeSeriesCollection timeseriescollection
= new TimeSeriesCollection();
timeseriescollection.addSeries(timeseries);
timeseriescollection.addSeries(timeseries1);
return timeseriescollection;
}

public static JPanel createDemoPanel() {
JFreeChart jfreechart
= createChart(createDataset());
return new ChartPanel(jfreechart);
}

// 根据JFreeChart对象生成对应的图片
public static String generateLineChart(HttpSession session, PrintWriter pw) {
String filename
= null;
JFreeChart chart
= createChart(createDataset());
// chart.setBackgroundPaint(java.awt.Color.white);

ChartRenderingInfo info
= new ChartRenderingInfo(
new StandardEntityCollection());
try {
filename
= ServletUtilities.saveChartAsPNG(chart, 500, 350, info,
session);
}
catch (IOException e) {
e.printStackTrace();
}
try {
ChartUtilities.writeImageMap(pw, filename, info,
false);
}
catch (IOException e) {
e.printStackTrace();
}
pw.flush();
return filename;
}

public static void main(String args[]) {
TimeSeriesDemo timeseriesdemo1
= new TimeSeriesDemo(
"Time Series Demo 1");
timeseriesdemo1.pack();
RefineryUtilities.centerFrameOnScreen(timeseriesdemo1);
timeseriesdemo1.setVisible(
true);
}
}
复制代码

posted on   archie2010  阅读(4853)  评论(1编辑  收藏  举报

编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
< 2011年3月 >
27 28 1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31 1 2
3 4 5 6 7 8 9

统计

点击右上角即可分享
微信分享提示