Description
Creating Excel-compatible Pie Charts is trivial to do with SpreadsheetGear. At its most basic level you call IShapes.AddChart(...) and then set the returned chart object to use a ChartType.Pie chart type (or one of the other pie-based chart types).
- Opens an Excel workbook (
ChartData.xlsx) pre-populated with chart source data located in the Defined Name "TotalByQuarter" which points to the rangeL3:L6. This will be used to create a single series in the chart. - Charts are positioned on the worksheet relative to the top-left edge of the worksheet (in Points); however, you might want to position the column chart based on row / column indexes. SpreadsheetGear can take care of this conversion by using the
IWorksheetWindowInfo.ColumnToPoints(double columnIndex)andRowToPoints(double rowIndex)methods. - Adds the chart at the specified worksheet location by calling
IShapes.AddChart(double top, double left, double width, double height)and setting the source data for the chart by callingIChart.SetSourceData(IRange source, RowCol rowCol). - Sets the chart's type (
IChart.ChartType) toChartType.Pie. Other pie-based chart types are available, such asChartType.PieExplodedand others. - The single series in this piechart is accessed via
IChart.SeriesCollection[...]to:- Set the pie series labels (shown in the legend) via
ISeries.XValueswhich are sourced from another Defined Name "Quarters" which points to the rangeG3:G6and so will use the axis labels "Q1", "Q2", "Q3" and "Q4". - Add data labels to each pie slice (
ISeries.HasDataLabels = true), showing each data point's percentage (ISeries.DataLabels.ShowPercentage = true) and hiding the underlying data point's value (ISeries.DataLabels.ShowValue = false).
- Set the pie series labels (shown in the legend) via
- Adds a chart title by setting
IChart.HasTitletotrue, then sets the title text and other chart title options under theIChart.ChartTitleproperty.