Sunday, April 22, 2007

BIRT: Style Sheets and Themes

One of the things that I have the tendency to completely overlooked are style sheets. I typically just regard them as tricks for graphic designers, and have little to no consequence for a developer. However, with BIRT, I actually have an opportunity to play with and learn how to apply style sheets since BIRT supports style sheets for reports.

Style sheets are a means of creating a layout configuration that can be reused consistently throughout a page. This allows for uniform and consistent layouts, and helps prevent developers from having to constantly redo the same graphical properties, such as font attributes, alignment, colors, and so forth.

Creating style-sheets in BIRT is a fairly simple task, and in practice doesn’t deviate too much from web design environments such as Dreamweaver. With BIRT, however, you have pre-defined styles that will automatically apply to various sections of your report, such as to table rows, headers, or footers. This is nice since you can build a whole slew of these in a library and use them in reports as themes, and keep a consistent look and feel among all your reports.

In the below examples, we will demonstrate how to build a theme into a library and apply them to a report.

First thing you need to do is create a new library. So in your BIRT report projects, from the Project Navigator, right mouse click on the project, and select new library. For my example, I called my library styles.rptlibrary. Open up the Outline tab, and look under the Themes group. This is where your themes and your styles get stored in libraries. Themes are overall, general layouts for reports, and contain any number of styles. Styles are individual style-sheets. These apply to a single report entity, such as report headers, table headers, rows, cells, etc. For our example, I want to create a new Theme called CheeseStyles that will contain a very simple theme for my demo reports.

Figure 1. The CheeseStyle Theme with a few styles

As illustrated in my above screenshot, I already have created 2 styles, one for my table detail rows and one for my table header. The requirements for my layouts are that my headers have an all black background with bolded white text for the column headers, and that my detail rows alternate between light grey to light blue, with black text. In order to accomplish this, I will create a few style sheets, one for the header and one for the detail row.

Since the header row is the easier one of the two to create, I will demonstrate it first. In order to create it I will select my Theme, right-mouse click, and choose New Style. I could create a new style, but there are already pre-defined style elements for most of visual report elements, so I will choose “table-header” from the list of predefined styles. Once selected, I will select the following properties:

  • -Font:
    • -- Color = White
    • -- Size = Large
    • -- Weight = Bolder
  • -Background:
    • -- Color = Black
  • -Text Block:
    • -- Text Alignment = Left
  • -Box:
    • -- All Padding Elements = 1 Points
  • -Border:
    • -- Color:
      • ---- All Elements = Black

This will take care of all of my headers for my reports when I apply this theme to them. Next I want to create my row details (table-detail from predefined styles), and will use the following parameters:

  • -Background:
    • -- Background Color = Silver
  • - Box:
    • -- Padding All parameters = 1 Points
  • -Border:
    • -- Color All parameters = White
  • -Highlights:
    • -- This one is a little tricky to set up. What you want to do is create a new highlight rule, with the condition set to “(row.__rownum % 2) equal 0”. Set the background color to RGB(128, 128, 255). This will allow every other row in the style to alternate background colors, right from a style sheet.

And that’s it, my basic theme is created and stored in my library. Now, what I want to do is create a new report. In my example I created a report called testStyleReport.rptdesign. I used the Classic Cars database as my data source, and used the following query:

select *


I drag over this new data set to my report design in order to create my report. Now that I have a basic report, I want to apply my new theme. In order to do this, I go over to the Library Explorer, and under Shared Libraries, I select my style.rptlibrary file, right-mouse click, and choose Use Library. Now that this library is included in my report design, I go to the outline, choose my testStyleReport main root element from the tree view, and under the Property Editor, I open the General tab, and under the Themes drop down list, I select styles.CheeseStyle. That’s all there is to it. I now have my Theme applied to my report.

Figure 2. Report with Theme Applied


Bill Ford said...

Thanks for the information, I had some initial success then hit a hard roadblock. I created a new library, then created a theme. I then put two styles in my them, which were table-detail and table-header, just like you did.

Worked great. So the next day I go back to my custom them to add some label styles. But the right-click menu no longer has "New Style", just has "Refresh" and "Add New Library". I rebooted, I created a new library, I cursed; never could get the "New Style" option to come back. Has anyone experienced this?

I am running version 2.1.3 of Birt and Eclipse 3.2; I downloaded and installed them using the All-In-One install last week (12/2007). Vista Home Premium, Java 1.6. I'm a Leo.

N. Nagaraju said...

Thanks a lot. I have gotten a great help from u.