1、利用 JFreeChart 创建按颜色分类的水果销售报表
package com.wcy.chart.bar; import javax.servlet.http.HttpSession; import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.servlet.ServletUtilities;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.general.DatasetUtilities; public class BarChart3 { public static String genBarChart(HttpSession session)throws Exception{
double[][] data = new double[][]{{1320},{720},{830},{400}};
String[] rowKeys = {"苹果","香蕉","橘子","梨子"};
String[] columnKeys = {"深圳"};
CategoryDataset dataset = DatasetUtilities.createCategoryDataset(rowKeys, columnKeys,data);
JFreeChart chart = ChartFactory.createBarChart3D("水果销售统计图", "水果", "销售",
dataset, PlotOrientation.VERTICAL, true, true, true);
String fileName = ServletUtilities.saveChartAsPNG(chart, 700, 500, session);
return fileName;
}
}
<%@page import="com.wcy.chart.bar.BarChart3"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
String fileName = BarChart3.genBarChart(session);
%>
<img alt="" src="DisplayChart?filename=<%=fileName %>" width="700" height="500">
</body>
</html>
2、利用 JFreeChart 创建按颜色分类并且按地区分类水果销售报表
package com.wcy.chart.bar; import javax.servlet.http.HttpSession; import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.servlet.ServletUtilities;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.general.DatasetUtilities; public class BarChart4 { public static String genBarChart(HttpSession session)throws Exception{
double[][] data = new double[][]{{1390,690,542,630},{529,650,1200,690},{780,450,1300,1120},{890,640,750,500}};
String[] rowKeys = {"苹果","香蕉","橘子","梨子"};
String[] columnKeys = {"上海","北京","贵州","广州"};
CategoryDataset dataset = DatasetUtilities.createCategoryDataset(rowKeys,columnKeys,data);
JFreeChart chart = ChartFactory.createBarChart3D("水果销售统计图", "水果", "销售", dataset, PlotOrientation.VERTICAL, true, true, true);
String fileName = ServletUtilities.saveChartAsPNG(chart, 700, 500, session);
return fileName;
}
}
<%@page import="com.wcy.chart.bar.BarChart4"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
String fileName = BarChart4.genBarChart(session);
%>
<img alt="" src="DisplayChart?filename=<%=fileName %>" width="700" height="500" border="0">
</body>
</html>
3、利用 JFreeChart 创建自定义 3D 柱状报表
package com.wcy.chart.bar; import java.awt.Color; import javax.servlet.http.HttpSession; import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.labels.ItemLabelAnchor;
import org.jfree.chart.labels.ItemLabelPosition;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer3D;
import org.jfree.chart.servlet.ServletUtilities;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.general.DatasetUtilities;
import org.jfree.ui.TextAnchor; public class BarChart5 { public static String genBarChart(HttpSession session)throws Exception{
double[][] data = new double[][]{{1299,459,980,470},{590,564,300,1130},{1200,357,910,380},{509,1300,650,810}};
String[] rowKeys = {"苹果","香蕉","橘子","梨子"};
String[] columnKeys = {"上海","北京","厦门","贵州"};
CategoryDataset dataset = DatasetUtilities.createCategoryDataset(rowKeys, columnKeys,data);
JFreeChart chart = ChartFactory.createBarChart3D("水果销售统计图", "水果", "销售", dataset, PlotOrientation.VERTICAL, true, true, true); CategoryPlot plot = chart.getCategoryPlot(); // 设置网格背景颜色
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); String fileName = ServletUtilities.saveChartAsPNG(chart, 700, 500, session);
return fileName;
}
}
<%@page import="com.wcy.chart.bar.BarChart5"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
String fileName = BarChart5.genBarChart(session);
%>
<img alt="" src="DisplayChart?filename=<%=fileName %>" width="700" height="500" border="0">
</body>
</html>
声明:此程序代码本人只是用于学习总结,非原创,如有侵权,联系本人。