折线图 line.jsp
XY曲线图 xy.jsp
时间曲线图 timeserver.jsp
其他曲线图 MultipleAxisDemo.jsp
折线图 line.jsp :
<%@ page contentType="text/html;charset=GBK"%> <%@ page import = "org.jfree.chart.ChartFactory, org.jfree.chart.ChartPanel, org.jfree.chart.JFreeChart, org.jfree.chart.StandardLegend, org.jfree.chart.axis.NumberAxis, org.jfree.chart.plot.CategoryPlot, org.jfree.chart.plot.PlotOrientation, org.jfree.chart.renderer.LineAndShapeRenderer, org.jfree.data.CategoryDataset, org.jfree.data.DefaultCategoryDataset, org.jfree.chart.servlet.ServletUtilities "%> <% // row keys... String series1 = "First"; String series2 = "Second"; String series3 = "Third"; // column keys... String type1 = "Type 1"; String type2 = "Type 2"; String type3 = "Type 3"; String type4 = "Type 4"; String type5 = "Type 5"; String type6 = "Type 6"; String type7 = "Type 7"; String type8 = "Type 8"; // create the dataset... DefaultCategoryDataset dataset = new DefaultCategoryDataset(); dataset.addValue(1.0, series1, type1); dataset.addValue(4.0, series1, type2); dataset.addValue(3.0, series1, type3); dataset.addValue(5.0, series1, type4); dataset.addValue(5.0, series1, type5); dataset.addValue(7.0, series1, type6); dataset.addValue(7.0, series1, type7); dataset.addValue(8.0, series1, type8); dataset.addValue(5.0, series2, type1); dataset.addValue(7.0, series2, type2); dataset.addValue(6.0, series2, type3); dataset.addValue(8.0, series2, type4); dataset.addValue(4.0, series2, type5); dataset.addValue(4.0, series2, type6); dataset.addValue(2.0, series2, type7); dataset.addValue(1.0, series2, type8); dataset.addValue(4.0, series3, type1); dataset.addValue(3.0, series3, type2); dataset.addValue(2.0, series3, type3); dataset.addValue(3.0, series3, type4); dataset.addValue(6.0, series3, type5); dataset.addValue(3.0, series3, type6); dataset.addValue(4.0, series3, type7); dataset.addValue(3.0, series3, type8); // create the chart... JFreeChart chart = ChartFactory.createLineChart( "血压水平分布图", // chart title "Type", // domain axis label "Value", // range axis label dataset, // data PlotOrientation.VERTICAL, // orientation true, // include legend true, // tooltips false // urls ); // NOW DO SOME OPTIONAL CUSTOMISATION OF THE CHART... StandardLegend legend = (StandardLegend) chart.getLegend(); legend.setDisplaySeriesShapes(true); legend.setShapeScaleX(1.5); legend.setShapeScaleY(1.5); legend.setDisplaySeriesLines(true); CategoryPlot plot = chart.getCategoryPlot(); // customise the range axis... NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis(); rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits()); rangeAxis.setAutoRangeIncludesZero(true); rangeAxis.setUpperMargin(0.20); rangeAxis.setLabelAngle(Math.PI / 2.0); LineAndShapeRenderer renderer = (LineAndShapeRenderer) plot.getRenderer(); renderer.setItemLabelsVisible(true); String filename = ServletUtilities.saveChartAsPNG(chart, 500, 300, null, session); String graphURL = request.getContextPath() + "/DisplayChart?filename=" + filename; %> <img src="<%= graphURL %>" width=500 height=300 border=0 usemap="#<%= filename %>"> |
XY曲线图 xy.jsp :
<%@ page contentType="text/html;charset=GBK"%> <%@ page import =" java.awt.*"%> <%@ page import = " java.awt.geom.Ellipse2D"%> <%@ page import = " java.io.PrintWriter"%> <%@ page import = " java.text.SimpleDateFormat"%> <%@ page import = " java.util.Date"%> <%@ page import = " java.util.ArrayList"%> <%@ page import = " java.util.Iterator"%> <%@ page import = " java.util.Locale"%> <%@ page import = " java.text.NumberFormat"%> <%@ page import = " javax.servlet.http.HttpSession"%> <%@ page import = " org.jfree.data.*"%> <%@ page import = " org.jfree.chart.*"%> <%@ page import = " org.jfree.chart.axis.*"%> <%@ page import = " org.jfree.chart.renderer.BarRenderer"%> <%@ page import = " org.jfree.chart.renderer.StandardXYItemRenderer"%> <%@ page import = " org.jfree.chart.renderer.StackedXYAreaRenderer"%> <%@ page import = " org.jfree.chart.renderer.XYAreaRenderer"%> <%@ page import = " org.jfree.chart.plot.*"%> <%@ page import = " org.jfree.chart.entity.*"%> <%@ page import = " org.jfree.chart.labels.*"%> <%@ page import = " org.jfree.chart.urls.*"%> <%@ page import ="org.jfree.chart.servlet.*"%> <%@ page import = "java.io.PrintWriter"%> <%@ page import = "java.util.ArrayList"%> <%@ page import = "java.util.Iterator,org.jfree.data.time.*,java.awt.Font"%> <% XYSeries dataSeries = new XYSeries("增长"); for (int i = 0; i <= 10; i++) { int x=(int) (100*Math.random()); int y=(int) (100*Math.random()); dataSeries.add(x,y); } XYSeries dataSeries1 = new XYSeries("趋势"); for (int j = 0; j <= 100; j++) { int x=(int) (100*Math.random()); int y=(int) (100*Math.random()); dataSeries1.add(j,40); } XYSeriesCollection xyDataset = new XYSeriesCollection(); xyDataset.addSeries(dataSeries); xyDataset.addSeries(dataSeries1); String filename = ""; SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy", Locale.UK); StandardXYToolTipGenerator ttg = new StandardXYToolTipGenerator(StandardXYToolTipGenerator.DEFAULT_TOOL_TIP_FORMAT,sdf,NumberFormat.getInstance()); TimeSeriesURLGenerator urlg = new TimeSeriesURLGenerator(sdf, "pie_chart.jsp", "series", "hitDate"); // Create the chart object ValueAxis xAxis = new NumberAxis("X轴坐标"); //x轴坐标 ValueAxis yAxis = new NumberAxis("Y轴坐标"); //y轴坐标 StandardXYItemRenderer renderer = new StandardXYItemRenderer(StandardXYItemRenderer.LINES ,ttg, urlg); renderer.setShapesFilled(true); Font font=new Font("黑体",Font.TRUETYPE_FONT, 12); XYPlot plot = new XYPlot(xyDataset, xAxis, yAxis, renderer); JFreeChart chart = new JFreeChart("测试曲线图", font, plot, true); chart.setBackgroundPaint(java.awt.Color.white); // Write the chart image to the temporary directory ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection()); filename = ServletUtilities.saveChartAsPNG(chart, 500, 300, info, session); String graphURL = request.getContextPath() + "/DisplayChart?filename=" + filename;%> <html> <head> </head> <body> <table border=0> <tr> <td> <br> <img src="<%= graphURL %>" width=500 height=300 border=0 usemap="#<%= filename %>"> </td> </tr> </table> </body> </html> |
时间曲线图 timeserver.jsp :
<%@ page contentType="text/html;charset=GBK"%> <%@ page import = "org.jfree.chart.ChartFactory, org.jfree.chart.ChartPanel, org.jfree.chart.JFreeChart, org.jfree.chart.servlet.ServletUtilities, org.jfree.chart.*, org.jfree.chart.StandardLegend, org.jfree.chart.axis.DateAxis, org.jfree.chart.plot.XYPlot, org.jfree.chart.renderer.StandardXYItemRenderer, org.jfree.chart.renderer.XYItemRenderer, org.jfree.data.XYDataset, org.jfree.data.time.Month, org.jfree.data.time.TimeSeries, org.jfree.data.time.TimeSeriesCollection "%> <% TimeSeries s1 = new TimeSeries("L&G European Index Trust", Month.class); s1.add(new Month(2, 2001), 181.8); s1.add(new Month(3, 2001), 167.3); s1.add(new Month(4, 2001), 153.8); s1.add(new Month(5, 2001), 167.6); s1.add(new Month(6, 2001), 158.8); s1.add(new Month(7, 2001), 148.3); s1.add(new Month(8, 2001), 153.9); s1.add(new Month(9, 2001), 142.7); s1.add(new Month(10, 2001), 123.2); s1.add(new Month(11, 2001), 131.8); s1.add(new Month(12, 2001), 139.6); s1.add(new Month(1, 2002), 142.9); s1.add(new Month(2, 2002), 138.7); s1.add(new Month(3, 2002), 137.3); s1.add(new Month(4, 2002), 143.9); s1.add(new Month(5, 2002), 139.8); s1.add(new Month(6, 2002), 137.0); s1.add(new Month(7, 2002), 132.8); TimeSeries s2 = new TimeSeries("L&G UK Index Trust", Month.class); s2.add(new Month(2, 2001), 129.6); s2.add(new Month(3, 2001), 123.2); s2.add(new Month(4, 2001), 117.2); s2.add(new Month(5, 2001), 124.1); s2.add(new Month(6, 2001), 122.6); s2.add(new Month(7, 2001), 119.2); s2.add(new Month(8, 2001), 116.5); s2.add(new Month(9, 2001), 112.7); s2.add(new Month(10, 2001), 101.5); s2.add(new Month(11, 2001), 106.1); s2.add(new Month(12, 2001), 110.3); s2.add(new Month(1, 2002), 111.7); s2.add(new Month(2, 2002), 111.0); s2.add(new Month(3, 2002), 109.6); s2.add(new Month(4, 2002), 113.2); s2.add(new Month(5, 2002), 111.6); s2.add(new Month(6, 2002), 108.8); s2.add(new Month(7, 2002), 101.6); TimeSeriesCollection dataset = new TimeSeriesCollection(); dataset.addSeries(s1); dataset.addSeries(s2); dataset.setDomainIsPointsInTime(true); JFreeChart chart = ChartFactory.createTimeSeriesChart( "Legal & General Unit Trust Prices", "Date", "Price Per Unit", dataset, true, true, false ); //chart.setBackgroundPaint(Color.white); StandardLegend sl = (StandardLegend) chart.getLegend(); sl.setDisplaySeriesShapes(true); XYPlot plot = chart.getXYPlot(); // plot.setBackgroundPaint(Color.lightGray); //plot.setDomainGridlinePaint(Color.white); //plot.setRangeGridlinePaint(Color.white); //plot.setAxisOffset(new Spacer(Spacer.ABSOLUTE, 5.0, 5.0, 5.0, 5.0)); plot.setDomainCrosshairVisible(true); plot.setRangeCrosshairVisible(true); XYItemRenderer renderer = plot.getRenderer(); if (renderer instanceof StandardXYItemRenderer) { StandardXYItemRenderer rr = (StandardXYItemRenderer) renderer; rr.setPlotShapes(true); rr.setShapesFilled(true); } DateAxis axis = (DateAxis) plot.getDomainAxis(); //axis.setDateFormatOverride(new SimpleDateFormat("MM-yyyy")); String filename = ServletUtilities.saveChartAsPNG(chart, 500, 300, null, session); String graphURL = request.getContextPath() + "/DisplayChart?filename=" + filename; %> <img src="<%= graphURL %>" width=500 height=300 border=0 usemap="#<%= filename %>"> |
其他曲线图 MultipleAxisDemo.jsp :
<%@ page contentType="text/html;charset=GBK"%> <%@ page import = "org.jfree.chart.ChartFactory, org.jfree.chart.ChartPanel, org.jfree.chart.JFreeChart, org.jfree.chart.servlet.ServletUtilities, org.jfree.chart.axis.AxisLocation, org.jfree.chart.axis.NumberAxis, org.jfree.chart.plot.PlotOrientation, org.jfree.chart.plot.XYPlot, org.jfree.chart.renderer.StandardXYItemRenderer, org.jfree.chart.title.TextTitle, org.jfree.data.XYDataset, org.jfree.data.time.Minute, org.jfree.data.time.RegularTimePeriod, org.jfree.data.time.TimeSeries, org.jfree.data.time.TimeSeriesCollection "%> <% String name="Series 1"; double base=100.0; RegularTimePeriod start=new Minute(); int count=200; TimeSeries series = new TimeSeries(name, start.getClass()); RegularTimePeriod period = start; double value = base; for (int i = 0; i < count; i++) { series.add(period, value); period = period.next(); value = value * (1 + (Math.random() - 0.495) / 10.0); } TimeSeriesCollection dataset = new TimeSeriesCollection(); dataset.addSeries(series); JFreeChart chart = ChartFactory.createTimeSeriesChart( "Multiple Axis Demo 1", "Time of Day", "Primary Range Axis", dataset, true, true, false ); //chart.setBackgroundPaint(Color.white); chart.addSubtitle(new TextTitle("Four datasets and four range axes.")); XYPlot plot = chart.getXYPlot(); plot.setOrientation(PlotOrientation.VERTICAL); //plot.setBackgroundPaint(Color.lightGray); //plot.setDomainGridlinePaint(Color.white); //plot.setRangeGridlinePaint(Color.white); //plot.setAxisOffset(new Spacer(Spacer.ABSOLUTE, 5.0, 5.0, 5.0, 5.0)); StandardXYItemRenderer renderer = (StandardXYItemRenderer) plot.getRenderer(); //renderer.setPaint(Color.black); /* // AXIS 2 NumberAxis axis2 = new NumberAxis("Range Axis 2"); axis2.setAutoRangeIncludesZero(false); //axis2.setLabelPaint(Color.red); //axis2.setTickLabelPaint(Color.red); //plot.setSecondaryRangeAxis(0, axis2); //plot.setSecondaryRangeAxisLocation(0, AxisLocation.BOTTOM_OR_LEFT); XYDataset dataset2 = createDataset("Series 2", 1000.0, new Minute(), 170); plot.setSecondaryDataset(0, dataset2); plot.mapSecondaryDatasetToRangeAxis(0, new Integer(0)); plot.setSecondaryRenderer(0, new StandardXYItemRenderer()); // plot.getSecondaryRenderer(0).setSeriesPaint(0, Color.red); // AXIS 3 NumberAxis axis3 = new NumberAxis("Range Axis 3"); //axis3.setLabelPaint(Color.blue); //axis3.setTickLabelPaint(Color.blue); //plot.setSecondaryRangeAxis(1, axis3); XYDataset dataset3 = createDataset("Series 3", 10000.0, new Minute(), 170); plot.setSecondaryDataset(1, dataset3); plot.mapSecondaryDatasetToRangeAxis(1, new Integer(1)); plot.setSecondaryRenderer(1, new StandardXYItemRenderer()); //plot.getSecondaryRenderer(1).setSeriesPaint(0, Color.blue); // AXIS 4 NumberAxis axis4 = new NumberAxis("Range Axis 4"); axis4.setLabelPaint(Color.green); axis4.setTickLabelPaint(Color.green); // plot.setSecondaryRangeAxis(2, axis4); XYDataset dataset4 = createDataset("Series 4", 25.0, new Minute(), 200); plot.setSecondaryDataset(2, dataset4); plot.mapSecondaryDatasetToRangeAxis(2, new Integer(2)); plot.setSecondaryRenderer(2, new StandardXYItemRenderer()); //plot.getSecondaryRenderer(2).setSeriesPaint(0, Color.green); */ String filename = ServletUtilities.saveChartAsPNG(chart, 500, 300, null, session); String graphURL = request.getContextPath() + "/DisplayChart?filename=" + filename; %> <img src="<%= graphURL %>" width=500 height=300 border=0 usemap="#<%= filename %>"> |
注意以上代码,均在:jfreechart-0.9.20.jar、jcommon-0.9.5.jar下调试通过!
web.xml配置servlet代码:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd"> <web-app> <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> </web-app> |