Home
Analytics
Formatting Data Types within BIRT Report via script
sbrar
Hi,
I have a cross tab report, my data is an element called 'BaseData' I have pulled from my dataset. This contains a mixture of unit types - 'Number', '?000', 'Kilo' and 'Percentage'. I need to format them as following:
1. Number, ?000 and Kilo to 0 decimal places with.
2. Percentage to 1 decimal place with the % symbol showing.
I understand I can add onto the script of the data element itself under the 'OnRender' tab but have limited scripting knowledge and was hoping someone can help out.
Thanks
Find more posts tagged with
Comments
mwilliams
If I'm understanding correctly, you should be able to use something like the following in your script to format the numbers:
this.getStyle().numberFormat = "formatString"
sbrar
Hi,
Thanks for your reply. Unfortunately that still does not mean a great deal to me, have you got some detailed explanation as to how I can use this specifically on the data types I stated previously?
Thanks
mwilliams
You would use that in your onCreate() script method of your measure element you're wanting to format. Can you show a sample of what your crosstab looks like currently? It will be easier to describe what you need to do.
sbrar
Hi,
Its fairly simple looking:
[PeriodName]
[MeasureName]
[BaseData]
Its on the [BaseData] data element I am looking to add some script. If I for example wanted to apply the script to format the 'Percentage' data type using (this.getStyle().numberFormat = "formatString") how would I go about that?
Thanks
mwilliams
How do you know whether the current value in the column is a percent or a regular number? The way I'm understanding this is that you have a general column that has different numbers in it that you want to format dynamically. Is this not the case? If the value only needs to be formatted one way for a given crosstab, you could just use the property editor - Format Number section. Does the "Measure Name" tell you what you're looking at so that you can format the current "BaseData"? Let me know.
sbrar
Hi,
Your understanding is correct, the way we know the value is a percentage or number is based on the 'Unit Type' within the Measure properties in BIRT Performance Scorecard. For example, all percentages are of Unit Type 'Percentage' and so on...Yes, the MeasureName does tell you what you are looking at e.g. the first row within the cross tab will show the MeasureName and the Numbers (BaseData) for the 12 month period. As I understand I am unable to use the property editor as there are various types I need to format all within the 1 data element 'BaseData' so I was hoping scripting can overcome this.
Thanks
mwilliams
Ok, what is your BIRT version? I'll work on it in your version and see if I can figure something out.
sbrar
Thanks, my BIRT version is 3.4.2.
mwilliams
Take a look at the attached example. I put some script in the onCreate of the measure element to format the dollar values with currency. To show that they can be changed depending on the dimension value, I also included the dimension value as part of the format. Let me know if you have any questions.
sbrar
Hi,
Thanks, this is a start on what I am after, how can you incorporate other unit types into the same onCreate script? E.g. adding the percentage 'Data Type' to 1 decimal place with a %.
Thanks
mwilliams
Your dimension will have to key you off onto how you need to format your number. For a percent, you'd need to do something like "##.0%" as the format code. The example I made just showed that you can change the code for each row separately and that you can access the row dimension to be able to determine your code.