如何使用C#在Excel中的图表上添加多个系列

时间:2022-09-28 17:47:36

I would like to add a chart like the following picture.

我想添加如下图所示的图表。

如何使用C#在Excel中的图表上添加多个系列

This chart has 3 series (Black, Red, Blue).

此图表有3个系列(黑色,红色,蓝色)。

The following is a block of code that creates "one" series on a chart...

以下是在图表上创建“一个”系列的代码块...

Excel._Workbook oWorkbook = (Excel._Workbook)oSheet.Parent;
Excel._Chart oChart = (Excel._Chart)oWorkbook.Charts.Add(oSheet, Type.Missing, Type.Missing, Type.Missing);

// Y axis data
Excel.Range oRange = oSheet.get_Range(yRange, Type.Missing);

// Creates a chart
oChart.ChartWizard(oRange, chartType, 2, Excel.XlRowCol.xlColumns, Type.Missing, Type.Missing, false, title, xAxisTitle, yAxisTitle, Type.Missing);

// Sets X axis category
Excel.Series oSeries = (Excel.Series)oChart.SeriesCollection(1);    
oSeries.XValues = oSheet.get_Range(xRange, Type.Missing);
oChart.Name = chartName;

MSDN API is not helpful enough and I can hardly find any tutorial or example on this problem. (Or maybe I am not that good searching them)
It would be appreciated if someone gives me a solution.

MSDN API没有用,我几乎找不到任何有关此问题的教程或示例。 (或者也许我不是那么好搜索它们)如果有人给我一个解决方案,我将不胜感激。

2 个解决方案

#1


3  

I could solve this issue with very simple solution.
If I set the yRange (oRange) right, "ChartWizard" method automatically creates the graphs.
So instead of range having "A2:A100", "A2:A100,C2:C100" will generates two lines (series) on one chart and also if the data range includes the heading (or series label), the "ChartWizard" will automatically put the series name in the legend.

我可以用非常简单的解决方案解决这个问题。如果我将yRange(oRange)设置为右,“ChartWizard”方法会自动创建图形。因此,代替具有“A2:A100”的范围,“A2:A100,C2:C100”将在一个图表上生成两行(系列),并且如果数据范围包括标题(或系列标签),则“ChartWizard”将自动将系列名称放在图例中。

#2


-1  

I am not too Familiar with C# but you could try something along the lines of

我不太熟悉C#,但你可以尝试一些类似的东西

Excel.Range NewRangeObject = oSheet.get_Range(SecondyRange, Type.Missing);        

oChart.NewSeries
oChart.SeriesCollection(2).Name = "New Series"
oChart.SeriesCollection(2).Value = NewRangeObject

To be clear I am kind of guessing based on how the VBA code would look.

要清楚,我有点猜测基于VBA代码的外观。

A higher level solution is to record a macro in excel directly of setting up the chart exactly the way you want.... then port the Code over to C#. It seems that most of the commands are similiar but wrapped in a slightly different snytax.

更高级别的解决方案是直接在Excel中记录宏,以完全按照您想要的方式设置图表....然后将代码移植到C#。似乎大多数命令都是类似的,但包含在略有不同的snytax中。

#1


3  

I could solve this issue with very simple solution.
If I set the yRange (oRange) right, "ChartWizard" method automatically creates the graphs.
So instead of range having "A2:A100", "A2:A100,C2:C100" will generates two lines (series) on one chart and also if the data range includes the heading (or series label), the "ChartWizard" will automatically put the series name in the legend.

我可以用非常简单的解决方案解决这个问题。如果我将yRange(oRange)设置为右,“ChartWizard”方法会自动创建图形。因此,代替具有“A2:A100”的范围,“A2:A100,C2:C100”将在一个图表上生成两行(系列),并且如果数据范围包括标题(或系列标签),则“ChartWizard”将自动将系列名称放在图例中。

#2


-1  

I am not too Familiar with C# but you could try something along the lines of

我不太熟悉C#,但你可以尝试一些类似的东西

Excel.Range NewRangeObject = oSheet.get_Range(SecondyRange, Type.Missing);        

oChart.NewSeries
oChart.SeriesCollection(2).Name = "New Series"
oChart.SeriesCollection(2).Value = NewRangeObject

To be clear I am kind of guessing based on how the VBA code would look.

要清楚,我有点猜测基于VBA代码的外观。

A higher level solution is to record a macro in excel directly of setting up the chart exactly the way you want.... then port the Code over to C#. It seems that most of the commands are similiar but wrapped in a slightly different snytax.

更高级别的解决方案是直接在Excel中记录宏,以完全按照您想要的方式设置图表....然后将代码移植到C#。似乎大多数命令都是类似的,但包含在略有不同的snytax中。