This tutorial shows how to generate a chart from SAP data and display it in an SAP transaction. Our example is as follows.

In transaction MM03 (Display Material) we add a button "Sales chart":

When the user clicks the button we display a bar chart (generated from current SAP data) that shows the product's monthly sales figures for the past 12 months:

 

For implementation we need the following steps:

(1) Reading the data from SAP

This can be done via BAPI calls, via direct database access, or via existing SAP transaction. For our example we use the standard transaction "MC+Q" that displays the sales figures as ABAP list.

(2) Passing the data to an external tool that generates the chart

For our example we use "ChartDirector" from "Advanced Software Engineering Limited",  http://www.advsofteng.com. We pass the data form GuiXT to the graphics component via VBScript.

(3) Displaying the chart in the SAP GUI window

We use the standard Windows Forms image control "Forms.Image.1"

GuiXT script for MM03:

// saplmgmm.e0060
if
V[MM03_display_sales_chart=X]
 
Control (4,1) (26,120) name="MM03_sales_chart" progid="Forms.Image.1"
initflag="chartinit"

  if V[chartinit=X]
    
CallVbs generate_chart "&V
[MM03_sales_chart]"
 
endif

  Pushbutton (4,121) "x" process="mm03_display_sales_chart_reset.txt"

else

  Pushbutton (4,1) "Sales chart" process="mm03_display_sales_chart.txt"

endif

The InputScript "mm03_display_sales_chart.txt" calls up the sales figures display via transaction MC+Q and extracts the data needed form the ABAP list. Here is the InputScript together with the screens that are processed in the background by the InputScript:

// save material number
Set
V[MM03_matnr] "&F[Material]"

// transaction MC+Q
Enter "/NMC+Q

 

 

// Material Analysis: Invoiced Sales: Selection
Screen
RMCV0200.1000

  // Month selection, e.g. 07.2010 - 06.2011
  Set V[y2] "&V[today_y]"
  Set V[m2] "&V[today_m]" + 0     // e.g.: 01 -> 1

  if V[m2=12]
 
  Set V[y1] "&V[y2]"
 
  Set V[m1] "1"
 
else
 
  Set V[y1] &V[y2] - 1
 
  Set V[m1] &V[m2] + 1
  endif


  Set F[Material] "&V[MM03_matnr]"
  Set F[Sales Organization] ""
 
Set F[Distribution Channel] " "
 
Set F[Month] "&V[m1].&V[y1]"
 
Set F[Month to] "&V[m2].&V[y2]"

  Enter "/8" // Execute

 

 

// Information screen may occur, e.g. "no data available"
Screen
SAPMSDYP.0010
 
Enter

// back to selection screen? then no data found for this selection
Screen
RMCV0200.1000
 
Message "E: No sales data found for product number &V[MM03_matnr]" -statusline
 
Enter "/NMM03"
 
Leave

// Material Analysis: Invoiced Sales: Basic List
Screen
RMCV0200.0120
 
Enter "/7" // Switch drilldown...

 

// Switch drilldown
Screen
SAPLMCS2.0201
 
Set R[Month] "X"
 
Enter

 

Screen RMCV0200.0120 

  // Month names
  Set V[monthname.1] "January"
  Set V[monthname.2] "February"
 
Set V[monthname.3] "March"
 
Set V[monthname.4] "April"
 
Set V[monthname.5] "May"
 
Set V[monthname.6] "June"
 
Set V[monthname.7] "July"
 
Set V[monthname.8] "August"
 
Set V[monthname.9] "September"
 
Set V[monthname.10] "October"
 
Set V[monthname.11] "November"
 
Set V[monthname.12] "December"

  // Set chart title
  Set V[chart.title] "Monthly Sales &V[MM03_matnr] &V[monthname.&V[m1]] &V[y1] - &V[monthname.&V[m2]] &V[y2]"

  // Set labels
  Set V[k] 1
  Set V[month] "&V[m1]"

label next_month_label

  // set label
  Set V[chart.label.&V[k]] "&V[monthname.&V[month]]"

  // reset month values
 
Set V[chart.amount.&V[k]] "0"

  // reverse month index
  // necessary since the SAP list does not always contain each month
 
Set V[reverse_month.&V[month]] "&V[k]"

  if V[k<12]
   
Set V[k] &V[k] + 1

    if V[month=12]
     
Set V[month] "1"
   
else
     
Set V[month] &V[month] + 1
   
endif

    goto next_month_label

  endif

  // xtract sales figures from ABAP list

  Set V[k] 6

label next_month_in_list

  Set V[dot] "&V[_listline(&V[k])](4-4)"
 
if not V[dot=.]
   
goto end_month_in_list
 
endif

  // month, year and sales amount from list display
 
Set V[month] "&V[_listline(&V[k])](2-3)" + 0 // 01 -> 1 etc.
 
Set V[amount] "&V[_listline(&V[k])](21-34)" / 1000 decimals=0 decimalseparator=UserDefault
 
Set V[chart.amount.&V[reverse_month.&V[month]]] "&V[amount]"

  // next month
 
Set V[k] &V[k] + 1
 
goto next_month_in_list

label end_month_in_list

  Set V[MM03_display_sales_chart] "X"

  Enter "/NMM03"

 

The InputScript "mm03_display_sales_chart_reset.txt" is very simple:

Set V[MM03_sales_chart] ""
Set
V[MM03_display_sales_chart] ""
Return

 

Our VBScript function "generate_chart" uses the "ChartDirector" component in order to generate the chart and pass it to the Image control. It is based on one of the numerous examples contained in the ChartDirector help file.

' The example uses ChartDirector from Advanced Software Engineering Limited
' http://www.advsofteng.com/

Function generate_chart(img)
         
    Dim cd 
    Set cd = guixt.CreateObject("ChartDirector.API")
    
    ' Set license code
    cd.setLicenseCode("xxxx-xxxx-xxxx-xxxx-xxxx-xxxx")
    
    ' The data for the bar chart
    Dim data(11)
    Dim labels(11)
   
    For k = 1To12
        data(k-1) = CInt(guixt.Get("chart.amount."  & k))
        labels(k-1) = guixt.Get("chart.label."  & k)
    Next
        
   
    ' The colors for the bar chart
    Dim colors 
    colors = Array(&Hb8bc9c, &Ha0bdc4, &H999966, &Hb8bc9c,  &Ha0bdc4, &H999966,&Hb8bc9c, &Ha0bdc4, &H999966,&Hb8bc9c, &Ha0bdc4, &H30cc30)
    
    ' Create an XYChart object of size 800 x 460 pixels, golden background
    Dim c 
    Set c = cd.XYChart(800, 460, cd.goldColor(), -1, 2)
    
    ' Add a title box using 12 point Arial Bold font. 
    Call c.addTitle(guixt.Get("chart.title"), "arialbd.ttf", 12, &H606060)
    
    ' Set the plotarea at (60, 40) and of size 720 x 360 pixels
    Call c.setPlotArea(60, 40, 720, 360)
    
    ' Add a multi-color bar chart layer using the given data and colors
    Dim layer
    Set layer = c.addBarLayer3(data, colors)
    
    '  Use a 1 pixel 3D border for the bars.
    Call layer.setBorderColor(-1, 1)
    
    ' Set bar shape to circular (cylinder)
    Call layer.setBarShape(cd.CircleShape)
    
    ' Enable bar label for the whole bar
    Call layer.setAggregateLabelStyle()
    
    ' Set the labels on the x axis.
    Call c.xAxis().setLabels(labels)
    
    ' Add a title to the y axis
    Call c.yAxis().setTitle("in thousands €")
    
    
    
    ' Output the chart
    Set img.Picture = c.makePicture()
    
EndFunction