MindFusion.Wpf Pack Programmer's Guide
Creation and Initialization

Creating the Chart

Chart objects are created indirectly, by calling one of the AddChart methods of the Drawing class. The drawing of a worksheet is accessible through the Drawing property. The following example demonstrates how to create and add a new chart to an existing worksheet:

C#  Copy Code

worksheet.Drawing.AddChart();

Visual Basic  Copy Code

worksheet.Drawing.AddChart()

Setting the Data

There are two ways to set the data of the chart. The first one is by calling the SetDataSource method and specifying the cell range that contains the data. The second method is by manually creating the Series objects and adding them to the chart's Series collection.

To specify the data of the chart by the SetDataSource method, pass to the method a string representing a cell range (in the A1 or R1C1 format) or an expression that evaluates to a cell range. The resulting cell range is used to obtain the data of the chart. Note, that the data can identify cells from another worksheet, not necessarily the worksheet containing the chart. The data in the specified cell range is fetched and analyzed based on the chart type and the values of the other SetDataSource parameters, and the appropriate series are created and added to Series collection of the chart.

To specify the data of the chart manually, create instances of the Series class, set their Name, Labels and Values properties and add them to the chart's Series collection. The series are created indirectly, by calling the Add method of the Series collection of the chart. The Name of the series can be an expression. In this case the actual name is the result of the expression evaluation. The Labels and Values properties can represent a cell range or an expression, which evaluates to a cell range or an array. The cell range must be a vector, that is its width or height must be equal to 1.

The following example illustrates both methods. The example assumes that chart identifies an existing chart.

C#  Copy Code

// Method 1
chart.SetDataSource("DataSheet!$A$1:$D$10", PlotBy.Column, null, null);

// Method 2
var series = chart.Series.Add();
series.Name = "My Series";
series.Values = "=A1:A5";

Visual Basic  Copy Code

' Method 1
chart.SetDataSource("DataSheet!$A$1:$D$10", PlotBy.Column, Nothing, Nothing)

' Method 2
Dim series = chart.Series.Add()
series.Name = "My Series"
series.Values = "=A1:A5"

Positioning

The chart object can be positioned inside the worksheet programmatically through a set of properties. The Anchor property specifies whether the object is positioned relatively to a cell or to the top-left corner of the worksheet. The FromColumn and FromRow properties specify the relative cell and the HorizontalOffset and VerticalOffset properties specify the distance of the chart from its origin. The chart can be resized through the Width and Height properties.

The chart can be positioned and resized interactively by selecting the chart and dragging the handles at the four corners with the mouse.