private void PopulateMainChart()
{
//--- Get the data from the database.
DataTable[] dtArr = GetCategoryQuarterlySales();
//--- Find the main chart based on its title text, and get
//--- a reference to it.
Chart mainChart = FindChart("Quarterly Sales 2003");
//--- Clear all the existing series objects from the collection
SeriesCollection seriesCol = mainChart.SeriesCollection;
while (seriesCol.Count > 0)
seriesCol.Remove(0);
//--- Each DataTable has a single row of data.
int iRow = 29;
for (int i = 0; i < dtArr.Length; i++)
{
DataTable dt = dtArr[i];
//--- Import data from the DataTable.
ws.ImportData(dt, ws[iRow, 1]);
//--- Add the imported data as a new Series object
//--- in the chart's collection. There will be a variable
//--- number of series objects depending on which categories
//--- were selected for display.
Area a = ws.CreateArea(iRow, 2, 1, 4);
Series srs = seriesCol.CreateSeries(a);
srs.NameFormula = ws[iRow, 1].Name;
iRow++;
}
//--- Re-set the category data. The size will vary
//--- depending on the selected categories.
seriesCol.CategoryData =
ws.CreateArea(29, 1, dtArr.Length, 1).Dimensions;
//--- Add a legend to the chart if desired
if (bLegend)
{
mainChart.Legend.Visible = true;
mainChart.Legend.Location = Legend.LegendLocation.Top;
}
else
{
//--- The legend is hidden in the template workbook.
//--- Hide it again in case it's made visible.
mainChart.Legend.Visible = false;
}
}
|