JFreeChart练习

JFreeChart练习
1,jsp页面
  <body>
<h3>请选择喜欢的项目</h3>
<s:form action="viewResult">
<s:checkbox name="interest" label="足球" fieldValue="football" labelposition="left"/>
<s:checkbox name="interest" label="篮球" fieldValue="basketball" labelposition="left"/>
<s:checkbox name="interest" label="排球" fieldValue="volleyball" labelposition="left"/>
<s:checkbox name="interest" label="羽毛球" fieldValue="badminton" labelposition="left"/>
<!--
<s:checkboxlist list="#{'computer':'计算机','math':'数学'}" name="interest" label="课程" labelposition="top"></s:checkboxlist>
-->
<s:submit value="submit"/>
</s:form>
 
  </body>
2,struts.xml配置文件
<action name="viewResult" class="com.test.action.ViewResultAction">
<result name="success" type="chart">
    <param name="height">450</param>
    <param name="width">500</param>
</result>
</action>
3,对应的action
package com.test.action;

import java.awt.Font;
import java.util.List;
import java.util.Map;

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.TextTitle;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class ViewResultAction extends ActionSupport {

private List<String> interest;

private JFreeChart chart;

public JFreeChart getChart() {

chart = ChartFactory.createBarChart3D("兴趣统计结果", "项目", "结果",
getDateset(), PlotOrientation.VERTICAL, true, true, false);

chart.setTitle(new TextTitle("兴趣统计结果",new Font("宋体",Font.BOLD,12)));

CategoryPlot plot = chart.getCategoryPlot();
//垂直轴
CategoryAxis axis = plot.getDomainAxis();
axis.setLabelFont(new Font("宋体",Font.BOLD,12));
axis.setTickLabelFont(new Font("宋体",Font.BOLD,12));
axis.setCategoryLabelPositions(CategoryLabelPositions.UP_45);
//水平轴
NumberAxis numberAxis = (NumberAxis)plot.getRangeAxis();
numberAxis.setLabelFont(new Font("宋体",Font.BOLD,12));

return chart;
}

// public void setChart(JFreeChart chart) {
// this.chart = chart;
// }
public List<String> getInterest() {
return interest;
}

public void setInterest(List<String> interest) {
this.interest = interest;
}

@Override
public String execute() throws Exception {

return SUCCESS;
}

@SuppressWarnings("unchecked")
private void increaseResult(List<String> list) {
// 像调用数据库一样//将最新的数据放在Application中
ActionContext context = ActionContext.getContext();
Map map = context.getApplication();

for (String str : list) {

if (null == map.get(str)) {
map.put(str, 1);// 自动装箱,复习一下
} else {
map.put(str, (Integer) map.get(str) + 1);
}
}
}
@SuppressWarnings("unchecked")
private CategoryDataset getDateset() {
DefaultCategoryDataset dataSet = new DefaultCategoryDataset();
this.increaseResult(this.getInterest());
//得到application中的数据并放在数据集里面
ActionContext context = ActionContext.getContext();
Map map = context.getApplication();

dataSet.setValue((Integer) map.get("football"), "", "足球");
dataSet.setValue((Integer) map.get("basketball"), "", "篮球");
dataSet.setValue((Integer) map.get("volleyball"), "", "排球");
dataSet.setValue((Integer) map.get("badminton"), "", "羽毛球");

return dataSet;
}
}
==============
个人学习

你可能感兴趣的:(xml,jsp,struts,jfreechart,UP)