在我前面的博文讲到了JFreeChart报表图形:JFreeChart 3D饼图 JFreeChart 柱形图
public static String genLineChart(HttpSession session)throws Exception{ // A网站的访问量统计 TimeSeries timeSeries=new TimeSeries("A网站访问量统计", Month.class); // 添加数据 如果你是从数据库中获取数据,你就写个循环塞值就行了。 timeSeries.add(new Month(1,2013), 100); timeSeries.add(new Month(2,2013), 200); timeSeries.add(new Month(3,2013), 300); timeSeries.add(new Month(4,2013), 400); timeSeries.add(new Month(5,2013), 560); timeSeries.add(new Month(6,2013), 600); timeSeries.add(new Month(7,2013), 750); timeSeries.add(new Month(8,2013), 890); timeSeries.add(new Month(9,2013), 120); timeSeries.add(new Month(10,2013), 400); timeSeries.add(new Month(11,2013), 1200); timeSeries.add(new Month(12,2013), 1600); // B网站的访问量统计 //如果有更多的就继续添加就行了 TimeSeries timeSeries2=new TimeSeries("B网站访问量统计", Month.class); // 添加数据 timeSeries2.add(new Month(1,2013), 50); timeSeries2.add(new Month(2,2013), 100); timeSeries2.add(new Month(3,2013), 150); timeSeries2.add(new Month(4,2013), 200); timeSeries2.add(new Month(5,2013), 220); timeSeries2.add(new Month(6,2013), 300); timeSeries2.add(new Month(7,2013), 340); timeSeries2.add(new Month(8,2013), 400); timeSeries2.add(new Month(9,2013), 450); timeSeries2.add(new Month(10,2013), 500); timeSeries2.add(new Month(11,2013), 70); timeSeries2.add(new Month(12,2013), 800); // 定义时间序列的集合 TimeSeriesCollection lineDataset=new TimeSeriesCollection(); lineDataset.addSeries(timeSeries); lineDataset.addSeries(timeSeries2); JFreeChart chart=ChartFactory.createTimeSeriesChart("访问量统计时间折线图", "月份", "访问量", lineDataset, true, true, true); //设置主标题 chart.setTitle(new TextTitle("A,B网站访问量统计对比图", new Font("隶书", Font.ITALIC, 15))); //设置子标题 TextTitle subtitle = new TextTitle("2016年度", new Font("黑体", Font.BOLD, 12)); chart.addSubtitle(subtitle); chart.setAntiAlias(true); //设置时间轴的范围。 XYPlot plot = (XYPlot) chart.getPlot(); DateAxis dateaxis = (DateAxis)plot.getDomainAxis(); dateaxis.setDateFormatOverride(new java.text.SimpleDateFormat("M月")); dateaxis.setTickUnit(new DateTickUnit(DateTickUnit.MONTH,1)); //设置曲线是否显示数据点 XYLineAndShapeRenderer xylinerenderer = (XYLineAndShapeRenderer)plot.getRenderer(); xylinerenderer.setBaseShapesVisible(true); //设置曲线显示各数据点的值 XYItemRenderer xyitem = plot.getRenderer(); xyitem.setBaseItemLabelsVisible(true); xyitem.setBasePositiveItemLabelPosition(new ItemLabelPosition(ItemLabelAnchor.OUTSIDE12, TextAnchor.BASELINE_CENTER)); xyitem.setBaseItemLabelGenerator(new StandardXYItemLabelGenerator()); xyitem.setBaseItemLabelFont(new Font("Dialog", 1, 12)); plot.setRenderer(xyitem); //最后返回组成的折线图数值 String fileName=ServletUtilities.saveChartAsPNG(chart, 700, 500, session); return fileName; }3.编写jsp显示页面:[java] view plain copy print?<%@page import=“com.java1234.chart.line.LineChart2”%> <%@ page language=”java” contentType=“text/html; charset=utf-8” pageEncoding=”utf-8”%> <!DOCTYPE html PUBLIC ”-//W3C//DTD HTML 4.01 Transitional//EN” “http://www.w3.org/TR/html4/loose.dtd”> <html> <head> <meta http-equiv=”Content-Type” content=“text/html; charset=utf-8”> <title>Insert title here</title> </head> <body> <% String fileName=LineChart2.genLineChart(session); System.out.println(fileName); %> <img src=”DisplayChart?filename=<%=fileName %>” width=“700” height=“500” border=“0”/> </body> </html><%@page import="com.java1234.chart.line.LineChart2"%><%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><title>Insert title here</title></head><body><% String fileName=LineChart2.genLineChart(session); System.out.println(fileName);%><img src="DisplayChart?filename=<%=fileName %>" width="700" height="500" border="0"/></body></html>4.配置web.xml核心文件:[java] view plain copy print?<servlet> <servlet-name>DisplayChart</servlet-name> <servlet-class>org.jfree.chart.servlet.DisplayChart</servlet-class> </servlet> <servlet-mapping> <servlet-name>DisplayChart</servlet-name> <url-pattern>/DisplayChart</url-pattern> </servlet-mapping><servlet> <servlet-name>DisplayChart</servlet-name> <servlet-class>org.jfree.chart.servlet.DisplayChart</servlet-class> </servlet> <servlet-mapping> <servlet-name>DisplayChart</servlet-name> <url-pattern>/DisplayChart</url-pattern> </servlet-mapping>5.最后完成的显示效果:
新闻热点
疑难解答