Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » BIRT » How to create functions which can be reused in different chart script
How to create functions which can be reused in different chart script [message #898586] Thu, 26 July 2012 17:13 Go to next message
DONG YAO is currently offline DONG YAO
Messages: 20
Registered: May 2012
Junior Member
I have two charts on one report. Both charts need to do the same logic when drawing the xAxis label.

Can I create one function and in both beforDrawAxisLabel call that function?

How to do that?

Thanks
Re: How to create functions which can be reused in different chart script [message #898630 is a reply to message #898586] Thu, 26 July 2012 22:22 Go to previous messageGo to next message
Jason Weathersby is currently offline Jason Weathersby
Messages: 9167
Registered: July 2009
Senior Member

One way of doing this is to add the function to the report beforeRender
like:

function changeLabel ( label ){
label.getCaption().setValue(label.getCaption().getValue()+"Changed");
}
//set it as a global
reportContext.setGlobalVariable("changeLabel", changeLabel);


Then in chart script call it like:

function beforeDrawDataPointLabel( dph, label, icsc )
{
var cl
=icsc.getExternalContext().getScriptable().getGlobalVariable("changeLabel");
cl( label );

}


In chart script this line:
icsc.getExternalContext().getScriptable() is effectively the
reportContext object in normal report script

Jason


On 7/26/2012 1:13 PM, DONG YAO wrote:
> I have two charts on one report. Both charts need to do the same logic
> when drawing the xAxis label.
>
> Can I create one function and in both beforDrawAxisLabel call that
> function?
> How to do that?
>
> Thanks
Re: How to create functions which can be reused in different chart script [message #898841 is a reply to message #898630] Fri, 27 July 2012 21:55 Go to previous messageGo to next message
DONG YAO is currently offline DONG YAO
Messages: 20
Registered: May 2012
Junior Member
Many thanks, Jason!
Re: How to create functions which can be reused in different chart script [message #898845 is a reply to message #898841] Fri, 27 July 2012 23:33 Go to previous messageGo to next message
DONG YAO is currently offline DONG YAO
Messages: 20
Registered: May 2012
Junior Member
Now I have a further question.

If I have two reports and they both have the similar logic. How can I create script functions which could be shared by two reports?

Thanks!
Re: How to create functions which can be reused in different chart script [message #899142 is a reply to message #898845] Mon, 30 July 2012 16:19 Go to previous messageGo to next message
Jason Weathersby is currently offline Jason Weathersby
Messages: 9167
Registered: July 2009
Senior Member

Great question. The best way to do this is to create the js file in the
resource directory and then add the js file to the report as shown here:
http://www.eclipse.org/birt/phoenix/project/notable2.3.php#jump_8

If you add the js file this way it will make it available to the normal
report script environment meaning you can add a data element with the
expression getMyValue(); or in script for example on a label like:


this.text = reportContext.evaluate("getMyValue()");

Chart script context is not the same thing as reportContext but it does
have access to the reportContext. To call this same function from chart
script you could use:


function beforeDrawAxisTitle( axis, label, icsc )
{
label.getCaption().setValue(icsc.getExternalContext().getScriptable().evaluate("getMyValue()"));
}

This:
icsc.getExternalContext().getScriptable()

Is effectively the reportContext object.

Jason


On 7/27/2012 7:33 PM, DONG YAO wrote:
> Now I have a further question.
> If I have two reports and they both have the similar logic. How can I
> create script functions which could be shared by two reports?
>
> Thanks!
Re: How to create functions which can be reused in different chart script [message #899184 is a reply to message #899142] Mon, 30 July 2012 22:46 Go to previous messageGo to next message
DONG YAO is currently offline DONG YAO
Messages: 20
Registered: May 2012
Junior Member
my function has parameters. How to call it in evaluate?

here is my function

function func1(chart, icsc, direction) {...
Re: How to create functions which can be reused in different chart script [message #899185 is a reply to message #899184] Mon, 30 July 2012 23:22 Go to previous messageGo to next message
DONG YAO is currently offline DONG YAO
Messages: 20
Registered: May 2012
Junior Member
Ok, I figure it out.
In chart script, use the code below can get the reference of the function defined in external js file.

func1 = icsc.getExternalContext().getScriptable().evaluate("func1");

Then you can call
func1(param1, param2,...)

Many Many thanks to Jason!!!

[Updated on: Mon, 30 July 2012 23:24]

Report message to a moderator

Re: How to create functions which can be reused in different chart script [message #899200 is a reply to message #899185] Tue, 31 July 2012 03:50 Go to previous message
Jason Weathersby is currently offline Jason Weathersby
Messages: 9167
Registered: July 2009
Senior Member

I think I may write a blog post on this to give others the details.

Jason

On 7/30/2012 7:22 PM, DONG YAO wrote:
> Ok, I figure it out. In chart script, use the code below can get the
> reference of the function.
> func1 = icsc.getExternalContext().getScriptable().evaluate("func1");
>
> Then you can call func1(param1, param2,...)
> Many thanks to Jason
Previous Topic:How to change Y Series to reflect data in chart
Next Topic:How to set text in master page header using data from report?
Goto Forum:
  


Current Time: Fri Sep 19 14:03:28 GMT 2014

Powered by FUDForum. Page generated in 0.02500 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software