Description
Creating Excel-compatible Area 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.Area chart type (or one of the other area-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.Area. Other area-based chart types are available, such asChartType.AreaStacked,ChartType.AreaStacked100and others. - The single series in this area chart is accessed via
IChart.SeriesCollection[...]to:- Set the category axis (X-axis) labels 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". - Changes the fill color of the area chart to use a theme color (
ColorSchemeIndex.Accent4) by setting theISeries.Format.Fill.ForeColor.ThemeColorproperty.
- Set the category axis (X-axis) labels via
- Hides the legend (
IChart.HasLegend = false) which is displayed by default. - Additional chart options are configured, including:
- Adding a chart title by setting
IChart.HasTitletotrue, then setting the title text and other chart title options under theIChart.ChartTitleproperty. - Accessing the "value" axis (Y-axis) via the
IChart.Axes[...]collection to hide the major gridlines.
- Adding a chart title by setting