HelloWorldController.java
--------------------------------------------------------------------------------------
package net.spring.controller;
import java.awt.Font;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.block.BlockBorder;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
import org.jfree.chart.servlet.ServletUtilities;
import org.jfree.chart.title.LegendTitle;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.RectangleEdge;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class HelloWorldController {
@RequestMapping("/hello")
public ModelAndView helloWorld(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap)
throws Exception {
// String message = "Hello World, Spring 3.0!";
// System.out.println(message);
// return new ModelAndView("test2", "message", message);
// DefaultCategoryDataset dataset = new DefaultCategoryDataset();
// dataset.addValue(300, "广州", "苹果");
// dataset.addValue(200, "广州", "梨子");
// dataset.addValue(500, "广州", "葡萄");
// dataset.addValue(340, "广州", "芒果");
// dataset.addValue(280, "广州", "荔枝");
//
// JFreeChart chart = ChartFactory.createBarChart3D("水果销量统计图", "水果", "销量", dataset, PlotOrientation.VERTICAL, false,
// false, false);
// row keys...
String series1 = "First";
String series2 = "Second";
String series3 = "Third";
// column keys...
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";
// create the dataset...
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
dataset.addValue(1.0, series1, type1);
dataset.addValue(4.0, series1, type2);
dataset.addValue(3.0, series1, type3);
dataset.addValue(5.0, series1, type4);
dataset.addValue(5.0, series1, type5);
dataset.addValue(7.0, series1, type6);
dataset.addValue(7.0, series1, type7);
dataset.addValue(8.0, series1, type8);
dataset.addValue(5.0, series2, type1);
dataset.addValue(7.0, series2, type2);
dataset.addValue(6.0, series2, type3);
dataset.addValue(8.0, series2, type4);
dataset.addValue(4.0, series2, type5);
dataset.addValue(4.0, series2, type6);
dataset.addValue(2.0, series2, type7);
dataset.addValue(1.0, series2, type8);
dataset.addValue(4.0, series3, type1);
dataset.addValue(3.0, series3, type2);
dataset.addValue(2.0, series3, type3);
dataset.addValue(3.0, series3, type4);
dataset.addValue(6.0, series3, type5);
dataset.addValue(3.0, series3, type6);
dataset.addValue(4.0, series3, type7);
dataset.addValue(3.0, series3, type8);
// create the chart...
JFreeChart chart = ChartFactory.createLineChart("血压水平分布图", // chart title
"Type", // domain axis label
"Value", // range axis label
dataset, // data
PlotOrientation.VERTICAL, // orientation
true, // include legend
true, // tooltips
false // urls
);
// NOW DO SOME OPTIONAL CUSTOMISATION OF THE CHART...
LegendTitle legend = (LegendTitle) chart.getLegend();
legend.setPosition(RectangleEdge.BOTTOM);
legend.setBorder(BlockBorder.NONE);
CategoryPlot plot = chart.getCategoryPlot();
// customise the range axis...
NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
rangeAxis.setAutoRangeIncludesZero(true);
rangeAxis.setUpperMargin(0.20);
rangeAxis.setLabelAngle(Math.PI / 2.0);
LineAndShapeRenderer renderer = (LineAndShapeRenderer) plot.getRenderer();
renderer.setItemLabelsVisible(true);
// 解决乱码问题
getChartByFont(chart);
String fileName = ServletUtilities.saveChartAsJPEG(chart, 800, 600, null, request.getSession());
String chartURL = request.getContextPath() + "/chart?filename=" + fileName;
modelMap.put("chartURL", chartURL);
return new ModelAndView("demo", modelMap);
}
private static void getChartByFont(JFreeChart chart) {
TextTitle textTitle = chart.getTitle();
textTitle.setFont(new Font("宋体", Font.BOLD, 20));
LegendTitle legend = chart.getLegend();
if (legend != null) {
legend.setItemFont(new Font("宋体", Font.BOLD, 20));
}
CategoryPlot plot = (CategoryPlot) chart.getPlot();
CategoryAxis axis = plot.getDomainAxis();
// 设置X轴坐标上标题的文字
axis.setLabelFont(new Font("宋体", Font.BOLD, 22));
// 设置X轴坐标上的文字,
axis.setTickLabelFont(new Font("宋体", Font.BOLD, 12));
ValueAxis valueAxis = plot.getRangeAxis();
// 设置Y轴坐标上标题的文字
valueAxis.setLabelFont(new Font("宋体", Font.BOLD, 12));
// 设置Y轴坐标上的文字
valueAxis.setTickLabelFont(new Font("sans-serif", Font.BOLD, 12));
}
}
/WEB-INF/jsp/demo.jsp
--------------------------------------------------------------------------------------
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
/WEB-INF/web.xml
--------------------------------------------------------------------------------------
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
/WEB-INF/spring-servlet.xml
--------------------------------------------------------------------------------------
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">