Monday, 17 August 2015

Using Automation to Create a Graph in Microsoft Excel

In this walkthrough, you will transfer data for top 10 Customers Sales Contribution to Microsoft Excel and create a graph.

This example shows how to handle enumerations by creating a graph in Excel that shows the distribution of Sales by Customer.

ExcelChart-1

You will run the codeunit directly from Object Designer. In a real application, you would call it from an appropriate place, such as from a menu or any other window.

About This Walkthrough

This walkthrough illustrates the following tasks:

  • Creating a codeunit that declares the Automation variables that are required for using Excel Automation.

  • Adding a function to calculate Top 10 Customers Sales Contribution.

  • Adding C/AL code to the codeunit to run the Automation object that opens Excel.

  • Adding C/AL code to the Automation codeunit to transfer data from a table record to Excel.

  • Adding C/AL code that creates a graph in Excel. 


Prerequisites 

To complete this walkthrough, you will need:

  • Microsoft Dynamics NAV 2015 with a developer license.

  • The CRONUS International Ltd. demo data company.

  • Microsoft Excel 2013 or Microsoft Excel 2010.


Creating the Codeunit and Declaring Variables

To create the codeunit and declare variables

  • To implement Automation in a codeunit, you define the Automation variables. To define an Automation variable, you specify an Automation server and the Automation object.

  • The language in the regional settings of your computer matches the language version of Microsoft Excel.



  • In Object Designer, choose Codeunit, and then choose the New button to create a new codeunit.

  • On the View menu, choose C/AL Globals.

  • On the Variables tab, add the following variables:


Note

For the Automation data type variables, the subtype Microsoft Excel 15.0/14.0 Object Library defines the Automation server, and the class specifies the Automation object of the Microsoft Excel 15.0/14.0 Object Library.

ExcelChart-2











































































































































































NameDataTypeSubtypeLength
xlAppAutomation'Microsoft Excel 15.0 Object Library'.Application
xlBookAutomation'Microsoft Excel 15.0 Object Library'.Workbook
xlSheetAutomation'Microsoft Excel 15.0 Object Library'.Worksheet
xlChartAutomation'Microsoft Excel 15.0 Object Library'.Chart
xlRangeAutomation'Microsoft Excel 15.0 Object Library'.Range
CustRecordCustomer
WindowDialog
CustAmountRecordCustomer Amount
CustFilterText
CustDateFilterText30
ShowTypeOption [Sales (LCY),Balance (LCY)]
NoOfRecordsToPrintInteger
CustSalesLCYDecimal
CustBalanceLCYDecimal
MaxAmountDecimal
BarTextText50
iInteger
TotalSalesDecimal
TotalBalanceDecimal
ChartTypeOption [Bar chart,Pie chart]
ChartTypeNoInteger
ShowTypeNoInteger
ChartTypeVisibleBoolean
IntegerRecordInteger
CustomerRecordCustomer
CellNo1Text5
CellNo2Text5


  • Close the C/AL Globals window.


Adding the Code

Now you add the code for the codeunit.

To add the code

  • Add a Function to calculate Top 10 Customers Sales Contribution as:


ExcelChart-3
Add code to it as:

I have not used all the values, just shown this way also you can think of while you design any such code & functions.
Window.OPEN(Text000);

i := 0;

Cust.RESET;

IF Cust.FINDSET THEN

REPEAT

Window.UPDATE(1,Cust."No.");

Cust.CALCFIELDS("Sales (LCY)","Balance (LCY)");

IF (Cust."Sales (LCY)" <> 0) OR (Cust."Balance (LCY)" <> 0) THEN

BEGIN

CustAmount.INIT;

CustAmount."Customer No." := Cust."No.";

IF ShowType = ShowType::"Sales (LCY)" THEN BEGIN

CustAmount."Amount (LCY)" := -Cust."Sales (LCY)";

CustAmount."Amount 2 (LCY)" := -Cust."Balance (LCY)";

END ELSE BEGIN

CustAmount."Amount (LCY)" := -Cust."Balance (LCY)";

CustAmount."Amount 2 (LCY)" := -Cust."Sales (LCY)";

END;

CustAmount.INSERT;

IF (NoOfRecordsToPrint = 0) OR (i < NoOfRecordsToPrint) THEN

i := i + 1

ELSE BEGIN

CustAmount.FIND('+');

CustAmount.DELETE;

END;

TotalSales += Cust."Sales (LCY)";

TotalBalance += Cust."Balance (LCY)";

ChartTypeNo := ChartType;

ShowTypeNo := ShowType;

END;

UNTIL Cust.NEXT = 0;

CustSalesLCY := Cust."Sales (LCY)";

CustBalanceLCY := Cust."Balance (LCY)";

Window.CLOSE;

IF CustAmount.FIND('-') THEN

REPEAT

CustAmount."Amount (LCY)" := -CustAmount."Amount (LCY)";

Customer.GET(CustAmount."Customer No.");

Customer.CALCFIELDS("Sales (LCY)","Balance (LCY)");

IF MaxAmount = 0 THEN

MaxAmount := CustAmount."Amount (LCY)";

CustAmount."Amount (LCY)" := -CustAmount."Amount (LCY)";

UNTIL CustAmount.NEXT = 0;


  • In the C/AL Editor, make call to above function by adding the following code to the OnRun trigger.


CustAmount.DELETEALL;

TopTenCustomer(10,ShowType::"Sales (LCY)",ChartType::"Bar chart");


  • Create an instance of Excel by adding the following code.


CREATE(xlApp, FALSE, TRUE);


  • Add a new workbook to Excel.


xlBook := xlApp.Workbooks.Add(-4167);

xlSheet:= xlApp.ActiveSheet;

xlSheet.Name := ‘Top 10 Customer';

The following describes the code:


    • In the first line, you use the Add method of the Workbooks collection to return a new workbook. The attribute -4167 is the enumerator value of worksheets as they apply to Workbook objects.

    • In the second line, you use the ActiveSheet property of the Application class to ensure that what is done next affects the active sheet of the new workbook.

    • In the third line, you use the Name property to name the sheet.



Transferring Data

To transfer the data, you must calculate the data and transfer the results of the calculation.

To transfer data

  • In the C/AL Editor, on the codeunit, use following code to transfer data of Top 10 Customers to Excel. To transfer the data to Microsoft Excel, add the following code.


CellNo1 := 'A1';

CellNo2 := 'B1';

IF CustAmount.FINDSET THEN

REPEAT

CellNo1 := INCSTR(CellNo1);

xlSheet.Range(CellNo1).Value := CustAmount."Customer No.";

CellNo2 := INCSTR(CellNo2);

xlSheet.Range(CellNo2).Value := ABS(CustAmount."Amount (LCY)");

UNTIL CustAmount.NEXT = 0;


  • The final step is to create the graph. You will use the ChartWizard method to create chart. This is a fast and simple way to do it. You can more tightly control the design of the graph by setting it up using the methods and properties of the various Chart objects, such as ChartArea and Legend.


Creating the Graph

The final step is to create the graph. You will use the ChartWizard method to create chart. This is a fast and simple way to do it. You can more tightly control the design of the graph by setting it up using the methods and properties of the various Chart objects, such as ChartArea and Legend.

To create the graph

  •   In the C/AL Editor, on the current codeunit, define a range for the data in the graph.


xlRange := xlSheet.Range('A2:'+FORMAT(CellNo2));


  • Add a new chart sheet and give it a name.xlChart.Name := ' Top 10 Customer - Graph';


xlChart := xlBook.Charts.Add;

xlChart.Name := ' Top 10 Customer - Graph';


  • Create the graph.


xlChart.ChartWizard(xlRange,-4101,7,2,1,0,1,'Top 10 Customer');

The following table describes the optional arguments that are used in the ChartWizard method.
















































Argument Description Value in method call
SourceThe range that contains the source data for the new chart.xlRange – The object returned by xlSheet.Range(‘A2:C3’).
GalleryThe chart type.-4101 – The enumerator for the Chart Shown above.
FormatThe option number for the built-in auto formats.7
PlotByAn integer specifying whether the data for each series is in rows or columns.2 – The enumerator for the xlRows XlRowCol enumerator.
CategoryLabelsAn integer specifying the number of rows or columns within the source range that contains category labels.1 – There is one row with category labels (the department names).
SeriesLabelsAn integer specifying the number of rows or columns within the source range that contains series labels.0 – There are no series labels in your data.
HasLegendTRUE to include a legend.1
TitleVARIANT with the title of the chart.You pass a string such as ‘Top 10 Customer’.


  • Make Excel visible by adding the following code.


xlApp.Visible := TRUE;

Excel produces a General Protection Fault error when you close a new Excel worksheet that is created when Excel is invisible. To resolve this, you can make Excel visible immediately after you create a new worksheet. You can also make Excel visible just before you create a new Excel worksheet and then make it invisible again immediately after creating the new Excel worksheet. In this case, you would add the following code.
xlApp.Visible := TRUE;

xlBook := xlApp.Workbooks.Open(FileName);

xlApp.Visible := FALSE;


  •  Clearing the Temp Table by adding the following code.


CustAmount.DELETEALL;


  • Complete code in OnRun trigger should look like below:


ExcelChart-4
Saving and Running the Codeunit

You can test the codeunit for creating the graph by running the codeunit from Object Designer.

To save and run a codeunit

  1. On the File menu, choose Save.

  2. In the Save As window, enter an ID and name, and then choose the OK

  3. In Object Designer, select the codeunit, and then choose the Run


The Microsoft Excel graph should appear. As show above in beginning of the post.

Note

If you get an error states Old format or invalid type library, then make sure that the language in the regional settings of your computer matches the language version of Microsoft Excel.

Below detailed reference to the values used in above code:

 
XlChartType






















































































































































































































































































































































































NameValueDescription
xl3DArea-40983D Area.
xl3DAreaStacked783D Stacked Area.
xl3DAreaStacked10079100% Stacked Area.
xl3DBarClustered603D Clustered Bar.
xl3DBarStacked613D Stacked Bar.
xl3DBarStacked100623D 100% Stacked Bar.
xl3DColumn-41003D Column.
xl3DColumnClustered543D Clustered Column.
xl3DColumnStacked553D Stacked Column.
xl3DColumnStacked100563D 100% Stacked Column.
xl3DLine-41013D Line.
xl3DPie-41023D Pie.
xl3DPieExploded70Exploded 3D Pie.
xlArea1Area
xlAreaStacked76Stacked Area.
xlAreaStacked10077100% Stacked Area.
xlBarClustered57Clustered Bar.
xlBarOfPie71Bar of Pie.
xlBarStacked58Stacked Bar.
xlBarStacked10059100% Stacked Bar.
xlBubble15Bubble.
xlBubble3DEffect87Bubble with 3D effects.
xlColumnClustered51Clustered Column.
xlColumnStacked52Stacked Column.
xlColumnStacked10053100% Stacked Column.
xlConeBarClustered102Clustered Cone Bar.
xlConeBarStacked103Stacked Cone Bar.
xlConeBarStacked100104100% Stacked Cone Bar.
xlConeCol1053D Cone Column.
xlConeColClustered99Clustered Cone Column.
xlConeColStacked100Stacked Cone Column.
xlConeColStacked100101100% Stacked Cone Column.
xlCylinderBarClustered95Clustered Cylinder Bar.
xlCylinderBarStacked96Stacked Cylinder Bar.
xlCylinderBarStacked10097100% Stacked Cylinder Bar.
xlCylinderCol983D Cylinder Column.
xlCylinderColClustered92Clustered Cone Column.
xlCylinderColStacked93Stacked Cone Column.
xlCylinderColStacked10094100% Stacked Cylinder Column.
xlDoughnut-4120Doughnut.
xlDoughnutExploded80Exploded Doughnut.
xlLine4Line.
xlLineMarkers65Line with Markers.
xlLineMarkersStacked66Stacked Line with Markers.
xlLineMarkersStacked10067100% Stacked Line with Markers.
xlLineStacked63Stacked Line.
xlLineStacked10064100% Stacked Line.
xlPie5Pie.
xlPieExploded69Exploded Pie.
xlPieOfPie68Pie of Pie.
xlPyramidBarClustered109Clustered Pyramid Bar.
xlPyramidBarStacked110Stacked Pyramid Bar.
xlPyramidBarStacked100111100% Stacked Pyramid Bar.
xlPyramidCol1123D Pyramid Column.
xlPyramidColClustered106Clustered Pyramid Column.
xlPyramidColStacked107Stacked Pyramid Column.
xlPyramidColStacked100108100% Stacked Pyramid Column.
xlRadar-4151Radar.
xlRadarFilled82Filled Radar.
xlRadarMarkers81Radar with Data Markers.
xlStockHLC88High-Low-Close.
xlStockOHLC89Open-High-Low-Close.
xlStockVHLC90Volume-High-Low-Close.
xlStockVOHLC91Volume-Open-High-Low-Close.
xlSurface833D Surface.
xlSurfaceTopView85Surface (Top View).
xlSurfaceTopViewWireframe86Surface (Top View wireframe).
xlSurfaceWireframe843D Surface (wireframe).
xlXYScatter-4169Scatter.
xlXYScatterLines74Scatter with Lines.
xlXYScatterLinesNoMarkers75Scatter with Lines and No Data Markers.
xlXYScatterSmooth72Scatter with Smoothed Lines.
xlXYScatterSmoothNoMarkers73Scatter with Smoothed Lines and No Data Markers.

expression .ChartWizard(Source, Gallery, Format, PlotBy, CategoryLabels, SeriesLabels, HasLegend, Title, CategoryTitle, ValueTitle, ExtraTitle)

expression A variable that represents a Chart object.

Parameters













































































NameRequired/OptionalData TypeDescription
SourceOptionalVariantThe range that contains the source data for the new chart. If this argument is omitted, Microsoft Excel edits the active chart sheet or the selected chart on the active worksheet.
GalleryOptionalVariantOne of the constants of XlChartType specifying the chart type.
FormatOptionalVariantThe option number for the built-in autoformats. Can be a number from 1 through 10, depending on the gallery type. If this argument is omitted, Microsoft Excel chooses a default value based on the gallery type and data source.
PlotByOptionalVariantSpecifies whether the data for each series is in rows or columns. Can be one of the following XlRowCol constants: xlRows or xlColumns. Values can be [1 or 2]
CategoryLabelsOptionalVariantAn integer specifying the number of rows or columns within the source range that contain category labels. Legal values are from 0 (zero) through one less than the maximum number of the corresponding categories or series.
SeriesLabelsOptionalVariantAn integer specifying the number of rows or columns within the source range that contain series labels. Legal values are from 0 (zero) through one less than the maximum number of the corresponding categories or series.
HasLegendOptionalVariantTrue to include a legend.
TitleOptionalVariantThe chart title text.
CategoryTitleOptionalVariantThe category axis title text.
ValueTitleOptionalVariantThe value axis title text.
ExtraTitleOptionalVariantThe series axis title for 3-D charts or the second value axis title for 2-D charts.

Remarks

If Source is omitted and either the selection isn't an embedded chart on the active worksheet or the active sheet isn't an existing chart, this method fails and an error occurs.

You can use other values from above table to create graph of your choice.

1 comment: