1. 3个jar包
jcommon,jfreechart,strust2-jfreechart-plugin
2
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> 3 <struts> 4 <package name="default2" namespace="/" extends="jfreechart-default,struts-default"> 5 <action name="resultAction" class="com.goshop.action.ShowResultAction" > 6 <result name="success" type="chart"> 7 <param name="width">400</param> 8 <param name="height">300</param> 9 </result> 10 </action> 11 </package> 12 </struts>
3.
1 package com.goshop.action; 2 3 import java.awt.Font; 4 import java.util.List; 5 6 import javax.annotation.Resource; 7 8 import org.jfree.chart.ChartFactory; 9 import org.jfree.chart.JFreeChart; 10 import org.jfree.chart.axis.CategoryAxis; 11 import org.jfree.chart.axis.NumberAxis; 12 import org.jfree.chart.plot.CategoryPlot; 13 import org.jfree.chart.plot.PlotOrientation; 14 import org.jfree.data.category.DefaultCategoryDataset; 15 import org.springframework.stereotype.Controller; 16 17 import com.goshop.domain.VoteOption; 18 import com.goshop.service.IVoteOptionService; 19 import com.opensymphony.xwork2.ActionSupport; 20 21 @Controller 22 public class ShowResultAction extends ActionSupport { 23 24 private int voteID; 25 private int channelID; 26 27 public int getVoteID() { 28 return voteID; 29 } 30 31 public void setVoteID(int voteID) { 32 this.voteID = voteID; 33 } 34 35 public int getChannelID() { 36 return channelID; 37 } 38 39 public void setChannelID(int channelID) { 40 this.channelID = channelID; 41 } 42 43 @Resource 44 private IVoteOptionService optionService; 45 private JFreeChart chart; 46 47 public void setChart(JFreeChart chart) { 48 this.chart = chart; 49 } 50 public JFreeChart getChart(){ 51 52 List<VoteOption> voteOptionList = optionService 53 .selectVoteOptionsByVoteId(voteID); 54 DefaultCategoryDataset ds = new DefaultCategoryDataset(); 55 for (VoteOption option : voteOptionList) { 56 ds.addValue(option.getTicketNum(), option.getVoteOptionName(), ""); 57 } 58 JFreeChart chart = ChartFactory.createBarChart("球星投票结果", "投票选项", "投票数", 59 ds, PlotOrientation.VERTICAL, true, false, false); 60 CategoryPlot categoryplot = (CategoryPlot) chart.getPlot(); 61 NumberAxis numberaxis = (NumberAxis) categoryplot.getRangeAxis(); 62 CategoryAxis domainAxis = categoryplot.getDomainAxis(); 63 64 /*------设置X轴坐标上的文字-----------*/ 65 domainAxis.setLabelFont(new Font("sans-serif", Font.PLAIN, 11)); 66 /*------设置X轴的标题文字------------*/ 67 domainAxis.setLabelFont(new Font("宋体", Font.PLAIN, 12)); 68 /*------设置Y轴坐标上的文字-----------*/ 69 numberaxis.setTickLabelFont(new Font("sans-serif", Font.PLAIN, 12)); 70 /*------设置Y轴的标题文字------------*/ 71 numberaxis.setLabelFont(new Font("黑体", Font.PLAIN, 12)); 72 73 /*------这句代码解决了底部汉字乱码的问题-----------*/ 74 chart.getLegend().setItemFont(new Font("宋体", Font.PLAIN, 12)); 75 /******* 这句代码解决了标题汉字乱码的问题 ********/ 76 /******* 这句代码解决了标题汉字乱码的问题 ********/ 77 chart.getTitle().setFont(new Font("宋体", Font.PLAIN, 12)); 78 79 return chart; 80 } 81 82 public String execute() throws Exception { 83 return "success"; 84 } 85 }