我用的是struts2.1.6,和jfreechart1.0.12
大家可以去
http://struts.apache.org/download.cgi#struts216(下載struts2相關jar文件)
http://sourceforge.net/projects/jfreechart/files/(下載jfreechart和jcommon和相關jar文件)
好了,當你都下載完了之後,裏面的jar不是必須的,我們只需要導入一部分即可
- struts2-core-2.1.6.jar
- freemarker-2.3.13.jar
- xwork-2.1.2.jar
- ognl-2.6.11.jar
- commons-fileupload-1.2.1.jar
- commons-logging-1.0.4.jar
- struts2-jfreechart-plugin-2.1.6.jar
- jcommon-1.0.15.jar
- jfreechart-1.0.12.jar
前6個是struts2所需要的jar文件,如果大家用的不是struts2.1.6的話,commons-fileupload-1.2.1.jar可以不用導入
我們要實現的效果就是,在一個頁面上面用一個img標籤顯示jfreechart生成的圖片
1.jsp頁面代碼非常簡單
- <body>
- <img alt="jfreechart" src="jfreechart.action"/>
- </body>
- <body>
- <img alt="jfreechart" src="jfreechart.action"/>
- </body>
就是一個img標籤,src連接一個action
2.我們就寫jfreechart代碼,在這裏我也是寫一個非常簡單的例子,就畫一個餅圖
- public class JfreeChartTest {
- public static JFreeChart createChart() throws IOException {
- // 數據集
- DefaultPieDataset dpd = new DefaultPieDataset();
- dpd.setValue("管理人員", 25);
- dpd.setValue("市場人員", 25);
- dpd.setValue("開發人員", 45);
- dpd.setValue("其它人員", 10);
- // 創建PieChart對象
- JFreeChart chart = ChartFactory.createPieChart3D("某公司人員組織結構圖", dpd,
- true, true, false);
- utils.setFont(chart);
- return chart;
- }
- }
- /**
- * 設置字體
- *
- * @author zyong
- *
- */
- class utils {
- public static void setFont(JFreeChart chart) {
- Font font = new Font("宋體", Font.ITALIC, 12);
- PiePlot plot = (PiePlot) chart.getPlot();
- chart.getTitle().setFont(font);
- plot.setLabelFont(font);
- chart.getLegend().setItemFont(font);
- }
- }
- public class JfreeChartTest {
- public static JFreeChart createChart() throws IOException {
- // 數據集
- DefaultPieDataset dpd = new DefaultPieDataset();
- dpd.setValue("管理人員", 25);
- dpd.setValue("市場人員", 25);
- dpd.setValue("開發人員", 45);
- dpd.setValue("其它人員", 10);
- // 創建PieChart對象
- JFreeChart chart = ChartFactory.createPieChart3D("某公司人員組織結構圖", dpd,
- true, true, false);
- utils.setFont(chart);
- return chart;
- }
- }
- /**
- * 設置字體
- *
- * @author zyong
- *
- */
- class utils {
- public static void setFont(JFreeChart chart) {
- Font font = new Font("宋體", Font.ITALIC, 12);
- PiePlot plot = (PiePlot) chart.getPlot();
- chart.getTitle().setFont(font);
- plot.setLabelFont(font);
- chart.getLegend().setItemFont(font);
- }
- }
3.編寫action
- @SuppressWarnings("serial")
- public class JfreeCharAction extends ActionSupport {
- /**
- * 定義JFreeChart對象 大家請注意在這裏JFreeChart對象名只能爲chart
- * 不能是別的
- * 關於這點
- * 大家可以上struts2網站上面查看一下
- *
- * http://struts.apache.org/2.x/docs/jfreechart-plugin.html
- */
- private JFreeChart chart;
- public JFreeChart getChart() {
- return chart;
- }
- public void setChart(JFreeChart chart) {
- this.chart = chart;
- }
- @Override
- public String execute() throws Exception {
- // 調用方法
- this.chart = JfreeChartTest.createChart();
- return SUCCESS;
- }
- }
- @SuppressWarnings("serial")
- public class JfreeCharAction extends ActionSupport {
- /**
- * 定義JFreeChart對象 大家請注意在這裏JFreeChart對象名只能爲chart
- * 不能是別的
- * 關於這點
- * 大家可以上struts2網站上面查看一下
- *
- * http://struts.apache.org/2.x/docs/jfreechart-plugin.html
- */
- private JFreeChart chart;
- public JFreeChart getChart() {
- return chart;
- }
- public void setChart(JFreeChart chart) {
- this.chart = chart;
- }
- @Override
- public String execute() throws Exception {
- // 調用方法
- this.chart = JfreeChartTest.createChart();
- return SUCCESS;
- }
- }
4.編寫struts.xml
- <struts>
- <!--
- 關於extends繼承jfreechart-default這點請大家注意
- 因爲在struts-default這個包裏並沒有result-type爲chart的
- chart 定義在前面我們導入的struts2-jfreechart-plugin-2.1.6.jar
- 下面的struts-plugin.xml文件中
- -->
- <package name="jfreechart" extends="jfreechart-default">
- <action name="jfreechart" class="jfreechartAction">
- <result name="success" type="chart">
- <param name="width">600</param>
- <param name="height">400</param>
- </result>
- </action>
- </package>
- </struts>
- <struts>
- <!--
- 關於extends繼承jfreechart-default這點請大家注意
- 因爲在struts-default這個包裏並沒有result-type爲chart的
- chart 定義在前面我們導入的struts2-jfreechart-plugin-2.1.6.jar
- 下面的struts-plugin.xml文件中
- -->
- <package name="jfreechart" extends="jfreechart-default">
- <action name="jfreechart" class="jfreechartAction">
- <result name="success" type="chart">
- <param name="width">600</param>
- <param name="height">400</param>
- </result>
- </action>
- </package>
- </struts>
5.大家把上面的工作都做好了之後,就可以開始服務器,運行一下就可以看見自己的成果