Other API Samples C# logo

Bar Chart SpreadsheetGear API Sample

Description

Demonstrates generating a basic Bar Chart.

Sample Source Code

// Open workbook with some data for the chart and get a reference to the active 
// sheet and its cells.
SpreadsheetGear.IWorkbook workbook = 
    SpreadsheetGear.Factory.GetWorkbook("ChartData.xlsx");
SpreadsheetGear.IWorksheet worksheet = workbook.ActiveWorksheet;
SpreadsheetGear.IRange cells = worksheet.Cells;

// Get a reference to the worksheet window info and shapes collection.
SpreadsheetGear.IWorksheetWindowInfo windowInfo = worksheet.WindowInfo;
SpreadsheetGear.Shapes.IShapes shapes = worksheet.Shapes;

// Add a chart to the worksheet's shape collection.
// NOTE: Calculate the coordinates of the chart by converting row and column
//       coordinates to points.  Use fractional row and column values to get 
//       coordinates anywhere in between row and column boundaries.
double left = windowInfo.ColumnToPoints(0.15);
double top = windowInfo.RowToPoints(0.5);
double right = windowInfo.ColumnToPoints(5.85);
double bottom = windowInfo.RowToPoints(13.5);
SpreadsheetGear.Charts.IChart chart =
    shapes.AddChart(left, top, right - left, bottom - top).Chart;

// Get the source data range from an existing defined name.
SpreadsheetGear.IRange source = workbook.Names["RegionalSales"].RefersToRange;

// Set the chart's source data range, plotting series in columns.
chart.SetSourceData(source, SpreadsheetGear.Charts.RowCol.Columns);

// Set the chart type to a stacked bar.
chart.ChartType = SpreadsheetGear.Charts.ChartType.BarStacked;

// Set the distance between bars as a percentage of the bar width.
chart.ChartGroups[0].GapWidth = 50;

// Add a chart title and change the font size.
chart.HasTitle = true;
chart.ChartTitle.Text = "Combined Sales by Quarter";
chart.ChartTitle.Font.Size = 12;

// Get a reference to the value axis and set the gridline dash style.
SpreadsheetGear.Charts.IAxis valueAxis =
    chart.Axes[SpreadsheetGear.Charts.AxisType.Value];
valueAxis.MajorGridlines.Format.Line.DashStyle =
    SpreadsheetGear.Shapes.LineDashStyle.Dash;