Using Charts
RadSpreadProcessing allows you to add and manipulate charts in your spreadsheet documents. The available API enables you to insert and modify different types of charts.
The chart objects are stored in the Charts collection of the worksheet and are represented by the FloatingChartShape class. The FloatingChartShape object exposes a Chart property of type DocumentChart.
FloatingChartShape
The charts are wrapped in shapes. The FloatingChartShape class derives from FloatingShapeBase and represents the wrapper that allows you to add a chart to a document.
The FloatingChartShape class exposes the following constructors, which parse the data in the chartDataRange parameter and create a chart with all data already filled:
-
FloatingChartShape(Worksheet worksheet, CellIndex cellIndex, CellRange chartDataRange, params ChartType[] chartTypes) -
FloatingChartShape(Worksheet worksheet, CellIndex cellIndex, CellRange chartDataRange, SeriesRangesOrientation seriesRangesOrientation, params ChartType[] chartTypes)The parameters accepted by the constructors are as follows:
Parameter Description worksheetThe worksheet that the shape is associated with. cellIndexThe cell index where the top left corner of the shape is positioned. chartDataRangeThe range containing the chart data. seriesRangesOrientationA value indicating whether the series of the chart refer to vertical or horizontal ranges, or if the direction is determined automatically. chartTypesThe types of chart to create. Passing more than one type creates a combo chart. The number of
chartTypesmust be no more than the number of columns inside thechartDataRangeminus one (the first column populates the X axis). Otherwise, aSystem.NullReferenceExceptionis thrown.
Once you have created a FloatingChartShape, you can insert it in the document through the Add() method of the worksheet Charts property.
Ensure that you have set the size of the
FloatingChartShapeobject. Otherwise, it is inserted in the worksheet with zero size and is invisible.
Example 1: Create a Chart Through FloatingChartShape and Add It to a Worksheet
FloatingChartShape chartShape = new FloatingChartShape(worksheet, new CellIndex(6, 4), new CellRange(1, 1, 5, 2), ChartType.Column)
{
Width = 460,
Height = 250
};
worksheet.Charts.Add(chartShape);
SeriesGroup seriesGroup = chartShape.Chart.SeriesGroups.First(); // type BarChart
int seriesCount = seriesGroup.Series.Count(); // 1
SeriesBase series = seriesGroup.Series.First(); // type BarSeries
The result of executing the code in Example 1 looks like Figure 1.
Figure 1: Chart added to a worksheet

The Chart property of FloatingChartShape holds an object of type DocumentChart.
Example 2: Create a Combo (Column and Line) Chart Through FloatingChartShape and Add It to a Worksheet
FloatingChartShape chartShape = new FloatingChartShape(worksheet, new CellIndex(0, 5), new CellRange(0, 0, 12, 3), new[] { ChartType.Line, ChartType.Line, ChartType.Column })
{
Width = 400,
Height = 250
};
worksheet.Charts.Add(chartShape);
The result of executing the code in Example 2 looks like Figure 2.
Figure 2: Combo chart added to a worksheet

Changing the Appearance of FloatingChartShape
The FloatingChartShape class exposes properties that allow you to customize the shape appearance. You can control the outline of the shape and its fill.
Example 3: Customize the Fill and Outline of FloatingChartShape
FloatingChartShape chartShape = new FloatingChartShape(workbook.ActiveWorksheet, new CellIndex(2, 7), new CellRange(0, 0, 4, 3), ChartType.Column)
{
Width = 480,
Height = 288,
};
chartShape.Outline.Fill = new SolidFill(new ThemableColor(Colors.LightGray));
chartShape.Outline.Width = 5;
chartShape.Fill = new SolidFill(new ThemableColor(Colors.Aquamarine));
The result of executing the code in Example 3 over a cell range containing sample data looks like Figure 3.
Figure 3: Customized FloatingChartShape

The series are styled using the colors defined in the Document Theme.
DocumentChart
The DocumentChart object represents the chart itself and contains the following properties:
| Property | Description |
|---|---|
SeriesGroup | Represents a collection of the groups in which the series of the chart are organized. |
PrimaryAxes | Represents the primary group of axes of the chart. |
SecondaryAxes | Represents the secondary group of axes of the chart. Used when there is more than one group of series (combo chart). |
Title | Represents the title of the chart. |
Legend | Represents the legend of the chart. |
The class also exposes a Clone() method, which creates a deep copy of the object.
You can create an empty DocumentChart object and then set the desired values manually.
Example 4: Create an Empty Chart and Set Its Values Manually
DocumentChart chart = new DocumentChart();
//Fill the chart with data
SeriesGroup seriesGroup = new BarSeriesGroup();
chart.SeriesGroups.Add(seriesGroup);
WorkbookFormulaChartData seriesValuesData = new WorkbookFormulaChartData(worksheet, new CellRange(2, 2, 5, 2));
WorkbookFormulaChartData seriesCategoriesData = new WorkbookFormulaChartData(worksheet, new CellRange(2, 1, 5, 1));
seriesGroup.Series.Add(seriesCategoriesData, seriesValuesData);
chart.PrimaryAxes.CategoryAxis = new DateAxis();
chart.PrimaryAxes.ValueAxis = new ValueAxis();
(seriesGroup as ISupportAxes).AxisGroupName = AxisGroupName.Primary;
You can then use the chart to replace the chart in an existing FloatingChartShape.
Example 5: Add the DocumentChart to a Worksheet
chartShape.Chart = chart;
worksheet.Charts.Add(chartShape);
For more information about series, see the Series article. For a description of the axes objects, see Working with Axes.
The initial data and the resulting chart are shown in Figure 1.
Iterating the Charts of a Worksheet
You can access the Charts collection of the Shapes collection of the Worksheet instance and enumerate the charts.
Example 6: Iterate All the Charts in a Worksheet
foreach (FloatingChartShape chartShape in worksheet.Charts)
{
DocumentChart chart = chartShape.Chart;
SeriesGroup group = chart.SeriesGroups.First();
SeriesBase series = group.Series.First();
}