Jump to content
We've recently updated our Privacy Statement, available here ×

chart scriptlet real example


2004 IR Help

Recommended Posts

By: Adrian Dimulescu - dadi

chart scriptlet real example

2003-11-28 08:03

Hi,

 

anybody has a real chart-generation scriptlet example (one that actually get its data from the report and not from hardcoded arrays?)

 

Thanks in advance,

Adrian.

 

 

 

 

By: Adrian Dimulescu - dadi

RE: chart scriptlet real example

2003-12-01 06:51

Hello again,

 

I'm coming back to this question, reformulating it:

I have a report like this:

 

<group name="site" ...

<group name="an" ...

<detail ...

 

I attached a scriptlet that contains:

 

public void afterDetailEval() throws JRScriptletException {

System.out.println(">>>> afterDetailEval");

}

 

public void beforeDetailEval() throws JRScriptletException {

System.out.println(">>>> beforeDetailEval");

}

 

 

Ok and now the question: is it normal that when running the report, I don't see the debug lines in the two methodes (before and after detail eval) ?

 

Thanks for any ideas !

Adrian.

 

 

 

 

By: Adrian Dimulescu - dadi

how are scriptlet callback methods called ?

2003-12-03 02:01

Hello everybody,

 

I still can't get my chart-generating scriptlet to work after reading several times the available documentation and browsing forums :(

 

I have attached a little debug scriptlet to my report; my scriptlet inherits JRAbstractScriptlet; its methods only do a System.out.println for debugging purposes. The group callback methods alse write the name of the groupName String parameter.

 

I find the sequence in which the callback methods are called strange. Could anyone explain it to me ?

 

beforeReportInit

beforePageInit

beforeColumnInit

beforeGroupInit: site

beforeGroupInit: an

afterGroupInit: an

afterGroupInit: site

afterColumnInit

afterPageInit

beforeDetailEval

afterDetailEval

beforeDetailEval

afterDetailEval

 

... lots of before/afterDetailEval ...

 

beforeDetailEval

afterDetailEval

beforeGroupInit: site

beforeGroupInit: an

afterGroupInit: an

afterGroupInit: site

beforePageInit

beforeColumnInit

afterColumnInit

afterPageInit

beforeDetailEval

afterDetailEval

beforeDetailEval

afterDetailEval

beforeDetailEval

afterDetailEval

beforeDetailEval

afterDetailEval

beforeDetailEval

afterDetailEval

beforeDetailEval

afterDetailEval

beforeDetailEval

afterDetailEval

beforeDetailEval

afterDetailEval

beforeDetailEval

afterDetailEval

beforeDetailEval

afterDetailEval

beforeDetailEval

afterDetailEval

beforeGroupInit: site

beforeGroupInit: an

afterGroupInit: an

afterGroupInit: site

beforeDetailEval

afterDetailEval

beforePageInit

beforeColumnInit

afterColumnInit

afterPageInit

 

Obviously, I have to groups: "site" and "an".

 

What troubles me is that afterGroupInit is triggered before even the first beforeDetailEval. I would like to put a chart in the groupFooter of the group "an" and I thought I might use afterDetailEval() to add data to an internal array of the scriptlet and afterGroupInit() in order to actually build the chart, at the end of the group.

 

This obviously is not the case. But I really can't understand how could I do that. Which is the callback method which signals the footer of a group ?

 

Needless to say, I would enourmously appreciate some help on this issue.

 

 

 

 

 

By: Teodor Danciu - teodord

RE: how are scriptlet callback methods called

2003-12-03 04:56

 

Hi,

 

There is no explicit call when a group ends.

This is mostly because a group ends where the next

one begins.

So, beforeGroupInit() could serve as a scriptlet

callback for when the group ends, except for the last group, because we do not have an end report scriptlet callback either. I'm studying the possibility

to add such a callback for the end of the report.

 

"afterGroupInit" is called even before "beforeDetailEval" because we are talking about "init"

operations. Don't interpret "afterGroupInit" as some

sort of an "endGroup".

The only difference between "beforeXxx" and "afterXxx" is that the first one are called before the

engine evaluates the report variables and the second after the report variables were evaluated / incremented.

Besides that, the two callbacks are made roughly the

same time.

 

I would suggest that you use the scriptlet only

to gather data for your chart and I guess you could

employ "afterDetailEval" as you would gather the data

reading from every record in the data source, and in

the group footer, you put an image that has an

expression which makes a callback on the scriptlet

object to get an instant generated image from the

data that was gathered.

 

I hope this helps.

Teodor

 

 

 

 

 

By: Adrian Dimulescu - dadi

RE: chart scriptlet real example

2003-12-04 01:36

Hello Teodor,

 

thanks a lot for your answer. It really clarifies a lot. I'll put it to work as soon as I get the chance (again). I may contribute my chart example back to JasperReports -- if you think it is a good idea -- as I think a real chart would be much more informative than the existing ones.

 

I'll let you know what happens when I try it -- but I already have the feeling it will work :)

 

 

 

 

 

By: Tinku - tinku

RE: chart scriptlet real example

2005-02-21 23:33

If you had a solution please mail to tinkumani@yahoo.com, please.

 

 

 

 

By: kai - krenken

RE: chart scriptlet real example

2005-02-22 23:41

Why Mail it? Post it here so everyone can use it!

 

 

 

 

By: Tinku - tinku

RE: chart scriptlet real example

2005-02-23 01:02

Ok Right

 

Let me post a real example

---------------------------------------JRXML

<?xml version="1.0" encoding="UTF-8" ?>

<!-- Created with iReport - A designer for JasperReports -->

<!DOCTYPE jasperReport PUBLIC "//JasperReports//DTD Report Design//EN" "http://jasperreports.sourceforge.net/dtds/jasperreport.dtd">

<jasperReport

name="YieldChartReport"

columnCount="1"

printOrder="Vertical"

orientation="Portrait"

pageWidth="595"

pageHeight="842"

columnWidth="535"

columnSpacing="0"

leftMargin="30"

rightMargin="30"

topMargin="20"

bottomMargin="20"

whenNoDataType="NoPages"

scriptletClass="com.brainscript.yield.yieldgraph.service.YieldChartReportScriptlet"

isTitleNewPage="false"

isSummaryNewPage="false">

<property name="ireport.scriptlethandling" value="2" />

<field name="itemId" class="java.lang.String"/>

<field name="itemName" class="java.lang.String"/>

<field name="fromToDate" class="java.lang.String"/>

<field name="yield" class="java.lang.String"/>

<field name="fromDate" class="java.lang.String"/>

<field name="toDate" class="java.lang.String"/>

<variable name="ReportData" class="java.util.ArrayList" resetType="Group" resetGroup="itemId" calculation="System">

</variable>

<group name="itemId" isStartNewColumn="false" isStartNewPage="true" isResetPageNumber="false" isReprintHeaderOnEachPage="false" minHeightToStartNewPage="0" >

<groupExpression><![CDATA[$F{itemName}]]></groupExpression>

<groupHeader>

<band height="34" isSplitAllowed="true" >

<textField isStretchWithOverflow="false" pattern="" isBlankWhenNull="true" evaluationTime="Now" hyperlinkType="None" > <reportElement

mode="Opaque"

x="108"

y="14"

width="100"

height="18"

forecolor="#000000"

backcolor="#FFFFFF"

key="textField"

stretchType="NoStretch"

positionType="FixRelativeToTop"

isPrintRepeatedValues="true"

isRemoveLineWhenBlank="false"

isPrintInFirstWholeBand="false"

isPrintWhenDetailOverflows="false"/>

<textElement textAlignment="Left" verticalAlignment="Top" rotation="None" lineSpacing="Single">

<font fontName="Arial" pdfFontName="Helvetica" size="10" isBold="false" isItalic="false" isUnderline="false" isPdfEmbedded ="false" pdfEncoding ="Cp1252" isStrikeThrough="false" />

</textElement>

<textFieldExpression class="java.lang.String"><![CDATA[$F{itemName}]]></textFieldExpression>

</textField>

<staticText>

<reportElement

mode="Opaque"

x="17"

y="14"

width="83"

height="14"

forecolor="#000000"

backcolor="#FFFFFF"

key="staticText-1"

stretchType="NoStretch"

positionType="FixRelativeToTop"

isPrintRepeatedValues="true"

isRemoveLineWhenBlank="false"

isPrintInFirstWholeBand="false"

isPrintWhenDetailOverflows="false"/>

<textElement textAlignment="Left" verticalAlignment="Top" rotation="None" lineSpacing="Single">

<font fontName="Arial" pdfFontName="Helvetica" size="12" isBold="true" isItalic="false" isUnderline="false" isPdfEmbedded ="false" pdfEncoding ="Cp1252" isStrikeThrough="false" />

</textElement>

<text><![CDATA[item]]></text>

</staticText>

</band>

</groupHeader>

<groupFooter>

<band height="363" isSplitAllowed="true" >

<image scaleImage="RetainShape" vAlign="Top" hAlign="Left" isUsingCache="true" evaluationTime="Now" hyperlinkType="None"> <reportElement

mode="Opaque"

x="5"

y="2"

width="522"

height="360"

forecolor="#000000"

backcolor="#FFFFFF"

key="image-1"

stretchType="NoStretch"

positionType="FixRelativeToTop"

isPrintRepeatedValues="true"

isRemoveLineWhenBlank="false"

isPrintInFirstWholeBand="false"

isPrintWhenDetailOverflows="false"/>

<graphicElement stretchType="NoStretch" pen="None" fill="Solid" />

<imageExpression class="java.awt.Image"><![CDATA[((com.brainscript.yield.yieldgraph.service.YieldChartReportScriptlet)$P{REPORT_SCRIPTLET}).generateGraph()]]></imageExpression> </image>

</band>

</groupFooter>

</group>

<background>

<band height="0" isSplitAllowed="true" >

</band>

</background>

<title>

<band height="14" isSplitAllowed="true" >

</band>

</title>

<pageHeader>

<band height="34" isSplitAllowed="true" >

<textField isStretchWithOverflow="false" pattern="" isBlankWhenNull="true" evaluationTime="Now" hyperlinkType="None" > <reportElement

mode="Opaque"

x="92"

y="15"

width="83"

height="14"

forecolor="#000000"

backcolor="#FFFFFF"

key="textField"

stretchType="NoStretch"

positionType="FixRelativeToTop"

isPrintRepeatedValues="true"

isRemoveLineWhenBlank="false"

isPrintInFirstWholeBand="false"

isPrintWhenDetailOverflows="false"/>

<textElement textAlignment="Left" verticalAlignment="Top" rotation="None" lineSpacing="Single">

<font fontName="Arial" pdfFontName="Helvetica" size="10" isBold="false" isItalic="false" isUnderline="false" isPdfEmbedded ="false" pdfEncoding ="Cp1252" isStrikeThrough="false" />

</textElement>

<textFieldExpression class="java.lang.String"><![CDATA[$F{fromDate}]]></textFieldExpression>

</textField>

<textField isStretchWithOverflow="false" pattern="" isBlankWhenNull="true" evaluationTime="Now" hyperlinkType="None" > <reportElement

mode="Opaque"

x="324"

y="15"

width="83"

height="14"

forecolor="#000000"

backcolor="#FFFFFF"

key="textField"

stretchType="NoStretch"

positionType="FixRelativeToTop"

isPrintRepeatedValues="true"

isRemoveLineWhenBlank="false"

isPrintInFirstWholeBand="false"

isPrintWhenDetailOverflows="false"/>

<textElement textAlignment="Left" verticalAlignment="Top" rotation="None" lineSpacing="Single">

<font fontName="Arial" pdfFontName="Helvetica" size="10" isBold="false" isItalic="false" isUnderline="false" isPdfEmbedded ="false" pdfEncoding ="Cp1252" isStrikeThrough="false" />

</textElement>

<textFieldExpression class="java.lang.String"><![CDATA[$F{toDate}]]></textFieldExpression>

</textField>

<staticText>

<reportElement

mode="Opaque"

x="3"

y="15"

width="83"

height="14"

forecolor="#000000"

backcolor="#FFFFFF"

key="staticText-1"

stretchType="NoStretch"

positionType="FixRelativeToTop"

isPrintRepeatedValues="true"

isRemoveLineWhenBlank="false"

isPrintInFirstWholeBand="false"

isPrintWhenDetailOverflows="false"/>

<textElement textAlignment="Left" verticalAlignment="Top" rotation="None" lineSpacing="Single">

<font fontName="Arial" pdfFontName="Helvetica" size="12" isBold="true" isItalic="false" isUnderline="false" isPdfEmbedded ="false" pdfEncoding ="Cp1252" isStrikeThrough="false" />

</textElement>

<text><![CDATA[From]]></text>

</staticText>

<staticText>

<reportElement

mode="Opaque"

x="232"

y="15"

width="83"

height="14"

forecolor="#000000"

backcolor="#FFFFFF"

key="staticText-1"

stretchType="NoStretch"

positionType="FixRelativeToTop"

isPrintRepeatedValues="true"

isRemoveLineWhenBlank="false"

isPrintInFirstWholeBand="false"

isPrintWhenDetailOverflows="false"/>

<textElement textAlignment="Left" verticalAlignment="Top" rotation="None" lineSpacing="Single">

<font fontName="Arial" pdfFontName="Helvetica" size="12" isBold="true" isItalic="false" isUnderline="false" isPdfEmbedded ="false" pdfEncoding ="Cp1252" isStrikeThrough="false" />

</textElement>

<text><![CDATA[To]]></text>

</staticText>

</band>

</pageHeader>

<columnHeader>

<band height="0" isSplitAllowed="true" >

</band>

</columnHeader>

<detail>

<band height="0" isSplitAllowed="true" >

</band>

</detail>

<columnFooter>

<band height="0" isSplitAllowed="true" >

</band>

</columnFooter>

<pageFooter>

<band height="0" isSplitAllowed="true" >

</band>

</pageFooter>

<summary>

<band height="0" isSplitAllowed="true" >

</band>

</summary>

</jasperReport>

----------------------Scriptlet

 

/*

* @ (#) YieldChartReportScriptlet.java version 1.0 Feb 22, 2005

* Copyright 2005 Brainscript Tech, All rights reserved.

* PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.

*/

package com.brainscript.yield.yieldgraph.service;

 

import java.awt.Color;

import java.awt.Image;

import java.awt.Paint;

import java.util.ArrayList;

import java.util.Iterator;

 

import net.sf.jasperreports.engine.JRDefaultScriptlet;

import net.sf.jasperreports.engine.JRScriptletException;

 

import org.apache.commons.lang.builder.ToStringBuilder;

import org.apache.commons.lang.builder.ToStringStyle;

import org.jfree.chart.ChartFactory;

import org.jfree.chart.JFreeChart;

import org.jfree.chart.axis.CategoryAxis;

import org.jfree.chart.axis.CategoryLabelPositions;

import org.jfree.chart.axis.NumberAxis;

import org.jfree.chart.labels.ItemLabelAnchor;

import org.jfree.chart.labels.ItemLabelPosition;

import org.jfree.chart.labels.StandardCategoryLabelGenerator;

import org.jfree.chart.plot.CategoryPlot;

import org.jfree.chart.plot.PlotOrientation;

import org.jfree.chart.renderer.category.BarRenderer;

import org.jfree.chart.renderer.category.BarRenderer3D;

import org.jfree.data.category.CategoryDataset;

import org.jfree.data.category.DefaultCategoryDataset;

import org.jfree.ui.TextAnchor;

/**

* TODO Details Of File

*

* @version 1.0 Feb 22, 2005

* @author Tinku

*/

 

public class YieldChartReportScriptlet extends JRDefaultScriptlet {

//public static boolean groupCmplted=false;

 

 

public void beforeDetailEval() throws JRScriptletException {

 

String fromToDate=(String) this.getFieldValue("fromToDate");

String yield=(String)this.getFieldValue("yield");

 

ArrayList arrayList=(ArrayList) this.getVariableValue("ReportData");

if(arrayList==null)arrayList=new ArrayList();

arrayList.add(new FromToYieldObject(fromToDate,yield));

this.setVariableValue("ReportData",arrayList);

}

 

 

public Image generateGraph()

{

try{

ArrayList ReportData= (ArrayList) this.getVariableValue("ReportData");

DefaultCategoryDataset defaultcategorydataset = new DefaultCategoryDataset();

 

for (Iterator reportIter = ReportData.iterator(); reportIter.hasNext();) {

FromToYieldObject fromToObject = (FromToYieldObject) reportIter.next();

defaultcategorydataset.addValue(Double.parseDouble(fromToObject.getYield()), "Yield", fromToObject.getFromToDate());

}

JFreeChart chart=ChartFactory.createBarChart3D("Yield Analysis","Range","Yield",defaultcategorydataset,PlotOrientation.VERTICAL,false,false,true);

 

CategoryPlot categoryplot = chart.getCategoryPlot();

categoryplot.setDomainGridlinesVisible(true);

CategoryAxis categoryaxis = categoryplot.getDomainAxis();

categoryaxis.setCategoryLabelPositions(CategoryLabelPositions.createUpRotationLabelPositions(Math.toRadians(355)));

BarRenderer3D barrenderer3d = (BarRenderer3D)categoryplot.getRenderer();

barrenderer3d.setDrawBarOutline(true);

CustomRenderer customrenderer = new CustomRenderer(new Paint[] {

Color.red, Color.blue, Color.green, Color.yellow, Color.orange, Color.cyan, Color.magenta, Color.gray

});

ItemLabelPosition itemlabelposition = new ItemLabelPosition(ItemLabelAnchor.CENTER, TextAnchor.CENTER, TextAnchor.CENTER, Math.toRadians(90D));

customrenderer.setPositiveItemLabelPosition(itemlabelposition,true);

customrenderer.setNegativeItemLabelPosition(itemlabelposition,true);

customrenderer.setItemLabelsVisible(true);

categoryplot.setRenderer(customrenderer);

 

 

this.setVariableValue("ReportData",null);

return chart.createBufferedImage(522,360);

}catch(JRScriptletException exception){exception.printStackTrace();}

return null;

}

 

static class CustomRenderer extends BarRenderer3D

{

 

public Paint getItemPaint(int i, int j)

{

return colors[j % colors.length];

}

 

private Paint colors[];

 

public CustomRenderer(Paint apaint[])

{

colors = apaint;

}

}

 

class FromToYieldObject

{

 

private String fromToDate;

private String yield;

 

public String toString() {

return new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE)

.append("fromToDate", fromToDate).append("yield", yield)

.toString();

}

 

/**

* @param fromToDate

* @param yield

*/

public FromToYieldObject(String fromToDate, String yield) {

super();

this.fromToDate = fromToDate;

this.yield = yield;

}

public String getFromToDate() {

return fromToDate;

}

public void setFromToDate(String fromToDate) {

this.fromToDate = fromToDate;

}

public String getYield() {

return yield;

}

public void setYield(String yield) {

this.yield = yield;

}

}

 

 

}

 

 

 

 

 

//End of class YieldChartReportScriptlet.java

Link to comment
Share on other sites

  • Replies 0
  • Created
  • Last Reply

Top Posters In This Topic

Popular Days

Top Posters In This Topic

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...