柱状图:
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
* JFreechat 建立 圆柱型数据图
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
double[][] data = new double[][] {{
(int)(Math.random()*1000), //猪肉(广州 深圳 东莞 佛山
(int)(Math.random()*1000),
(int)(Math.random()*1000),
(int)(Math.random()*1000)},
{720, 700, 680, 640}, //牛肉
{1130, 1020, 980, 800}, //鸡肉
{440, 400, 360, 300}}; //鱼肉
String[] rowKeys = {"猪肉", "牛肉","鸡肉", "鱼肉"};
String[] columnKeys = {"广州", "深圳", "东莞", "佛山"};
CategoryDataset dataset = DatasetUtilities.createCategoryDataset(rowKeys, columnKeys, data);
// dataset.addValue(223, "北京", "猪肉");
// dataset.addValue(1000, "上海", "牛肉");
// dataset.addValue(530, "深圳", "鸡肉");
// dataset.addValue(340, "山东", "鱼肉");
JFreeChart chart = ChartFactory.createBarChart3D("" +
"肉类销量统计图", //图表标题
"肉类", //目录轴显示
"销量", //数值轴显示标签
dataset, //数据集
PlotOrientation.VERTICAL, //图标 柱状图横向(HORIZONTAL) 竖直(VERTICAL)
true, //是否显示图例(对于简单的柱状图必须是false)
true, // 是否生成工
true); // 是否生成URL链接
chart.getCategoryPlot().setNoDataMessageFont(new Font("宋体", Font.ITALIC, 20));
chart.getTitle().setFont(new Font("宋体", Font.ITALIC, 20));//设置标题
chart.setBackgroundPaint(Color.white); //设定背景色为白色
CategoryPlot categoryPlot=chart.getCategoryPlot();//获得 plot,用于设置显示特性
//设置图表的纵轴和横轴org.jfree.chart.axis.CategoryAxis
//图表的字体设置
CategoryAxis domainAxis=categoryPlot.getDomainAxis(); //水平底部列表
domainAxis.setLabelFont(new Font("楷体",Font.BOLD,14)); //X轴标题
domainAxis.setTickLabelFont(new Font("宋体",Font.BOLD,12)); //x轴下标
domainAxis.setLowerMargin(0.1);//设置距离图片左端距离此时为10%
domainAxis.setUpperMargin(0.1);//设置距离图片右端距离此时为百分之10
domainAxis.setCategoryLabelPositionOffset(10);//图表横轴与标签的距离(10像素)
domainAxis.setCategoryMargin(0.2);//横轴标签之间的距离20%
//设置纵横坐标的显示位置
categoryPlot.setBackgroundPaint(Color.WHITE);
categoryPlot.setDomainGridlinePaint(Color.BLACK);//分类轴网格线条颜色
categoryPlot.setDomainGridlinesVisible(true);
categoryPlot.setRangeGridlinePaint(Color.GREEN);//数据轴网格线条颜色
//显示每个柱的数值,并修改该数值的字体属性
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.3);
categoryPlot.setRenderer(renderer);
//设置地区、销量的显示位置
//将下方的“肉类”放到上方
categoryPlot.setDomainAxisLocation(AxisLocation.TOP_OR_RIGHT);
//将默认放在左边的“销量”放到右方
categoryPlot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_LEFT);
ValueAxis rangeAxis=categoryPlot.getRangeAxis();//获取柱状
rangeAxis.setLabelFont(new Font("黑体",Font.BOLD,15)); //Y轴标题
//设置类型小标题字体
chart.getLegend().setItemFont(new Font("黑体", Font.BOLD, 15));
HttpSession session = request.getSession();
String filename = ServletUtilities.saveChartAsPNG(chart, 700, 400, null, session);
String graphURL = request.getContextPath() + "/DisplayChart?filename=" + filename;
//将图片url返回
request.setAttribute("graphURL", graphURL);
request.setAttribute("filename", filename);
//ajax 返回数据
PrintWriter out = response.getWriter();
//调用打印方法
//response.getWriter().write(graphURL.toString());
out.print(graphURL);
out.close();
//request.getRequestDispatcher("cylinder.jsp").forward(request, response);
}
突出效果饼图
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
* 饼图显示数据信息
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//设置数据集
DefaultPieDataset dataset = new DefaultPieDataset();
dataset.setValue("初中高级程序员", 0.55);
dataset.setValue("项目经理", 0.1);
dataset.setValue("系统分析师", 0.1);
dataset.setValue("软件架构师", 0.1);
dataset.setValue("其他", 0.2);
//通过工厂类生成JFreeChart对象
//JFreeChart chart = ChartFactory.createPieChart3D("IT行业职业分布图", dataset, true, false, false);
//实用createPieChart3D 无法突出饼图 所以实用createPieChart
JFreeChart chart = ChartFactory.createPieChart("IT行业职业分布图", dataset, true, false, false);
PiePlot pieplot = (PiePlot) chart.getPlot();
//标题文字乱码
TextTitle textTitle = chart.getTitle();
textTitle.setFont(new Font("宋体", Font.PLAIN, 20));
//饼上的文字乱码
pieplot.setLabelFont(new Font("宋体", Font.BOLD, 16));
//图例文字乱码
chart.getLegend().setItemFont(new Font("宋体", Font.PLAIN, 12));
BarRenderer3D renderer = new BarRenderer3D();
renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
renderer.setBaseItemLabelsVisible(true);
//将某部分凸显出来的效果
pieplot.setExplodePercent("其他", 0.3);
//设置类型小标题字体
chart.getLegend().setItemFont(new Font("黑体", Font.BOLD, 15));
//没有数据的时候显示的内容
pieplot.setNoDataMessage("无数据显示");
pieplot.setCircular(false);
pieplot.setLabelGap(0.02D);
HttpSession session = request.getSession();
String filename = ServletUtilities.saveChartAsPNG(chart, 500, 300, null, session);
String graphURL = request.getContextPath() + "/DisplayChart?filename=" + filename;
//将图片url返回
request.setAttribute("graphURL", graphURL);
request.setAttribute("filename", filename);
request.getRequestDispatcher("circle.jsp").forward(request, response);
}
曲线图:
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
* 曲线图显示数据信息
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//访问量统计时间线
TimeSeries timeSeries2006 = new TimeSeries("2006年度", Month.class);
TimeSeries timeSeries2007 = new TimeSeries("2007年度", Month.class);
//时间曲线数据集合
TimeSeriesCollection lineDataset = new TimeSeriesCollection();
//构造数据集合
timeSeries2006.add(new Month(1, 2007), 7200);
timeSeries2006.add(new Month(2, 2007), 7000);
timeSeries2006.add(new Month(3, 2007), 4200);
timeSeries2006.add(new Month(4, 2007), 8200);
timeSeries2006.add(new Month(5, 2007), 7300);
timeSeries2006.add(new Month(6, 2007), 8200);
timeSeries2006.add(new Month(7, 2007), 9200);
timeSeries2006.add(new Month(8, 2007), 7300);
timeSeries2006.add(new Month(9, 2007), 9400);
timeSeries2006.add(new Month(10, 2007), 7500);
timeSeries2006.add(new Month(11, 2007), 6600);
timeSeries2006.add(new Month(12, 2007), 3500);
timeSeries2007.add(new Month(1, 2007), 10200);
timeSeries2007.add(new Month(2, 2007), 9000);
timeSeries2007.add(new Month(3, 2007), 6200);
timeSeries2007.add(new Month(4, 2007), 8200);
timeSeries2007.add(new Month(5, 2007), 8200);
timeSeries2007.add(new Month(6, 2007), 11200);
timeSeries2007.add(new Month(7, 2007), 13200);
timeSeries2007.add(new Month(8, 2007), 8300);
timeSeries2007.add(new Month(9, 2007), 10400);
timeSeries2007.add(new Month(10, 2007), 12500);
timeSeries2007.add(new Month(11, 2007), 10600);
timeSeries2007.add(new Month(12, 2007), 10500);
lineDataset.addSeries(timeSeries2006);
lineDataset.addSeries(timeSeries2007);
JFreeChart chart = ChartFactory.createTimeSeriesChart("访问量统计时间线", "月份", "访问量", lineDataset, true, true, true);
//设置子标题
TextTitle subtitle = new TextTitle("2006/2007年度访问量对比", new Font("黑体", Font.BOLD, 12));
chart.addSubtitle(subtitle);
//设置主标题
chart.setTitle(new TextTitle("blog访问量统计", new Font("宋体", Font.ITALIC, 15)));
chart.setAntiAlias(true);
//设置类型小标题字体
chart.getLegend().setItemFont(new Font("黑体", Font.BOLD, 15));
XYPlot xyPlot=chart.getXYPlot();//获得 plot,用于设置显示特性
xyPlot.setBackgroundPaint(Color.WHITE);
xyPlot.setDomainGridlinePaint(Color.BLACK);//分类轴网格线条颜色
xyPlot.setDomainGridlinesVisible(true);
xyPlot.setRangeGridlinePaint(Color.GREEN);//数据轴网格线条颜色
//图表的字体设置
// ValueAxis domainAxis = chart.getXYPlot().getDomainAxis();
// domainAxis.setLabelFont(new Font("宋体", Font.BOLD, 15));
HttpSession session = request.getSession();
String filename = ServletUtilities.saveChartAsPNG(chart, 500, 300, null, session);
String graphURL = request.getContextPath() + "/DisplayChart?filename=" + filename;
//将图片url返回
request.setAttribute("graphURL", graphURL);
request.setAttribute("filename", filename);
request.getRequestDispatcher("curve.jsp").forward(request, response);
}