Dynamic Font Sizing in Birt Tutorial


This is a brief guide on how to increase or decrease the font size of an element in Birt depending the length of a string value from a data set / variable.
This can be used where space is limited but the full text is required - eg product desciptions and names on Price Labels.
I will use the CLASSICMODELS sample Dataset from the Birt documentation for my example, and have included a working example file below.


Steps

  1. Select the element and open the scripts section.
  2. In the OnRender script enter the below code
  3. // Initialize to length zero 
    var totalLength = 0;
    // Replace YourColumnName with the name of your data value
    if (row["YourColumnName"]) {
     totalLength += row["YourColumnName"].length;
    }
    switch (true) {
     case (totalLength < 10):
     this.getStyle().fontSize = "40pt";
     break;
     case (totalLength > 10 && totalLength <18):
     this.getStyle().fontSize = "30pt";
     break;
     case (totalLength > 18):
     this.getStyle().fontSize = "18t";
     break;
     default:
     this.getStyle().fontSize = "10pt";
     break;
    }
    
    
  4. Save & Preview will give the below output. Note I have applied to the Product name element.

    A simple example of the output of the above. A) String of length 16 B) String of Length 23.

An example can be found in the attached file here

Comments