Excel xlXYScatter has lines when generated by C#
I'm using an app written in WPF C# to generate an Excel Worksheet and plot out some data.
xlXYScatterLines
, xlXYSca开发者_StackOverflow中文版tterLinesNoMarkers
, xlXYScatterSmooth
, xlXYScatterSmoothNomarkers
all worked out fine.
Except for xlXYScatter
which always produces connects the data point with lines (= xlXYScatterLines
), while it is supposed to display only scattered dots. Below is my code.
Excel.ChartObjects xlCharts = (Excel.ChartObjects)oSheet.ChartObjects(Type.Missing);
Excel.ChartObject myChart = (Excel.ChartObject)xlCharts.Add(350,20,500,350);
Excel.Chart chartPage = myChart.Chart;
chartPage.ChartType = Excel.XlChartType.xlXYScatter;
is this a bug?
[SOLUTION - not answer]
After two days of playing around, I accidentally found if I set the source data before defining chart type. The problem would not occur and xlXYScatter will show as xlXYScatter, not xlXYScatterLines.
chartPage.SetSourceData(Range, Missing.Value);
chartPage.ChartType = Excel.XlChartType.xlXYScatter;
I do not understand why and how this gets around the problem because it a intuitive to define the source after you define the chart, and in the opposite doesn't make much sense. So I consider this as a solution, but not an answer. Hope someone can still answer the question.
I haven't done this in C#, but in VBA I had some similar grief. I ended up having to create the chart as xlXYScatterLinesNoMarkers
. Then after the chart was created, I changed the type to xlXYScatter
. Ugly, but it seemed to work for me in VBA.
精彩评论