I. Added JAR PACKAGE
- Struts2-jfreechart-plugin-2.1.6.jar in the corresponding jar package for STRUTS2
- Jcommon-1.0.23.jar in the Jfreechart official website to find
- Jfreechart-1.0.19.jar in the Jfreechart official website to find
Jfreechart the corresponding JAR package address: Http://pan.baidu.com/s/1gfygIob
The corresponding JSP page:
<src= "${pagecontext.request.contextpath}/jfreechart.action?id=<s:property Value="vote.id"/>"/>
The corresponding Struts.xml configuration: (Note that the header file should be changed from the XML in the arrow to introduce
< Packagename= "Jfreechart"extends= "Jfreechart-default"namespace="/"> <Actionname= "Jfreechart"class= "Jfreechartaction"> <resultname= "Success"type= "Chart"> <paramname= "width">700</param> <paramname= "height">250</param> </result> </Action> </ Package>
The corresponding action:
PackageAction;Importjava.util.List;ImportJfreechart. Jfreechartservice;ImportOrg.apache.struts2.ServletActionContext;ImportOrg.jfree.chart.JFreeChart;Importservice. Selectionservice;Importservice. Voteservice;ImportVo. Selection;ImportCom.opensymphony.xwork2.ActionSupport; @SuppressWarnings ("Serial" ) Public classJfreecharactionextendsActionsupport {/*** Define Jfreechart Object Note Here Jfreechart object name can only be chart *http://struts.apache.org/2.x/docs/jfreechart-plugin.html */ PrivateJfreechart Chart; PublicJfreechart Getchart () {returnChart; } Public voidSetchart (Jfreechart chart) { This. Chart =Chart; } PrivateVoteservice Voteservice; PrivateSelectionservice Selectionservice; Public voidSetvoteservice (Voteservice voteservice) { This. Voteservice =Voteservice; } Public voidSetselectionservice (Selectionservice selectionservice) { This. Selectionservice =Selectionservice; } PrivateJfreechartservice Jfreechartservice; Public voidSetjfreechartservice (Jfreechartservice jfreechartservice) { This. Jfreechartservice =Jfreechartservice; } PrivateList<selection>select_list; @Override PublicString Execute ()throwsException {Integer ID=integer.valueof (Servletactioncontext.getrequest (). GetParameter ("id")); Select_list=selectionservice.find (ID); This. Chart =Jfreechartservice.getchart (select_list); returnSUCCESS; } }
The corresponding service:
The note here is very easy to appear garbled, change the font on OK
PackageJfreechart;ImportJava.awt.Color;ImportJava.awt.Font;Importjava.util.List;Importorg.jfree.chart.ChartFactory;ImportOrg.jfree.chart.JFreeChart;ImportOrg.jfree.chart.axis.CategoryAxis;ImportOrg.jfree.chart.axis.ValueAxis;ImportOrg.jfree.chart.labels.ItemLabelAnchor;Importorg.jfree.chart.labels.ItemLabelPosition;ImportOrg.jfree.chart.labels.StandardCategoryItemLabelGenerator;ImportOrg.jfree.chart.plot.CategoryPlot;Importorg.jfree.chart.plot.PlotOrientation;ImportOrg.jfree.chart.renderer.category.BarRenderer;ImportOrg.jfree.data.category.DefaultCategoryDataset;ImportOrg.jfree.ui.TextAnchor;ImportVo. Selection; Public classJfreechartservice { PublicJfreechart Getchart (list<selection>select_list) {Defaultcategorydataset DataSet=NewDefaultcategorydataset (); for(inti = 0; I < select_list.size (); i + +) {Dataset.addvalue (Select_list.get (i). GetNumber (), Select_list.get (i). Getindex_ ()+"", Select_list.get (i). GetContent ()); } Jfreechart Chart= Chartfactory.createbarchart ("Poll result graph", Options, "Number of votes (a)", DataSet, Plotorientation.horizontal, false, false, false); Categoryplot plot=Chart.getcategoryplot (); //set the grid background colorPlot.setbackgroundpaint (Color.White); //set the grid vertical line colorPlot.setdomaingridlinepaint (Color.pink); //Set Grid horizontal colorPlot.setrangegridlinepaint (Color.pink); //displays the value of each bar and modifies the font properties of the numberBarrenderer renderer =NewBarrenderer (); Renderer.setbaseitemlabelgenerator (Newstandardcategoryitemlabelgenerator ()); Renderer.setbaseitemlabelsvisible (true); //The default number is displayed in the column, with the following two sentences to adjust the display of the number//Note: This sentence is very important, if there is no such sentence, the display of the number will be overwritten, give the number of the problem is not shownRenderer.setbasepositiveitemlabelposition (NewItemlabelposition (Itemlabelanchor.outside3, textanchor.baseline_right)); Renderer.setitemlabelanchoroffset (20D); Plot.setrenderer (renderer);
//The following paragraph is to prevent garbled Font Font=NewFont ("Arial", Font.Italic, 12); Categoryaxis Domainaxis= Plot.getdomainaxis ();//(X-axis of the histogram)Domainaxis.setticklabelfont (font);//set the font on x-axis coordinatesDomainaxis.setlabelfont (font);//set the font for headings on the x-axisValueaxis Valueaxis = Plot.getrangeaxis ();//(Y-axis of the bar chart)Valueaxis.setticklabelfont (font);//set the font on y-axis coordinatesValueaxis.setlabelfont (font);//set the font for headings on y-axis coordinateschart.gettitle (). SetFont (font); returnChart; } }
The final effect is:
Jfreechart Integrated Sturts2 Kind