Reputation:
Please can someone help me with this as it's driving me nuts!
I'm creating an excel chart using C# and the COM interface pragmatically.
I've created the chart using the chart wizard.
I want to then add more series to this chart. I can add the series but the extra data is on new columns and they are not automatically created.
Am I going about this the wrong way?
Add Chart:
public void MakeExcelChart(string startRange, string endRange, string chartTitle, string seriesName)
{
ExcelChart = (Excel.Chart)ExcelWBook.Charts.Add(Missing.Value, Missing.Value, Missing.Value, Missing.Value);
ExcelApp.Visible = true;
ExcelChart.HasTitle = true;
ExcelChart.ChartTitle.Text = chartTitle;
ExcelRange = ExcelWSheet.get_Range(startRange, endRange);
ExcelChart.ChartWizard(ExcelRange, Excel.XlChartType.xlColumnClustered, Missing.Value, Excel.XlRowCol.xlColumns, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
ExcelChart.ApplyDataLabels(Microsoft.Office.Interop.Excel.XlDataLabelsType.xlDataLabelsShowBubbleSizes, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
ExcelChart.ChartArea.Fill.OneColorGradient(Microsoft.Office.Core.MsoGradientStyle.msoGradientHorizontal, 1, 1);
GetSeriesCollection();
ExcelSeries = ExcelSeriesCollection.Item(1);
ExcelSeries.Name = seriesName;
}
And to add series:
public void AddSeries(string col1, string col2, string startRange, string endRange, string seriesName)
{
ExcelSeries = ExcelSeriesCollection.NewSeries();
ExcelSeries.HasDataLabels = true;
ExcelRange = ExcelWSheet.get_Range(col1+startRange, col1+endRange);
ExcelSeries.XValues = ExcelRange;
ExcelRange = ExcelWSheet.get_Range(col2+startRange, col2+endRange);
ExcelSeries.Values = ExcelRange;
ExcelChart.HasLegend = true;
ExcelSeries.Name = seriesName;
}
Upvotes: 0
Views: 10305
Reputation: 181
try something like this. It works for me pretty well.
Excel.ChartObjects xlChart = (Excel.ChartObjects)xlWorkSheet.ChartObjects(Type.Missing);
Excel.ChartObject myChart = (Excel.ChartObject)xlChart.Add(1050, 865, 690, 265);
Excel.Chart chartPage = myChart.Chart;
chartPage.ChartType = Excel.XlChartType.xlColumnClustered;
chartPage.HasTitle = true;
chartPage.ChartTitle.Text = "title";
chartPage.HasLegend = true;
Excel.SeriesCollection oSeriesCollection = (Excel.SeriesCollection)myChart.Chart.SeriesCollection(misValue);
Excel.Series series1 = oSeriesCollection.NewSeries();
Excel.Series series2 = oSeriesCollection.NewSeries();
Excel.Series series3 = oSeriesCollection.NewSeries();
Excel.Range series1_range = xlWorkSheet.get_Range("start_range1","end_range1");
Excel.Range series2_range = xlWorkSheet.get_Range("start_range2","end_range2");
Excel.Range series3_range = xlWorkSheet.get_Range("start_range3","end_range3");
series1.Values = series1_range;
series2.Values = series2_range;
series3.Values = series3_range;
you can then change parameters of each series individually for example:
series1.Name = "some name";
and so on.
Upvotes: 2
Reputation:
You need to extend the Excel table that is defining the data. Good example is at http://blogs.msdn.com/vsod/archive/2009/06/15/creating-charts-in-word-and-powerpoint-using-newly-introduced-object-model-in-office-2007-service-pack-2.aspx
Upvotes: 0