分享带热点图提示的 TimeSeries Chart

写这篇文章是为了把自己研究的东西分享给大家,可能大家已经知道了,那我就献丑了。
在做金融相关项目的时候,往往会碰到要画时间序列图,大家都知道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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值