Description
Creating Excel-compatible Bar 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.BarStacked chart type (or one of the other bar-based chart types).
- Opens an Excel workbook (
ChartData.xlsx) pre-populated with chart source data located in the Defined Name "RegionalSales" which points to the rangeG2:K6. - Charts are positioned on the worksheet relative to the top-left edge of the worksheet (in Points); however, you might want to position the bar 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.BarStacked. Other bar-based chart types are available, such asChartType.BarStacked100,ChartType.BarClusteredand others. - Additional chart options are configured, including:
- Setting the "gap width" via
IChartGroup.GapWidth, i.e., the distance between bars as a percentage of the bar width. - 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 (X-axis) via the
IChart.Axes[...]collection to apply a dashed line for the major gridlines.
- Setting the "gap width" via