JFreeChart练习

JFreeChart练习
[b]1,jsp页面[/b]
<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>
[b]2,struts.xml配置文件[/b]
<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>
[b]3,对应的action[/b]
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;
}
}
==============
个人学习
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值