Sunday, May 6, 2018

How to refresh graph (based on CL_GUI_CHART_ENGINE / GRAPHICS_GUI_CE_DEMO)

Many of us used the sample simple nice program GRAPHICS_GUI_CE_DEMO as a pattern how to work with graphs. Unfortunately this program does not solve the question how to update/refresh  graph data, furthermore you can find misleading info on some forums.  I do not say the bellow pattern is the best, but it is working fine :)

In my case I have dedicated modal window for graph. Each time I press ALV toolbar button I want to see related graph to the grid. I have always prepared an internal table with correct graph data (see below t_graph within the coding).




Screen call e.g.


    CALL SCREEN 500 STARTING AT 13 ENDING AT 175 37.



PBO


MODULE status_0500 OUTPUT.

  SET PF-STATUS 'DEFAULT_STATUS'.
  SET TITLEBAR 'GRAPH'.

  lcl_main=>self->generate_graph).

ENDMODULE.


PAI

MODULE exit_command_0500 INPUT.

  LEAVE TO SCREEN 0" Back to main screen

ENDMODULE.


The PBO method


I am keeping just two global variables outside the method itself. There is no need to call any screen container destructor to reuse the graph object again etc. The base screen under the modal window can take care for calling destructors first and freeing the variables..


The globals


DATA
  go_ce_container   TYPE REF TO cl_gui_container.
  go_ce_viewer      TYPE REF TO cl_gui_chart_engine.



The generator method coding


The whole question is what should be setup each screen call (local variables) and what should remain in memory (global ones). The local class  lo_graph is just my rewrite of two performs/subroutines  from the demo program GRAPHICS_GUI_CE_DEMO. It's methods are responsible for filling the XML data.


  METHOD generate_graph.

    DATA:
      lo_graph           TYPE REF TO lcl_graph" custom class preparing XML data for graph
      lo_ixml_data_doc   TYPE REF TO if_ixml_document,
      lo_ixml_custom_doc TYPE REF TO if_ixml_document,
      lo_ixml            TYPE REF TO if_ixml,
      lo_ixml_sf         TYPE REF TO if_ixml_stream_factory,
      lo_ostream         TYPE REF TO if_ixml_ostream,
      lv_xstr            TYPE xstring.

    IF go_ce_container IS INITIAL.

       go_ce_container = generate_graph_container).

      CREATE OBJECT go_ce_viewer
        EXPORTING
          parent go_ce_container

    ENDIF.

    lo_ixml cl_ixml=>create).
    lo_ixml_sf lo_ixml->create_stream_factory).

    CREATE OBJECT lo_graph.

    lo_graph->create_data(
      EXPORTING
        it_graph    t_graph
        iv_ixml     lo_ixml
      CHANGING
        cv_ixml_doc lo_ixml_data_doc
    ).
    lo_ostream lo_ixml_sf->create_ostream_xstringlv_xstr ).
    lo_ixml_data_doc->renderostream lo_ostream ).
    go_ce_viewer->set_dataxdata lv_xstr ).

    CLEAR lv_xstr.

    lo_graph->create_custom(
      EXPORTING
        iv_title    v_grid_title 
        iv_ixml     lo_ixml
      CHANGING
        cv_ixml_doc lo_ixml_custom_doc
    ).
    lo_ostream lo_ixml_sf->create_ostream_xstringlv_xstr ).
    lo_ixml_custom_doc->renderostream lo_ostream ).
    go_ce_viewer->set_customizingxdata lv_xstr ).

    go_ce_viewer->render).

  ENDMETHOD.                    "generate_graph