1: Import Rack Package Jfreechart-1.0.9.jar
2: Write action
Packageactions;Importjava.util.List;Importorg.jfree.chart.ChartFactory;ImportOrg.jfree.chart.JFreeChart;Importorg.jfree.chart.plot.PlotOrientation;ImportOrg.jfree.data.category.DefaultCategoryDataset;ImportBean. Vote;ImportBean. voteoption;ImportCom.opensymphony.xwork2.ActionSupport;ImportDAO. Votedao;ImportDAO. Voteoptiondao;Importdaofactory.votedaofactory;Importdaofactory.voteoptiondaofactory; Public classVoteresultactionextendsActionsupport {PrivateJfreechart Chart; Private intVoteid;//Poll ID Public intGetvoteid () {returnVoteid; } Public voidSetvoteid (intVoteid) { This. Voteid =Voteid; } PublicJfreechart Getchart () {Votedao Votedao= Votedaofactory.getvotedaoinstance ();//Get Votedao InstancesVoteoptiondao Voteoptiondao =voteoptiondaofactory.getvoteoptiondaoinstance ();//Get Voteoption Instances//voting by vote IDVote Vote =Votedao.findvotebyid (Voteid); String Votename= Vote.getvotename ();//get the voting name//get all voting options based on the voting IDList<voteoption> voteoptions =Voteoptiondao.findvoteoptionbyvoteid (Voteid); Defaultcategorydataset DCD=NewDefaultcategorydataset ();//Data Source//Setting up data for(voteoption voteoption:voteoptions) {Dcd.setvalue (Voteoption.getticketnum (),"", Voteoption.getvoteoptionname ()); } //using the factory class to create a bar chartJfreechart chart =Chartfactory.createbarchart3d (Votename,"Voting Options", "Number of votes", DCD, plotorientation.vertical,false, true, false); returnChart; } Public voidSetchart (Jfreechart chart) { This. Chart =Chart; } PublicString Execute ()throwsException {returnSUCCESS; }}
Configuration information:
<Actionname= "Voteresult"class= "actions. Voteresultaction "> <resultname= "Success"type= "Chart"> <paramname= "width">400</param> <paramname= "height">300</param> </result> </Action>
Integration of Struts-jfreechart