SAP ABAP 图表显示开发案例

2020-11-30 10:04:07 浏览数 (1)

代码语言:javascript复制
REPORT ZMATINAL_GRAPH.
*** 因为是顺序执行的,要优先包含进来
INCLUDE GFW_DC_PRES.
TYPES:GFW_TEXT TYPE TEXT40.
DATA: BEGIN OF GT_TOP2 OCCURS 0,
        TOTAL LIKE COEP-MEGBTR,
        COMPL LIKE COEP-MEGBTR,
        DIFFE LIKE COEP-MEGBTR,
      END OF GT_TOP2.
DATA: G_COMP TYPE I,
      G_TOTA TYPE I.
DATA:
  OK_CODE          TYPE SY-UCOMM,
  FIRSTCALL        TYPE I,
  CUSTOM_CONTAINER TYPE REF TO CL_GUI_CUSTOM_CONTAINER,
  DC_INST          TYPE REF TO LCL_DC_PRES,
  DC_MANAGE        TYPE REF TO IF_DC_MANAGEMENT,
  MY_ID_AT_DC      TYPE I,
  RETVAL           TYPE SYMSGNO,
  GP_INST          TYPE REF TO CL_GUI_GP_PRES,
  GRPIDS           TYPE GFW_GRPID_LIST.
DATA GO_CONT_C    TYPE REF TO CL_GUI_CONTAINER.
CONSTANTS    CO_GFW_PROG_OBJID_1    TYPE  GFW_TEXT VALUE '1'.
CONSTANTS    CO_GFW_PROG_OBJID_2    TYPE  GFW_TEXT VALUE '2'.
CONSTANTS    CO_GFW_PROG_OBJID_3    TYPE  GFW_TEXT VALUE '3'.
 
*&---------------------------------------------------------------------*
*&     START-OF-SELECTION.
*&---------------------------------------------------------------------*
START-OF-SELECTION.
  CLEAR:GT_TOP2,G_TOTA, G_COMP.
  CALL SCREEN 9000.
*&---------------------------------------------------------------------*
*&      Form  MAKE_TOP2_PROC
*&---------------------------------------------------------------------*
FORM MAKE_TOP2_PROC .
*** 总数
  G_TOTA = 375.
*** 已完成
  G_COMP = 250.
 
  GT_TOP2-TOTAL = G_TOTA.
  GT_TOP2-COMPL = G_COMP.
  GT_TOP2-DIFFE = GT_TOP2-TOTAL - GT_TOP2-COMPL.
  COLLECT GT_TOP2.
 
*** 图表输出
  PERFORM CALL_GRAPH_PROC2.
ENDFORM.                    " MAKE_TOP2_PROC
*&---------------------------------------------------------------------*
*&      Form  CALL_GRAPH_PROC2
*&---------------------------------------------------------------------*
FORM CALL_GRAPH_PROC2 .
  RETVAL = CL_GFW=>OK.
*   create, initialize and fill data container
  CREATE OBJECT DC_INST.
  DC_MANAGE = DC_INST.
  CALL METHOD DC_MANAGE->INIT
    IMPORTING
      ID     = MY_ID_AT_DC
      RETVAL = RETVAL.
  IF RETVAL <> CL_GFW=>OK.
    CALL METHOD CL_GFW=>SHOW_MSG
      EXPORTING
        MSGNO = RETVAL.
    CLEAR DC_INST.
    CLEAR DC_MANAGE.
  ELSE.
    PERFORM FILL_DC CHANGING RETVAL.
    IF RETVAL <> CL_GFW=>OK.
      CALL METHOD CL_GFW=>SHOW_MSG
        EXPORTING
          MSGNO = RETVAL.
    ELSE.
**       create a container on the dynpro
*        CREATE OBJECT CUSTOM_CONTAINER
*          EXPORTING
*            CONTAINER_NAME = 'CONTAINER'.
*       create, initialize and activate graphic proxy
      IF GP_INST IS NOT INITIAL.
        CALL METHOD GP_INST->IF_GRAPHIC_PROXY~FREE
          IMPORTING
            RETVAL = RETVAL.
      ENDIF.
      CREATE OBJECT GP_INST.
      CALL METHOD GP_INST->IF_GRAPHIC_PROXY~INIT
        EXPORTING
          PARENT     = GO_CONT_C
          DC         = DC_INST
          PROD_ID    = CL_GUI_GP_PRES=>CO_PROD_CHART
          FORCE_PROD = GFW_TRUE
        IMPORTING
          RETVAL     = RETVAL.
      IF RETVAL = CL_GFW=>OK.
*         set dc attributes
        CALL METHOD GP_INST->SET_DC_NAMES
          EXPORTING
            OBJ_ID    = 'OBJID'
            DIM1      = 'X_VAL'
            DIM2      = 'Y_VAL'
            GRP_ID    = 'GRPID'
            TEXT      = 'TEXT'      " for labels of a point
            OBJREF_ID = 'CU_REFOBJ' " for customizing of a point
          IMPORTING
            RETVAL    = RETVAL.
      ENDIF.
*       set customizing objects
      IF RETVAL = CL_GFW=>OK.
        "颜色填充
        PERFORM SET_CUSTOMIZING.
      ENDIF.
 
      IF RETVAL = CL_GFW=>OK.
        CALL METHOD GP_INST->IF_GRAPHIC_PROXY~ACTIVATE
          IMPORTING
            RETVAL = RETVAL.
      ENDIF.
 
      IF RETVAL <> CL_GFW=>OK.
        CALL METHOD CL_GFW=>SHOW_MSG
          EXPORTING
            MSGNO = RETVAL.
      ENDIF.
    ENDIF.
    FIRSTCALL = 1.
  ENDIF.
* **** distribute changes (to all subscribed graphic proxies)
  IF NOT DC_MANAGE IS INITIAL.
    CALL METHOD DC_MANAGE->DISTRIBUTE_CHANGES
      IMPORTING
        RETVAL = RETVAL.
 
    IF RETVAL <> CL_GFW=>OK.
      CALL METHOD CL_GFW=>SHOW_MSG
        EXPORTING
          MSGNO = RETVAL.
    ENDIF.
  ENDIF.
 
ENDFORM.                    " CALL_GRAPH_PROC2
*&---------------------------------------------------------------------*
*&      Form  FILL_DC
*&---------------------------------------------------------------------*
* 단순 그래프 출력
FORM FILL_DC CHANGING VALUE(RETVAL) TYPE SYMSGNO.
  DATA: OBJ   TYPE GFWDCPRES,
        L_TXT TYPE CHAR6.
* fill dc with initial data
  IF DC_MANAGE IS INITIAL.
    RETVAL = CL_GFW=>E_GP_DCHANDLE.
    EXIT.
  ENDIF.
  RETVAL = CL_GFW=>OK.
* insert first group (corresponding customizing class: cl_cu_values)
  OBJ-OBJID = CO_GFW_PROG_OBJID_1.
  OBJ-GRPID = TEXT-001.
*  OBJ-X_VAL = CO_GFW_PROG_CATEGORY1.
  OBJ-Y_VAL = G_TOTA.
  L_TXT = G_TOTA.
  CONDENSE L_TXT.
  OBJ-TEXT  = L_TXT.       " label of this point
  CALL METHOD DC_INST->SET_OBJ_VALUES
    EXPORTING
      ID     = MY_ID_AT_DC
      OBJ    = OBJ
    IMPORTING
      RETVAL = RETVAL.
  IF RETVAL <> CL_GFW=>OK.
    EXIT.
  ENDIF.
 
  OBJ-OBJID = CO_GFW_PROG_OBJID_2.
  OBJ-GRPID = TEXT-002.
*  OBJ-X_VAL = CO_GFW_PROG_CATEGORY1.
  OBJ-Y_VAL = G_COMP.
  L_TXT = G_COMP.
  CONDENSE L_TXT.
  OBJ-TEXT  = L_TXT.       " label of this point
  OBJ-CU_REFOBJ = SPACE. " connection to cl_cu_point
  CALL METHOD DC_INST->SET_OBJ_VALUES
    EXPORTING
      ID     = MY_ID_AT_DC
      OBJ    = OBJ
    IMPORTING
      RETVAL = RETVAL.
  IF RETVAL <> CL_GFW=>OK.
    EXIT.
  ENDIF.
 
  OBJ-OBJID = CO_GFW_PROG_OBJID_3.
  OBJ-GRPID = TEXT-003.
*  OBJ-X_VAL = CO_GFW_PROG_CATEGORY1.
  OBJ-Y_VAL = G_TOTA - G_COMP.
  L_TXT = G_TOTA - G_COMP.
  CONDENSE L_TXT.
  OBJ-TEXT  = L_TXT.       " label of this point
  OBJ-CU_REFOBJ = SPACE. " connection to cl_cu_point
  CALL METHOD DC_INST->SET_OBJ_VALUES
    EXPORTING
      ID     = MY_ID_AT_DC
      OBJ    = OBJ
    IMPORTING
      RETVAL = RETVAL.
  IF RETVAL <> CL_GFW=>OK.
    EXIT.
  ENDIF.
 
ENDFORM.                    " FILL_DC
*&---------------------------------------------------------------------*
*&      Form  SET_CUSTOMIZING
*&---------------------------------------------------------------------*
FORM SET_CUSTOMIZING .
  DATA: BUNDLE_DISPLAY TYPE REF TO CL_CU_DISPLAY_CONTEXT,
        BUNDLE_VALUES1 TYPE REF TO CL_CU_VALUES,
        BUNDLE_POINT1  TYPE REF TO CL_CU_POINT,
        BUNDLE_SHEET   TYPE REF TO CL_CU_DATA_SHEET.
* prepare display context for values
  CREATE OBJECT BUNDLE_DISPLAY
    EXPORTING
      INSTANCE_ID = 'dummy'.
* set color of data series
  CALL METHOD BUNDLE_DISPLAY->IF_CUSTOMIZING~SET
    EXPORTING
      ATTR_ID = CL_CU_DISPLAY_CONTEXT=>CO_BG_CLR_PLT_ID
      VALUE   = 4. " 黄色
* values (group 1)
  CREATE OBJECT BUNDLE_VALUES1
    EXPORTING
      INSTANCE_ID = 'GFWLABEL'.
* copy display context into values
  CALL METHOD BUNDLE_VALUES1->IF_CUSTOMIZING~SET
    EXPORTING
      ATTR_ID = CL_CU_VALUES=>CO_CURVE_CONTEXT
      VALUE   = BUNDLE_DISPLAY.
* copy display context into values
  CALL METHOD BUNDLE_VALUES1->IF_CUSTOMIZING~SET
    EXPORTING
      ATTR_ID = CL_CU_VALUES=>CO_LABEL_AUTO
      VALUE   = GFW_FALSE.
*      VALUE   = GFW_TRUE.
* tell the proxy where to use the bundle
  CALL METHOD GP_INST->IF_GRAPHIC_PROXY~ADD_CU_BUNDLE
    EXPORTING
      PORT   = IF_GRAPHIC_PROXY=>CO_PORT_CHART
      KEY    = TEXT-001
      BUNDLE = BUNDLE_VALUES1
    IMPORTING
      RETVAL = RETVAL.
 
 
 
* prepare display context for values
  CREATE OBJECT BUNDLE_DISPLAY
    EXPORTING
      INSTANCE_ID = 'dummy1'.
* set color of data series
  CALL METHOD BUNDLE_DISPLAY->IF_CUSTOMIZING~SET
    EXPORTING
      ATTR_ID = CL_CU_DISPLAY_CONTEXT=>CO_BG_CLR_PLT_ID
      VALUE   = 5. " 绿色
* values (group 1)
  CREATE OBJECT BUNDLE_VALUES1
    EXPORTING
      INSTANCE_ID = 'GFWLABEL1'.
* copy display context into values
  CALL METHOD BUNDLE_VALUES1->IF_CUSTOMIZING~SET
    EXPORTING
      ATTR_ID = CL_CU_VALUES=>CO_CURVE_CONTEXT
      VALUE   = BUNDLE_DISPLAY.
* copy display context into values
  CALL METHOD BUNDLE_VALUES1->IF_CUSTOMIZING~SET
    EXPORTING
      ATTR_ID = CL_CU_VALUES=>CO_LABEL_AUTO
      VALUE   = GFW_FALSE.
*      VALUE   = GFW_TRUE.
* tell the proxy where to use the bundle
  CALL METHOD GP_INST->IF_GRAPHIC_PROXY~ADD_CU_BUNDLE
    EXPORTING
      PORT   = IF_GRAPHIC_PROXY=>CO_PORT_CHART
      KEY    = TEXT-002
      BUNDLE = BUNDLE_VALUES1
    IMPORTING
      RETVAL = RETVAL.
* prepare display context for values
  CREATE OBJECT BUNDLE_DISPLAY
    EXPORTING
      INSTANCE_ID = 'dummy2'.
* set color of data series
  CALL METHOD BUNDLE_DISPLAY->IF_CUSTOMIZING~SET
    EXPORTING
      ATTR_ID = CL_CU_DISPLAY_CONTEXT=>CO_BG_CLR_PLT_ID
      VALUE   = 7. " 红色
* values (group 1)
  CREATE OBJECT BUNDLE_VALUES1
    EXPORTING
      INSTANCE_ID = 'GFWLABEL2'.
* copy display context into values
  CALL METHOD BUNDLE_VALUES1->IF_CUSTOMIZING~SET
    EXPORTING
      ATTR_ID = CL_CU_VALUES=>CO_CURVE_CONTEXT
      VALUE   = BUNDLE_DISPLAY.
* copy display context into values
  CALL METHOD BUNDLE_VALUES1->IF_CUSTOMIZING~SET
    EXPORTING
      ATTR_ID = CL_CU_VALUES=>CO_LABEL_AUTO
      VALUE   = GFW_FALSE.
*      VALUE   = GFW_TRUE.
* tell the proxy where to use the bundle
  CALL METHOD GP_INST->IF_GRAPHIC_PROXY~ADD_CU_BUNDLE
    EXPORTING
      PORT   = IF_GRAPHIC_PROXY=>CO_PORT_CHART
      KEY    = TEXT-003
      BUNDLE = BUNDLE_VALUES1
    IMPORTING
      RETVAL = RETVAL.
 
 
 
** prepare display context for point
*  CREATE OBJECT BUNDLE_DISPLAY
*    EXPORTING
*      INSTANCE_ID = 'dummy'.
*
** set color of this point
*  CALL METHOD BUNDLE_DISPLAY->IF_CUSTOMIZING~SET
*    EXPORTING
*      ATTR_ID = CL_CU_DISPLAY_CONTEXT=>CO_BG_CLR_PLT_ID
*      VALUE   = 5. " green
*
*  CREATE OBJECT BUNDLE_POINT1
*    EXPORTING
*      INSTANCE_ID = 'GFWLABEL'.
*
** copy display context into point
*  CALL METHOD BUNDLE_POINT1->IF_CUSTOMIZING~SET
*    EXPORTING
*      ATTR_ID = CL_CU_POINT=>CO_DISPLAY_CONTEXT
*      VALUE   = BUNDLE_DISPLAY.
*
** show text from DC
*  CALL METHOD BUNDLE_POINT1->IF_CUSTOMIZING~SET
*    EXPORTING
*      ATTR_ID = CL_CU_POINT=>CO_AUTO
*      VALUE   = GFW_FALSE.
*
** tell the proxy where to use the bundle
*  CALL METHOD GP_INST->IF_GRAPHIC_PROXY~ADD_CU_BUNDLE
*    EXPORTING
*      PORT   = IF_GRAPHIC_PROXY=>CO_PORT_CHART
*      KEY    = CO_GFW_PROG_OBJID_2
*      BUNDLE = BUNDLE_POINT1
*    IMPORTING
*      RETVAL = RETVAL.
* prepare display context for drawing area
  CREATE OBJECT BUNDLE_DISPLAY
    EXPORTING
      INSTANCE_ID = 'dummy'.
* disable data sheet
  CALL METHOD BUNDLE_DISPLAY->IF_CUSTOMIZING~SET
    EXPORTING
      ATTR_ID = CL_CU_DISPLAY_CONTEXT=>CO_VISIBILITY
      VALUE   = GFW_FALSE.
* data sheet
  CREATE OBJECT BUNDLE_SHEET
    EXPORTING
      INSTANCE_ID = 'GFWLABEL'.
* copy display context into data sheet
  CALL METHOD BUNDLE_SHEET->IF_CUSTOMIZING~SET
    EXPORTING
      ATTR_ID = CL_CU_DATA_SHEET=>CO_DISPLAY_CONTEXT
      VALUE   = BUNDLE_DISPLAY.
* tell the proxy where to use the bundle
  CALL METHOD GP_INST->IF_GRAPHIC_PROXY~ADD_CU_BUNDLE
    EXPORTING
      PORT   = IF_GRAPHIC_PROXY=>CO_PORT_CHART
      BUNDLE = BUNDLE_SHEET
    IMPORTING
      RETVAL = RETVAL.
ENDFORM.                    " SET_CUSTOMIZING
*&---------------------------------------------------------------------*
*&      Module  STATUS_9000  OUTPUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
MODULE STATUS_9000 OUTPUT.
  SET PF-STATUS '9000'.
  SET TITLEBAR 'TITLE'.
  PERFORM MAKE_TOP2_PROC.
ENDMODULE.
*&---------------------------------------------------------------------*
*&      Module  USER_COMMAND_9000  INPUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
MODULE USER_COMMAND_9000 INPUT.
  CASE OK_CODE.
    WHEN 'BACK' OR 'CANCEL' OR 'EXIT'.
      LEAVE TO SCREEN 0.
  ENDCASE.
ENDMODULE.

0 人点赞