我正在使用jaspersoft studio 6.2。我已将条形图放在摘要区域中,如何设置它以计算值表达式中某个字段的不同之处?例如
我有此查询/数据集:
select 'xx' as UsageDate, 'a' as ProductName, 1 as CustomerKey
union all select 'xx' as UsageDate, 'b' as ProductName, 1 as CustomerKey
union all select 'xx' as UsageDate, 'a' as ProductName, 2 as CustomerKey
union all select 'yy'as UsageDate, 'a' as ProductName, 1 as CustomerKey
union all select 'yy' as UsageDate, 'b' as ProductName, 3 as CustomerKey
我想要实现的是
select usagedate, productname, count(distinct customerkey) as val from (
select 'xx' as UsageDate, 'a' as ProductName, 1 as CustomerKey
union all select 'xx' as UsageDate, 'b' as ProductName, 1 as CustomerKey
union all select 'xx' as UsageDate, 'a' as ProductName, 2 as CustomerKey
union all select 'yy'as UsageDate, 'a' as ProductName, 1 as CustomerKey
union all select 'yy' as UsageDate, 'b' as ProductName, 3 as CustomerKey
) as t
group by usagedate, productname
usagedate
是类别,productname
是系列。如何将图表中的值设置为count(distinct customerkey)
?我知道我可以使用第二个查询作为数据集,并将val字段设置为图表中的值,但是我还需要在报表中显示详细信息,因此只希望使用一个查询/数据集来完成所有操作。
这可能吗?
您的图表需要一个数据源,因此毫无疑问,在subdatset中执行另一个查询是最简单的方法。但是,如果您不喜欢重新查询,则另一个解决方案是使用报表scriptlet。
创建一个扩展JRDefaultScriptlet的类,以覆盖afterDetailEval
从您的字段中获取的值,存储该值及其计数。
然后让您的脚本返回图表的JRDataSource。
编辑:作为在注释中要求的简单示例
散文集
public class CountScriptlet extends JRDefaultScriptlet {
private static final String COUNT_FIELD = "fieldNameYouLikeToCount";
//Map to hold our count and dataset
private Map<String,CountResult> ds = new HashMap<String, CountResult>();
@Override
public void afterDetailEval() throws JRScriptletException
{
String key = (String)this.getFieldValue(COUNT_FIELD);
CountResult cr = ds.get(key); //Check if we have it
if (cr==null){
cr = new CountResult(key); //No, then create it count = 0
ds.put(key, cr);
}
cr.increment(); //Increment it (new 0-->1, old i-->i+1
}
public JRBeanCollectionDataSource getDataSource(){
return new JRBeanCollectionDataSource(ds.values()); //Return our class as datasource, if you need to sort'em use Collections
}
}
保持计数的类
public class CountResult {
private String description;
private int count;
public CountResult(String description) {
this.description = description;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public void increment(){
count++;
}
}
告诉jrxml使用scriplet(您需要指出完整的类名称,包括程序包名称)
在jasperReport
标签集中scriptletClass="CountScriptlet"
定义子数据集
<subDataset name="countDatasource" uuid="f6b4337c-45f4-4fc6-909a-ffbbef3a1b2f">
<field name="description" class="java.lang.String"/>
<field name="count" class="java.lang.Integer"/>
</subDataset>
在需要的地方使用数据源(piechart,表等)
<datasetRun subDataset="countDatasource" uuid="92579588-802b-4073-a5ee-79672c9b6e66">
<dataSourceExpression><![CDATA[$P{REPORT_SCRIPTLET}.getDataSource()]]></dataSourceExpression>
</datasetRun>
唯一的限制是,该报告需要填写详细信息区域(完计数),所以你可以在使用summary
带或reportElement
带evaluationTime="Report"
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句