birt动态设置要显示的图表数据
首先新建报表,新建数据源,使用birt默认数据源,新建数据集,使用刚才的数据源,查询语句里写上:
select *
from orderdetails
这里我选择orderdetails表,因为该表的数值数据列比较多,方便后面的设计。且我设置了只取数据集的前10行数据,如下图:
因为birt似乎不支持top 10或者哪里应该注意的我没注意到,反正总是提示错误,拿出来运行就可以。
我们再来设计一个参数,新建参数,设置该参数为列表框,值为静态的"PRICEEACH"和“QUANTITYORDERED”(这是我们查询的数据列).如下图:
好了,我们来设计一个柱状图,设置要显示的数据,如下图:
然后设置一下柱状图的名字为chart1(过会会用到).
现在我们点击设计器空白处,选择script,在beforeFactory方法里写上以下代码:
?????????? importPackage(Packages.org.eclipse.birt.chart.model.data.impl);
????importPackage(Packages.org.eclipse.birt.chart.model.component.impl);
????importPackage(Packages.org.eclipse.birt.chart.model.type.impl);
????importPackage(Packages.org.eclipse.birt.chart.model.attribute);
????importPackage(Packages.org.eclipse.birt.chart.model.attribute.impl);
????chart1 = reportContext.getDesignHandle().findElement("chart1");?? //获取我们设置的chart图
????chartIns = chart1.getReportItem().getProperty( "chart.instance" );
????
???? xAxis =chartIns.getAxes().get(0);????????????//得到x轴对象
???? yAxis1 = xAxis.getAssociatedAxes().get(0);????????//得到y轴对象
????
???? for( i=0; i < params["param1"].value.length; i++){????对我们选择的参数进行遍历????
????????
????????var seriesDefinit = SeriesDefinitionImpl.create();???????????????? //生成一个SeriesDefinition对象
????????var lineSeries = LineSeriesImpl.create();?????????????????????????????? //生成一个折线系列
????????lineSeries.getLabel().setVisible(true);???????????????????? //设置折线显示数值
????????lineSeries.getMarker().setType(MarkerType.STAR_LITERAL);??????????//设置拆线上的标记为星型
????????lineSeries.getLineAttributes().setColor(ColorDefinitionImpl.create(111,111,111));???? //设置折线的颜色
????????var data = QueryImpl.create("row["" + params["param1"].value +?? ""]" );?????? //得到我们选择的参数相应的数值
????????lineSeries.getDataDefinition().add(data)?????????????????? //把数据添加到折线里
????????seriesDefinit.getSeries().add( lineSeries );????????//添加这个折线到SeriesDefinition对象里
????????yAxis1.getSeriesDefinitions().add( seriesDefinit );??????????????????
????}
大概就是这样,最后发点牢骚,LineSeriesImpl.create()这里生成的居然不是LineSeries对象,而是Series对象,要程序运行时自动判断,我不知道为什么birt要这样设计哦。还有,setType(MarkerType.STAR_LITERAL),查看了MarkerType的字段,似乎还有一个是STAR,不过是int类型,而STAR_LITERAL却是MarkerType类型,不知道为什么这样设置,我传了个int类型的STAR进去,死活提示脚本有错(birt的错误提示能让人发疯)。传了个MarkerType类型的STAR_LITERAL就正常运行了。更多内容,请查看birt家园