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)
Hide series if empty using a script
paqman
Hey guys,<br />
<br />
I've done some searching online but haven't found a solution for this yet. Our chart, showing data month-by-month, has up to 4 series:<br />
<br />
- Value (always visible)<br />
- Baseline data (optional)<br />
- Target data (not optional, but may not always return data)<br />
- Temperature data (optional)<br />
<br />
Our current code, before I added the target series, checked if baseline/temperature parameters were specified. If neither were, then it hid the legend (since it would only have 1 item). If either of them were provided, it would show the legend, and just hide the series which didn't have a corresponding parameter provided.<br />
<br />
Adding the target was more tricky - it is not a parameter, but may end up returning no data. So rather than checking if a parameter was provided, it needs to check if the series is empty. Then apply the same logic - if all 3 are empty, don't show the legend.<br />
<br />
I'm stuck trying to iterate through the series - my current code is:<br />
<br />
<pre class='_prettyXprint _lang-auto _linenums:0'>
// Hide target series if no values returned
var count = 0;
yAxis = chart.getPrimaryOrthogonalAxis(chart.primaryBaseAxes[0]);
var targetData = yAxis.seriesDefinitions[0].series[2].getDataSet();
for (i = 0; i < targetData.length; i++)
{
count += ((targetData[i] == null) ? 0 : 1);
}
if (count == 0)
{
yAxis.seriesDefinitions[0].series[2].visible = false;
noTarget = true;
}</pre>
<br />
The for loop is what I'm not sure of - this clearly isn't working but I can't find anything to suggest what function would iterate through all of the data series values. <br />
<br />
Thanks for the help!<br />
<br />
David
Find more posts tagged with
Comments
mwilliams
Maybe this will help:
http://www.birt-exchange.org/org/devshare/designing-birt-reports/1114-turning-a-chart-series-off-in-script/
Or is your only issue figuring out if there is data for the series? If so, can you do that external to the chart and pass it in through a persistent global variable?
paqman
Yeah I'm just am stuck figuring out if there is data for the series. I guess I need to add a hidden table to aggregate the data and then set a global variable to show/hide that series then...? Is there another way to go through all the rows in the script though?
mwilliams
Unless you're filtering after the dataSet, you could check for values in the dataSet's onFetch script. You might also be able to find this out in the AfterDataSetFilled function in your chart script.
paqman
Aha - afterDataSetFilled is exactly where I needed to look. The following code accomplishes what I was looking for:<br />
<br />
<pre class='_prettyXprint _lang-auto _linenums:0'>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;
}
}
}</pre>
<br />
Thanks for the help Michael!
mwilliams
Great! Thanks for the update! If you've got time, you might make a simple example showing this in the devShare!
paqman
Good idea!
http://www.birt-exchange.org/org/devshare/designing-birt-reports/1448-hiding-a-chart-series-based-on-dataset-content/