Description
Creating Excel-compatible Line 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.Line, ChartType.LineMarkers or ChartType.LineMarkersStacked chart type (or one of the other line-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 line 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.LineMarkers. Other line-based chart types are available, such asChartType.Line,ChartType.LineMarkersStackedand others. - Additional chart options are configured, including:
- Adding data labels by setting
IChart.HasDataLabelstotrue, then setting the position of each label (above each data point in this example). - Accessing the "value" axis (Y-axis) via the
IChart.Axes[...]collection to hide the the major gridlines. - Adding a chart title by setting
IChart.HasTitletotrue, then setting the title text and other chart title options under theIChart.ChartTitleproperty
- Adding data labels by setting