<%@ page language="java" contentType="image/png;charset=GB2312" import="java.awt.*" import="java.text.*" import="org.jfree.chart.*" import="org.jfree.chart.axis.*" import="org.jfree.chart.servlet.*" import="org.jfree.chart.plot.*" import="org.jfree.chart.renderer.*" import="org.jfree.chart.axis.*" import="org.jfree.chart.entity.*" import="org.jfree.chart.labels.*" import="org.jfree.chart.title.*" import="org.jfree.chart.renderer.xy.*" import="org.jfree.data.*" import="org.jfree.data.xy.*" import="org.jfree.data.time.*" import="org.jfree.ui.*" import="org.jfree.util.*" %> <%! String bookTitle[] = {"Python", "JAVA"}; String chartTitle = "计算机编程类图书2004年月销售量时序统计图"; // 创建数据集 public XYDataset createDataset() { TimeSeriesCollection dataset = new TimeSeriesCollection(); TimeSeries ts[] = new TimeSeries[bookTitle.length]; int bookSales = 0; for (int i =0; i< bookTitle.length; i++) { ts[i] = new TimeSeries(bookTitle[i], Month.class); for (int month = 1; month <= 12; month++) { bookSales = 70 + (int)(Math.random() * 100); ts[i].add(new Month(month, 2004), bookSales); } dataset.addSeries(ts[i]); } return dataset; } public JFreeChart createChart(XYDataset dataset) { // 创建图表对象 JFreeChart chart = ChartFactory.createTimeSeriesChart ( chartTitle, // 图表标题 "统计时间", // 坐标标题 "月销售量", // 坐标标题 dataset, // 定义绘制数据 true, // 定义图表是否包含图例 true, // 定义图表是否包含提示 false // 定义图表是否包含URL ); return chart; } %> <% // 创建一个 500X375 的图像 int width=500, height=375; XYDataset dataset = createDataset(); JFreeChart chart = createChart(dataset); // 开始自定义图表绘制的相关属性 // 设置整个图表的背景颜色 GradientPaint bgGP = new GradientPaint(0, 0, Color.LIGHT_GRAY, 0, height, Color.WHITE, false); chart.setBackgroundPaint(bgGP); // 设置图表对象的绘制属性 XYPlot plot = chart.getXYPlot(); // 自定义图例 StandardLegend sl = (StandardLegend) chart.getLegend(); sl.setDisplaySeriesShapes(true); // 设置横轴上的时间刻度的显示格式 DateAxis domainAxis = new DateAxis("统计时段:2004年"); DateFormat df = new SimpleDateFormat("yyyy-MM"); DateTickUnit unit = new DateTickUnit(DateTickUnit.MONTH, 1, df); domainAxis.setTickUnit(unit); domainAxis.setVerticalTickLabels(true); plot.setDomainAxis(domainAxis); StandardXYItemRenderer renderer = (StandardXYItemRenderer) plot.getRenderer(); renderer.setPlotShapes(true); renderer.setSeriesShapesFilled(0, Boolean.TRUE); renderer.setSeriesShapesFilled(1, Boolean.FALSE); // 结束自定义图表绘制的相关属性 ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection()); // 设置图片生成格式 String fileName = ServletUtilities.saveChartAsPNG(chart, width, height, info, session); // 设置图片生成路径 String graphURL = request.getContextPath() + "/servlet/DisplayChart?filename=" + fileName; %>
|