Hiding a chart series based on dataset content

Options
paqman
edited February 11, 2022 in Analytics #1
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.<br />
To accomplish this, I added the following script to the chart:hasTarget = false;<br />
<br />
/**
 * Called before generation of chart model to GeneratedChartState.
 *
 * @param chart
 *            Chart
 * @param icsc
 *            IChartScriptContext
 */

function beforeGeneration( chart, icsc )
{
        yAxis = chart.getPrimaryOrthogonalAxis(chart.primaryBaseAxes&#91;0]);
       
        if (hasTarget == false)
        {
                yAxis.seriesDefinitions&#91;2].series&#91;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) &gt; 0)
                {
                        hasTarget = true;
                }
        }
}
<br />
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.