University of the project is a function of the statistical results of the chart analysis, find some information after the feeling Jfreechar get started very fast, simple to do a few demos are also very good to achieve, but also to meet the needs of the project, so decided to use this tool. Here you will learn some of the knowledge about Jfreechar to tidy up a bit.
1. Introduction of the JAR package
Use this tool to be sure to bring his package into the project, in the University project to make use of MAVEN to manage these external files, so for the introduction of the jar package This step becomes particularly simple, As long as you add Jfreechar dependencies to the MAVEN project's Pom.xml file (maven automatically retrieves the jar package in the repository and downloads it locally)
<dependency><span style= "White-space:pre" ></span> <groupid>jfreechart</groupid > <artifactId>jfreechart</artifactId> <version>1.0.0</version></ Dependency>
We will then see the jar package Jfreechar needs in Maven depenencies (if not, update the project-shortcut key is Alt+f5).
2. Modify the Web. xml file
Add the following code to the Web. xml file
<!--jfreechart Report configuration- <servlet> <servlet-name>DisplayChart</servlet-name> <servlet-class>org.jfree.chart.servlet.DisplayChart</servlet-class> </servlet> < servlet-mapping> <servlet-name>DisplayChart</servlet-name> <url-pattern>/chart </url-pattern></servlet-mapping>
The above two steps are the integration of Jfreechar and Springmvc+maven.
3. Implement a Column chart
Here is a small example to familiarize yourself with this tool-Create a column chart
Code in the controller in the Web tier
Package Com.tgb.itoo.chart.controller;import Java.awt.font;import Javax.servlet.http.httpservletrequest;import Javax.servlet.http.httpservletresponse;import Org.jfree.chart.chartcolor;import org.jfree.chart.ChartFactory; Import Org.jfree.chart.jfreechart;import Org.jfree.chart.axis.categoryaxis;import Org.jfree.chart.axis.ValueAxis; Import Org.jfree.chart.plot.categoryplot;import Org.jfree.chart.plot.plotorientation;import Org.jfree.chart.renderer.category.barrenderer;import Org.jfree.chart.servlet.servletutilities;import Org.jfree.chart.title.texttitle;import Org.jfree.data.category.categorydataset;import Org.jfree.data.category.defaultcategorydataset;import Org.springframework.stereotype.controller;import Org.springframework.ui.modelmap;import Org.springframework.web.bind.annotation.requestmapping;import Org.springframework.web.servlet.ModelAndView, @Controllerpublic class Columnchartcontroller {@RequestMapping ("/ Resultmap ") Public String Resultmap () {return"/resultmap ";} Display Bar Chart @RequEstmapping (value = "/getcolumnchart") public Modelandview Getcolumnchart (HttpServletRequest request, HttpServletResponse response, Modelmap modelmap) throws exception{//1. Get Data collection Categorydataset DataSet = GetData Set (); 2. Create a histogram Jfreechart chart = Chartfactory.createbarchart3d ("Student Satisfaction with teachers",//Chart title "Course Name",//Catalog Axis display label "percent",//value axis display label DataSet,//DataSet plotorientation.vertical,//Chart direction: horizontal, Vertical False,//whether the legend is displayed (must be false for a simple histogram),//Whether the build tool is false//generates a URL link ); 3. Sets the color and text of the entire histogram (the setting of the Char object is set for the entire graph) Chart.setbackgroundpaint (chartcolor.white); Sets the total background color//4. Gets a drawing object and sets the color text of the graphic through this object Categoryplot p = chart.getcategoryplot ();//Get the Chart object P.setbackgroundpaint (chartcolor . lightgray);//Graphics background color p.setrangegridlinepaint (chartcolor.white);//graphic table color//5. Set column width Barrenderer rendereR = (barrenderer) p.getrenderer (); Renderer.setmaximumbarwidth (0.06); Solve garbled problem Getchartbyfont (chart); 6. Convert the graphic to a picture, to the foreground String FileName = servletutilities.savechartasjpeg (chart, request.getsession, zero, NULL, and ()); String Charturl=request.getcontextpath () + "/chart?filename=" +filename; Modelmap.put ("Charturl", Charturl); return new Modelandview ("Resultmap", Modelmap); }//Set text style private static void Getchartbyfont (Jfreechart chart) {//1. Graphic caption text settings Texttitle Texttitle = Chart.gettitle (); Texttitle.setfont (New Font ("Arial", font.bold,20)); 2. Graphical x-axis coordinate text setting Categoryplot plot = (Categoryplot) chart.getplot (); Categoryaxis axis = Plot.getdomainaxis (); Axis.setlabelfont (New Font ("Arial", font.bold,22)); Sets the text Axis.setticklabelfont of the title on the x-axis coordinates (new Font ("Arial", font.bold,15)); Sets the text//2 on the x-axis coordinates. Graphical y-axis coordinate text settings Valueaxis Valueaxis = Plot.getrangEaxis (); Valueaxis.setlabelfont (New Font ("Arial", font.bold,15)); Sets the text Valueaxis.setticklabelfont (new Font ("Sans-serif", font.bold,12)) of the title on the y-axis coordinates;//sets the text on y-axis coordinates}//Get a demo of the combined data Set object private static Categorydataset GetDataSet () {defaultcategorydataset DataSet = new Defaultcategorydataset (); Dataset.addvalue (40, "", "ordinary Zoology"); Dataset.addvalue (50, "", "biology"); Dataset.addvalue (60, "", "Animal Anatomy"); Dataset.addvalue (70, "", "Biology Theory class"); Dataset.addvalue (80, "", "Animal Theory class"); return dataset; }}
Configuration in the Spring-mvc
<bean name= "Columnchartcontroller" class= "Com.tgb.itoo.chart.controller.ColumnChartController"/>
JSP pages in the Web tier
<%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8"%><%string path = Request.getcontextpath () ; String basepath = request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/";%> <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >
Results show:
Jfreechart and Springmvc+maven Integration