JFreeChart使用实例

有些东西我只是看到里面的东西可以就复制过来了,至于能不能运行 我没调试,只能做参考

<%@ page language="java" pageEncoding="GBK"%>
<%@ page import="org.jfree.chart.JFreeChart"%><!--图形生成类-->
<%@ page import="org.jfree.chart.ChartFactory"%><!--基于JFreeChart创建图形具体方法类-->
<%@ page import="org.jfree.data.category.DefaultCategoryDataset"%><!--数据容器类柱形和折线图-->
<%@ page import="org.jfree.data.general.DefaultPieDataset"%><!--数据容器类饼形图专用-->
<%@ page import="org.jfree.chart.plot.PlotOrientation"%><!--坐标轴类型类(HORIZONTAL/VERTICAL)-->
<%@ page import="org.jfree.chart.plot.CategoryPlot"%><!--美化坐标-->
<%@ page import="org.jfree.chart.axis.CategoryAxis"%><!--绘制坐标-->
<%@ page import="org.jfree.chart.axis.ValueAxis"%>
<%@ page import="org.jfree.chart.axis.AxisLocation"%>
<%@ page import="org.jfree.chart.labels.StandardCategoryItemLabelGenerator"%>
<%@ page import="org.jfree.chart.renderer.category.BarRenderer3D"%><!-- 柱形图和折线图-->
<%@ page import="org.jfree.chart.ChartFactory"%><!-- 饼形图-->
<%@ page import="org.jfree.util.TableOrder"%><!--饼图排序方式(BY_ROW/BY_COLUMN)-->
<%@ page import="java.awt.Color"%><!--颜色设置函数-->
<%@ page import="org.jfree.chart.servlet.ServletUtilities"%><!--生成图片类-->
<%@ page import="org.jfree.chart.plot.PiePlot"%><!--美化坐标-->
<%@ page import="org.jfree.ui.RectangleInsets"%>
<%@ page import="org.jfree.util.Rotation"%><!--饼图旋转方向(ANTICLOCKWISE/CLOCKWISE)-->
<%@ page import="org.jfree.chart.renderer.category.LineAndShapeRenderer"%><!--折线图用-->
<%@ page import="org.jfree.chart.labels.*" %>
<%@ page import="java.awt.Font"%>
<%@ page import="org.jfree.chart.labels.*"%>
<jsp:directive.page import="java.text.DecimalFormat"/>
<jsp:directive.page import="org.jfree.chart.labels.PieSectionLabelGenerator"/>
<jsp:directive.page import="org.jfree.chart.axis.CategoryLabelPositions"/>

<%
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 'my.jsp' starting page</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->

</head>

<body>
This is my JSP page. <br>
<a href="<%=basePath%>managersystem/district/district_index.jsp">地区</a>
<a href="<%=path%>/foreground/tubiao/tubiao_main.jsp">tb区</a><br>
<a href="<%=path%>/foreground/tubiao2/tubiao2_main.jsp">tb区2</a><br>
<a href="<%=path%>/foreground/tubiao3/tubiao3_main.jsp">tb区3</a><br>
<a href="<%=path%>/foreground/tubiao4/tubiao4_main.jsp">tb区4</a><br>
<a href="<%=path%>/foreground/xqtubiao/xqtubiao_main.jsp">xqtb区</a><br>
<table><tr><td>
<%
//**************收到数据集*******************************


//******************如查数据集不为空则开始显示显示统计图************************
//取出标题信息,数据对象第一维第一个为标题、第二个为x轴名称、第三个Y轴
//String title=temp[0][0];//标题
//String xname=temp[0][1];//X坐标名称
//String yname=temp[0][2];//Y坐标名称

//***************添加数据集(1、类型 2、值 3、区域)*************************************************
DefaultPieDataset datasetB = new DefaultPieDataset();//创建饼图数据集对象

datasetB.setValue("A",25);//(如:"选择A",300)
datasetB.setValue("B",25);
datasetB.setValue("C",25);
datasetB.setValue("D",25);
datasetB.setValue("E",3);
JFreeChart chartB = ChartFactory.createPieChart3D("啊",datasetB,true,false,false);
//chart.setBackgroundPaint(java.awt.Color.white);//可选,设置图片背景色
chartB.setBackgroundPaint(Color.WHITE);//设置背景颜色为白色
//chartB.setTitle("3D饼图 by zjun");//可选,设置图片标题
//chartB.setTextAntiAlias(true);//设置标题平滑效果
//chartB.setBorderVisible(true);//设置图片外边框显示
//chartB.setAntiAlias(false);//设置内边框平滑效果


PiePlot plotB = (PiePlot) chartB.getPlot();


//设置饼图标签显示项,参数1为显示内容("{1}"为文本,"{2}"为数值,"{3}"为百分比),参数2为显示值的格式,参数3为百分比格式
plotB.setLabelGenerator(new StandardPieSectionLabelGenerator("{1}",new DecimalFormat("0"), new DecimalFormat("0.0%")));
//设置饼图参照表标签显示项,参数1为显示内容("{1}"为文本,"{2}"为数值,"{3}"为百分比),参数2为显示值的格式,参数3为百分比格式
plotB.setLegendLabelGenerator(new StandardPieSectionLabelGenerator("{0}={2}",new DecimalFormat("0"), new DecimalFormat("0.0%")));

//设置饼图大小,注意参数1小于参数2
//plotB.setExplodePercent(1,1.9);

//设置标签颜色
plotB.setLabelPaint(new Color(205, 000, 105));

//设置标签阴影颜色
//plotB.setLabelShadowPaint(new Color(255, 100, 255));

//设置标签外边框线颜色
plotB.setLabelOutlinePaint( Color.green);

//设置标签字体
//plotB.setLabelFont(new Font("SansSerif",Font.BOLD,22));

//设置是否显示标签线
//plotB.setLabelLinksVisible(false);

//设置值为null时是否显示该项
//plotB.setIgnoreNullValues(true);

//设置值为0时是否显示该项
//plotB.setIgnoreZeroValues(true);

//设置第一个 section 的开始位置,默认是12点钟方向
//plotB.setStartAngle(0);

//指定 section 轮廓线的厚度
//plotB.setSectionOutlineStroke(new java.awt.BasicStroke(1));
//指定 section 的色彩
plotB.setSectionPaint(0,new Color(205, 100, 205));

//指定显示的饼图上圆形还椭圆形
//plotB.setCircular(true);
//指定 section 按逆时针方向依次显示,默认是顺时针方向
//plotB.setDirection(Rotation.ANTICLOCKWISE);
//设置柱的透明度
//plotB.setForegroundAlpha(0.8f);

//输出图形
String filename = ServletUtilities.saveChartAsPNG(chartB, 500, 300, null, session);
String graphURL = request.getContextPath() + "/servlet/DisplayChart?filename=" + filename;
%>

<img src="<%=graphURL%>" name="tubiao" width=500 height=300 border=0 usemap="#<%=filename%>"/>
</td>
</tr>
<tr>
<td>
<%
//******************显示三维柱形统计图************************
//取出标题信息,数据对象第一维第一个为标题、第二个为x轴名称、第三个Y轴
String title="t";//标题
String xname="x";//X坐标名称
String yname="y";//Y坐标名称

//***************添加数据集(1、类型 2、值 3、区域)*************************************************
DefaultCategoryDataset datasetA = new DefaultCategoryDataset();//创建柱型和拆线图的数据集对象
//for(int i=1;i<temp.length;i++){
// datasetA.addValue(Integer.valueOf(temp[1]), temp[2], temp[0]);//(如:300,"满意度调查","选择A")
// datasetB.setValue(temp[0],Integer.valueOf(temp[1]));//(如:"选择A",300)
//}
//datasetA.setValue(300,"五华区","满意度调查");//(如:300,"满意度调查","选择A")
//datasetA.setValue(1000,"盘龙区","满意度调查");
//datasetA.setValue(2000,"西山区","满意度调查");
//datasetA.setValue(1000,"盘龙区2","满意度调查1");

datasetA.setValue(300,"满意度调查","五华区");
datasetA.setValue(430,"满意度调查","盘龙区");
datasetA.setValue(500,"满意度调查","西山区");
//*******************************生成三维柱形图********************************************
//创建柱图形对象并设置标题、类型(列)、数值(行)(3D柱型图:createBarChart3D)
JFreeChart chartA = ChartFactory.createBarChart3D(title,
xname,yname,datasetA,//显示数据
PlotOrientation.VERTICAL,//坐标轴类型 水平、垂直
true,
false,
false);

chartA.setBackgroundPaint(Color.WHITE);//设置背景颜色为白色
CategoryPlot plotA = chartA.getCategoryPlot();
//--------------------------------------------------------------------------
CategoryAxis domainAxis = plotA.getDomainAxis();//横坐标标尺
domainAxis.setCategoryLabelPositionOffset(0);//图表横轴与标签的距离(10像素)
//domainAxis.setAxisLineVisible(true);//坐标轴线条是否可见(3D轴无效)
//domainAxis.setAxisLinePaint(Color.RED);//坐标轴线条颜色(3D轴无效)
//domainAxis.setLabel("aaaA");////X坐标名称
//domainAxis.setLabelAngle(0.5);//坐标轴标题旋转角度(纵坐标可以旋转)
//domainAxis.setCategoryLabelPositions(new CategoryLabelPositions());//横坐标标签位置(不确定)
//domainAxis.setTickLabelPaint(Color.red);//横坐标标尺值的颜色(Tick:底部标记)
//domainAxis.setTickMarkPaint(Color.red);//坐标轴标尺颜色
//domainAxis.setTickMarksVisible(true);//坐标轴标尺显示
//domainAxis.setTickLabelsVisible(false);//坐标轴标尺值是否显示
plotA.setDomainAxis(domainAxis);
//-----------------------------------------------------
ValueAxis rangeAxis = plotA.getRangeAxis();//纵坐标标尺
//设置最高的一个 Item 与图片顶端的距离
rangeAxis.setUpperMargin(0.15);
//设置最低的一个 Item 与图片底端的距离
rangeAxis.setLowerMargin(0.15);
//rangeAxis.setVerticalTickLabels(true);//数据轴数据标签是否旋转到垂直
//rangeAxis.setAutoRange(true);//自动设置数据轴数据
//rangeAxis.setAutoRangeMinimumSize(10000.00);//自动设置数据轴数据范围时数据轴的值最小跨度
//rangeAxis.setAutoTickUnitSelection(false);//数据轴的数据标签是否自动确定(默认为true)
//rangeAxis.setFixedAutoRange(500);//设置数据轴刻度范围
//rangeAxis.setFixedDimension(300);//设置数据轴向右压缩?????
//rangeAxis.setAxisLinePaint(Color.red);//坐标轴线条颜色(3D轴无效)
plotA.setRangeAxis(rangeAxis);
//-------------------------------------------------------------------
BarRenderer3D renderer = new BarRenderer3D();

//设置柱体边框颜色
//renderer.setBaseOutlinePaint(Color.BLACK);

//设置侧边框的颜色
//renderer.setWallPaint(Color.ORANGE);

//设置各个地区代表的颜色
renderer.setBaseSeriesVisible(false);
//renderer.setSeriesPaint(0, new Color(0, 0, 255));
//renderer.setSeriesPaint(1, new Color(255, 255, 0));
//renderer.setSeriesPaint(2, Color.GREEN);

//设置每个地区所包含的平行柱的之间距离
renderer.setItemMargin(0.1);

//显示每个柱的数值,并修改该数值的字体属性
renderer.setItemLabelGenerator(new StandardCategoryItemLabelGenerator("{3}={2}",new DecimalFormat("0"), new DecimalFormat("0.0%")));
//设置图列标签显示值只有{0}有用
//renderer.setLegendItemLabelGenerator(new StandardCategorySeriesLabelGenerator("{0}"));
renderer.setItemLabelsVisible(true);//是否显示柱体数值
plotA.setRenderer(renderer);
//------------------------------------------------------------------------
//plotA.setOutlinePaint(Color.red);//图表边框颜色
//plotA.setOutlineVisible(false);//图表边框显示
//plotA.setAnchorValue(208.5);//未知

//设置柱的透明度
plotA.setForegroundAlpha(0.6f);
//设置地区、销量的显示位置
plotA.setDomainAxisLocation(AxisLocation.BOTTOM_OR_LEFT);
plotA.setRangeAxisLocation(AxisLocation.TOP_OR_LEFT);

//输出图形
String filenameA = ServletUtilities.saveChartAsPNG(chartA, 500, 300, null, session);
String graphURLA = request.getContextPath() + "/servlet/DisplayChart?filename=" + filenameA;
%>
<img src="<%=graphURLA%>" name="tubiao" width=500 height=300 border=0 usemap="#<%=filenameA%>"/>
</td>
</tr>
<script language="javascript" type="text/javascript"> <!-- leadcode('Content212'); //--> </script>
<tr>
<td>
<%//************************生成三维折线图*********************************************

//**************收到数据集*******************************
//String temp[][]=(String[][])request.getAttribute("dataset");
String temp[][]=new String[11][3];
temp[0][0]="XXX项目一年期调查情况统计表";
temp[0][1]="月份";
temp[0][2]="平均值";

temp[1][1]="300";
temp[1][0]="2007/01";
temp[1][2]="大理";

temp[2][1]="400";
temp[2][0]="2007/05";
temp[2][2]="大理";

temp[3][1]="375";
temp[3][0]="2007/06";
temp[3][2]="大理";

temp[4][1]="350";
temp[4][0]="2007/09";
temp[4][2]="大理";

temp[5][1]="500";
temp[5][0]="2007/12";
temp[5][2]="大理";


temp[6][1]="100";
temp[6][0]="2007/01";
temp[6][2]="曲靖";

temp[7][1]="450";
temp[7][0]="2007/05";
temp[7][2]="曲靖";

temp[8][1]="360";
temp[8][0]="2007/06";
temp[8][2]="曲靖";

temp[9][1]="600";
temp[9][0]="2007/09";
temp[9][2]="曲靖";

temp[10][1]="250";
temp[10][0]="2007/12";
temp[10][2]="曲靖";

//******************如查数据集不为空则开始显示显示统计图************************
//取出标题信息,数据对象第一维第一个为标题、第二个为x轴名称、第三个Y轴
//String title=temp[0][0];//标题
//String xname=temp[0][1];//X坐标名称
//String yname=temp[0][2];//Y坐标名称

//***************添加数据集(1、类型 2、值 3、区域)*************************************************
DefaultCategoryDataset datasetC = new DefaultCategoryDataset();//创建柱型和拆线图的数据集对象
for(int i=1;i<temp.length;i++){
if(temp[1]==null){
datasetC.addValue(null, temp[2], temp[0]);//(如:300,"满意度调查","选择A")
}else{
datasetC.addValue(Integer.valueOf(temp[1]), temp[2], temp[0]);//(如:300,"满意度调查","选择A")
}
}

//*******************************************************************************
JFreeChart chartC = ChartFactory.createLineChart(temp[0][0],
temp[0][1],
temp[0][2],
datasetC,
PlotOrientation.VERTICAL, true, true, false);

chartC.setBackgroundPaint(Color.WHITE);//设置背景颜色为白色
chartC.setBorderVisible(true);//设置边界是否可见
chartC.setBorderPaint(Color.CYAN);//设置边界顔色
//设定数据点
CategoryPlot plotC = (CategoryPlot)chartC.getPlot();
plotC.setDrawSharedDomainAxis(false );//设置值刻度间距模式(false为大,true为小)
LineAndShapeRenderer theRenderer = new LineAndShapeRenderer();
theRenderer.setBaseSeriesVisible(true);//是否显示数据区
theRenderer.setBaseLinesVisible(true);//是否显示连接线
theRenderer.setSeriesShape(0, new java.awt.geom.Ellipse2D.Double(-5D, -5D, 10D, 10D));//设置点样式
//theRenderer.setSeriesVisibleInLegend(1,new Boolean(false));设置是否显示图例
//theRenderer.setSeriesVisible(1,new Boolean(false));//设置一整条线是否显示
//*************************************************
theRenderer.setShapesVisible(true); //是否显示点还保持连接
theRenderer.setDrawOutlines(true); //是否显示点断开连接
theRenderer.setUseFillPaint(false); //点是否为空心
theRenderer.setBaseItemLabelsVisible(true); //是否显示点的数据标签
theRenderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());//配合才有点的数据标签
theRenderer.setBaseItemLabelPaint(Color.WHITE); //数值标签的颜色
plotC.setRenderer(theRenderer);

//********************设置刻度跨度*******************************
ValueAxis axis=plotC.getRangeAxis();
axis.setAutoRange(false);//是否自动设置刻度范围
axis.setAutoTickUnitSelection(true);//设置是否自动设定数据轴标签
axis.setAutoRangeMinimumSize(200);//最小跨度
//axis.setFixedDimension(200);//设置数据显示区的宽度
axis.setUpperBound(1000);//最大跨度
axis.setLowerBound(0);//最小值

String filenameC = ServletUtilities.saveChartAsPNG(chartC, 500, 300, null, session);
String graphURLC = request.getContextPath() + "/servlet/DisplayChart?filename=" + filenameC;
%>
<img src="<%=graphURLC%>" name="tubiao" width=500 height=300 border=0 usemap="#<%=filenameC%>"/>
</td>
</tr>
</table>
</body>
</html> <script language="javascript" type="text/javascript"> <!-- leadcode('Content213'); //--> </script>

你可能感兴趣的:(jfreechart)