30/07/2010

ABAP e GOOGLE API

Necessidade: Sair da rotina...

Solução: Utilizar a API do Google para geração de relatórios e graficos.

Esta é só uma pequena demonstração, podemos alcançar uma gama muito maior de componentes e com certeza acrescentar em muito a satisfação do cliente. Como se diz, uma imagem vale muito mais que linhas após linhas de informação.

Pensando nisso resolvi propor a utilização da API do Google na geração de gráficos facilitando para o cliente o entendimento do seu negócio.

Não utilizei classes globais e nem um module Pool pois ficaria muito mais complicado expôr a ideia que aqui quero apresentar.


REPORT zrr_teste_html.

*----------------------------------------------------------------------*
* -> CLASS cl_html_chart_column DEFINITION
*----------------------------------------------------------------------*
* -> Class For Chart Column
*----------------------------------------------------------------------*
CLASS cl_html_chart_column DEFINITION.

PUBLIC SECTION.

DATA : col_type TYPE c LENGTH 10,
col_value TYPE c LENGTH 100.

ENDCLASS. "cl_html_chart_column DEFINITION

*----------------------------------------------------------------------*
* -> CLASS cl_html_chart_column IMPLEMENTATION
*----------------------------------------------------------------------*
* -> Class For Chart Column
*----------------------------------------------------------------------*
CLASS cl_html_chart_column IMPLEMENTATION.

ENDCLASS. "cl_html_chart_column IMPLEMENTATION

*----------------------------------------------------------------------*
* -> CLASS cl_html_chart_columns DEFINITION
*----------------------------------------------------------------------*
* -> Class For Chart Columns
*----------------------------------------------------------------------*
CLASS cl_html_chart_columns DEFINITION.

PUBLIC SECTION.

METHODS : add_column IMPORTING p_column TYPE REF TO cl_html_chart_column,
get_columns EXPORTING p_string_columns TYPE w3htmltab.

PRIVATE SECTION.

DATA : it_columns TYPE STANDARD TABLE OF REF TO cl_html_chart_column.

ENDCLASS. "cl_html_chart_columns DEFINITION

*----------------------------------------------------------------------*
* -> CLASS cl_html_chart_columns IMPLEMENTATION
*----------------------------------------------------------------------*
* -> Class For Chart Columns
*----------------------------------------------------------------------*
CLASS cl_html_chart_columns IMPLEMENTATION.

METHOD add_column.

APPEND p_column TO me->it_columns.

ENDMETHOD. "add_column

METHOD get_columns.

FIELD-SYMBOLS : <fs_column> LIKE LINE OF me->it_columns.

DATA : v_html TYPE char255.

LOOP AT me->it_columns ASSIGNING <fs_column>.

IF <fs_column> IS ASSIGNED.

CONCATENATE `data.addColumn('` <fs_column>->col_type `', '` <fs_column>->col_value `'); ` INTO v_html.

APPEND v_html TO p_string_columns.

CLEAR v_html.

ENDIF.

ENDLOOP.

ENDMETHOD. "get_columns

ENDCLASS. "cl_html_chart_columns IMPLEMENTATION

*----------------------------------------------------------------------*
* -> CLASS cl_html_chart_row DEFINITION
*----------------------------------------------------------------------*
* -> Class For Chart Row
*----------------------------------------------------------------------*
CLASS cl_html_chart_row DEFINITION.

PUBLIC SECTION.

DATA : row_value TYPE c LENGTH 200.

ENDCLASS. "cl_html_chart_row DEFINITION

*----------------------------------------------------------------------*
* -> CLASS cl_html_chart_row IMPLEMENTATION
*----------------------------------------------------------------------*
* -> Class For Chart Row
*----------------------------------------------------------------------*
CLASS cl_html_chart_row IMPLEMENTATION.


ENDCLASS. "cl_html_chart_row IMPLEMENTATION

*----------------------------------------------------------------------*
* -> CLASS cl_html_chart_rows DEFINITION
*----------------------------------------------------------------------*
* -> Class For Chart Rows
*----------------------------------------------------------------------*
CLASS cl_html_chart_rows DEFINITION.

PUBLIC SECTION.

METHODS : add_row IMPORTING p_row TYPE REF TO cl_html_chart_row,
get_rows EXPORTING p_string_rows TYPE w3htmltab.

PRIVATE SECTION.

DATA : it_rows TYPE STANDARD TABLE OF REF TO cl_html_chart_row.

ENDCLASS. "cl_html_chart_rows DEFINITION

*----------------------------------------------------------------------*
* - >CLASS cl_html_chart_rows IMPLEMENTATION
*----------------------------------------------------------------------*
* -> Class For Chart Rows
*----------------------------------------------------------------------*
CLASS cl_html_chart_rows IMPLEMENTATION.

METHOD add_row.

APPEND p_row TO me->it_rows.

ENDMETHOD. "add_row

METHOD get_rows.

FIELD-SYMBOLS : <fs_row> LIKE LINE OF me->it_rows.

DATA : v_html TYPE char255,
v_size TYPE i.

v_size = LINES( me->it_rows ).

MOVE `data.addRows([ ` TO v_html.

APPEND v_html TO p_string_rows.

CLEAR v_html.

LOOP AT me->it_rows ASSIGNING <fs_row>.

IF <fs_row> IS ASSIGNED.

IF sy-tabix NE v_size.

CONCATENATE `[` <fs_row>->row_value `], ` INTO v_html.

APPEND v_html TO p_string_rows.

CLEAR v_html.

ELSE.

CONCATENATE `[` <fs_row>->row_value `] ` INTO v_html.

APPEND v_html TO p_string_rows.

CLEAR v_html.

ENDIF.

ENDIF.

ENDLOOP.

MOVE `]); ` TO v_html.

APPEND v_html TO p_string_rows.

CLEAR v_html.

ENDMETHOD. "get_rows

ENDCLASS. "cl_html_chart_rows IMPLEMENTATION

*----------------------------------------------------------------------*
* - > CLASS cl_html_chart DEFINITION
*----------------------------------------------------------------------*
* - > Class For Chart Google API
*----------------------------------------------------------------------*
CLASS cl_html_chart DEFINITION.

PUBLIC SECTION.

METHODS : constructor,
add_column IMPORTING p_column TYPE REF TO cl_html_chart_column,
add_row IMPORTING p_row TYPE REF TO cl_html_chart_row,
display_chart.

PRIVATE SECTION.

DATA : o_html_viewer TYPE REF TO cl_gui_html_viewer.

DATA : o_cl_html_chart_columns TYPE REF TO cl_html_chart_columns,
o_cl_html_chart_rows TYPE REF TO cl_html_chart_rows.

DATA : it_html TYPE w3htmltab,
it_html_rows TYPE w3htmltab,
it_html_cols TYPE w3htmltab,
it_html_header TYPE w3htmltab,
it_html_footer TYPE w3htmltab.

DATA : url TYPE c LENGTH 255.

METHODS : get_html_header EXPORTING p_html_header TYPE w3htmltab,
get_html_footer EXPORTING p_html_footer TYPE w3htmltab.

ENDCLASS. "cl_html_chart DEFINITION

*----------------------------------------------------------------------*
* CLASS cl_html_chart IMPLEMENTATION
*----------------------------------------------------------------------*
* - > Class For Chart Google API
*----------------------------------------------------------------------*
CLASS cl_html_chart IMPLEMENTATION.

METHOD constructor.

CREATE OBJECT : o_cl_html_chart_columns,
o_cl_html_chart_rows.

ENDMETHOD. "constructor

METHOD add_column.

me->o_cl_html_chart_columns->add_column( p_column ).

ENDMETHOD. "add_column

METHOD add_row.

me->o_cl_html_chart_rows->add_row( p_row ).

ENDMETHOD. "add_row

METHOD get_html_header.

APPEND '<html>' TO p_html_header.
APPEND '<head>' TO p_html_header.
APPEND '<script type="text/javascript" src="http://www.google.com/jsapi"></script>' TO p_html_header.
APPEND '<script type="text/javascript"> ' TO p_html_header.
APPEND 'google.load("visualization", "1", {packages:["corechart"]}); ' TO p_html_header.
APPEND 'google.setOnLoadCallback(drawChart); ' TO p_html_header.
APPEND 'function drawChart() { ' TO p_html_header.
APPEND 'var data = new google.visualization.DataTable(); ' TO p_html_header.

ENDMETHOD. "get_html_header

METHOD get_html_footer.

APPEND `var chart = new google.visualization.AreaChart(document.getElementById('chart_div')); ` TO p_html_footer.
APPEND `chart.draw(data, {width: 1200, height: 800, title: 'Relatorio de Vendas', ` TO p_html_footer.
APPEND `hAxis: {title: 'Vendas Anuais', titleColor:'#FF0000'} ` TO p_html_footer.
APPEND '}); ' TO p_html_footer.
APPEND '} ' TO p_html_footer.
APPEND '</script> ' TO p_html_footer.
APPEND '</head> ' TO p_html_footer.
APPEND '<body> ' TO p_html_footer.
APPEND '<div id="chart_div"></div> ' TO p_html_footer.
APPEND '</body> ' TO p_html_footer.
APPEND '</html>' TO p_html_footer.

ENDMETHOD. "get_html_footer

METHOD display_chart.

me->get_html_header( IMPORTING p_html_header = it_html_header ).

me->o_cl_html_chart_columns->get_columns( IMPORTING p_string_columns = it_html_cols ).

me->o_cl_html_chart_rows->get_rows( IMPORTING p_string_rows = it_html_rows ).

me->get_html_footer( IMPORTING p_html_footer = it_html_footer ).

APPEND LINES OF it_html_header TO it_html.
APPEND LINES OF it_html_cols TO it_html.
APPEND LINES OF it_html_rows TO it_html.
APPEND LINES OF it_html_footer TO it_html.

CLEAR : it_html_header,
it_html_cols,
it_html_rows,
it_html_footer.

CREATE OBJECT o_html_viewer
EXPORTING
parent = cl_gui_container=>screen0.

o_html_viewer->load_data( IMPORTING assigned_url = url
CHANGING data_table = it_html ).

o_html_viewer->show_url( url = url ).

ENDMETHOD. "display_chart

ENDCLASS. "cl_html_chart IMPLEMENTATION

PARAMETERS: p_x TYPE c LENGTH 1 VISIBLE LENGTH 0.

AT SELECTION-SCREEN OUTPUT.

DATA : o_cl_html_chart TYPE REF TO cl_html_chart.

IF o_cl_html_chart IS NOT BOUND.

DATA : o_cl_html_chart_column1 TYPE REF TO cl_html_chart_column,
o_cl_html_chart_column2 TYPE REF TO cl_html_chart_column,
o_cl_html_chart_column3 TYPE REF TO cl_html_chart_column,
o_cl_html_chart_column4 TYPE REF TO cl_html_chart_column,
o_cl_html_chart_row1 TYPE REF TO cl_html_chart_row,
o_cl_html_chart_row2 TYPE REF TO cl_html_chart_row,
o_cl_html_chart_row3 TYPE REF TO cl_html_chart_row,
o_cl_html_chart_row4 TYPE REF TO cl_html_chart_row,
o_cl_html_chart_row5 TYPE REF TO cl_html_chart_row,
o_cl_html_chart_row6 TYPE REF TO cl_html_chart_row.

CREATE OBJECT : o_cl_html_chart_column1,
o_cl_html_chart_column2,
o_cl_html_chart_column3,
o_cl_html_chart_column4.

o_cl_html_chart_column1->col_type = `string`.
o_cl_html_chart_column1->col_value = `Ano`.

o_cl_html_chart_column2->col_type = `number`.
o_cl_html_chart_column2->col_value = `Vendas`.

o_cl_html_chart_column3->col_type = `number`.
o_cl_html_chart_column3->col_value = `Perdas`.

o_cl_html_chart_column4->col_type = `number`.
o_cl_html_chart_column4->col_value = `Não Vendas`.

CREATE OBJECT : o_cl_html_chart_row1,
o_cl_html_chart_row2,
o_cl_html_chart_row3,
o_cl_html_chart_row4,
o_cl_html_chart_row5,
o_cl_html_chart_row6.

o_cl_html_chart_row1->row_value = `'2004', 12458, 457, 154`.
o_cl_html_chart_row2->row_value = `'2005', 42521, 8521, 1447`.
o_cl_html_chart_row3->row_value = `'2006', 1425, 45, 525`.
o_cl_html_chart_row4->row_value = `'2007', 8544, 452, 447`.
o_cl_html_chart_row5->row_value = `'2008', 2452, 21, 4475`.
o_cl_html_chart_row6->row_value = `'2009', 45452, 4521, 2278`.

CREATE OBJECT o_cl_html_chart.

o_cl_html_chart->add_column( o_cl_html_chart_column1 ).
o_cl_html_chart->add_column( o_cl_html_chart_column2 ).
o_cl_html_chart->add_column( o_cl_html_chart_column3 ).
o_cl_html_chart->add_column( o_cl_html_chart_column4 ).

o_cl_html_chart->add_row( o_cl_html_chart_row1 ).
o_cl_html_chart->add_row( o_cl_html_chart_row2 ).
o_cl_html_chart->add_row( o_cl_html_chart_row3 ).
o_cl_html_chart->add_row( o_cl_html_chart_row4 ).
o_cl_html_chart->add_row( o_cl_html_chart_row5 ).
o_cl_html_chart->add_row( o_cl_html_chart_row6 ).

o_cl_html_chart->display_chart( ).

ENDIF.


Veja o Resultado:

Um comentário:

  1. Lindo! Até que enfim um abap que sai da caixa!!!
    Parabens!!

    ResponderExcluir