public class JFreeChart1 extends HttpServlet {
private static final long serialVersionUID = 1L;
public JFreeChart1() {
super();
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
TimeSeries xyseries = new TimeSeries("finince income", Month.class);
TimeSeriesCollection xyseriescollection = new TimeSeriesCollection();
xyseriescollection.addSeries(xyseries);
JFreeChart jfreechart = ChartFactory.createTimeSeriesChart("Example",
"y", "x", xyseriescollection, true, true, true);
jfreechart.setBackgroundPaint(Color.white);
XYPlot xyplot = jfreechart.getXYPlot();
xyplot.setNoDataMessage("no data to display");// 此名可用来做当没有数据的时候jfreechart面板上的显示
StandardEntityCollection sec = new StandardEntityCollection();
ChartRenderingInfo info = new ChartRenderingInfo(sec);
ChartUtilities.writeChartAsPNG(response.getOutputStream(), jfreechart,
600, 400, info);
}
}
已有 0 人发表留言,猛击->>这里<<-参与讨论
JavaEye推荐
文章来源:
http://java-jun.javaeye.com/blog/602654
posted @
2010-02-25 10:59 uqnetlove 阅读(297) |
评论 (1) |
编辑 收藏
写这篇文章是为了把自己研究的东西分享给大家,可能大家已经知道了,那我就献丑了。
在做金融相关项目的时候,往往会碰到要画时间序列图,大家都知道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推荐
文章来源:
http://java-jun.javaeye.com/blog/602629
posted @
2010-02-25 10:54 uqnetlove 阅读(194) |
评论 (0) |
编辑 收藏
带热点提示的饼状图(即鼠标放在图型上可以看到当前数据项)
<%@
page contentType="text/html;charset=UTF-8"
%>
<%@
page import="org.jfree.data.general.DefaultPieDataset"
%>
<%@
page import="org.jfree.chart.*"
%>
<%@
page import="org.jfree.chart.plot.*"
%>
<%@
page import="org.jfree.chart.servlet.ServletUtilities"
%>
<%@
page import="org.jfree.chart.labels.StandardPieToolTipGenerator"
%>
<%@
page import="org.jfree.chart.urls.StandardPieURLGenerator"
%>
<%@
page import="org.jfree.chart.entity.StandardEntityCollection"
%>
<%@
page import="java.io.*"
%>
<
html
>
<head
>
<meta http-equiv=Content-Type content="text/html; charset=UTF-8"
>
<title></title
>
</head
>
<body
>
<%
DefaultPieDataset data =
new
DefaultPieDataset();
data.setValue(
"高中以下"
, 370);
data.setValue(
"高中"
, 1530);
data.setValue(
"大专"
, 5700);
data.setValue(
"本科"
, 8280);
data.setValue(
"硕士"
, 4420);
data.setValue(
"博士"
, 80);
PiePlot3D plot =
new PiePlot3D(data);
//3D饼图
plot.setURLGenerator(
new StandardPieURLGenerator("barview.jsp"));
//设定链接
JFreeChart chart =
new JFreeChart(""
,
JFreeChart.DEFAULT_TITLE_FONT, plot,
true
);
chart.setBackgroundPaint(java.awt.Color.white);
//可选,设置图片背景色
chart.setTitle(
"程序员学历情况调查表");
//可选,设置图片标题
plot.setToolTipGenerator(
new
StandardPieToolTipGenerator());
StandardEntityCollection sec =
new
StandardEntityCollection();
ChartRenderingInfo info =
new
ChartRenderingInfo(sec);
PrintWriter w =
new PrintWriter(out);
//输出MAP信息
//500是图片长度,300是图片高度
String filename = ServletUtilities.saveChartAsPNG(chart, 500, 300,
info, session);
ChartUtilities.writeImageMap(w,
"map0", info, false
);
String graphURL = request.getContextPath()
+
"/servlet/DisplayChart?filename="
+ filename;
%>
<p ALIGN="CENTER"
>
<img src="<%=graphURL%>" width=500 height=300 border=0 usemap="#map0"
>
</p
>
</body
>
</
html
>
已有 0 人发表留言,猛击->>这里<<-参与讨论
JavaEye推荐
文章来源:
http://java-jun.javaeye.com/blog/602623
posted @
2010-02-25 10:47 uqnetlove 阅读(105) |
评论 (0) |
编辑 收藏
摘要: 柱状图的一个演示(不带热点提示的) index.jsp 往 barview.jsp 提交 index.jsp code:
<%@
page
language="java" pageEncoding="ISO-8859-1"%>
<
html>
<
head>
<t...
阅读全文
posted @
2010-02-25 10:26 uqnetlove 阅读(101) |
评论 (0) |
编辑 收藏