//给你个源代码,上面有注释说明哪段是显示数据的 <%@ page contentType="text/html;charset=GBK"%> <%@ page import="java.awt.*"%> <%@ page import="org.jfree.chart.*"%> <%@ page import="org.jfree.chart.servlet.*"%> <%@ page import="org.jfree.chart.plot.*"%> <%@ page import="org.jfree.chart.renderer.*"%> <%@ page import="org.jfree.chart.entity.*"%> <%@ page import="org.jfree.chart.title.*"%> <%@ page import="org.jfree.chart.axis.*"%> <%@ page import="org.jfree.chart.renderer.category.LineAndShapeRenderer"%> <%@ page import="org.jfree.data.*"%> <%@ page import="org.jfree.data.general.*"%> <%@ page import="org.jfree.data.category.*"%> <%@ page import="org.jfree.ui.*"%> <%@ page import="org.jfree.util.*"%> <%@ page import="org.jfree.chart.labels.*"%> <%! String bookTitle[] = {"Python", "JAVA", "C#", "Perl", "PHP"}; String category[] = {"第1周", "第2周", "第3周", "第4周" }; double bookSales; String chartTitle = "JFreeChart实例11: 自定义线段图"; // 创建数据集 public CategoryDataset createDataset() { DefaultCategoryDataset dataset = new DefaultCategoryDataset(); for (int i=0; i < bookTitle.length; i++) { for (int j=0; j < category.length; j++ ) { bookSales = 1 + (Math.random() * 100); dataset.addValue(bookSales, bookTitle[i], category[j]); } } return dataset; } public JFreeChart createChart(CategoryDataset dataset) { // 创建图表对象 JFreeChart chart = ChartFactory.createLineChart ( chartTitle, // 图表标题 "销售时间:2005年2月", // 坐标标题 "销售量", // 坐标标题 dataset, // 定义绘制数据 PlotOrientation.VERTICAL, // 直方图的方向 true, // 定义图表是否包含图例 true, // 定义图表是否包含提示 false // 定义图表是否包含URL ); return chart; } %> <% // 创建一个 500X375 的图像 int width=500, height=375; CategoryDataset dataset = createDataset(); JFreeChart chart = createChart(dataset); // 开始自定义图表绘制的相关属性 // 设置图表的背景颜色 chart.setBackgroundPaint(new Color(205, 241, 197)); // 自定义图表的标题的字体和颜色 TextTitle title = chart.getTitle(); title.setFont(new Font("黑体", Font.BOLD, 25)); // 获得图表对象的引用,用于设置更多的自定义绘制属性 CategoryPlot plot = (CategoryPlot) chart.getPlot(); GradientPaint bg = new GradientPaint(0, 50, new Color(248, 253, 255), 0, 250, new Color(205, 237, 252)); plot.setBackgroundPaint(bg); plot.setDomainGridlinePaint(Color.BLACK); plot.setDomainGridlinesVisible(true); plot.setRangeGridlinePaint(Color.RED); // 设置横轴标题的字体 CategoryAxis domainAxis = plot.getDomainAxis(); domainAxis.setLabelFont(new Font("黑体", Font.BOLD, 15)); // 设置纵轴标题文字的字体及其旋转方向 ValueAxis rangeAxis = plot.getRangeAxis(); rangeAxis.setLabelFont(new Font("黑体", Font.BOLD, 15)); rangeAxis.setLabelAngle(Math.PI/2); // 自定义图例的显示风格 StandardLegend legend = (StandardLegend) chart.getLegend(); legend.setDisplaySeriesShapes(true); legend.setShapeScaleX(1.5); legend.setShapeScaleY(1.5); legend.setDisplaySeriesLines(true); // 获取渲染对象 LineAndShapeRenderer renderer = (LineAndShapeRenderer) plot.getRenderer(); renderer.setBaseItemLabelsVisible(true); //renderer.setDrawShapes(true); //renderer.setShapesFilled(true); //设置数据显示位置 //ItemLabelPosition p = new ItemLabelPosition(ItemLabelAnchor.CENTER, TextAnchor.CENTER_LEFT,TextAnchor.CENTER_LEFT, -Math.PI / 2.0 ); renderer.setBasePositiveItemLabelPosition(new ItemLabelPosition(ItemLabelAnchor.OUTSIDE12, TextAnchor.BASELINE_CENTER)); //显示折点相应数据 renderer.setBaseLabelGenerator(new StandardCategoryLabelGenerator()); // 自定义线段的绘制颜色 Color color[] = new Color[bookTitle.length]; color[0] = new Color(99,99,0); color[1] = new Color(255,169,66); color[2] = new Color(33,255, 66); color[3] = new Color(33,0,255); color[4] = new Color(255,0,66); for (int i = 0; i < color.length; i++) { renderer.setSeriesPaint(i, color[i]); } // 自定义线段的绘制风格 BasicStroke bs ; for (int i = 0; i < bookTitle.length; i++) { float dashes[] = {10.0f}; bs = new BasicStroke(2.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND, 10.f, dashes, 0.0f); if (i % 2 != 0) renderer.setSeriesStroke(i, bs); else renderer.setSeriesStroke(i, new BasicStroke(2.0f)); } // 结束自定义图表绘制的相关属性 ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection()); // 设置图片生成格式 String fileName = ServletUtilities.saveChartAsPNG(chart, width, height, info, session); // 设置图片生成路径 String graphURL = request.getContextPath() + "/servlet/DisplayChart?filename=" + fileName; %> <HTML> <HEAD> <TITLE><%=chartTitle%></TITLE> </HEAD> <BODY> <P ALIGN="CENTER"> <img src="<%=graphURL %>" border="1" > </P> </BODY> </HTML>