Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Eclipse's BIRT: Setting Bar-Chart Colors Programmatically via Scripting

DZone's Guide to

Eclipse's BIRT: Setting Bar-Chart Colors Programmatically via Scripting

· Big Data Zone
Free Resource

Need to build an application around your data? Learn more about dataflow programming for rapid development and greater creativity. 

Using Eclipse's BIRT for creating reports, especially the chart item report element, you can come to a situation where the colors of the chart must be changed depending on a dataset (datarow) or report parameter. There are two main solutions to finish this task. One way to set the colors is to use scripting[ref] with JavaScript (Rhino[ref]) in the report designer. The other solution is to use the BIRT DEAPI[ref] and add the script via Java. In this article I want to describe the approach by using scripting in the report design/library/template file.

The idea for this solution can be found in the BIRT developer forum[ref][ref]. It was my question which was answered from one member of the forum moderator team which are working as eclipse BIRT support as well.

Before describing the solution, some terms used in eclipse's BIRT should be presented.

Report parameters

One option to pass data to the report is using the report parameter. The report parameter can have simple structure like numbers, strings or more complex structure like any kind of java objects. They can be used anywhere, in every text like report item or script, in the report for manipulating the behavior or the layout of the report depend on them. In this example I will try to show how color information are pass as report parameters which are used later as bar chart colors. The syntax for using an report item in rptdesign, rptlibrary or rpttemplate file can look like:

params["someparameter"].value

Datasource / Dataset

The main content of the report is delivered via datasets which are bound on data sources. They can have any nature such as SQL select statements on database tables or XPath to some destination on the incoming XML file, Web-Services, CSV file or java objects. In this example I will use data set which is holding the color information as integer values. The syntax for using an report item in rptdesign, rptlibrary or rpttemplate file can look like:

row["somecolumn"]

Persistent global variable

Persistent global variables like the name says are variables which are saved globally and can be used anywhere in the report. For this task the colors are saved in a persistent global variable as red, green, blue integer values (0-255). The syntax for the variable can look like:

//write
reportContex.setPersistentGlobalVariable("globalParameterId", "content");

//read
reportContex.getPersistentGlobalVariable("red");

Solution description

The data from the dataset / datarow or report parameter should be copied in a global report parameter objects and then, they can be used in one of the chart methods where the content is set to the color properties of the chart. The solution for this task can be split into several tasks:

  • Setting the color data to a persistent global variable.
  • Setting the data from the persistent global variable to the chart.

 Setting the color data to a persistent global variable

Hint: The following code should be placed in a "onCreate"-method of a parent item of the chart such as the grid report item when the information is in a dataset and is rolled out in a table or list report items element. If the colors as passed via report parameters then the script can be placed global on the report element.

//examples for incomming parameters
reportContex.setPersistentGlobalVariable("red", params["red"].value);
reportContex.setPersistentGlobalVariable("blue", params["blue"].value);
reportContex.setPersistentGlobalVariable("green", params["green"]value);

//examples for datarow from dataset
reportContex.setPersistentGlobalVariable("red", row["Red"]);
reportContex.setPersistentGlobalVariable("blue", row["Blue");
reportContex.setPersistentGlobalVariable("green", row["Green"));

From the code above you can see how the color information are passed to the global variables. The next step is to set the colors in the bar chart. For that we need to know what kind of chart we have (here bar chart) and how many series are used for that chart since we need to put the colors to the series as well.

Setting the data from the persistent global variable to the chart

The last step of the task is to put the data from the persistnent global variable into the bar chart color property. This is done in the "onRender"-method of the chart report item and has structure like in the following example:

importPackage(Package.org.eclipse.birt.chart.model.impl);
importPackage(Package.org.eclipse.birt.chart.model.type.impl);
importPackage(Package.org.eclipse.birt.chart.model.attribute);
importPackage(Package.org.eclipse.birt.chart.model.attribute.impl);

function beforeGeneration( chart, icsc ) {
	//one series
	sd = chart.getAxes().get(0).getAssociatedAxes().get(0).getSeriesDefinitions().get(0);
	sd.getSeriesPalette().getEntries().clear();
	var red = parseInt(icsc.getExternalContex().getScriptable().getPersistentGlobalVariable("red"));
	var blue = parseInt(icsc.getExternalContex().getScriptable().getPersistentGlobalVariable("blue"));
	var green = parseInt(icsc.getExternalContex().getScriptable().getPersistentGlobalVariable("green"));
	sd.getSeriesPalette().getEntries().add(GradientImpl.create(ColorDefinitionImpl.create(red, blue, green), ColorDefinitionImpl.create(red, blue, green), -35, false));
	//two series - adding one color more
	sd = chart.getAxes().get(0).getAssociatedAxes().get(1).getSeriesDefinitions().get(0);
	sd.getSeriesPalette().getEntries().clear();
	var red = parseInt(icsc.getExternalContex().getScriptable().getPersistentGlobalVariable("red"));
	var blue = parseInt(icsc.getExternalContex().getScriptable().getPersistentGlobalVariable("blue"));
	var green = parseInt(icsc.getExternalContex().getScriptable().getPersistentGlobalVariable("green"));
	sd.getSeriesPalette().getEntries().add(GradientImpl.create(ColorDefinitionImpl.create(red, blue, green), ColorDefinitionImpl.create(red, blue, green), -35, false));
	// if you need one color without gradient
	sd.getSeriesPalette().getEntries().add(ColorDefinitionImpl.GREEN());	
}

You can also iterate trough the series with an iterator which is not shown here.

Résumé

This article was dedicated to setting chart colors via scripting in eclipse's BIRT. Beside that some basic terms where mentioned like report parameters, dataset/datasources and persistent global variables. Acctually the approach to do this is easy if you know the right syntax. That was the main motivation to write this article.

Check out the Exaptive data application Studio. Technology agnostic. No glue code. Use what you know and rely on the community for what you don't. Try the community version.

Topics:

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}