Change Bar Color based on Value

micajblock
edited February 11, 2022 in Analytics #1
This simple example will change the bar color to green if positive (or 0) value and red if negative. It uses a simple scripted data set so you should be able to run it as is.<br />
<br />
The design is using BIRT 4.2 but will work in almost any version.<br />
<br />
The code for the scripted data source is just used to populate dummy data and looks like this:
counter++;
if (counter == 1) {
row&#91;"Category"] = "Category A";
row&#91;"Value"] = 3;
return true;
}
else if (counter == 2) {
row&#91;"Category"] = "Category B";
row&#91;"Value"] = -1;
return true;
}
else if (counter == 3) {
row&#91;"Category"] = "Category C";
row&#91;"Value"] = 2;
return true;
}
else if (counter == 4) {
row&#91;"Category"] = "Category D";
row&#91;"Value"] = -2;
return true;
}
else  {
return false;
}
<br />
The code on the chart is on the onRender event and looks like this:
function beforeDrawDataPoint( dph, fill, icsc )
{
var seriesName = dph.getSeriesDisplayValue();


if (seriesName == "Series 1") {


  var val = dph.getOrthogonalValue();
  var lab = dph.getBaseDisplayValue();


if (val &lt; 0) {
   // Set to Red
        fill.set(255, 0, 0);
   }
  else  {
  // Set to Green
        fill.set(0, 255, 0);
        }


}
}
<br />
Note you can also get the label value.