2016-04-18 9 views
1

jaspersoftスタジオ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 

私は何を達成したいことは、カテゴリ、productnameているシリーズであること

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です。グラフの値をcount(distinct customerkey)に設定するにはどうすればよいですか?私はデータセットとして2番目のクエリを使用することができ、チャートの値としてvalフィールドを設定することができますが、私はまた、クエリ/データセットだけを好むので、レポートの詳細を表示する必要があります。

これは可能ですか?

答えて

0

チャートのデータソースが必要なので、間違いなく別のクエリをサブデータセットで実行するのが最も簡単な方法です。しかし、別のソリューションを再クエリしたくない場合はレポートスクリプトレットを使用してください。

JRDefaultScriptletを拡張するクラスを作成すると、afterDetailEvalの値を取得し、その値とその数を格納します。

あなたのscripletは、JRDataSourceを返します。

EDIT:コメントで

のためにスクリプトレット

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に知らせますscripを使うjasperReportタグで(パッケージ名を含む完全なクラス名を指定する必要があります)

サブデータセット

<subDataset name="countDatasource" uuid="f6b4337c-45f4-4fc6-909a-ffbbef3a1b2f"> 
    <field name="description" class="java.lang.String"/> 
    <field name="count" class="java.lang.Integer"/> 
</subDataset> 

はあなたが好きなデータソース(円グラフを、使用定義scriptletClass="CountScriptlet"

を設定してみましょうテーブルecc。

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

唯一の制限は、あなたがより多くの詳細を与えることができ、あなたがevaluationTime="Report"

+0

reportElementsummaryバンドで使用したりすることができますので、報告書は、(カウントを終えた)詳細バンドを満たしている必要があるということですか?私はJava開発者でなく、Jasperスタジオを初めて使っています。しかし、それをする方法を学びたいと思います。私は伝統的なBIの世界(SSRS、OBI)から来ています。この種の操作は非常に簡単で直感的です。ジャスパーはJava開発者のためのものではありますが、学びたいと思っています。ありがとう。 – thotwielder

+0

@thotwielder、そこに行く、しかし、あなたはそれのハングアップを取得するいくつかのJavaが必要... ...)、楽しい –

関連する問題