Home DevShare Designing BIRT Reports

Hiding a chart series based on dataset content

Share

by paqman

Forum - 50 postsLeaderboard - 1 timePoll VoterDevShare - 1 post
Posted 16 Dec 2011 - 12:55 PM

(0)  (0)   (565 views)

Demonstrates how to toggle series visibility based on the series data using a script.

Birt Version:-3.7,2.6

One report I was designing needed to hide a series if it did not contain any data greater than 0. In the case of this report, the chart contained 3 series, and the third one needed this special ability to disappear if it was empty.

To accomplish this, I added the following script to the chart:

hasTarget = false;

/**
 * Called before generation of chart model to GeneratedChartState.
 *
 * @param chart
 *            Chart
 * @param icsc
 *            IChartScriptContext
 */


function beforeGeneration( chart, icsc )
{
        yAxis
= chart.getPrimaryOrthogonalAxis(chart.primaryBaseAxes[0]);
       
       
if (hasTarget == false)
       
{
                yAxis
.seriesDefinitions[2].series[0].visible = false;
       
}
}

/**
 * Called after populating the series dataset.
 *
 * @param series
 *            Series
 * @param dataSet
 *            DataSet
 * @param icsc
 *            IChartScriptContext
 */


function afterDataSetFilled(series, dataSet, icsc)
{
        importPackage
(Packages.java.io);
        importPackage
(Packages.org.eclipse.birt.chart.model.type.impl);
        importPackage
(Packages.org.eclipse.birt.chart.util);
        importPackage
(Packages.java.util);
        ps
= PluginSettings.instance();

       
if (series.getSeriesIdentifier() == "Series 3")
       
{
                dsp
= ps.getDataSetProcessor(series.getClass());
               
if (dsp.getMaximum(dataSet) > 0)
               
{
                        hasTarget
= true;
               
}
       
}
}

The function afterDataSetFilled checks for the maximum value of the third series. If it is greater than 0 (has data) a global variable hasTarget is set to true. Later on, the function beforeGeneration checks the value of hasTarget, and hides the third series if necessary.

 
Filter More