Java 如何在不执行另一个查询的情况下在我的图表中获得不同的计数?
我正在使用jaspersoft studio 6.2。我已经将条形图放在摘要栏中,如何将其设置为计算值表达式中某个字段的不同值? e、 g 我有这个查询/数据集:Java 如何在不执行另一个查询的情况下在我的图表中获得不同的计数?,java,jasper-reports,Java,Jasper Reports,我正在使用jaspersoft studio 6.2。我已经将条形图放在摘要栏中,如何将其设置为计算值表达式中某个字段的不同值? e、 g 我有这个查询/数据集: 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
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字段设置为图表中的值,但我还需要在报告中显示详细信息,因此只需一个查询/数据集即可
这可能吗 图表需要一个数据源,因此在子数据集中执行另一个查询无疑是最简单的方法。但是,如果您不想重新查询,另一种解决方案是使用报表scriptlet 创建一个类,该类扩展覆盖afterDetailEval从字段中获取值,存储值及其计数 然后,让您的scriplet返回图表的名称 编辑:如评论中所要求的简单示例 小纸条
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++;
}
}
<subDataset name="countDatasource" uuid="f6b4337c-45f4-4fc6-909a-ffbbef3a1b2f">
<field name="description" class="java.lang.String"/>
<field name="count" class="java.lang.Integer"/>
</subDataset>
告诉jrxml使用scriplet(您需要指明完整的类名,包括包名)
在jasperReport
标记集scriptletClass=“CountScriptlet”
定义子数据集
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++;
}
}
<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.)
唯一的限制是报表需要填写详细栏位(完成计数),因此您可以在
摘要
栏位中使用它,或者在reportElement
上使用evaluationTime=“report”
您的图表需要一个数据源,因此毫无疑问,在子数据集中执行另一个查询是最简单的方法。但是,如果您不想重新查询,另一种解决方案是使用报表scriptlet
创建一个类,该类扩展覆盖afterDetailEval从字段中获取值,存储值及其计数
然后,让您的scriplet返回图表的名称
编辑:如评论中所要求的简单示例
小纸条
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++;
}
}
<subDataset name="countDatasource" uuid="f6b4337c-45f4-4fc6-909a-ffbbef3a1b2f">
<field name="description" class="java.lang.String"/>
<field name="count" class="java.lang.Integer"/>
</subDataset>
告诉jrxml使用scriplet(您需要指明完整的类名,包括包名)
在jasperReport
标记集scriptletClass=“CountScriptlet”
定义子数据集
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++;
}
}
<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.)
唯一的限制是报告需要填写详细栏位(完成计数),因此您可以在
摘要
栏位或reportElement
上使用它,使用evaluationTime=“report”
您能提供更多详细信息吗?我不是Java开发人员,也是Jasper studio的新手。但我想学习如何去做。我来自传统的BI世界(SSRS,OBI),在那里这种操作非常简单直观。我知道Jasper可能更适合Java开发人员,但我想学习。谢谢。@thotwielder,好了,但你需要一些java来掌握窍门…:),你能提供更多细节吗?我不是Java开发人员,也是Jasper studio的新手。但我想学习如何去做。我来自传统的BI世界(SSRS,OBI),在那里这种操作非常简单直观。我知道Jasper可能更适合Java开发人员,但我想学习。谢谢。@thotwielder,好了,但你需要一些java来掌握窍门…:),玩得高兴