Struts2整合JFreeChart图表说明


生成3D饼图
public class PieChartDemo
{
public static void main(String[] args) throws IOException
{
DefaultPieDataset data = getDataSet();
//JFreeChart chart = ChartFactory.createPieChart(
//生成3D饼图
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, 18));
//设定背景透明度(0-1.0之间)
plot.setBackgroundAlpha(0.9f);
//设定前景透明度(0-1.0之间)
plot.setForegroundAlpha(0.50f);
FileOutputStream fos = new FileOutputStream("book.jpg");
ChartUtilities.writeChartAsJPEG(
fos, //输出到哪个输出流
1, //JPEG图片的质量,0~1之间
chart, //统计图标对象
800, //宽
600,//宽
null //ChartRenderingInfo 信息
);
fos.close();
}
private static DefaultPieDataset getDataSet()
{
DefaultPieDataset dataset = new DefaultPieDataset();
dataset.setValue("J2ME嵌入式开发",47000);
dataset.setValue("J2EE web应用开发",38000);
dataset.setValue("基于J2EE的Ajax开发",31000);
dataset.setValue("JavaScript权威指南",29000);
dataset.setValue("J2SE应用开发",25000);
return dataset;
}
}

LineChartDemo
public class LineChartDemo
{
public static void main(String[] args) throws IOException
{
JFreeChart chart = ChartFactory.createTimeSeriesChart(
"水果销量统计图", // 图表标题
"水果", // 目录轴的显示标签
"销量", // 数值轴的显示标签
getDataSet(), // 数据集
//PlotOrientation.HORIZONTAL , // 图表方向:水平
//PlotOrientation.VERTICAL , // 图表方向:垂直
true, // 是否显示图例(对于简单的柱状图必须是false)
false, // 是否生成工具
false // 是否生成URL链接
);
//重新设置图标标题,改变字体
chart.setTitle(new TextTitle("水果销量统计图", new Font("黑体", Font.ITALIC , 22)));
//取得统计图标的第一个图例
LegendTitle legend = chart.getLegend(0);
//修改图例的字体
legend.setItemFont(new Font("宋体", Font.BOLD, 14));
XYPlot plot = (XYPlot)chart.getPlot();
//取得横轴
ValueAxis categoryAxis = plot.getDomainAxis();
//设置横轴显示标签的字体
categoryAxis.setLabelFont(new Font("宋体" , Font.BOLD , 22));
categoryAxis.setTickLabelFont(new Font("宋体" , Font.BOLD , 18));
//取得纵轴
NumberAxis numberAxis = (NumberAxis)plot.getRangeAxis();
//设置纵轴显示标签的字体
numberAxis.setLabelFont(new Font("宋体" , Font.BOLD , 22));
FileOutputStream fos = null;
fos = new FileOutputStream("fruitLine.jpg");
//将统计图标输出成JPG文件
ChartUtilities.writeChartAsJPEG(
fos, //输出到哪个输出流
1, //JPEG图片的质量,0~1之间
chart, //统计图标对象
800, //宽
600,//宽
null //ChartRenderingInfo 信息
);
fos.close();
}
//返回一个CategoryDataset实例
private static XYDataset getDataSet()
{
TimeSeries apple =new TimeSeries("苹果",Month.class);
apple.add(new Month(10,2007),3900);
apple.add(new Month(11,2007),900);
apple.add(new Month(12,2007),2500);
apple.add(new Month(1,2008),3900);
apple.add(new Month(2,2008),2000);
apple.add(new Month(3,2008),3300);
TimeSeries orange=new TimeSeries("桔子",Month.class);
orange.add(new Month(10,2007),3300);
orange.add(new Month(11,2007),2680);
orange.add(new Month(12,2007),2000);
orange.add(new Month(1,2008),1900);
orange.add(new Month(2,2008),2000);
orange.add(new Month(3,2008),2300);
TimeSeriesCollection dataset=new TimeSeriesCollection();
dataset.addSeries(apple);
dataset.addSeries(orange);
return dataset;
}
}


配置struts2,web.xml
<!-- 定义Struts2的FilterDispathcer的Filter -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<!-- FilterDispatcher用来初始化struts2并且处理所有的WEB请求。 -->
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

struts.xml
<package name="jCuckoo" namespace="" extends="jfreechart-default">//此处需要注意
<action name="bookChart" class="jCuckoo.ChartAction">
<result type="chart">
<param name="width">600</param>
<param name="height">450</param>
</result>
</action>
<action name="barChart3Dt" class="jCuckoo.BarChart3DAction">
<result type="chart">
<param name="width">600</param>
<param name="height">450</param>
</result>
</action>
</package>

同时修改struts2-jfreechart-plugin-2.0.14.jar中的struts2-jfreechart-plugin-2.0.14.jar
<package name="jfreechart-default" extends="struts-default">//此处注意
<result-types>
<result-type name="chart" class="org.apache.struts2.dispatcher.ChartResult">
<param name="height">150</param>
<param name="width">200</param>
</result-type>
</result-types>
</package>

ChartAction
public class ChartAction extends ActionSupport {
private JFreeChart chart;
public JFreeChart getChart()
{
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, 18));
//设定背景透明度(0-1.0之间)
plot.setBackgroundAlpha(0.9f);
//设定前景透明度(0-1.0之间)
plot.setForegroundAlpha(0.50f);
return chart;
}
private DefaultPieDataset getDataSet()
{
DefaultPieDataset dataset = new DefaultPieDataset();
dataset.setValue("J2ME嵌入式开发",47000);
dataset.setValue("J2EE web应用开发",38000);
dataset.setValue("基于J2EE的Ajax开发",31000);
dataset.setValue("JavaScript权威指南",29000);
dataset.setValue("J2SE应用开发",25000);
return dataset;
}
}


BarChart3DAction
public class BarChart3DAction extends ActionSupport
{
private JFreeChart chart;
public JFreeChart getChart(){
chart = ChartFactory.createBarChart3D(
"图书销量统计图", // 图表标题
"图书", // 目录轴的显示标签
"销量", // 数值轴的显示标签
getDataSet(), // 数据集
//PlotOrientation.HORIZONTAL , // 图表方向:水平
PlotOrientation.VERTICAL , // 图表方向:垂直
false, // 是否显示图例(对于简单的柱状图必须是false)
false, // 是否生成工具
false // 是否生成URL链接
);
//重新设置图标标题,改变字体
chart.setTitle(new TextTitle("图书销量统计图", new Font("黑体", Font.ITALIC , 22)));
CategoryPlot plot = (CategoryPlot)chart.getPlot();
//取得横轴
CategoryAxis categoryAxis = plot.getDomainAxis();
//设置横轴显示标签的字体
categoryAxis.setLabelFont(new Font("宋体" , Font.BOLD , 22));
//分类标签以45度角倾斜
categoryAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45);
categoryAxis.setTickLabelFont(new Font("宋体" , Font.BOLD , 18));
//取得纵轴
NumberAxis numberAxis = (NumberAxis)plot.getRangeAxis();
//设置纵轴显示标签的字体
numberAxis.setLabelFont(new Font("宋体" , Font.BOLD , 22));
return chart;
}
//返回一个CategoryDataset实例
private CategoryDataset getDataSet()
{
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
dataset.addValue(47000 , "" , "J2ME嵌入式开发");
dataset.addValue(38000 , "" , "J2EE web应用开发");
dataset.addValue(31000 , "" , "基于J2EE的Ajax开发");
dataset.addValue(29000 , "" , "JavaScript权威指南");
dataset.addValue(25000 , "" , "J2SE应用开发");
return dataset;
}


index.jsp
<body>
<img src="bookChart.action"/>
<img src="barChart3Dt.action"/>
</body>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值