进阶:案例五: Dynamic 创建 Business Graphic

效果图:

step:

无需节点无需UI

1、添加属性

2、代码:

method WDDOMODIFYVIEW .
  DATA:lr_graph TYPE REF TO cl_wd_business_graphics,
      lr_cat TYPE REF TO cl_wd_category,
      lr_series TYPE REF TO cl_wd_simple_series,
      lr_container TYPE REF TO cl_wd_uielement_container,
      lr_flow TYPE REF TO cl_wd_flow_data.
lr_container ?= view->get_element( 'ROOTUIELEMENTCONTAINER' ).
lr_graph = cl_wd_business_graphics=>new_business_graphics(
bind_series_source = 'NODE_DYN'
chart_type = cl_wd_business_graphics=>e_chart_type-BARS
height = 340
width = 750
* BIND_TOOLTIP = 'GRAPH.TT'
id = 'GRAPH' ).
lr_flow = cl_wd_flow_data=>new_flow_data( element = lr_graph ).
lr_container->add_child( lr_graph ).
wd_this->value_cs ?= lr_graph.
wd_this->mr_view ?= view.
DATA: lr_bgr_ss TYPE REF TO cl_wd_simple_series.
lr_bgr_ss = cl_wd_simple_series=>new_simple_series(
bind_value = 'NODE_DYN.SERIES1'
label = 'Series_1'
view = wd_this->mr_view ).
wd_this->value_cs->ADD_SERIES( lr_bgr_ss ).
lr_bgr_ss = cl_wd_simple_series=>new_simple_series(
bind_value = 'NODE_DYN.SERIES2'
label = 'Series_2'
view = wd_this->mr_view ).
wd_this->value_cs->ADD_SERIES( lr_bgr_ss ).
DATA: lr_bgr_cs TYPE REF TO cl_wd_category.
lr_bgr_cs = cl_wd_category=>new_category(
view = wd_this->mr_view bind_description = 'NODE_DYN.CATEGORY').
wd_this->value_cs->set_category( lr_bgr_cs ).
endmethod.
View Code

3、代码:

method INSERT_DATA_CONTEXT .
    DATA: lo_node TYPE REF TO if_wd_context_node.
* ls_elem type WD_THIS->element_NODE_DYN,
* lt_elem type WD_THIS->elements_NODE_DYN.
  TYPES: BEGIN OF typ_st,
        series1 TYPE i,
        series2 TYPE i,
        category TYPE string,
    END OF typ_st.
  DATA: ls_elem TYPE typ_st,
        lt_elem TYPE TABLE OF typ_st.
  ls_elem-series1 = 10.
  ls_elem-series2 = 15.
  ls_elem-category = 'Category 1'.
  APPEND ls_elem TO lt_elem.
  ls_elem-series1 = 20.
  ls_elem-series2 = 25.
  ls_elem-category = 'Category 2'.

  APPEND ls_elem TO lt_elem.
  ls_elem-series1 = 30.
  ls_elem-series2 = 35.
  ls_elem-category = 'Category 3'.
  APPEND ls_elem TO lt_elem.
  lo_node = wd_context->get_child_node( name = 'NODE_DYN' ).
  lo_node->bind_table( lt_elem ) .
endmethod.
View Code

4、代码:

method CREATE_CONTEXT_NODE .
    DATA: lo_nodeinfo TYPE REF TO if_wd_context_node_info,
        typ_struct type REF TO cl_abap_structdescr,
        typ_table TYPE REF TO cl_abap_tabledescr,
        com_tab type cl_abap_structdescr=>component_table,
        com like LINE OF com_tab.
* component info
  com-name = 'SERIES1'.
  com-type ?= cl_abap_datadescr=>describe_by_name( 'I' ).
  append com to com_tab.
  com-name = 'SERIES2'.
  com-type ?= cl_abap_datadescr=>describe_by_name( 'I' ).
  append com to com_tab.
  com-name = 'CATEGORY'.
  com-type ?= cl_abap_datadescr=>describe_by_name( 'STRING' ).
  append com to com_tab.
* create the structure
  typ_struct = cl_abap_structdescr=>create( com_tab ).
* dynamic create the context node
  lo_nodeinfo = wd_context->get_node_info( ).
  lo_nodeinfo = lo_nodeinfo->add_new_child_node(
  name = 'NODE_DYN'
  IS_MANDATORY = ABAP_false
  IS_MULTIPLE = ABAP_true
  STATIC_ELEMENT_RTTI = typ_struct IS_STATIC = ABAP_false ).
endmethod.
View Code

5、初始化代码:

method WDDOINIT .
* create one Context Node for Business Graphic
  wd_this->create_context_node( ).

* node, insert the data to the newly created context node
 wd_this->insert_data_context( ).
endmethod.
View Code
原文地址:https://www.cnblogs.com/caizjian/p/4368227.html