Plug-in jfreechart + Shh for tree chart column chart line chart

Source: Internet
Author: User

When I was working on the background of an examination system recently, I had to use three types of images to analyze the examinee's score (pie chart (class score distribution ), bar Chart (comparison between classes), line chart (score fluctuation ),

Fortunately, Java provides good support for graphics, and the use of the freechart framework is very simple. freechart is really powerful, supports almost all graphics, and supports Web and application classes.

1. Struts. xml

<! Doctype struts public "-// Apache Software Foundation // DTD struts configuration 2.0/EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <package name = "jfreechartdemonstration" extends = "struts-Default "namespace ="/"> <result-types> <result-type name =" chart "class =" org. apache. struts2.dispatcher. chartresult "> </result-type> </result-types> <! -- Pie chart of class score analysis --> <action name = "showgraphaction" class = "com. ht. action. exam. examresultaction "> <result name =" success "type =" freemarker ">/WEB-INF/admin/showgraph. FTL </result> </Action> <! -- Display the pie chart --> <action name = "piechartaction" class = "com. ht. action. admin. exportgraph "method =" getexportgraph "> <result type =" chart "> <Param name =" width "> 400 </param> <Param name =" height "> 300 </ param> </result> </Action> <! -- Class bar chart --> <action name = "showsgraphaction" class = "com. ht. action. exam. examresultaction "> <result name =" success "type =" freemarker "> ........ </result> <! -- This is the page to be displayed. You only need to use the IMG label to display the page on that page. In SRC, the page is connected to the action. --> </Action> <! -- Bar chart --> <action name = "columnarchartaction" class = "com. ht. action. admin. exportgraph "method =" getexportcolumnargraph "> <result type =" chart "> <Param name =" width "> 400 </param> <Param name =" height "> 300 </ param> </result> </Action> <! -- Select a line chart --> <action name = "showfoldlineaction" class = "com. ht. action. admin. exportgraph "> <result name =" success "type =" freemarker ">/WEB-INF/admin/showfoldline. FTL </result> </Action> <! -- Display line chart --> <action name = "foldlineaction" class = "com. ht. action. admin. exportgraph "method =" getfoldlinegraph "> <result type =" chart "> <Param name =" width "> 400 </param> <Param name =" height "> 300 </ param> </result> </Action> </package> </struts>

 

 

4. Action

/** Generate pie chart **/Public String getexportgraph () {// set data defaultpiedataset DATA = new defaultpiedataset (); Data. setvalue ("90 ~ 100 "," 2 ", 4); Data. setvalue (" 70 ~ 90 "," 3 ", 5); Data. setvalue (" 60 ~ 70 "," 5 ", 2); data. setvalue ("<60", "5", 4); // generate jfreechart object chart = chartfactory. createpiechart ("", Data, true, true, false); // pie chart pieplot plot = (pieplot) chart. getplot (); plot. setexplodepercentdata. setvalue ("<60", "5", 4, 0.2); // empty data and 0 data plot are not displayed. setignorezerovalues (true); plot. setignorenullvalues (true); Return success;}/** generate a bar chart */Public String getexportcolumnargraph () {defaultcategorydataset datase T = new defacategcategorydataset (); dataset. setvalue (4, 2, "90 ~ 100 "); // Number of students, class, score dataset. setvalue (," 70 ~ 90 "); dataset. setvalue (4, 2," 60 ~ 70 "); dataset. setvalue (, "<60"); // set the title style chart = chartfactory. createbarchart3d ("", "score segment", "Number of people", dataset, plotorientation. vertical, true, true, false); // chart. settitle (New texttitle ("", new font ("", Font. bold, 22); // categoryplot plot = (categoryplot) chart in the middle of the report. getplot (); // set the horizontal style categoryaxis = plot. getdomainaxis (); categoryaxis. setlabelfont (new font ("", Font. bold, 12); categoryaxis. setcategorylabelpositions (categorylabelpositions. up_45); // set the vertical style numberaxis = (numberaxis) plot. getrangeaxis (); numberaxis. setstandardtickunits (numberaxis. createintegertickunits (); // number display area, which can only be an integer numberaxis. setlabelfont (new font ("", Font. layout_no_limit_context, 12); // obtain the chart below the report. getlegend (); // set each column to display its own information. barrenderer3d Renderer = new barrenderer3d (); Renderer. setbaseitemlabelgenerator (New standardcategoryitemlabelgenerator (); Renderer. setbaseitemlabelsvisible (true); Renderer. setbasepositiveitemlabelposition (New itemlabelposition (itemlabelanchor. outside12, textanchor. baseline_left); Renderer. setitemlabelanchoroffset (10d); plot. setrenderer (Renderer); Return success;}/** generate line chart **/Public String getfoldlinegraph () {double [] [] DATA = new double [] [] {34, 56, 78, 55, 67}, // line data {44, 67, 88, 99, 34}, {44, 53, 78, 78, 45 },}; string [] rowkeys = {"", "", ""}; // ordinate string [] columnkeys = {"simulation 1 ", "simulate 2", "simulate 3", "simulate 4", "simulate 5"}; // The horizontal coordinate shows categorydataset dataset = getbardata (data, rowkeys, columnkeys ); createtimexychar ("score fluctuation Chart", "Exam name", "score segment", dataset, ""); Return success;} public categorydataset getbardata (double [] [] data, string [] rowkeys, string [] columnkeys) {return datasetutilities. createcategorydataset (rowkeys, columnkeys, data);} public void createtimexychar (string charttitle, string X, string y, categorydataset xydataset, string charname) {chart = chartfactory. createlinechart (charttitle, X, Y, xydataset, plotorientation. vertical, true, true, false); font font00 = new font ("", Font. layout_no_limit_context, 13); legendtitle legend = chart. getlegend (); legend. setitemfont (font00); // set the comment font chart. settextantialias (false); // set the font of the graph title and reset the title Font = new font ("", Font. layout_no_limit_context, 15); texttitle Title = new texttitle (charttitle); title. setfont (font); chart. settitle (title); categoryplot = (categoryplot) chart. getplot (); // determines whether the X axis category axis mesh is visible in categoryplot. setdomaingridlinesvisible (true); // whether the grid of the Y axis data is visible in categoryplot. setrangegridlinesvisible (true); categoryplot. setrangegridlinepaint (color. pink); // dotted line color categoryplot. setdomaingridlinepaint (color. pink); // dotted line color categoryplot. setbackgroundpaint (color. white); // set the distance between the axis and the panel. setaxisoffset (New rectangleinsets (0d, 0d, 0d, 0d); categoryaxis domainaxis = categoryplot. getdomainaxis (); domainaxis. setlabelfont (new font ("", Font. layout_no_limit_context, 13); // axis title domainaxis. setticklabelfont (new font ("", Font. layout_no_limit_context, 13); // The axis value domainaxis. setcategorylabelpositions (categorylabelpositions. up_45); // set the distance from the left side of the image to domainaxis. setlowermargin (0.1); // set the distance from the right side of the image to domainaxis. setuppermargin (0.1); numberaxis = (numberaxis) categoryplot. getrangeaxis (); numberaxis. setstandardtickunits (numberaxis. createintegertickunits (); numberaxis. setautorangeincludeszero (true); numberaxis. setlabelfont (new font ("", Font. layout_no_limit_context, 13); // you can specify the maximum distance from the top of the image to numberaxis. setuppermargin (0.15); // set the distance between the lowest value and the bottom of the image. // numberaxis. setlowermargin (0.15); // obtain the Renderer lineandshaperenderer = (lineandshaperenderer) categoryplot. getrenderer (); lineandshaperenderer. setbaseshapesvisible (true); // lineandshaperenderer can be seen in series points (that is, data points. setbaselinesvisible (true); // there is a line between the series points (that is, the data points) to see/display the discount point data lineandshaperenderer. setbaseitemlabelgenerator (New standardcategoryitemlabelgenerator (); lineandshaperenderer. setbaseitemlabelsvisible (true );}

 

 

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.