Thursday, February 25, 2010

Customize CDF jfreechart component

I was dealing with CDF Dashboards during these days and I had to set line-width and line-style attributes in my jFreechartComponent's LineChart widget.

Looking at the jFreechartComponent documentation you clearly see that in the standard implementation some attributes, specifics only to some chart types, aren't implemented. This was also for the two attributes line-width and line-style related to the LineChart widget.  But this is not a problem because we can add that missing attibutes very easily and I'll show you how. The interesting thing is that I can manage all the attributes that are available for a specific JFreeChart chart type.

If you need to add support for missed JFreeChart chart's attributes you can follow the steps summarized below. We suppose to add line-width and line-style attributes and to access them from CDF jFreechartComponent as two properties respectively named lineWidth and lineStyle.

1) Go to <biserver-home>/pentaho-solutions/cdf/components and open jfreechart.xaction and open it with your favourite editor.

2) Add two new elements to the inputs section of the .xaction file. Below an example of the file with the two new inputs added. Remember here that the value of the request element (see lines 5 and 11 below)  has to be the same in name and in case as the name of the property we want to put in our CDF jFreechartComponent definition. Then be careful to correctly assign a the default value needed in case that the property isn't provided (see lines 7 and 13) by you CDF component call. This is particularly important when our attribute is a number. If you don't assign it properly you'll get a NumberFormatException.

1:  <inputs>   
2:    <!-- Omitted for brevity -->       
3:       <LINEWIDTH type="string">  
4:            <sources>  
5:                 <request>lineWidth</request>  
6:            </sources>  
7:            <default-value>1.5</default-value>  
8:       </LINEWIDTH>  
9:       <LINESTYLE type="string">  
10:            <sources>  
11:                 <request>lineStyle</request>  
12:            </sources>  
13:            <default-value/>  
14:       </LINESTYLE>  
15:    <!-- Omitted for brevity -->       
16:  </inputs>  

3) Add the new inputs previously defined in the inputs section (see above) to the  action-inputs section of our ChartComponent action-definition (as shown below at lines 7 and 8). Then, use these new fields to populate two new property elements added to the chart-attributes section as shown at line 25-26. Be very careful here because the name of the element you give to every new chart attribute has to be equal to the related attribute for the JFreeChart library. For example: if in JFreeChart library the line width  attribute is named line-width that is the name to be considered as child element of the chart-attributes element. Then the value to be assigne to that new element is the name of the related action input element (for line-width we will assign as value the LINEWIDTH element. See line 25 below).

1:       <action-definition>   
2:            <component-name>ChartComponent</component-name>  
3:            <action-type>Chart</action-type>  
4:            <action-inputs>   
5:                 <chart-data type="result-set" mapping="newResults"/>   
6:                  <!-- Omitted for brevity -->       
7:                 <LINEWIDTH   type="string"/>  
8:                 <LINESTYLE   type="string"/>  
9:                 <!-- Omitted for brevity -->       
10:            </action-inputs>  
11:            <action-resources/>  
12:            <action-outputs>   
13:                 <chart-filename type="string"/>   
14:                 <base-url type="string"/>   
15:                 <chart-mapping type="string"/>   
16:                 <image-tag type="string"/>   
17:                 <chart-output type="content"/>   
18:            </action-outputs>  
19:            <component-definition>   
20:                 <width>{WIDTH}</width>   
21:                 <height>{HEIGHT}</height>   
22:                 <by-row>{BYROW}</by-row>   
23:                 <chart-attributes>   
24:                 <!-- Omitted for brevity -->       
25:                      <line-width>{LINEWIDTH}</line-width>  
26:                      <line-style>{LINESTYLE}</line-style>  
27:                 <!-- Omitted for brevity -->       
28:                 </chart-attributes>   
29:            </component-definition>   
30:       </action-definition>  

No comments:

Post a Comment