Setting BIRT chart series palette dynamically, part 3 – stacked bar charts
In what is rapidly becoming a mini-series, we have so far discussed how to set BIRT chart palette dynamically (based on category values) for pie charts and area charts. Today we cover stacked bar charts, i.e. bar charts with the Optional Grouping data element.
Once again, let’s start with a quick recap. In what is a fairly common scenario, one chooses the palette colours to match with particular category values. For example, in the chart below “High” is represented in green, “Mid” in orange and “Low” in red.
However, if for a particular dataset one of the of category values is not present, the palette colours are still used in the same order, meaning the “Mid” area is now shown as red and “High” as orange. This is confusing for the end user:
As in the previous examples, we solve this problem by adding a little bit of script to override the chart’s default behaviour.
function beforeDrawDataPoint( dph, fill, icsc )
{
var sValue = dph.getSeriesDisplayValue();
// set( R, G, B, alpha )
if( sValue == "1-Low" )
{
fill.set( 242, 88, 106, 255 );
}
else if( sValue == "2-Mid" )
{
fill.set( 232, 172, 57, 255 );
}
else if( sValue == "3-High" )
{
fill.set( 128, 255, 128, 255 );
}
}
We also need to override the beforeDrawLegendItem
method, so that colours in the legend match the chart colours.
function beforeDrawLegendItem( lerh, bounds, icsc )
{
var sValue = lerh.getLabel().getCaption().getValue();
var fill = lerh.getFill();
// set( R, G, B, alpha )
if( sValue == "1-Low" )
{
fill.set( 242, 88, 106, 255 );
}
else if( sValue == "2-Mid" )
{
fill.set( 232, 172, 57, 255 );
}
else if( sValue == "3-High" )
{
fill.set( 128, 255, 128, 255 );
}
}
The palette colours are now assigned dynamically based on actual category values. The rendered chart looks as shown below, independent of the colours selected in Chart Wizard and their order.
This is exactly what I need to do, but since I’m new to Birt, I do not know where to insert these scripts. Any help is appreciated.
thx!
Hello Mario,
The steps are pretty straightforward:
1. Open your report in Eclipse.
2. Left click on the chart so that it has focus.
3. Click on the Script tab at the bottom of the main sub-window (from left to right the tabs read Design | Master Page | Script | XML Source | Preview).
4. You can then simply copy and paste the code from this blog post into the Script pane (both functions).
5. Edit the code to replace “1-Low” etc. with the values you need as well as the colours you want.
6. Switch back to the Design pane, save your report and continue as usual.
Hope this helps!
Got it!! Thanks, Great Tutorials by the way..
Hi,
ok it works also in a line chart with the legend and the marks, but how with the lines?
thanks
Hello,
Thanks for your tuto, excellent, clear, very usefull for me.
good job !
In my chart i have 2 series,one is line and another is bar.i want to change the bar color based on line value.If bar value>line value then bar should be in red color and If bar value<line value then bar should be in green color.
thanks in advance.