Jfreechart使用案例

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ page
import="org.jfree.chart.ChartFactory,org.jfree.chart.JFreeChart, org.jfree.chart.servlet.ServletUtilities,

org.jfree.chart.title.TextTitle, org.jfree.data.time.TimeSeries,

org.jfree.data.time.Month,org.jfree.chart.plot.XYPlot,

org.jfree.data.time.TimeSeriesCollection,java.awt.Font,

org.jfree.chart.renderer.xy.XYLineAndShapeRenderer,

org.jfree.chart.renderer.xy.XYItemRenderer,

org.jfree.ui.RectangleInsets,java.awt.BasicStroke,

org.jfree.chart.labels.*,org.jfree.chart.renderer.category.LineAndShapeRenderer,

org.jfree.ui.*,org.jfree.data.category.DefaultCategoryDataset,
org.jfree.chart.plot.PlotOrientation,java.awt.Color,org.jfree.chart.plot.CategoryPlot,
org.jfree.chart.axis.*,java.util.*"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'chartOne.jsp' starting page</title>
<%
String series1 = "First";

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";
DefaultCategoryDataset defaultcategorydataset = new DefaultCategoryDataset();
defaultcategorydataset.addValue(1.0D, series1, type1);
defaultcategorydataset.addValue(4D, series1, type2);
defaultcategorydataset.addValue(3D, series1, type3);
defaultcategorydataset.addValue(5D, series1, type4);
defaultcategorydataset.addValue(5D, series1, type5);
defaultcategorydataset.addValue(7D, series1, type6);
defaultcategorydataset.addValue(7D, series1, type7);
defaultcategorydataset.addValue(8D, series1, type8);

JFreeChart jfreechart = ChartFactory.createLineChart(
"Line Chart Demo 1", "Type", "Value",
defaultcategorydataset, PlotOrientation.VERTICAL, true,
true, false);
jfreechart.setBackgroundPaint(Color.white);
CategoryPlot categoryplot = (CategoryPlot) jfreechart.getPlot();
categoryplot.setBackgroundPaint(Color.lightGray);
categoryplot.setRangeGridlinePaint(Color.white);
NumberAxis numberaxis = (NumberAxis) categoryplot.getRangeAxis();
numberaxis
.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
numberaxis.setAutoRangeIncludesZero(true);
//获得renderer 注意这里是下嗍造型到lineandshaperenderer!!
LineAndShapeRenderer lineandshaperenderer = (LineAndShapeRenderer) categoryplot
.getRenderer();
lineandshaperenderer.setShapesVisible(true); //series 点(即数据点)可见
lineandshaperenderer.setSeriesStroke(0, new BasicStroke(2.0F, 1, 1,
1.0F, new float[]
{ 10F, 6F }, 0.0F)); //定义series为"First"的(即series1)点之间的连线 ,这里是虚线,默认是直线

String filename = ServletUtilities.saveChartAsPNG(jfreechart, 500,
360, null, session);
String graphURL = request.getContextPath()
+ "/servlet/DisplayChart?filename=" + filename;
%>
</head>

<body>
This is my chart.
<br>
<img src="<%=graphURL%>" width=500 height=360 border=0>
</body>
</html>

你可能感兴趣的:(jfreechart)