Additional Blogs by SAP
cancel
Showing results for 
Search instead for 
Did you mean: 
kai_gutenkunst
Active Contributor

Modifying chart settings at runtime

Using the BI chart item in a web template is mandatory if you want to visualize data in an attractive way. Editing a chart item in the Web Application Designer is supported by a wizard that leads you through the major set of chart properties. In case you need even more properties to change the charts appearance you can also switch the editor to the ‘expert' mode to see and modify the complete set of chart attributes.
All those customizing settings of the chart are edited in WAD, are saved in the system and finally are sent to the IGS when you run the web template.
But sometimes you have the need to slightly modify some of the charts customizing settings at runtime - depending on e.g. the query result. The good news: setting properties of the chart dynamically is possible!
Let's see how to do this by implementing the BAdI RS_BADI_CHART (enhancement spot ENHS_RS_CHART, interface IF_RS_CHART_ENHANCEMENT~MODIFY_CHART_REQUEST).

As an easy example I want to mark a range of the value axis by a special color but define the minimum / maximum values of such a value range at runtime!

The idea now is to prepare such a value range using the WAD but also to implement some ABAP logic to calculate the min/max values and use them in the customizing settings of the chart.

Preparing the chart in WAD

Let's start with creating a new chart item in a web template. The new item is renamed to ‘MYSPECIALITEM'. Later on this helps to identify the chart as we want to modify this single item only!
Then continue with creating the static customizing settings: in your web template edit the chart item. As a value range is one of those ‘expert' settings you have to push ‘Refine'. Right-click on the Value axis - Grid line and choose the ‘Add Value Range' entry.



Add a new value range

For all those UI designers it's now the time to do some last beautifications for this value range e.g. by changing its color value or choosing a gradient... However, as I want to demonstrate how to use the BAdI here it's more important to have a look at the properties Start and End. These properties define where the value range starts and ends (minimum / maximum). After creating a value range both properties show their default value. As the idea is to replace both values at runtime by some dynamically calculated values there is no further action required now.
Before leaving the chart designer and saving our customizing settings in the system I just renamed the value range to ‘MyValueRange'. We will use this ID in the ABAP coding to ensure that only this special value range is manipulated in the BAdI.




Rename the value range

The chart with its value range

  

Implementing the BAdI

I assume that you already know how to create a new implementation for a BAdI, so let's directly continue with implementing the method IF_RS_CHART_ENHANCEMENT~MODIFY_CHART_REQUEST. This method is called just before the complete information is sent to the IGS for creating the image. The list of parameters does not only contain the customizing of the chart but also the item id, the result of the query and some other specialties. As most of the parameters are CHANGING parameters and therefore can be modified in your BAdI implementation you have to be very careful when doing changes here!
Evaluating the I_ITEM_ID parameter is extremely helpful. As this BAdI is called for each chart item our XML changes have to be restricted: Using this parameter I_ITEM_ID we can easily make sure that only this single special item is modified. All other chart items in the BI system shall be untouched.
In our scenario we want to change the customizing, i.e. use the parameter C_CUSTOMIZING_XML. This parameter contains the XML that was created in WAD when using the chart designer. We check the XML, look for the value range and change the Start and End properties.

 

method if_rs_chart_enhancement~modify_chart_request.

Check if this is our special chart item. If that's not the case the BAdI can return immediately and the IGS works with the original XML values.

check i_item_id eq 'MYSPECIALITEM'.

Do the usual XML preparation.

type-pools: ixml.
class cl_ixml definition load.

* create iXml factory
data: l_ixml type ref to if_ixml.
l_ixml = cl_ixml=>create( ).

* create stream factory
data: l_streamfactory type ref to if_ixml_stream_factory.
l_streamfactory = l_ixml->create_stream_factory( ).

* create input stream
data: l_istr type ref to if_ixml_istream.
l_istr = streamfactory->create_istream_xstring( c_customizing_xml ).

* create output stream
data: l_temp type xstring.
data: l_ostr type ref to if_ixml_ostream.
l_ostr = l_streamfactory->create_ostream_xstring( l_temp ).

* create document
data: l_document type ref to if_ixml_document.
l_document = l_ixml->create_document( ).

* parse Xml
data: l_parser type ref to if_ixml_parser.
l_parser = l_ixml->create_parser( stream_factory = l_streamfactory
  istream = l_istr
  document = l_document ).

* parsing successful?
if l_parser->parse( ) eq 0.

Now the interesting part starts. We look for the ValueRange tag in the XML and also make sure that the special value range is identified (having the ID ‘MyValueRange').

* find our special ValueRange tag
data: l_value_range type ref to if_ixml_element.
l_value_range = l_document->find_from_name( name = 'ValueRange' ).
check l_value_range is not INITIAL.
check l_value_range->get_attribute( name = 'id') = 'MyValueRange'.

After all these checks the properties of the value range can be changed. To do this we look for the Start and End tags and change their values.
To keep this example simple there is no dynamic calculation of a start and end value. Instead the coding uses some hard-coded values.

* and change its Start and End values
data: l_nodes type ref to if_ixml_node_collection,
  l_element type ref to if_ixml_element.
l_nodes = l_value_range->get_elements_by_tag_name( name = 'Start').
if not l_nodes is initial.
  if l_nodes->get_length( ) eq 1.
    l_element ?= l_nodes->get_item( index = 0 ).
    l_element->set_value( value = '0'). " some new value
  endif.
endif.
l_nodes = l_value_range->get_elements_by_tag_name( name = 'End').
if not l_nodes is initial.
  if l_nodes->get_length( ) eq 1.
    l_element ?= l_nodes->get_item( index = 0 ).
    l_element->set_value( value = '250000'). " and also here
  endif.
endif.


After changing the values of those two nodes the XML must be rendered again to update the C_CUSTOMIZING parameter properly.

* serialize Xml
data: l_renderer type ref to if_ixml_renderer,
l_rc type i.
l_renderer = l_ixml->create_renderer( ostream = l_ostr
  document = l_document ).
l_rc = renderer->render( ).

* set changing parameter
c_customizing_xml = l_temp.
endif.
endmethod.

 


The chart with a modified value range

Having seen this simple BAdI implementation the general idea of changing customizing settings dynamically is hopefully clear now 🙂 If you are unsure about the structure of the customizing XML I recommend that you download the SAP Chart Designer from the SDN Downloads page. Using this tool you can easily create and change charts independent of the WAD. It also allows saving the settings as a local XML file for further investigations.

The enhancement spot used in this article is available with BI 7.0 EhP 1.

2 Comments