Discussions
Categories
Groups
Community Home
Categories
INTERNAL ENABLEMENT
POPULAR
THRUST SERVICES & TOOLS
CLOUD EDITIONS
Quick Links
MY LINKS
HELPFUL TIPS
Back to website
Home
Intelligence (Analytics)
How to get x, y data in chart script (beforeDrawLegendItem)
dstoian
I have this BIRT report with a scatter chart.<br />
I need to customize the legend labels so that they show both x and y values (i.e., both value and category).<br />
I know how to get to the label to get the current text (which is the grouping text), but I could not find anywhere how to get to the data point associated with the current legend label being drawn.<br />
<br />
Here is what I'd like to do in my chart script (please note the "todo:" comments):<br />
<br />
<pre class='_prettyXprint _lang-auto _linenums:0'>
function beforeDrawLegendItem( lerh, bounds, icsc )
{
var sLocation = lerh.getLabel().getCaption().getValue();
var xValue = "x"; //todo: somehow get the "x" value;
var yValue = "y"; //todo: somehow get the "y" value;
var sFinal = sLocation + " (" + yValue + ", " + xValue + ")";
lerh.getLabel().getCaption().setValue( sFinal );
}
</pre>
<br />
Thank you very much in advance for any clues.
Find more posts tagged with
Comments
JasonW
Take a look at this example. It loads a javascript array that is then read in the legend script.
Jason
dstoian
Thanks, Jason, for the quick reply. I can see it working in the report you've attached, but when I put similar code in my report I get an exception on the line where I try to access the array inside beforeDrawLegendItem().<br />
<br />
Please see below, the line marked by comment.<br />
This is my script now:<br />
<br />
<pre class='_prettyXprint _lang-auto _linenums:0'>
y=[];
function beforeDrawLegendItem( lerh, bounds, icsc )
{
var sLocation = lerh.getLabel().getCaption().getValue();
//exception on next line: "y" array is undefined
var yValue = y[lerh.getDataIndex()][0];
var xValue = y[lerh.getDataIndex()][1];
var sFinal = sLocation + " (" + yValue + ", " + xValue + ")";
lerh.getLabel().getCaption().setValue( sFinal );
}
function beforeDrawDataPointLabel( dph, label, icsc )
{
var x=[];
x[0]=dph.getOrthogonalValue();
x[1]=dph.getBaseValue();
y[dph.getIndex()]=x;
}
</pre>
<br />
The error reads:<br />
<span style='font-family: courier'>An error occurred while viewing the report. Cannot view report. Cause: TypeError: Cannot read property "0.0" from undefined at line 63 of chart script:''.</span><br />
<br />
It all looks simple, but I can't figure why...
JasonW
try using the parseInt on the array index:
var y=[];
function beforeDrawLegendItem( lerh, bounds, icsc )
{
var newstr=y[parseInt(lerh.getDataIndex())][0] +"-"+ y[lerh.getDataIndex()][1];
lerh.getLabel().getCaption().setValue(newstr)
}
function beforeDrawDataPointLabel(dph, label, icsc)
{
var x=[];
x[0]=dph.getOrthogonalValue();
x[1]=dph.getBaseValue();
y[parseInt(dph.getIndex())]=x;
}
Jason
dstoian
I've tried what you suggested, Jason (using parseInt() on the index of the array. Still, the same error: "Cannot read property... from undefined".
But, I think the exception is that the array is undefined... I don't understand the difference: same code works in your example report.
Thanks.
JasonW
Put something like this in (Or debug the reprort):
function beforeDrawDataPointLabel( dph, label, icsc )
{
var x=[];
x[0]=dph.getOrthogonalValue();
x[1]=dph.getBaseValue();
y[dph.getIndex()]=x;
importPackage( Packages.java.io );
out = new PrintWriter( new FileWriter( "c:/test/chartevents.txt", true ) );
out.println( y[parseInt(dph.getIndex())][0] );
out.close();
}
dstoian
Jason,<br />
<br />
I've written out data to a log as you suggested, and here's what I found out: I have 3 data points which have identical coordinates, and somehow, in beforeDrawDataPoint event, all these 3 points get an index of 0 (zero).<br />
<br />
That's why in beforeDrawLegendItem I was getting the exception.<br />
<br />
I am confused as to why I don't see 10 indexes (0 to 9) because that's how my data is...<br />
<br />
This is my log:<br />
<pre class='_prettyXprint _lang-auto _linenums:0'>
beforeDrawDataPoint: dph.getIndex()=0; 117.46, 112.08
beforeDrawDataPoint: dph.getIndex()=0; 117.46, 112.08
beforeDrawDataPoint: dph.getIndex()=1; 122.61, 97.46
beforeDrawDataPoint: dph.getIndex()=0; 117.46, 112.08
beforeDrawDataPoint: dph.getIndex()=2; 115.93, 78.46
beforeDrawDataPoint: dph.getIndex()=3; 117.31, 88.35
beforeDrawDataPoint: dph.getIndex()=4; 117.64, 91.33
beforeDrawDataPoint: dph.getIndex()=5; 121.09, 83.96
beforeDrawDataPoint: dph.getIndex()=6; 117.29, 85.96
beforeDrawDataPoint: dph.getIndex()=7; 115.69, 91.4
beforeDrawLegendItem: lerh.getDataIndex()=0
beforeDrawLegendItem: lerh.getDataIndex()=1
beforeDrawLegendItem: lerh.getDataIndex()=2
beforeDrawLegendItem: lerh.getDataIndex()=3
beforeDrawLegendItem: lerh.getDataIndex()=4
beforeDrawLegendItem: lerh.getDataIndex()=5
beforeDrawLegendItem: lerh.getDataIndex()=6
beforeDrawLegendItem: lerh.getDataIndex()=7
beforeDrawLegendItem: lerh.getDataIndex()=8
</pre>
<br />
I'll be using my own counter to replace those indexes: that's already tested and it works.<br />
Thanks for all the help.
JasonW
Any chance you could post the report?
dstoian
Jason,<br />
<br />
I can post the script here, but if you want I can send you the report in an email... it's complicated, though, you won't be able to run it.<br />
<br />
Here's how the script for the chart looks now (working):<br />
<pre class='_prettyXprint _lang-auto _linenums:0'>
yarr=[];
npts=0;
nleg=0;
function beforeDrawDataPoint( dph, fill, icsc )
{
var x=[];
npts=npts+1;
x[0]=dph.getOrthogonalValue();
x[1]=dph.getBaseValue();
yarr[npts-1]=x;
}
function beforeDrawLegendItem( lerh, bounds, icsc )
{
nleg=nleg+1;
var sLocation = lerh.getLabel().getCaption().getValue();
var yValue = yarr[nleg-1][0] + "%";
var xValue = yarr[nleg-1][1] + "%";
var sFinal = sLocation + " (" + yValue + ", " + xValue + ")";
lerh.getLabel().getCaption().setValue( sFinal );
}
</pre>
JasonW
I was just wondering why the index did not work.
dstoian
<blockquote class='ipsBlockquote' data-author="JasonW"><p>I was just wondering why the index did not work.</p></blockquote>
<br />
I think it's pretty clear from my post with the log above (the same bug will show up in your example report if you have 3 points with the same coordinates).<br />
<br />
Data points with same coordinates get assigned same indexes (I had 10 data points to plot), so indexes went from 0-7 in "beforeDrawDataPoint", whereas in "beforeDrawLegendItem" the indexes went form 0-8... <br />
<br />
Why aren't there 10 indexes as one would expect, from 0-9 ?<br />
<br />
Thanks.
JasonW
Do you have multiple series or grouping?
If I use three datapoints with same y vals I still get 0,1,2 with the indexes.
dstoian
Oh, yes, I do have grouping, as can be seen in the attached image... I hope I'm not doing anything wrongly (as I might very easily
)