Hiding a chart series based on dataset content

edited November 2013 in DevShare Downloads
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.
Sign In or Register to comment.