一、cewolf 产生图形的流程
创建一个数据源(dataset)来包含将要在图形中显示的数据?????>><cewolf:chart/>标签对图形进行调整
??????>><cewolf:img/>标签把图形输出
1、创建一个数据源(dataset)
创建数据源基本上和上面一样,所不同的是 cewolf 对其重新进行了包装,它提供了一个DatasetProducer 接口,你需要
实现这一接口,下面是一个例子
DatasetProducer timeData = new DatasetProducer() {
public Object produceDataset(Map params) { //cewolf 对其重新进行了包装
TimeSeries ts = new TimeSeries("Cewolf Release Schedule", Month.class);//怎么样?和上面一样吧
ts.add(new Month(7, 2002), 0.1);
ts.add(new Month(8, 2002), 0.4);
ts.add(new Month(9, 2002), 0.9);
ts.add(new Month(10, 2002), 1.0);
return new TimeSeriesCollection(ts);
}
public String getProducerId() { //返回唯一的ID
return "TimeDataProducer";
}
public boolean hasExpired(Map params, Date since) { //默认就好
return false;
}
};
pageContext.setAttribute("timeData", timeData); //产生完以后要把它放到页面中保存以给 cewolf标签调用
2、<cewolf:chart/>标签
<cewolf:chart id="timeChart" //这个id要唯一,给下边<cewolf:img/>标签引用
title="TimeSeries" //图形的标题
type="timeseries" //图形的类型
xaxislabel="x-values" //横轴 Lable
yaxislabel="y-values"> //纵轴Lable
<cewolf:colorpaint color="#EEEEFF"/> //图形的背景色
<cewolf:data>
<cewolf:producer id="timeData"/> //引用上面产生的数据
</cewolf:data>
</cewolf:chart>
一些说明:
关于图形的背景色,还有两个选择,分别是
<cewolf:gradientpaint> //产生色彩倾斜的背景
<cewolf:point x="0" y="0" color="#AAAAFFEE" />
<cewolf:point x="300" y="0" color="#DDDDFF" />
</cewolf:gradientpaint>
和
<cewolf:texturepaint image="/img/bg.jpg" width="60" height="60" /> //加入背景图案
3、<cewolf:img/>标签
<cewolf:img chartid="timeChart" //就是上面那个 id
renderer="/cewolf" //这个是必需的!web.xml 中有配置
width="300" //宽
height="300" /> //高
还有一种图形输出方式:
<img src='<cewolf:imgurl chartid="foobar" renderer="/cewolf" width="100" height="100"/>'>
很明显,这种方式把图形包含到 Html 的<img/>中
4、进一步调整
看到上面的步骤,你可能会认为用 cewolf 输出图形是如此的简单,是这样的,但看了最开始的 jFreeChart 对图形
的一些调整,你会想我如何调整呢?我不想用默认值,我想输出更复杂的图形。很好,我是这样做的:
这里要介绍一个新的接口ChartPostProcessor 和一个标签<cewolf:chartpostprocessor/>
我们要写自己的类实现这个ChartPostProcessor 接口,然后再用标签<cewolf:chartpostprocessor/>调用我们所写
的类
ChartPostProcessor dataColor = new ChartPostProcessor() {
public void processChart(Object chart, Map params) { //这个接口就这一个方法
CategoryPlot plot = (CategoryPlot) ((JFreeChart) chart).getPlot(); //看到了什么??!!对,获得了plot!
下面就可以通过plot 对图形进行调整!!
plot.setAxisOffset(new RectangleInsets(5D, 5D, 5D, 5D)); //一个实验,坐标轴与图分离
for (int i = 0; i < params.size(); i++) { //这里的params 是通过标签<cewolf:chartpostprocessor/>输入的!
String colorStr = (String) params.get(String.valueOf(i));
plot.getRenderer().setSeriesPaint(i, java.awt.Color.decode(colorStr));//看到 renderer了吧,又可以大
干一场了,嘿嘿!
}
}
};
pageContext.setAttribute("dataColor", dataColor); //记得要放起来噢
具体引用:
<cewolf:chart id="stackedHorizontalBar" title="StackedHorizontalBar"
type="stackedHorizontalBar" xaxislabel="Fruit" yaxislabel="favorite">
<cewolf:data>
<cewolf:producer id="categoryData" />
</cewolf:data>
<cewolf:chartpostprocessor id="dataColor"> //在这里!!
<cewolf:param name="0" value='<%= "#FFFFAA" %>'/>
<cewolf:param name="1" value='<%= "#AAFFAA" %>'/>
<cewolf:param name="2" value='<%= "#FFAAFF" %>'/>
<cewolf:param name="3" value='<%= "#FFAAAA" %>'/>
</cewolf:chartpostprocessor>
</cewolf:chart>
我们当然也可以在实现ChartPostProcessor 接口的类里把一切都做好,然后这么用:
<cewolf:chartpostprocessor id="dataColor"/>
Cewolf生成图片和说明时,有两种方式,一种是以一幅图显示:
另外一种是分开显示
其他说明:
//设置Legend的位置
//((JFreeChart) chart).getLegend().setPosition(RectangleEdge.RIGHT);
//设置最高的一个 Item 与图片顶端的距离
plot.getRangeAxis().setUpperMargin(0.15);
//设置最低的一个 Item 与图片底端的距离
plot.getRangeAxis().setLowerMargin(0.15);
//坐标轴字体
plot.getDomainAxis().setLabelFont(new Font("宋体", Font.PLAIN, 12));
//横轴每个分类的字体
plot.getDomainAxis().setTickLabelFont(new Font("宋体", Font.BOLD, 12));
if(labelPositionsUP_45)
plot.getDomainAxis().setCategoryLabelPositions(CategoryLabelPositions.UP_45);
//柱图列宽度((BarRenderer) plot.getRenderer()).setMaximumBarWidth(barWidth);
因为cewolf的tag中不能使用el,所以设置一个零时变量。
创建bean对象,用来修饰显示的图形。现在只支持pie和verticalBar3D的,分别是PieChartPostProcessor、CategoryChartPostProcessor。
通过cewolf tag显示图形
部系" yaxislabel="金额" legendanchor="west">
id:必须,在整个页面中唯一
type:显示图形的类型,现在一般使用pie、verticalBar3D
xaxislabel,yaxislabel:X、Y轴的标题,不是必须
legendanchor:小图标(分类)显示的位置
还有一个showlegend属性,默认为 true,设置为false时,不显示小图标(分类)
指定数据来源,这里指向变量dp。
使用processor,控制图形的显示效果。这里可以设置一些颜色之类的属性。
显示图形,chartid指先前指定的id,renderer一般相同,指的是生成图片路径。Width、height就是图片高度和宽度。
在饼图中JFreeChart默认只显示选项和数值,没有显示各项所占比例。由于手头没有1.0版的JFreeChart Developer Guide(这可是要钱的,后来想想即使有,也未必能找到关于百分比这方面的说明),再加上DEMO中的饼图都没有显示百分比,无法参考。后来在网上找到了一个老版本的例子,其中能显示百分比。它是通过在PiePlot中设置的:
PiePlot pie;
pie.setPercentFormatString("#,###0.0#%");
但1.0版本中根本就找不到setPercentFormatString这方法,JFreeChart各版本之间改动比较大,很难兼容。还好它是开源的,把它的源码都搜索了一遍,认真读了一些源码,终于理出了头绪。
原来在1.0.0-rc1版中显示百分比已经调整到StandardPieItemLabelGenerator构造函数中了,StandardPieItemLabelGenerator有三个构造函数。StandardPieItemLabelGenerator()不显示各项所占比例。另外两个可以显示比例。代码如下:
plot.setLabelGenerator(new StandardPieItemLabelGenerator(StandardPieItemLabelGenerator.DEFAULT_TOOLTIP_FORMAT));
//或者采用下面自定义样式显示,{0}表示选项,{1}表示数值,{2}表示所占比例
plot.setLegendLabelGenerator(new StandardPieItemLabelGenerator("{0}: ({1}M, {2})"));
默认显示百分比是取整的,如果要让百分比保留二位小数,可以用第三个构造函数:
plot.setLabelGenerator(new StandardPieItemLabelGenerator(“{0}={1}({2})”,
NumberFormat.getNumberInstance(),
new DecimalFormat("0.00%")));
发表于 @ 2008年02月29日 22:28:00|评论(loading...)|编辑