Discussions
Categories
Groups
Community Home
Categories
INTERNAL ENABLEMENT
POPULAR
PUBLIC CLOUD
PRIVATE CLOUD
Quick Links
MY LINKS
HELPFUL TIPS
Back to website
Home
Intelligence (Analytics)
Deleting a Series' Legend
zax
Hi,<br />
<br />
I am trying to delete a series' legend from a chart that is based on a cross tab. I have found this code, which checks the value of each label in the legend:<br />
<br />
<pre class='_prettyXprint _lang-auto _linenums:0'>function beforeDrawLegendItem(lerh, bounds, icsc)
{
if( lerh.getLabel().getCaption().getValue().compareToIgnoreCase("series1") == 0 ){
bounds.setHeight(0);
bounds.setWidth(0);
lerh.getLabel().setVisible(false);
}else{
lerh.getLabel().setVisible(true);
}
}</pre>
<br />
But the values in my legend are duplicated, like this:<br />
<strong class='bbc'>Series 1</strong><br />
<ul class='bbc'><li> 1</li><li> 3</li><li> 5</li><li> 7</li></ul>
<br />
<br />
<strong class='bbc'>Series 2</strong><br />
<ul class='bbc'><li> 1</li><li> 3</li><li> 5</li><li> 7</li></ul>
<br />
It seems that the event that triggers <strong class='bbc'>beforeDrawLegendItem</strong> does not trigger the creation of the series labels in the legend. I know this because I added the line:<br />
<br />
<pre class='_prettyXprint _lang-auto _linenums:0'>lerh.getLabel().getCaption().setValue("bob");</pre>
and I got the result:<br />
<br />
<strong class='bbc'>Series 1</strong><br />
<ul class='bbc'><li> bob</li><li> bob</li><li> bob</li><li> bob</li></ul>
<br />
<br />
<strong class='bbc'>Series 2</strong><br />
<ul class='bbc'><li> bob</li><li> bob</li><li> bob</li><li> bob</li></ul>
When I was expecting:<br />
<strong class='bbc'>bob</strong><br />
<ul class='bbc'><li> bob</li><li> bob</li><li> bob</li><li> bob</li></ul>
<br />
<br />
<strong class='bbc'>bob</strong><br />
<ul class='bbc'><li> bob</li><li> bob</li><li> bob</li><li> bob</li></ul>
<br />
So my question is.. how do I remove Series 2s' legend, but keep Series 1? Like so:<br />
<strong class='bbc'>Series 1</strong><br />
<ul class='bbc'><li> 1</li><li> 3</li><li> 5</li><li> 7</li></ul>
<br />
Many Thanks,<br />
Stephen
Find more posts tagged with
Comments
mwilliams
Try putting something like this in your chart script:<br />
<br />
<pre class='_prettyXprint _lang-auto _linenums:0'>
function beforeGeneration( chart, icsc )
{
importPackage(Packages.java.util);
importPackage(Packages.org.eclipse.birt.chart.model.data.impl);
importPackage(Packages.org.eclipse.birt.chart.model.component.impl);
importPackage(Packages.org.eclipse.birt.chart.model.type.impl);
importPackage(Packages.org.eclipse.birt.chart.model.attribute);
var xAxis = chart.getAxes().get(0);
var yAxis = xAxis.getAssociatedAxes().get(0);
var xSerieDef = xAxis.getSeriesDefinitions().get(0);
var ySerieDef1 = yAxis.getSeriesDefinitions().get(1);
ySerieDef1.getSeries().get(0).setSeriesIdentifier("");
icsc.getChartInstance().getLegend().getSeparator().setVisible(false);
}
firstLabel = null;
clearAll = false;
function beforeDrawLegendItem( lerh, bounds, icsc )
{
if (firstLabel == null){
firstLabel = lerh.getLabel().getCaption().getValue();
}
else{
if (firstLabel == lerh.getLabel().getCaption().getValue()){
clearAll = true;
}
}
if (clearAll){
lerh.getLabel().setVisible(false);
lerh.getLabel().getOutline().setVisible(false);
bounds.setHeight(0);
bounds.setWidth(0);
}
}
</pre>
zax
Michael,<br />
<br />
That is amazing, thanks for the help. As it turns out I need the second legend series that is drawn (even thought it is series 1 in Eclipse). So I changed the code a little for the function beforeDrawLegendItem:<br />
<br />
<pre class='_prettyXprint _lang-auto _linenums:0'>
function beforeDrawLegendItem( lerh, bounds, icsc )
{
if (firstLabel == null){
// Store the first label in a global variable.
firstLabel = lerh.getLabel().getCaption().getValue();
}else{
// The second time the label equals the same as the first label,
// we are dealing with the second series.
if(firstLabel == lerh.getLabel().getCaption().getValue()){
secondLabel = lerh.getLabel().getCaption().getValue();
}
}
// If the second label is not defined, hide the current label.
// If it is defined, ensure it is displayed.
if (secondLabel == null){
lerh.getLabel().setVisible(false);
lerh.getLabel().getOutline().setVisible(false);
bounds.setHeight(0);
bounds.setWidth(0);
} else {
lerh.getLabel().setVisible(true);
}
}
</pre>
<br />
But now I get the following:<br />
<ul class='bbc'><li>1</li><li>3</li><li>5</li><li>7</li></ul>
<br />
And the "Series 1" label has disappeared. I know why this is, as the code is looking for a repeat of the first value. So is there a clever way of keeping "Series 1"? If not, no problem, you have helped me a lot.<br />
<br />
Thanks,<br />
Stephen
mwilliams
What is your beforeGeneration code? This is where I got rid of the "Series 1" label. If you kept it the same as I did in the above, you might need to change it slightly. Let me know.
zax
This is very strange. My <strong class='bbc'>beforeGeneration</strong> code looks like this:<br />
<br />
<pre class='_prettyXprint _lang-auto _linenums:0'>
function beforeGeneration( chart, icsc ){
importPackage(Packages.java.util);
importPackage(Packages.org.eclipse.birt.chart.model.data.impl);
importPackage(Packages.org.eclipse.birt.chart.model.component.impl);
importPackage(Packages.org.eclipse.birt.chart.model.type.impl);
importPackage(Packages.org.eclipse.birt.chart.model.attribute);
var xAxis = chart.getAxes().get(0);
var yAxis = xAxis.getAssociatedAxes().get(0);
var xSerieDef = xAxis.getSeriesDefinitions().get(0);
var ySerieDef1 = yAxis.getSeriesDefinitions().get(1);
// Set the series identifier for the first displayed series an empty string.
//ySerieDef1.getSeries().get(0).setSeriesIdentifier("");
// Make the legend seperator invisible.
icsc.getChartInstance().getLegend().getSeparator().setVisible(false);
}
</pre>
As you can see the setting of the series identifier to an empty string is commented out yet I still see:<br />
<br />
<ul class='bbc'><li>1</li><li>3</li><li>5</li><li>7</li></ul>
So the only thing that this function is doing is hiding the legend separator. I forgot to mention that in this chart the x and y axis are flipped.<br />
<br />
Stephen
mwilliams
Can you recreate your issue with the sample database so I can run it and test? Thanks!
zax
I have spent some time on setting up the sample database.. I run Linux and the case sensitivity of the data files seems to be an issue. Also I had to convert the file's encoding to UTF-8. Small things really.
mwilliams
I meant sample database as in the Classic Models sample database that comes with BIRT. You shouldn't have to set anything up. The data is there already!
You just have to add the dataSource and make a query for the dataSet.