Bar Chart

To add a bar chart, put a data source on worksheet:

var worksheet = this.grid.CurrentWorksheet;

worksheet["A2"] = new object[,] {
  { null, 2008, 2009, 2010, 2011, 2012 },
  { "City 1", 3, 2, 4, 2, 6 },
  { "City 2", 7, 5, 3, 6, 4 },
  { "City 3", 13, 10, 9, 10, 9 },
  { "Total", "=SUM(B3:B5)", "=SUM(C3:C5)", "=SUM(D3:D5)", "=SUM(E3:E5)", "=SUM(F3:F5)" },
};

Create three ranges, data source range, serial names range and category names range, then add these ranges into collection of highlight ranges to display them on worksheet:

var dataRange = worksheet.Ranges["B3:F5"];
var rowTitleRange = worksheet.Ranges["A3:A6"];
var categoryNamesRange = worksheet.Ranges["B2:F2"];

worksheet.AddHighlightRange(rowTitleRange);
worksheet.AddHighlightRange(categoryNamesRange);
worksheet.AddHighlightRange(dataRange);

Result:

237

Create the row chart instance:

var c1 = new Chart.BarChart
{
  Location = new Graphics.Point(460, 30),
  Size = new Graphics.Size(400, 260),

  Title = "Bar Chart Sample",
  DataSource = new WorksheetChartDataSource(worksheet, rowTitleRange, dataRange)
  {
    CategoryNameRange = categoryNamesRange,
  },
};

Add the chart instance into worksheet to display it:

worksheet.FloatingObjects.Add(c1);

The result:

273


Was the content of the page helpful?