Struts整合JFreeChart

=========================================1==========================

/**
* 创建图表(Chart)
*
* @暂时只提供饼状3D图 柱状3D图
*/
public class ChartUtil {

static Font font = new Font("宋体",Font.BOLD, 22);

/**
* 创建饼状3D图
*
* @param map
* :类别map<"类别",类别的值>
* @param font
* :显示字体(系统要有的字体)
* @param title
* :图表标题
* @return
* @throws IOException
*/
public static JFreeChart createPie3DChart(HashMap<String, Integer> map,
String title) throws IOException {
DefaultPieDataset dpd = new DefaultPieDataset();
for (String k : map.keySet()) {
dpd.setValue(k, map.get(k));
}
JFreeChart chart = ChartFactory.createPieChart3D(title, dpd, true,
true, true);

// 》》》》》》》》》》》》以下为图表显示设置字体,中文的一定要设置,否则乱码《《《《《《《《《《《《《《

// 设置title
chart.setTitle(new TextTitle(title, font));

// 创建PieChart对象
PiePlot plot = (PiePlot) chart.getPlot();

// 设置标签
plot.setLabelFont(font);

// 设置项目说明
chart.getLegend().setItemFont(font);
return chart;
}

/**
* 创建柱状3D图
*
* @param map
* :类别map<"类别",类别的值>
* @param font
* :显示字体(系统要有的字体)
* @param title
* :图表标题
* @param x
* :图表横坐标名称
* @param y
* :图表纵坐标名称
* @return
* @throws IOException
*/
public static JFreeChart createBar3DChart(HashMap<String, Integer> map,
String title, String x, String y) throws IOException {
DefaultCategoryDataset dcd = new DefaultCategoryDataset();
for (String k : map.keySet()) {
dcd.setValue(map.get(k), k, k);
}
JFreeChart chart = ChartFactory.createBarChart3D(title, x, y, dcd,
PlotOrientation.VERTICAL, true, true, true);

// 》》》》》》》》》》》》以下为图表显示设置字体,中文的一定要设置,否则乱码《《《《《《《《《《《《《《

// 设置title
chart.setTitle(new TextTitle(title, font));

// 创建BarChart对象
CategoryPlot plot = (CategoryPlot) chart.getPlot();

// 设置标签
CategoryAxis categoryAxis = plot.getDomainAxis();

categoryAxis.setLabelFont(font);

categoryAxis.setTickLabelFont(font);

// 设置类别说明的倾斜度
categoryAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45);

NumberAxis numberAxis = (NumberAxis) plot.getRangeAxis();

numberAxis.setLabelFont(font);

chart.getLegend().setItemFont(font);

return chart;
}
}


===================================2================================
<!-- 图表test-->
<package name="test" extends="jfreechart-default">
<action name="test" class="testChartAction" method="chartR">
<result name="chartR" type="chart">
<param name="height">400</param>
<param name="width">600</param>
</result>
</action>
</package>


===================================3================================

/**
* chart应用演示
*/
@Controller("testCharAction")
@Scope("prototype")
public class TestChartAction {

// 固定名字
private JFreeChart chart;

public String chartR() {
HashMap<String, Integer> map = new HashMap<String, Integer>();
//模拟数据
map.put("同意", 4);
map.put("不同意", 5);
map.put("放弃", 1);

try {
// 创建柱状3D图
this.chart = ChartUtil.createBar3DChart(map, "测试", "项目", "结果");

// 创建饼状3D图
// this.chart = ChartUtil.createPie3DChart(map,"测试");

} catch (IOException e) {
e.printStackTrace();
}
return "chartR";
}

public JFreeChart getChart() {
return chart;
}
}

=================================4==================================
[img]http://dl.iteye.com/upload/attachment/380627/b323be9b-41d7-3904-80e5-44cc597636b3.jpg[/img]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值