Sample of how a bar chart can display dynamic marker lines

Clement WongClement Wong EM mod
edited July 30, 2014 in DevShare Downloads
If there is a requirement on a bar chart to display dynamic marker lines (such as a minimum line, maximum line, or average line), then this script will create a minumum, average, and maximum marker/threshold line on the Y-Axis.



In the afterDataSetFilled event, the minimum, maximum and average values are saved into a global variable.



In the beforeGeneration event, the line markers are drawn in.



Works with OS BIRT charts and commercial BIRT HTML5 charts. The attached code was targeted for OS BIRT -- the chart output is in PNG format.
function beforeGeneration( chart, icsc )
{
importPackage(Packages.org.eclipse.birt.chart.model.component.impl);
importPackage(Packages.org.eclipse.birt.chart.model.data.impl);
importPackage(Packages.org.eclipse.birt.chart.model.attribute);
importPackage(Packages.org.eclipse.birt.chart.model.attribute.impl);
var chart = icsc.getChartInstance();
var yAxis = chart.getAxes().get(0).getAssociatedAxes().get(0);
var min_value = icsc.getExternalContext().getScriptable().getGlobalVariable("minimum");
var avg_value = icsc.getExternalContext().getScriptable().getGlobalVariable("average");
var max_value = icsc.getExternalContext().getScriptable().getGlobalVariable("maximum");

min_ml = MarkerLineImpl.create(yAxis, NumberDataElementImpl.create(min_value));
min_ml.getLabel().getCaption().setValue("Min " + parseInt(min_value));             // parseInt
min_ml.getLabel().getCaption().getFont().setName("Arial");
min_ml.getLabel().getCaption().getFont().setSize(8);
min_ml.getLabel().getCaption().setColor( ColorDefinitionImpl.create(136,136,136));
min_ml.getLineAttributes().getColor().set(173,210,252);
min_ml.getLineAttributes().setThickness(1);

avg_ml = MarkerLineImpl.create(yAxis, NumberDataElementImpl.create(avg_value));
avg_ml.getLabel().getCaption().setValue("Average " + parseFloat(avg_value).toFixed(2));
avg_ml.getLabel().getCaption().getFont().setName("Arial");
avg_ml.getLabel().getCaption().getFont().setSize(8);
avg_ml.getLabel().getCaption().setColor( ColorDefinitionImpl.create(136,136,136));
avg_ml.getLineAttributes().getColor().set(173,210,252);
avg_ml.getLineAttributes().setThickness(1);

max_ml = MarkerLineImpl.create(yAxis, NumberDataElementImpl.create(max_value));
max_ml.getLabel().getCaption().setValue("Max " + parseInt(max_value));            // parseInt
max_ml.getLabel().getCaption().getFont().setName("Arial");
max_ml.getLabel().getCaption().getFont().setSize(8);
max_ml.getLabel().getCaption().setColor( ColorDefinitionImpl.create(136,136,136));
max_ml.getLineAttributes().getColor().set(173,210,252);
max_ml.getLineAttributes().setThickness(1);
}


function afterDataSetFilled( series, dataSet, icsc )
{
if( series.getSeriesIdentifier() == "Series 1"){
    var list = dataSet.getValues();
    
    var total=new Number();
    var theMin=new Number();
    var theMax=new Number();
    var items=list.length;
    
    theMin=list[0];
    for (i=0; i < items; i++) {
        total = total + Number(list[i]);
        if (list[i] < theMin) theMin = list[i];
        if (list[i] > theMax) theMax = list[i];
    }
 
    icsc.getExternalContext().getScriptable().setGlobalVariable("minimum",theMin);
    icsc.getExternalContext().getScriptable().setGlobalVariable("average",(total/items));
    icsc.getExternalContext().getScriptable().setGlobalVariable("maximum",theMax);
    }
}

Clement Wong
Principal Analytics Architect

OpenText Corporation
http://www.actuate.com | http://www.opentext.com

Sign In or Register to comment.