亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

在Struts2中使用JFreeChar

系統(tǒng) 2578 0

1、在struts2中,默認(rèn)的struts-default.xml中,并沒有包含chart的result-type,它是插件的形式使用的。把 struts2的解壓包的lib里找到struts2-jfreechart-plugin-2.0.11.jar,拷貝到你的項(xiàng)目的 classpath里,同時(shí)在struts.xml里面增加一個(gè)chart的result-type。
Java代碼
<result-types>??
??? <result-type name="chart" class="org.apache.struts2.dispatcher.ChartResult"/>??
</result-types>??
<action name="ListAllFruit4Bar2" class="chart.ListAllFruit4Bar2Action">??
??? <result name="success" type="chart">??
??????? <param name="chart">chart</param>??
??????? <param name="height">400</param>??
??????? <param name="width">700</param>??
??? </result>??
</action>??
<action name="ListAllFruit4Pie2" class="chart.ListAllFruit4Pie2Action">??
??? <result name="success" type="chart">??
??????? <param name="chart">chart</param>??
??????? <param name="height">400</param>??
??????? <param name="width">700</param>??
??? </result>??
</action>?

<result-types>
<result-type name="chart" class="org.apache.struts2.dispatcher.ChartResult"/>
</result-types>
<action name="ListAllFruit4Bar2" class="chart.ListAllFruit4Bar2Action">
<result name="success" type="chart">
<param name="chart">chart</param>
<param name="height">400</param>
<param name="width">700</param>
</result>
</action>
<action name="ListAllFruit4Pie2" class="chart.ListAllFruit4Pie2Action">
<result name="success" type="chart">
<param name="chart">chart</param>
<param name="height">400</param>
<param name="width">700</param>
</result>
</action>

2、需要再添加jfreechart-1.0.9.jar和jcommon-1.0.12.jar(JasperReports和JFreeChart都需要的)到你的classpath里面。
3、在你的action代碼里,返回一個(gè)JFreeChart對象即可。
ListAllFruit4Bar2Action代碼:
Java代碼
package chart;??
?
import org.jfree.chart.JFreeChart;??
?
import com.opensymphony.xwork2.ActionSupport;??
?
public class ListAllFruit4Bar2Action extends ActionSupport {??
??? private static final long serialVersionUID = 1L;??
??? private JFreeChart chart;??
??????
??? @Override?
??? public String execute() throws Exception {??
??????? chart = FruitService.createBarChart();??
??????? return SUCCESS;??
??? }??
??????
??? public JFreeChart getChart() {??
??????? return chart;??
??? }??
}?

package chart;

import org.jfree.chart.JFreeChart;

import com.opensymphony.xwork2.ActionSupport;

public class ListAllFruit4Bar2Action extends ActionSupport {
private static final long serialVersionUID = 1L;
??? private JFreeChart chart;

@Override
public String execute() throws Exception {
chart = FruitService.createBarChart();
return SUCCESS;
}

public JFreeChart getChart() {
return chart;
}
}

ListAllFruit4Pie2Action代碼:
Java代碼
package chart;??
?
import org.jfree.chart.JFreeChart;??
?
import com.opensymphony.xwork2.ActionSupport;??
?
public class ListAllFruit4Pie2Action extends ActionSupport {??
??? private static final long serialVersionUID = 1L;??
??? private JFreeChart chart;??
??????
??? @Override?
??? public String execute() throws Exception {??
??????? chart = FruitService.createPaiChart();??
??????? return SUCCESS;??
??? }??
??????
??? public JFreeChart getChart() {??
??????? return chart;??
??? }??
}?

package chart;

import org.jfree.chart.JFreeChart;

import com.opensymphony.xwork2.ActionSupport;

public class ListAllFruit4Pie2Action extends ActionSupport {
private static final long serialVersionUID = 1L;
??? private JFreeChart chart;

@Override
public String execute() throws Exception {
chart = FruitService.createPaiChart();
return SUCCESS;
}

public JFreeChart getChart() {
return chart;
}
}


FruitService代碼:
Java代碼
package chart;??
?
import org.jfree.chart.ChartFactory;??
import org.jfree.chart.JFreeChart;??
import org.jfree.chart.plot.PlotOrientation;??
import org.jfree.data.category.CategoryDataset;??
import org.jfree.data.category.DefaultCategoryDataset;??
?
public class FruitService {??
?
??? public static JFreeChart createBarChart() {??
??????? CategoryDataset dataset = getDataSet2();??
??????? JFreeChart chart = ChartFactory.createBarChart3D(??
??????????????? "水果產(chǎn)量圖", // 圖表標(biāo)題??
??????????????? "水果", // 目錄軸的顯示標(biāo)簽??
??????????????? "產(chǎn)量", // 數(shù)值軸的顯示標(biāo)簽??
??????????????? dataset, // 數(shù)據(jù)集??
??????????????? PlotOrientation.VERTICAL, // 圖表方向:水平、垂直??
??????????????? true,?? // 是否顯示圖例(對于簡單的柱狀圖必須是false)??
??????????????? true,?? // 是否生成工具??
??????????????? true??? // 是否生成URL鏈接??
??????????????? );??
??????? return chart;??
??? }??
?
??? private static CategoryDataset getDataSet2() {??
??????? DefaultCategoryDataset dataset = new DefaultCategoryDataset();??
??????? dataset.addValue(100, "北京", "蘋果");??
??????? dataset.addValue(100, "上海", "蘋果");??
??????? dataset.addValue(100, "廣州", "蘋果");??
??????? dataset.addValue(200, "北京", "梨子");??
??????? dataset.addValue(200, "上海", "梨子");??
??????? dataset.addValue(200, "廣州", "梨子");??
??????? dataset.addValue(300, "北京", "葡萄");??
??????? dataset.addValue(300, "上海", "葡萄");??
??????? dataset.addValue(300, "廣州", "葡萄");??
??????? dataset.addValue(400, "北京", "香蕉");??
??????? dataset.addValue(400, "上海", "香蕉");??
??????? dataset.addValue(400, "廣州", "香蕉");??
??????? dataset.addValue(500, "北京", "荔枝");??
??????? dataset.addValue(500, "上海", "荔枝");??
??????? dataset.addValue(500, "廣州", "荔枝");??
??????? return dataset;??
??? }??
???????
??? public static JFreeChart createPaiChart() {??
??????? DefaultPieDataset data = getDataSet();??
??????? JFreeChart chart = ChartFactory.createPieChart3D("水果產(chǎn)量圖",? // 圖表標(biāo)題??
??????? data,???
??????? true, // 是否顯示圖例??
??????? false,??
??????? false?
??????? );??
??????? PiePlot plot = (PiePlot) chart.getPlot();??
??????? resetPiePlot(plot);??
??????? return chart;??
??? }??
??????
??? private static void resetPiePlot(PiePlot plot) {??
??????? String unitSytle = "{0}={1}({2})";??
??????????
??????? plot.setNoDataMessage("無對應(yīng)的數(shù)據(jù),請重新查詢。");??
??????? plot.setNoDataMessagePaint(Color.red);??
??????????
??????? //指定 section 輪廓線的厚度(OutlinePaint不能為null)??
??????? plot.setOutlineStroke(new BasicStroke(0));??
??????????
??????? //設(shè)置第一個(gè) section 的開始位置,默認(rèn)是12點(diǎn)鐘方向??
??????? plot.setStartAngle(90);???????????
?
??????? plot.setToolTipGenerator(new StandardPieToolTipGenerator(unitSytle,??
??????????????? NumberFormat.getNumberInstance(),??
??????????????? new DecimalFormat("0.00%")));??
??????????
??????? //指定圖片的透明度??
??????? plot.setForegroundAlpha(0.65f);??
??????????
??????? //引出標(biāo)簽顯示樣式??
??????? plot.setLabelGenerator(new StandardPieSectionLabelGenerator(unitSytle,??
??????????????? NumberFormat.getNumberInstance(),??
??????????????? new DecimalFormat("0.00%")));??
??????????????
??????? //圖例顯示樣式??
??????? plot.setLegendLabelGenerator(new StandardPieSectionLabelGenerator(unitSytle,??
??????????????? NumberFormat.getNumberInstance(),??
??????????????? new DecimalFormat("0.00%")));??
??? }??
?
??? private static DefaultPieDataset getDataSet() {??
??????? DefaultPieDataset dataset = new DefaultPieDataset();??
??????? dataset.setValue("蘋果",100);??
??????? dataset.setValue("梨子",200);??
??????? dataset.setValue("葡萄",300);??
??????? dataset.setValue("香蕉",400);??
??????? dataset.setValue("荔枝",500);??
??????? return dataset;??
??? }??
?
}?

package chart;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;

public class FruitService {

public static JFreeChart createBarChart() {
CategoryDataset dataset = getDataSet2();
JFreeChart chart = ChartFactory.createBarChart3D(
"水果產(chǎn)量圖", // 圖表標(biāo)題
"水果", // 目錄軸的顯示標(biāo)簽
"產(chǎn)量", // 數(shù)值軸的顯示標(biāo)簽
dataset, // 數(shù)據(jù)集
PlotOrientation.VERTICAL, // 圖表方向:水平、垂直
true, // 是否顯示圖例(對于簡單的柱狀圖必須是false)
true, // 是否生成工具
true // 是否生成URL鏈接
);
return chart;
}

private static CategoryDataset getDataSet2() {
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
dataset.addValue(100, "北京", "蘋果");
dataset.addValue(100, "上海", "蘋果");
dataset.addValue(100, "廣州", "蘋果");
dataset.addValue(200, "北京", "梨子");
dataset.addValue(200, "上海", "梨子");
dataset.addValue(200, "廣州", "梨子");
dataset.addValue(300, "北京", "葡萄");
dataset.addValue(300, "上海", "葡萄");
dataset.addValue(300, "廣州", "葡萄");
dataset.addValue(400, "北京", "香蕉");
dataset.addValue(400, "上海", "香蕉");
dataset.addValue(400, "廣州", "香蕉");
dataset.addValue(500, "北京", "荔枝");
dataset.addValue(500, "上海", "荔枝");
dataset.addValue(500, "廣州", "荔枝");
return dataset;
}
????
??? public static JFreeChart createPaiChart() {
DefaultPieDataset data = getDataSet();
JFreeChart chart = ChartFactory.createPieChart3D("水果產(chǎn)量圖",? // 圖表標(biāo)題
data,
true, // 是否顯示圖例
false,
false
);
PiePlot plot = (PiePlot) chart.getPlot();
resetPiePlot(plot);
return chart;
}

private static void resetPiePlot(PiePlot plot) {
String unitSytle = "{0}={1}({2})";

plot.setNoDataMessage("無對應(yīng)的數(shù)據(jù),請重新查詢。");
plot.setNoDataMessagePaint(Color.red);

//指定 section 輪廓線的厚度(OutlinePaint不能為null)
plot.setOutlineStroke(new BasicStroke(0));

//設(shè)置第一個(gè) section 的開始位置,默認(rèn)是12點(diǎn)鐘方向
plot.setStartAngle(90);

plot.setToolTipGenerator(new StandardPieToolTipGenerator(unitSytle,
NumberFormat.getNumberInstance(),
new DecimalFormat("0.00%")));

//指定圖片的透明度
plot.setForegroundAlpha(0.65f);

//引出標(biāo)簽顯示樣式
plot.setLabelGenerator(new StandardPieSectionLabelGenerator(unitSytle,
NumberFormat.getNumberInstance(),
new DecimalFormat("0.00%")));

//圖例顯示樣式
plot.setLegendLabelGenerator(new StandardPieSectionLabelGenerator(unitSytle,
NumberFormat.getNumberInstance(),
new DecimalFormat("0.00%")));
}

private static DefaultPieDataset getDataSet() {
DefaultPieDataset dataset = new DefaultPieDataset();
dataset.setValue("蘋果",100);
dataset.setValue("梨子",200);
dataset.setValue("葡萄",300);
dataset.setValue("香蕉",400);
dataset.setValue("荔枝",500);
return dataset;
}

}

4、同時(shí)在jsp頁面里顯示--柱圖和餅圖。
增加一個(gè)ListAllFruit4BarAndPie的action:
Java代碼
<action name="ListAllFruit4BarAndPie" class="">??
??? <result name="success" type="dispatcher">/WEB-INF/chart/ListAllFruit4BarAndPie.jsp</result>??
</action>?

<action name="ListAllFruit4BarAndPie" class="">
<result name="success" type="dispatcher">/WEB-INF/chart/ListAllFruit4BarAndPie.jsp</result>
</action>

ListAllFruit4BarAndPie.jsp代碼:
Java代碼
<%@ page contentType="text/html; charset=UTF-8" %>??
<html>??
<head>??
??? <title>ListAllFruit4BarAndPie</title>??
</head>??
<body>??
??? <center><h2>使用柱圖和餅圖列取所有水果產(chǎn)量</h2></center>??
??? 柱圖:<img src="chart/ListAllFruit4Bar2.action"><br/>??
??? 餅圖:<img src="chart/ListAllFruit4Pie2.action">??
</body>??
</html>?

<%@ page contentType="text/html; charset=UTF-8" %>
<html>
<head>
??? <title>ListAllFruit4BarAndPie</title>
</head>
<body>
<center><h2>使用柱圖和餅圖列取所有水果產(chǎn)量</h2></center>
柱圖:<img src="chart/ListAllFruit4Bar2.action"><br/>
餅圖:<img src="chart/ListAllFruit4Pie2.action">
</body>
</html>


????????????
生成的HTML的效果圖如下:?
? 在Struts2中使用JFreeChar

?文章出處: http://liyanboss.iteye.com/blog/181282

在Struts2中使用JFreeChar


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯(lián)系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長非常感激您!手機(jī)微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發(fā)表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 黑人欧美一级毛片 | 女性特黄一级毛片 | 一级女人18毛片免费 | 特级黄一级播放 | 久久精品人人做人人看最新章 | 亚洲人成依人成综合网 | 亚洲欧美日韩成人网 | 呦系列视频一区二区三区 | 老司机精品视频个人在观看 | 最新国产精品精品视频 | 亚洲色啦啦狠狠网站 | 99热久久国产这里有只有精品 | 色综合久久一区二区三区 | 色偷偷亚洲天堂 | 国产极品嫩模大尺度福利视频 | 久久国产精品视频 | 他也色在线 | 日韩欧国产精品一区综合无码 | ass曰本人乱妇ass | 91探花国产综合在线精品 | 欧美日本黄色 | 四虎国产精品4hu永久 | 日日操夜夜爽 | 337p粉嫩大胆色噜噜噜 | 国产操比 | 国产精品日本一区二区在线看 | 色精品视频 | 精品国产免费一区二区三区五区 | 99热国产免费 | 国产动作大片中文字幕 | 中文字幕日韩亚洲 | 四虎影视永久在线 | 亚洲精品国产第一区二区多人 | 99爱免费观看视频在线 | 97超级碰碰碰碰精品 | 国产精品成人在线播放 | 国产成人亚洲欧美激情 | 免费一级大毛片a一观看不卡 | 伊人久久成人爱综合网 | 国产精品久久久视频 | 欧美国产精品日韩在线 |