Setting BIRT chart series palette dynamically, part 3 – stacked bar charts

September 9th, 2010 | Tags: ,

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.

Stacked bar chart with carefully selected=

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:

Stacked bar chart with only two categories, palette colours are confusing

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.

Stacked bar chart with two categories and dynamically assigned palette colours

  1. Mario Robles
    December 22nd, 2010 at 15:40
    Reply | Quote | #1

    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!

  2. December 28th, 2010 at 15:20
    Reply | Quote | #2

    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!

  3. Mario Robles
    January 28th, 2011 at 16:13
    Reply | Quote | #3

    Got it!! Thanks, Great Tutorials by the way..

  4. floh
    July 1st, 2011 at 12:37
    Reply | Quote | #4

    Hi,

    ok it works also in a line chart with the legend and the marks, but how with the lines?

    thanks

  5. roland
    June 15th, 2012 at 13:28
    Reply | Quote | #5

    Hello,
    Thanks for your tuto, excellent, clear, very usefull for me.

    good job !

  6. Siva Rao
    December 14th, 2017 at 10:02
    Reply | Quote | #6

    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.