柱状图代码如下
<%@page import="org.jfree.chart.plot.PiePlot"%>
<%@page import="org.jfree.chart.title.TextTitle"%>
<%@page import="org.jfree.chart.axis.AxisLocation"%>
<%@page import="org.jfree.chart.servlet.ServletUtilities"%>
<%@page import="org.jfree.ui.TextAnchor"%>
<%@page import="org.jfree.chart.labels.ItemLabelAnchor"%>
<%@page import="org.jfree.chart.labels.ItemLabelPosition"%>
<%@page import="org.jfree.chart.labels.StandardCategoryItemLabelGenerator"%>
<%@page import="java.awt.Color"%>
<%@page import="java.awt.Font"%>
<%@page import="org.jfree.chart.renderer.category.BarRenderer3D"%>
<%@page import="org.jfree.chart.axis.CategoryAxis"%>
<%@page import="org.jfree.chart.plot.CategoryPlot"%>
<%@page import="org.jfree.chart.plot.PlotOrientation"%>
<%@page import="org.jfree.chart.ChartFactory"%>
<%@page import="org.jfree.chart.JFreeChart"%>
<%@page import="org.jfree.data.general.DatasetUtilities"%>
<%@page import="org.jfree.data.category.CategoryDataset"%>
<%@page import="org.jfree.chart.axis.ValueAxis"%>
<%@ page language="java" contentType="text/html;charset=utf-8" pageEncoding="utf-8"%>
<%
double[][] data = new double[][] {{1230,1110,1120,1210}, {720,750,860,800}, {830,780,790,700,}, {400,380,390,450}};
String[] rowKeys = {"苹果", "香蕉", "橘子", "梨子"};//每列包含元素
String[] columnKeys = {"鹤壁","西安","深圳","北京"};//列元素
CategoryDataset dataset = DatasetUtilities.createCategoryDataset(rowKeys, columnKeys, data);
//另一种添加方式
//DefaultCategoryDataset dataset = new DefaultCategoryDataset();
//dataset.addValue(510, "深圳", "苹果");
//dataset.addValue(320, "深圳", "香蕉");
//dataset.addValue(580, "深圳", "橘子");
//dataset.addValue(390, "深圳", "梨子");
JFreeChart chart = ChartFactory.createBarChart3D("水果销量统计图", //图表标题,3D是否是3D样式
"水果种类",// 目录轴的显示标签
"销量",// 数值轴的显示标签
dataset,// 数据集
PlotOrientation.VERTICAL,//图形朝向,竖直或水平,
true,//是否显示图例(对于简单的柱状图必须是false)
true,// 是否生成工具
false);// 是否生成URL链接
/*以下是解决字体乱码代码 */
//设置字体
Font font = new Font("宋体",10,20);
TextTitle tt = chart.getTitle(); //设置标题
tt.setFont(font);
CategoryPlot plot = chart.getCategoryPlot(); //获取图标对象
CategoryAxis domainAxis=plot.getDomainAxis();
//水平底部列表
domainAxis.setLabelFont(new Font("黑体",Font.BOLD,14));
//水平底部标题
domainAxis.setTickLabelFont(new Font("宋体",Font.BOLD,12));
//垂直标题
ValueAxis rangeAxis=plot.getRangeAxis();//获取柱状
rangeAxis.setLabelFont(new Font("黑体",Font.BOLD,15));
chart.getLegend().setItemFont(new Font("黑体", Font.BOLD, 15));
//设置网格背景颜色
plot.setBackgroundPaint(Color.white);
//设置网格竖线颜色
plot.setDomainGridlinePaint(Color.pink);
//设置网格横线颜色
plot.setRangeGridlinePaint(Color.pink);
//显示每个柱的数值,并修改该数值的字体属性
BarRenderer3D renderer = new BarRenderer3D();
renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
renderer.setBaseItemLabelsVisible(true);
//默认的数字显示在柱子中,通过如下两句可调整数字的显示
//注意:此句很关键,若无此句,那数字的显示会被覆盖,给人数字没有显示出来的问题
renderer.setBasePositiveItemLabelPosition(new ItemLabelPosition(ItemLabelAnchor.OUTSIDE12, TextAnchor.BASELINE_LEFT));
renderer.setItemLabelAnchorOffset(10D);
//设置每个地区所包含的平行柱的之间距离
renderer.setItemMargin(0.4);
plot.setRenderer(renderer);
/*以上是解决字体乱码代码 */
//设置地区、销量的显示位置
//将下方的“水果”放到上方
//plot.setDomainAxisLocation(AxisLocation.TOP_OR_RIGHT);
//将默认放在左边的“销量”放到右方
//plot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_RIGHT);
String filename = ServletUtilities.saveChartAsPNG(chart, 500, 300, null, session);
String graphURL = request.getContextPath() + "/DisplayChart?filename=" + filename;
%>
<img src="<%= graphURL %>" width=530 height=320 border=0>
柱状图效果
代码如下
<%@page import="java.awt.Color"%>
<%@page import="org.jfree.chart.servlet.ServletUtilities"%>
<%@page import="java.awt.Font"%>
<%@page import="org.jfree.chart.plot.PiePlot"%>
<%@page import="org.jfree.data.general.PieDataset"%>
<%@page import="org.jfree.chart.ChartFactory"%>
<%@page import="org.jfree.chart.JFreeChart"%>
<%@page import="org.jfree.data.general.DefaultPieDataset"%>
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%
//生成饼图数据集对象
DefaultPieDataset PieDataset = new DefaultPieDataset();
PieDataset.setValue("管理人员",10.02D);
PieDataset.setValue("市场人员",20.23D);
PieDataset.setValue("开发人员",60.02D);
PieDataset.setValue("OEM人员",10.02D);
PieDataset.setValue("其他人员",5.11D);
JFreeChart jfreechart = ChartFactory.createPieChart("CityInfoPort公司组织架构图",//标题
PieDataset,//
true,//
true,//
false//
);
/*字体乱码异常解决代码*/
jfreechart.getTitle().setFont(new Font("黑体",Font.BOLD,20));//设置标题字体
PiePlot piePlot= (PiePlot) jfreechart.getPlot();//获取图表区域对象
piePlot.setLabelFont(new Font("黑体",Font.BOLD,10));
jfreechart.getLegend().setItemFont(new Font("黑体",Font.BOLD,10));
/*字体乱码异常解决代码*/
PiePlot pieplot = (PiePlot)jfreechart.getPlot();
//设置网格背景颜色
pieplot.setBackgroundPaint(Color.white);
//设置图表标签字体
pieplot.setLabelFont(new Font("宋体",Font.BOLD,12));
pieplot.setNoDataMessage("No data available");
pieplot.setCircular(true);
pieplot.setLabelGap(0.01D);//间距
String filename = ServletUtilities.saveChartAsPNG(jfreechart, 500, 300, null, session);
String graphURL = request.getContextPath() + "/DisplayChart?filename=" + filename;
%>
<img src="<%= graphURL %>" width=530 height=320 border=0>
饼图效果图
代码如下
<%@page import="org.jfree.chart.axis.CategoryAxis"%>
<%@page import="java.awt.Font"%>
<%@page import="org.jfree.chart.servlet.ServletUtilities"%>
<%@page import="org.jfree.data.category.DefaultCategoryDataset"%>
<%@page import="org.jfree.chart.plot.PlotOrientation"%>
<%@page import="org.jfree.chart.axis.NumberAxis"%>
<%@page import="org.jfree.chart.plot.CategoryPlot"%>
<%@page import="org.jfree.chart.ChartFactory"%>
<%@page import="org.jfree.chart.JFreeChart"%>
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%
DefaultCategoryDataset linedataset = new DefaultCategoryDataset();
// 各曲线名称
String series1 = "冰箱";
String series2 = "彩电";
String series3 = "洗衣机";
// 横轴名称(列名称)
String type1 = "1月";
String type2 = "2月";
String type3 = "3月";
linedataset.addValue(0.0, series1, type1);
linedataset.addValue(4.2, series1, type2);
linedataset.addValue(3.9, series1, type3);
linedataset.addValue(1.0, series2, type1);
linedataset.addValue(5.2, series2, type2);
linedataset.addValue(7.9, series2, type3);
linedataset.addValue(2.0, series3, type1);
linedataset.addValue(9.2, series3, type2);
linedataset.addValue(8.9, series3, type3);
//定义图表对象
JFreeChart chart = ChartFactory.createLineChart("折线图", // chart title
"时间", // domain axis label
"销售额(百万)", // range axis label
linedataset, // data
PlotOrientation.VERTICAL, // orientation
true, // include legend
true, // tooltips
false // urls
);
CategoryPlot plot = chart.getCategoryPlot();
/*字体乱码异常解决代码*/
chart.getTitle().setFont(new Font("宋体", Font.BOLD, 15));
chart.getLegend().setItemFont(new Font("黑体", Font.BOLD, 15));
CategoryAxis domainAxis = plot.getDomainAxis();
/*------设置X轴坐标上的文字-----------*/
domainAxis.setTickLabelFont(new Font("黑体", Font.PLAIN, 11));
/*------设置X轴的标题文字------------*/
domainAxis.setLabelFont(new Font("宋体", Font.PLAIN, 12));
NumberAxis numberaxis = (NumberAxis) plot.getRangeAxis();
/*------设置Y轴坐标上的文字-----------*/
numberaxis.setTickLabelFont(new Font("黑体", Font.PLAIN, 12));
/*------设置Y轴的标题文字------------*/
numberaxis.setLabelFont(new Font("黑体", Font.PLAIN, 12));
/*字体乱码异常解决代码*/
// 各种属性设置...
NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
rangeAxis.setAutoRangeIncludesZero(true);
rangeAxis.setUpperMargin(0.20);
rangeAxis.setLabelAngle(Math.PI / 2.0);
//
String filename = ServletUtilities.saveChartAsPNG(chart, 500, 300, null, session);
String graphURL = request.getContextPath() + "/DisplayChart?filename=" + filename;
%>
<img src="<%= graphURL %>" width=530 height=320 border=0>
折线图效果
<%@page import="org.jfree.chart.title.TextTitle"%>
<%@page import="java.awt.Font"%>
<%@page import="org.jfree.chart.plot.XYPlot"%>
<%@page import="org.jfree.chart.plot.CategoryPlot"%>
<%@page import="org.jfree.chart.servlet.ServletUtilities"%>
<%@page import="org.jfree.chart.renderer.xy.XYLineAndShapeRenderer"%>
<%@page import="java.awt.Color"%>
<%@page import="org.jfree.chart.ChartFactory"%>
<%@page import="org.jfree.chart.JFreeChart"%>
<%@page import="org.jfree.data.time.TimeSeriesCollection"%>
<%@page import="org.jfree.data.time.Month"%>
<%@page import="org.jfree.data.time.TimeSeries"%>
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%
TimeSeries s1 = new TimeSeries("项目A", 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("项目B", 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);
JFreeChart chart = ChartFactory.createTimeSeriesChart(
"时序图",
"日期",
"价格波动",
dataset,
true,
true,
false
);
/*纠正字体乱码*/
XYPlot plot = (XYPlot) chart.getPlot();
Font font = new Font("宋体",10,20);
TextTitle tt = chart.getTitle(); //设置标题
tt.setFont(font);
//纵轴字体
plot.getRangeAxis().setLabelFont(new Font("宋体", Font.BOLD, 15));
//横轴框里的标题字体
chart.getLegend().setItemFont(new Font("宋体", Font.ITALIC, 15));
//横轴列表字体
plot.getDomainAxis().setTickLabelFont(new Font("新宋体", 1, 15));
//横轴小标题字体
plot.getDomainAxis().setLabelFont(new Font("新宋体", 1, 12));
//设置背景色
//chart.setBackgroundPaint(Color.WHITE);
//设置时序图中线条的颜色
//XYLineAndShapeRenderer xylinerenderer=(XYLineAndShapeRenderer)chart.getXYPlot().getRenderer();
//以下分别给同一个曲线图中3条曲线设置颜色,0为第一条1为第二条,......
//xylinerenderer.setSeriesPaint(0,Color.YELLOW);
//chart.setBorderPaint(Color.BLUE);
String filename = ServletUtilities.saveChartAsPNG(chart, 500, 300, null, session);
String graphURL = request.getContextPath() + "/DisplayChart?filename=" + filename;
%>
<img src="<%= graphURL %>" width=530 height=320 border=0>
时序图效果如下