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)
Dynamically changing the legend of a singel series, grouped chart
kevinshih
Hi all,<br />
<br />
I have a line chart bound to a dataset. The chart graphs time along the x-axis and values along the y-axis. The y-axis has an additional y-series grouping so my line chart has 5 lines of data on it. The legend description for these lines shows "[color] 208 [color] 209 [color] 210 [color] 211 [color] 212" where [color] is a colored line that matches the chart lines.<br />
<br />
What I would like to do is change the "208", "209" etc text to something more meaningful for the reader. Have tried using:<br />
<br />
function beforeDrawLegendItem( lerh, bounds, icsc )<br />
{<br />
lerh.getLabel().getCaption().setValue("test");<br />
}<br />
<br />
but all that does is change all of the numbers to "test", so the legend looks like:<br />
[color] test [color] test [color] test [color] test [color] test<br />
<br />
Haven't found a function that will change the name of the grouping. Is that possible? Using 2.3.2.<br />
<br />
Thanks!
Find more posts tagged with
Comments
mwilliams
Hi kevinshih,
Are the groupings always the same? i.e. 208, 209, etc. If not, are there always 5 groups? Let me know.
kevinshih
Hi Michael,
No, the groupings are not always the same and there is not always 5 of them. The field that is being grouped is the parent record id. I'm only pulling in the record id from the child table rather than doing a join on with the parent table to get the label because of performance reasons.
Thanks!
Kevin
mwilliams
Kevin,
So, you have a dataSet that has values in it associated with the 208, 209, etc values that you want to use to replace these numbers?
kevinshih
Exactly. I pull the primary dataset to chart the data. It has the record ID that I can use to match another dataset I pull in the same report to look up the text I want to use for the legend labels.
Basically trying to do a join without doing a join :-)
mwilliams
Kevin,
You could store the label names in persistentGlobalVariables with the variables named after the 208, 209, etc. values in the dataSet that has the label names' script. This way, you can easily recall the correct variable by using the current legend label value to call the new label value.
kevinshih
Hi Michael,
I thought about that or doing a joint dataset. Either way, my bigger question is how to access the individual labels. Since I won't know how many groups there are, I would have to somehow loop through all of them, and then replace the text. The only script I found was the one I posted originally, but that replaced the label for each group with the same text, i.e.:
208 - 209 - 210 - 211 - 212
became
Test - Test - Test - Test - Test
Any suggestions on that?
mwilliams
Kevin,
Which is why you'd replace the label based on the value of the current label. The script you had replaced them all with the same because the script function runs for each label and you had no condition to stop it at only replacing one.
kevinshih
I saw examples where people were using a big if/else statement to test and change the labels. That wouldn't work for me as I don't know how many labels I would be testing.
I thought the legend labels would be an array or something I could loop through. Is there any type of counter I could use? I guess I could set another global variable that is the count of the parent data set but that seems kind of clugy.
mwilliams
Kevin,
Is the dataSet that holds the values of the labels limited by the same criteria that the dataSet you're using in your chart? This would limit the amount of variables you'd have to set. Either way, this should work to replace the values in the legend without a big if/else loop by naming the PGV's the same as the current legend label value. Hope this helps.
kevinshih
Got it. Makes sense. I'd be calling something like:
var recordId = lerh.getLabel().getCaption().getValue();
var newLegendValue = icsc.getExternalContext().getScriptable().getPersistentGlobalVariable(recordId);
lerh.getLabel().getCaption.setValue(newLegendValue);
Last question. What's the best function to use in the dataset to script setting a PGV for each row in the dataset?
mwilliams
Kevin,
You'd just have to set the value with reportContext.setPersistentGlobalVariable(row["currentLegendLabel"], row["newLegendLabel"]);
kevinshih
That was it. On the dataset, I used the following script in the onFetch() method:
reportContext.setGlobalVariable(row.OID,row.NAME);
On the chart, I used:
function beforeDrawLegendItem( lerh, bounds, icsc )
{
recordId = lerh.getLabel().getCaption().getValue();
newLegendValue = icsc.getExternalContext().getScriptable().getGlobalVariable(recordId);
lerh.getLabel().getCaption().setValue(newLegendValue);
}
And it all came together. Thanks for all your help!
mwilliams
Kevin,
No problem. Glad to help!
pratim
Hi Michael,<br />
<br />
Need your help.<br />
when i did this, for one of my line chart,<br />
<br />
function beforeDrawLegendItem( lerh, bounds, icsc )<br />
{<br />
lerh.getLabel().getCaption().setValue("test");<br />
}<br />
<br />
I am not able to see, any legend entries getting modified. I am using BIRT 2.3.2.<br />
Do i need to modify any other configuration? <br />
<br />
<blockquote class='ipsBlockquote' data-author="'mwilliams'" data-cid="54304" data-time="1253565093" data-date="21 September 2009 - 01:31 PM"><p>
Kevin,<br />
<br />
<br />
No problem. Glad to help!
<br /></p></blockquote>
mwilliams
Are you using y-series grouping?