开发环境:
eclipse Java EE IDE luna
struts2.3.20
maven3.2.5
pom.xml文件依赖配置
2.3.20 javax.servlet javax.servlet-api 3.0.1 provided org.apache.struts struts2-core ${struts.version} org.apache.struts struts2-jfreechart-plugin ${struts.version} junit junit 4.7 test jfree jfreechart 1.0.13
web.xml配置
Archetype Created Web Application struts-prepare org.apache.struts2.dispatcher.ng.filter.StrutsPrepareFilter struts-execute org.apache.struts2.dispatcher.ng.filter.StrutsExecuteFilter struts-prepare /* struts-execute /*
struts.xml配置
700 400 700 400 700 400
相关类:
package com.ouanui; import java.awt.Font; import org.jfree.chart.ChartFactory; import org.jfree.chart.JFreeChart; import org.jfree.chart.axis.CategoryAxis; import org.jfree.chart.axis.CategoryLabelPositions; import org.jfree.chart.axis.NumberAxis; import org.jfree.chart.plot.CategoryPlot; import org.jfree.chart.plot.PlotOrientation; import org.jfree.chart.title.LegendTitle; import org.jfree.chart.title.TextTitle; import org.jfree.data.category.CategoryDataset; import org.jfree.data.category.DefaultCategoryDataset; import com.opensymphony.xwork2.ActionSupport; public class BarChart3DAction extends ActionSupport { private static final long serialVersionUID = -493282687582801540L; private JFreeChart chart; public JFreeChart getChart() { chart = ChartFactory.createBarChart3D("学生成绩分析", "成绩", "人数", getDataset(), PlotOrientation.VERTICAL, true, false, false); chart.setTitle(new TextTitle("学生成绩分析", new Font("黑体", Font.ITALIC, 22))); LegendTitle legend = chart.getLegend(); // 修改图例的字体 legend.setItemFont(new Font("宋体", Font.ITALIC, 14)); CategoryPlot plot = (CategoryPlot) chart.getPlot(); // 取得横轴 CategoryAxis categoryAxis = plot.getDomainAxis(); categoryAxis.setLabelFont(new Font("宋体", Font.BOLD, 22)); // 分类标签以45度角倾斜 categoryAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45); categoryAxis.setTickLabelFont(new Font("宋体", Font.BOLD, 22)); // 取得纵轴 NumberAxis numberAxis = (NumberAxis) plot.getRangeAxis(); numberAxis.setLabelFont(new Font("宋体", Font.BOLD, 22)); return chart; } /** * * @return */ private CategoryDataset getDataset() { DefaultCategoryDataset dataset = new DefaultCategoryDataset(); dataset.addValue(2, "1班", "不及格"); dataset.addValue(4, "2班", "不及格"); dataset.addValue(5, "3班", "不及格"); dataset.addValue(8, "1班", "及格"); dataset.addValue(5, "2班", "及格"); dataset.addValue(10, "3班", "及格"); dataset.addValue(15, "1班", "中等"); dataset.addValue(10, "2班", "中等"); dataset.addValue(10, "3班", "中等"); dataset.addValue(15, "1班", "良好"); dataset.addValue(15, "2班", "良好"); dataset.addValue(15, "3班", "良好"); dataset.addValue(5, "1班", "优秀"); dataset.addValue(5, "2班", "优秀"); dataset.addValue(5, "3班", "优秀"); return dataset; } public void setChart(JFreeChart chart) { this.chart = chart; } }
package com.ouanui; import java.awt.Font; import org.jfree.chart.ChartFactory; import org.jfree.chart.JFreeChart; import org.jfree.chart.axis.NumberAxis; import org.jfree.chart.axis.ValueAxis; import org.jfree.chart.plot.XYPlot; import org.jfree.chart.title.LegendTitle; import org.jfree.chart.title.TextTitle; import org.jfree.data.time.Month; import org.jfree.data.time.TimeSeries; import org.jfree.data.time.TimeSeriesCollection; import org.jfree.data.xy.XYDataset; import com.opensymphony.xwork2.ActionSupport; public class LineChartAction extends ActionSupport { private static final long serialVersionUID = 1L; private JFreeChart chart; public JFreeChart getChart() { // title: // timeAxisLabel: // valueAxisLabel // dataset // legend:如果设置为 false,则在jsp页面不展示 // tooltips // urls chart = ChartFactory.createTimeSeriesChart("水果销量统计图", "水果", "销量", getDataSet(), true, false, false); // 重新设置图标标题,改变字体 chart.setTitle(new TextTitle("水果销量统计图", new Font("黑体", Font.ITALIC, 22))); // 取得统计图标的第一个图例 LegendTitle legend = chart.getLegend(0); // 修改图例的字体 legend.setItemFont(new Font("宋体", Font.BOLD, 14)); XYPlot plot = (XYPlot) chart.getPlot(); // 取得横轴 ValueAxis categoryAxis = plot.getDomainAxis(); // 设置横轴显示标签的字体 categoryAxis.setLabelFont(new Font("宋体", Font.BOLD, 22)); // x轴坐标样式 categoryAxis.setTickLabelFont(new Font("宋体", Font.BOLD, 10)); // 取得纵轴 NumberAxis numberAxis = (NumberAxis) plot.getRangeAxis(); // 设置纵轴显示标签的字体 numberAxis.setLabelFont(new Font("宋体", Font.BOLD, 22)); // // 显示y轴0值 // numberAxis.setAutoRangeIncludesZero(true); // 设置y轴取值范围 numberAxis.setRange(0, 5000); return chart; } public void setChart(JFreeChart chart) { this.chart = chart; } // 返回一个CategoryDataset实例 private static XYDataset getDataSet() { TimeSeries apple = new TimeSeries("苹果", null, null); apple.add(new Month(10, 2007), 3900); apple.add(new Month(11, 2007), 900); apple.add(new Month(12, 2007), 2500); apple.add(new Month(1, 2008), 3900); apple.add(new Month(2, 2008), 2000); apple.add(new Month(3, 2008), 3300); TimeSeries orange = new TimeSeries("桔子", null, null); orange.add(new Month(10, 2007), 3300); orange.add(new Month(11, 2007), 2680); orange.add(new Month(12, 2007), 2000); orange.add(new Month(1, 2008), 1900); orange.add(new Month(2, 2008), 2000); orange.add(new Month(3, 2008), 2300); TimeSeriesCollection dataset = new TimeSeriesCollection(); dataset.addSeries(apple); dataset.addSeries(orange); return dataset; } }
package com.ouanui; import java.awt.Font; import java.text.DecimalFormat; import java.text.NumberFormat; import org.jfree.chart.ChartFactory; import org.jfree.chart.JFreeChart; import org.jfree.chart.labels.StandardPieSectionLabelGenerator; import org.jfree.chart.plot.PiePlot3D; import org.jfree.chart.title.LegendTitle; import org.jfree.chart.title.TextTitle; import org.jfree.data.general.DefaultPieDataset; import com.opensymphony.xwork2.ActionSupport; public class PieChart3DAction extends ActionSupport { private static final long serialVersionUID = 2375583709245146510L; private JFreeChart chart; public JFreeChart getChart() { chart = ChartFactory.createPieChart3D("学生成绩分析", getDataset(), true, false, false); chart.setTitle(new TextTitle("学生成绩分析", new Font("黑体", Font.ITALIC, 22))); LegendTitle legend = chart.getLegend(); legend.setItemFont(new Font("宋体", Font.ITALIC, 14)); PiePlot3D plot = (PiePlot3D) chart.getPlot(); plot.setLabelFont(new Font("隶书", Font.ITALIC, 18)); // 设定背景透明度(0-1.0之间) plot.setBackgroundAlpha(0.9f); // 设定前景透明度(0-1.0之间) plot.setForegroundAlpha(0.50f); String unitStyle = "{0}={1}({2})"; // 设置图例显示样式 plot.setLabelGenerator(new StandardPieSectionLabelGenerator(unitStyle, NumberFormat.getNumberInstance(), new DecimalFormat("0.00%"))); // 设置引用标签显示样式 plot.setLegendLabelGenerator(new StandardPieSectionLabelGenerator(unitStyle, NumberFormat .getNumberInstance(), new DecimalFormat("0.00%"))); return chart; } public void setChart(JFreeChart chart) { this.chart = chart; } private DefaultPieDataset getDataset() { DefaultPieDataset dataset = new DefaultPieDataset(); dataset.setValue("不及格", 2); dataset.setValue("及格", 8); dataset.setValue("中等", 15); dataset.setValue("良好", 15); dataset.setValue("优秀", 5); dataset.setValue("优秀1", 5); return dataset; } }
jsp页面
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>Insert title here