【问题标题】:How to get count distinct in my chart without executing another query?如何在不执行另一个查询的情况下在我的图表中获得不同的计数?
【发布时间】:2016-04-18 08:51:56
【问题描述】:

我正在使用 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 字段设置为图表中的值,但我还需要在报告中显示详细信息,因此更喜欢只使用 one 查询/数据集来执行此操作全部。

这可能吗?

【问题讨论】:

    标签: java jasper-reports


    【解决方案1】:

    您的图表需要一个数据源,因此毫无疑问,在 subdatset 中执行另一个查询是最简单的方法。但是,如果您不喜欢重新查询,另一种解决方案是使用 report 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>
    

    在您喜欢的地方使用数据源(饼图、表格 ecc。)

    <datasetRun subDataset="countDatasource" uuid="92579588-802b-4073-a5ee-79672c9b6e66">
        <dataSourceExpression><![CDATA[$P{REPORT_SCRIPTLET}.getDataSource()]]></dataSourceExpression>
    </datasetRun>
    

    唯一的限制是报告需要填充详细信息带(完成计数),因此您可以在summary带或reportElementevaluationTime="Report"上使用它

    【讨论】:

    • 你能提供更多细节吗?我不是 Java 开发人员,也是 Jasper 工作室的新手。但是想学习怎么做。我来自传统的 BI 世界(SSRS、OBI),这种操作非常简单直观。我了解 Jasper 可能更适合 Java 开发人员但想学习。谢谢。
    • @thotwielder,你去吧,但你需要一些 java 来掌握它...... :),玩得开心
    猜你喜欢
    • 2021-06-17
    • 1970-01-01
    • 1970-01-01
    • 2018-01-06
    • 1970-01-01
    • 2017-05-24
    • 1970-01-01
    • 1970-01-01
    • 2012-03-28
    相关资源
    最近更新 更多