写这篇文章是为了把自己研究的东西分享给大家,可能大家已经知道了,那我就献丑了。
在做金融相关项目的时候,往往会碰到要画时间序列图,大家都知道jfreechart是我们java玩家选择得比较多的。
但是有个问题,jfreechart的时间序列图好像不能给出带热点提示的图出来,因为jfreechart生成出来的是一张图片,
我参考了网上很多的文章都没有给出一个能真正解决问题的方案,所以自己动手搞出来一个。鼠标放在生成出来的图片上一样可以出现数据提示!代码如下!
带热点提示的TimeSeries图,时间序列图
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.awt.*"%>
<%@ page import="java.io.*"%>
<%@ page import="org.jfree.chart.*"%>
<%@ page import="org.jfree.data.time.*"%>
<%@ page import="org.jfree.chart.plot.*"%>
<%@ page import="org.jfree.ui.*"%>
<%@ page import="org.jfree.data.time.TimeSeriesCollection"%>
<%@ page import="org.jfree.chart.renderer.xy.*"%>
<%@ page import="org.jfree.chart.axis.*"%>
<%@ page import="org.jfree.chart.entity.*"%>
<%@ page import="org.jfree.chart.servlet.*"%>
<%@ page import="org.jfree.chart.urls.*"%>
<HTML>
<HEAD>
<TITLE>time series view</TITLE>
</HEAD>
<body>
<%
TimeSeries xyseries = new TimeSeries("finince income", Month.class);
xyseries.add(new Month(1, 2007), 1.0D);
xyseries.add(new Month(2, 2007), 4D);
xyseries.add(new Month(3, 2007), 3D);
xyseries.add(new Month(4, 2007), 5D);
xyseries.add(new Month(5, 2007), 5D);
xyseries.add(new Month(6, 2007), 7D);
xyseries.add(new Month(7, 2007), 7D);
xyseries.add(new Month(8, 2007), 8D);
TimeSeries xyseries1 = new TimeSeries("avg incross rate",
Month.class);
xyseries1.add(new Month(1, 2007), 3.0D);
xyseries1.add(new Month(2, 2007), 4.9D);
xyseries1.add(new Month(3, 2007), 3.5D);
xyseries1.add(new Month(4, 2007), 4.5D);
xyseries1.add(new Month(5, 2007), 5.5D);
xyseries1.add(new Month(6, 2007), 7D);
xyseries1.add(new Month(7, 2007), 7.9D);
xyseries1.add(new Month(8, 2007), 1.8D);
TimeSeries xyseries2 = new TimeSeries("incross rate", Month.class);
xyseries2.add(new Month(1, 2007), 4.0D);
xyseries2.add(new Month(2, 2007), 6.4D);
xyseries2.add(new Month(3, 2007), 3.8D);
xyseries2.add(new Month(4, 2007), 5.9D);
xyseries2.add(new Month(5, 2007), 8.2D);
xyseries2.add(new Month(6, 2007), 4.2D);
xyseries2.add(new Month(7, 2007), 9.7D);
xyseries2.add(new Month(8, 2007), 5.6D);
TimeSeriesCollection xyseriescollection = new TimeSeriesCollection();
xyseriescollection.addSeries(xyseries);
xyseriescollection.addSeries(xyseries1);
xyseriescollection.addSeries(xyseries2);
JFreeChart jfreechart = ChartFactory.createTimeSeriesChart(
"Example", "y", "x", xyseriescollection, true, true, true);
jfreechart.setBackgroundPaint(Color.white);
XYPlot xyplot = jfreechart.getXYPlot();
xyplot.setBackgroundPaint(Color.white);
xyplot.setRangeGridlinePaint(Color.black);
xyplot.setDomainGridlinesVisible(false);
xyplot.setRangeGridlinesVisible(true);
xyplot.setAxisOffset(new RectangleInsets(5D, 5D, 5D, 5D));
xyplot.setDomainCrosshairVisible(true);
xyplot.setDomainCrosshairLockedOnData(false);
xyplot.setRangeCrosshairVisible(false);
xyplot.setBackgroundImageAlpha(1.0f);
XYItemRenderer xyitemrenderer = xyplot.getRenderer();
XYLineAndShapeRenderer render = (XYLineAndShapeRenderer) xyplot
.getRenderer();
render.setURLGenerator(new StandardXYURLGenerator("view.jsp"));
render.setBaseShapesVisible(true);
render.setBaseShapesFilled(true);
xyitemrenderer.setSeriesPaint(0, Color.black);
xyitemrenderer.setSeriesPaint(1, Color.red);
xyitemrenderer.setSeriesStroke(1, new BasicStroke(1.0F, 1, 1, 1.0F,
new float[] { 5F, 3F }, 0.0F));
IntervalMarker intervalmarker0 = new IntervalMarker(0D, 2D);
intervalmarker0.setLabel("qing");
intervalmarker0.setLabelFont(new Font("SansSerif", 2, 11));
intervalmarker0.setLabelAnchor(RectangleAnchor.LEFT);
intervalmarker0.setLabelTextAnchor(TextAnchor.CENTER_LEFT);
intervalmarker0.setPaint(Color.cyan);
xyplot.addRangeMarker(intervalmarker0, Layer.BACKGROUND);
IntervalMarker intervalmarker = new IntervalMarker(2D, 4D);
intervalmarker.setLabel("huang");
intervalmarker.setLabelFont(new Font("SansSerif", 2, 11));
intervalmarker.setLabelAnchor(RectangleAnchor.LEFT);
intervalmarker.setLabelTextAnchor(TextAnchor.CENTER_LEFT);
intervalmarker.setPaint(Color.yellow);
xyplot.addRangeMarker(intervalmarker, Layer.BACKGROUND);
IntervalMarker intervalmarker1 = new IntervalMarker(4D, 6D);
intervalmarker1.setLabel("nan");
intervalmarker1.setLabelFont(new Font("SansSerif", 2, 11));
intervalmarker1.setLabelAnchor(RectangleAnchor.LEFT);
intervalmarker1.setLabelTextAnchor(TextAnchor.CENTER_LEFT);
intervalmarker1.setPaint(Color.BLUE);
xyplot.addRangeMarker(intervalmarker1, Layer.BACKGROUND);
IntervalMarker intervalmarker2 = new IntervalMarker(6D, 8D);
intervalmarker2.setLabel("nu");
intervalmarker2.setLabelFont(new Font("SansSerif", 2, 11));
intervalmarker2.setLabelAnchor(RectangleAnchor.LEFT);
intervalmarker2.setLabelTextAnchor(TextAnchor.CENTER_LEFT);
intervalmarker2.setPaint(Color.green);
xyplot.addRangeMarker(intervalmarker2, Layer.BACKGROUND);
IntervalMarker intervalmarker3 = new IntervalMarker(8D, 10D);
intervalmarker3.setLabel("hong");
intervalmarker3.setLabelFont(new Font("SansSerif", 2, 11));
intervalmarker3.setLabelAnchor(RectangleAnchor.LEFT);
intervalmarker3.setLabelTextAnchor(TextAnchor.CENTER_LEFT);
intervalmarker3.setPaint(Color.RED);
xyplot.addRangeMarker(intervalmarker3, Layer.BACKGROUND);
DateAxis domainaxis = (DateAxis) xyplot.getDomainAxis();
domainaxis.setTickUnit(new DateTickUnit(DateTickUnit.MONTH, 2));
NumberAxis numberaxis = (NumberAxis) xyplot.getRangeAxis();
numberaxis.setTickUnit(new NumberTickUnit(2D));
String filename = null;
PrintWriter pw = new PrintWriter(out);
StandardEntityCollection sec = new StandardEntityCollection();
ChartRenderingInfo info = new ChartRenderingInfo(sec);
filename = ServletUtilities.saveChartAsPNG(jfreechart, 800, 600,
info, session);
ChartUtilities.writeImageMap(pw, "map0", info, false);// true or false has big diffenrence
// if choose true,it will js function in html code
String url = request.getContextPath()
+ "/servlet/DisplayChart?filename=" + filename;
%>
<P ALIGN="CENTER">
<img src="<%=url%>" width=800 height=600 border=1 usemap="#map0">
</P>
</body>
</html>
欢迎交流 qq 569021135
已有 0 人发表留言,猛击->>这里<<-参与讨论
JavaEye推荐
分享到:
相关推荐
使用R对time series数据进行预测分析的经典入门教材
小波神经网络timeseries 小波神经网络timeseries
Hamilton-Time Series Analysis Hamilton-Time Series Analysis Hamilton-Time Series Analysis
It has been used in multivariate time series analysis, leading to the decomposition of the series in a small set of latent factors. However, little is known on the statistical performances of matrix ...
matlab自学过程的小程序,用于缓解压力
Deep Learning for Time Series Forecasting Predict the Future with MLPs, CNNs and LSTMs in Python Jason Brownlee 5 25 step-by-step lessons, 575 pages. 深度学习方法为时间系列预测提供了许多希望,例如...
time series methods to a variety of data sets. The book assumes the reader has a knowledge typical of a first-year university statistics course and is based around lecture notes from a range of time ...
WAVELET METHODS FOR TIME SERIES ANALYSIS
Analyzing Neural Time Series Data图书
关于时间序列大数据分析的外文书籍。Introduction to time series.pdf (第三版)+ Deep Time Series Forecasting with Python.pdf 【高清】
A Little Book of R For Time Series R语言时间序列分析教程PDF
Introduction to Time Series Forecasting with Python.zip Python时间序列预测简介 课程代码
Nonlinear time series analysis
Effective time series outlier detection algorithm based on segmentation
timeseries-analysis, NPM软件包Timeseries分析,噪声去除,统计. . Timeseries分析一个可以链接的timeseries分析工具。转换你的数据,过滤它,平滑它,去除噪音,获取统计数据,获取数据的预览图表- - 。这个库是...
腾讯的研究论文,摘要:Time series anomaly detection is usually formulated as finding outlier data points relative to some usual data, which is also an important problem in industry and academia....
时间序列分析近期研究著作之一,UBC的大牛写的,还是值得一读,有很多的启发!
key of introduction to time series and forecasting
Time Series Analysis and Its Applications Time Series Analysis and Its Applications
Introduction to time series and forcasting Brockwell&Davis 很经典