[size=x-large]JFreeChart在struts2中实现柱状图 [/size]
BarChart.java:
在struts.xml中加入:
<include file="/jCuckoo/chart-struts.xml"></include>
现在就可以通过http://localhost:8080/项目名/jCuckoo/barChart.action进行访问了。
为了对柱状图更好的操作,可以通过jsp进行访问,在index.jsp中加入:
<img src="<%=path %>/jCuckoo/barChart.action">
通过访问http://localhost:8080/项目名/index.jsp就可以看到柱状图了。
[size=x-large]JFreeChart在struts2中实现饼状图[/size]
使用的是JFreeChart在struts2中的插件
1.将jfreechart的jar包放到项目中的lib文件夹下,servlet.jar和gnujaxp.jar不必放,第一个没有什么用处,因为tomcat中已有,第二个如果放进去了发布的时候可能会出现xml解析之类的错误,原因是由于你的SSH项目中可能已经有解析xml的jar文件了,产生冲突,这时首先:右击项目-->从configure build path中将其移除,再从lib下删除重新发布即可。
2.struts2与jfreechart的整合还需要struts2-jfreechart-plugin-2.1.8.jar,尽量下新版本的,这样查看其中的struts-plugin.xml,有extends="struts-default"。
3.假如出现找不到xwork下的LoggerFactory的异常,去下载一个高版本的xwork的jar文件就OK了。
好了,可以在struts2中制作图形报表了。
PieChartAction.java:
struts.xml中加入:
<include file="/jCuckoo/chart-struts.xml"></include>
好了,现在就可以通过http://localhost:8080/项目名/jCuckoo/pieChartAction.action进行访问了
如果想对生成的图进行更多的操作,可以通过jsp页面进行访问,new一个index.jsp
加入:<img src="<%=path %>/jCuckoo/pieChartAction.action">
然后就可以通过访问index页面看到饼状图了。
[size=x-large]
JFreeChart在struts2中实现折线图 [/size]
LineChart.java:
struts.xml中加入:
<include file="/jCuckoo/chart-struts.xml"></include>
好了,现在可以通过http://localhost:8080/项目名/jCuckoo/lineChart.action进行访问了
为了对折线图进行操作,可以通过jsp访问,在index.jsp中加入:
<img src="<%=path %>/jCuckoo/lineChart.action">
通过访问http://localhost:8080/项目名/index.jsp就可以看到折线图了。
BarChart.java:
public class BarChart extends ActionSupport {
private static final long serialVersionUID = 1L;
private JFreeChart chart;
public JFreeChart getChart(){
double[][] data = new double[][]
{
{ 672, 766, 223, 540, 126 },
{ 222, 540, 456, 678, 345 },
{ 512, 412, 621, 426, 532 },
};
String[] rowKeys = { "苹果","香蕉","梨子"};
String[] columnKeys = { "北京", "上海", "广州", "成都", "深圳" };
CategoryDataset dataset = DatasetUtilities.createCategoryDataset(rowKeys, columnKeys, data);
JFreeChart chart = ChartFactory.createBarChart3D("柱状图", "x轴", "y轴", dataset, PlotOrientation.VERTICAL, true, true, false);
//重新设置图标标题,改变字体
chart.setTitle(new TextTitle("柱状图", new Font("黑体", Font.ITALIC , 22)));
CategoryPlot plot = (CategoryPlot)chart.getPlot();
//取得横轴
CategoryAxis categoryAxis = plot.getDomainAxis();
//设置横轴显示标签的字体
categoryAxis.setLabelFont(new Font("宋体" , Font.BOLD , 18));
//分类标签以45度角倾斜
categoryAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45);
categoryAxis.setTickLabelFont(new Font("宋体" , Font.ITALIC , 15));
CategoryAxis domainAxis = plot.getDomainAxis();
//设置距离图片左端距离
domainAxis.setLowerMargin(0.1);
//设置距离图片右端距离
domainAxis.setUpperMargin(0.1);
BarRenderer3D renderer = new BarRenderer3D();
renderer.setItemMargin(0.1);//组内柱子间隔为组宽的10%
plot.setRenderer(renderer);//使用我们设计的效果
CategoryItemRenderer render =plot.getRenderer();
render.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
render.setBaseItemLabelsVisible(true);
//取得纵轴
NumberAxis numberAxis = (NumberAxis)plot.getRangeAxis();
//设置纵轴显示标签的字体
numberAxis.setLabelFont(new Font("宋体" , Font.BOLD , 18));
Font font00 = new Font("宋体",Font.BOLD,18);
LegendTitle legend = chart.getLegend();
legend.setItemFont(font00);//设置注释字体
return chart;
}
}
chart-struts.xml:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="jCuckoo" namespace="/jCuckoo"
extends="jfreechart-default">
<action name="barChart" class="jCuckoo.BarChart">
<result type="chart">
<param name="width">600</param>
<param name="height">450</param>
</result>
</action>
</package>
</struts>
在struts.xml中加入:
<include file="/jCuckoo/chart-struts.xml"></include>
现在就可以通过http://localhost:8080/项目名/jCuckoo/barChart.action进行访问了。
为了对柱状图更好的操作,可以通过jsp进行访问,在index.jsp中加入:
<img src="<%=path %>/jCuckoo/barChart.action">
通过访问http://localhost:8080/项目名/index.jsp就可以看到柱状图了。
[size=x-large]JFreeChart在struts2中实现饼状图[/size]
使用的是JFreeChart在struts2中的插件
1.将jfreechart的jar包放到项目中的lib文件夹下,servlet.jar和gnujaxp.jar不必放,第一个没有什么用处,因为tomcat中已有,第二个如果放进去了发布的时候可能会出现xml解析之类的错误,原因是由于你的SSH项目中可能已经有解析xml的jar文件了,产生冲突,这时首先:右击项目-->从configure build path中将其移除,再从lib下删除重新发布即可。
2.struts2与jfreechart的整合还需要struts2-jfreechart-plugin-2.1.8.jar,尽量下新版本的,这样查看其中的struts-plugin.xml,有extends="struts-default"。
3.假如出现找不到xwork下的LoggerFactory的异常,去下载一个高版本的xwork的jar文件就OK了。
好了,可以在struts2中制作图形报表了。
PieChartAction.java:
public class PieChartAction extends ActionSupport {
private static final long serialVersionUID = 5752180822913527064L;
private JFreeChart chart;
public String execute(){
//生成JFreeChart对象
chart = ChartFactory.createPieChart3D(
"饼状图", // 图表标题
getDataSet(), //数据
true, // 是否显示图例
false, //是否显示工具提示
false //是否生成URL
);
//重新设置图标标题,改变字体
chart.setTitle(new TextTitle("饼状图", new Font("黑体", Font.ITALIC , 22)));
//取得统计图标的第一个图例
LegendTitle legend = chart.getLegend(0);
//修改图例的字体
legend.setItemFont(new Font("宋体", Font.BOLD, 14));
//获得饼图的Plot对象
PiePlot plot = (PiePlot)chart.getPlot();
//设置饼图各部分的标签字体
plot.setLabelFont(new Font("隶书", Font.BOLD, 10));
//设定背景透明度(0-1.0之间)
plot.setBackgroundAlpha(0.9f);
//设定前景透明度(0-1.0之间)
plot.setForegroundAlpha(0.50f);
// 图片中显示百分比:自定义方式,{0} 表示选项, {1} 表示数值, {2} 表示所占比例 ,小数点后两位
plot.setLabelGenerator(new StandardPieSectionLabelGenerator("{0}:{1}({2})", NumberFormat.getNumberInstance(),
new DecimalFormat("0.00%")));
// 图例显示百分比:自定义方式, {0} 表示选项, {1} 表示数值, {2} 表示所占比例
plot.setLegendLabelGenerator(new StandardPieSectionLabelGenerator("{0}"));
return SUCCESS;
}
public DefaultPieDataset getDataSet(){
DefaultPieDataset data = new DefaultPieDataset();
data.setValue("Java", new Double(43.2));
data.setValue("Visual Basic", new Double(1.0));
data.setValue("C/C++", new Double(17.5));
data.setValue("tangjun书", new Double(60.0));
return data;
}
public JFreeChart getChart() {
return chart;
}
}
当然了,可以直接将execute方法去掉,其中的代码挪到getChart中,功能相同,访问时不用指定方法名。
chart-struts.xml:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="jCuckoo" namespace="/jCuckoo"
extends="jfreechart-default">
<action name="pieChartAction" class="jCuckoo.lee.PieChartAction">
<result type="chart">
<param name="width">600</param>
<param name="height">450</param>
</result>
</action>
</package>
</struts>
struts.xml中加入:
<include file="/jCuckoo/chart-struts.xml"></include>
好了,现在就可以通过http://localhost:8080/项目名/jCuckoo/pieChartAction.action进行访问了
如果想对生成的图进行更多的操作,可以通过jsp页面进行访问,new一个index.jsp
加入:<img src="<%=path %>/jCuckoo/pieChartAction.action">
然后就可以通过访问index页面看到饼状图了。
[size=x-large]
JFreeChart在struts2中实现折线图 [/size]
LineChart.java:
public class LineChart extends ActionSupport{
private static final long serialVersionUID = 1L;
JFreeChart chart;
public JFreeChart getChart() {
double[][] data = new double[][]
{
{ 672, 766, 223, 540, 126 },
{ 222, 540, 456, 678, 345 },
{ 512, 412, 621, 426, 532 },
};
String[] rowKeys = { "苹果","香蕉","梨子"};
String[] columnKeys = { "北京", "上海", "广州", "成都", "深圳" };
CategoryDataset dataset = getBarData(data, rowKeys, columnKeys);
createTimeXYChar("折线图", "x轴", "y轴", dataset, "");
return chart;
}
public CategoryDataset getBarData(double[][] data, String[] rowKeys,
String[] columnKeys)
{
return DatasetUtilities.createCategoryDataset(rowKeys, columnKeys, data);
}
public void createTimeXYChar(String chartTitle, String x, String y,
CategoryDataset xyDataset, String charName) {
chart = ChartFactory.createLineChart(chartTitle, x, y,
xyDataset, PlotOrientation.VERTICAL, true, true, false);
Font font00 = new Font("宋体",Font.BOLD,18);
LegendTitle legend = chart.getLegend();
legend.setItemFont(font00);//设置注释字体
chart.setTextAntiAlias(false);
// 设置图标题的字体重新设置title
Font font = new Font("隶书", Font.BOLD, 25);
TextTitle title = new TextTitle(chartTitle);
title.setFont(font);
chart.setTitle(title);
CategoryPlot categoryplot = (CategoryPlot) chart.getPlot();
// x轴分类轴网格是否可见
categoryplot.setDomainGridlinesVisible(true);
// y轴数据轴网格是否可见
categoryplot.setRangeGridlinesVisible(true);
categoryplot.setRangeGridlinePaint(Color.pink);// 虚线色彩
categoryplot.setDomainGridlinePaint(Color.pink);// 虚线色彩
categoryplot.setBackgroundPaint(Color.white);
// 设置轴和面板之间的距离
categoryplot.setAxisOffset(new RectangleInsets(0D, 0D, 0D, 0D));
CategoryAxis domainAxis = categoryplot.getDomainAxis();
domainAxis.setLabelFont(new Font("宋体" , Font.BOLD , 18));// 轴标题
domainAxis.setTickLabelFont(new Font("宋体" , Font.ITALIC , 15));// 轴数值
domainAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45); // 横轴上的
// 设置距离图片左端距离
domainAxis.setLowerMargin(0.1);
// 设置距离图片右端距离
domainAxis.setUpperMargin(0.1);
NumberAxis numberaxis = (NumberAxis) categoryplot.getRangeAxis();
numberaxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
numberaxis.setAutoRangeIncludesZero(true);
numberaxis.setLabelFont(new Font("宋体" , Font.BOLD , 18));
//设置最高的一个值与图片顶端的距离
numberaxis.setUpperMargin(0.15);
//设置最低的一个值与图片底端的距离
//numberaxis.setLowerMargin(0.15);
// 获得renderer
LineAndShapeRenderer lineandshaperenderer = (LineAndShapeRenderer) categoryplot.getRenderer();
lineandshaperenderer.setBaseShapesVisible(true); // series 点(即数据点)可见
lineandshaperenderer.setBaseLinesVisible(true); // series 点(即数据点)间有连线可见
// 显示折点数据
lineandshaperenderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
lineandshaperenderer.setBaseItemLabelsVisible(true);
}
}
chart-struts.xml:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="jCuckoo" namespace="/jCuckoo"
extends="jfreechart-default">
<action name="lineChart" class="jCuckoo.app.LineChart">
<result type="chart">
<param name="width">600</param>
<param name="height">450</param>
</result>
</action>
</package>
</struts>
struts.xml中加入:
<include file="/jCuckoo/chart-struts.xml"></include>
好了,现在可以通过http://localhost:8080/项目名/jCuckoo/lineChart.action进行访问了
为了对折线图进行操作,可以通过jsp访问,在index.jsp中加入:
<img src="<%=path %>/jCuckoo/lineChart.action">
通过访问http://localhost:8080/项目名/index.jsp就可以看到折线图了。