Saturday, July 4, 2009

Render Charts in SAP using Google Chart API


REPORT zpwtest1 .
 
DATA : i_html TYPE w3htmltabtype,
g_url TYPE w3url .
 
DATA: g_html_control TYPE REF TO cl_gui_html_viewer .
 
START-OF-SELECTION .
CALL SCREEN 100.
 
*&---------------------------------------------------------------------*
*& Module STATUS_0100 OUTPUT
*&---------------------------------------------------------------------*

MODULE status_0100 OUTPUT.
 
SET PF-STATUS 'PF01' .
SET TITLEBAR 'TIL01' WITH 'Sample'.
 
IF g_html_control IS INITIAL .
 
CREATE OBJECT g_html_control
EXPORTING
parent = cl_gui_container=>default_screen.
 
PERFORM bar_chart .
 
CALL METHOD g_html_control->load_data
EXPORTING
type = 'text'
subtype = 'html'
IMPORTING
assigned_url = g_url
CHANGING
data_table = i_html
EXCEPTIONS
dp_invalid_parameter = 1
dp_error_general = 2
cntl_error = 3
OTHERS = 4.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE 'S' NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
 
CALL METHOD g_html_control->show_url
EXPORTING
url = g_url.
 
ENDIF .
ENDMODULE. " STATUS_0100 OUTPUT
 
*&---------------------------------------------------------------------*
*& Module USER_COMMAND_0100 INPUT
*&---------------------------------------------------------------------*

MODULE user_command_0100 INPUT.
IF sy-ucomm = 'BACK' .
CLEAR g_html_control .
FREE g_html_control .
 
LEAVE TO SCREEN 0 .
ENDIF.
ENDMODULE. " USER_COMMAND_0100 INPUT
 
*&---------------------------------------------------------------------*
*& Form BAR_CHART
*&---------------------------------------------------------------------*

FORM bar_chart .
 
DATA lv_html TYPE string .
 
CONCATENATE
'<html>'
'<body>'
 
'<img src="http://chart.apis.google.com/chart?'
 
* Data

'&chd=t:10,20,90,40,70,60,50,40,20,40'
 
* Chart Type

'&cht=bhs'
 
* Size

'&chs=700x350'
 
* Scale

'&chds=0,100'
'&chxt=x,y'
'&chxl=1:|Dollar|Yen|Pound|Franks|Euro|Ruble|Peso|Dirham|Rupee|Dinar|'
'0:|0|10|20|30|40|50|60|70|80|90|100'
 
* Chart Heading

'&chtt=Sample+Bar+Chart'
 
* Backgroud lines

'&chg=10,0'
 
'&alt="Sample chart" />'
 
'</body>'
'</html>'
INTO lv_html .
 
CALL FUNCTION 'HR_EFI_CONVERT_STRING_TO_TABLE'
EXPORTING
i_string = lv_html
i_tabline_length = 255
TABLES
et_table = i_html.

ENDFORM. " BAR_CHART