如何使用C#将我的系列名称显示在Excel中的折线图图例中 [英] How do I get my series name to appear in my line chart legend in excel using C#

查看:273
本文介绍了如何使用C#将我的系列名称显示在Excel中的折线图图例中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Excel中更改折线图的系列名称。我正在使用C#(interop)来创建图表。这就是我到目前为止:



 Excel.Range xvalues = myWorkSheet.Range [  R3  R5 ]; 
Excel.Range values1 = myWorkSheet.Range [ S3 S5];
Excel.Range values2 = myWorkSheet.Range [ T3 T5];
Excel.Range values3 = myWorkSheet.Range [ U3 U5];
Excel.Range values4 = myWorkSheet.Range [ V3 V5];

Excel.SeriesCollection sc = myChart.SeriesCollection();
myChart.ChartType = Excel.XlChartType.xlLine;

Excel.Series series1 = sc.NewSeries();
Excel.Series series2 = sc.NewSeries();
Excel.Series series3 = sc.NewSeries();
Excel.Series series4 = sc.NewSeries();

series1.XValues = xValues;
series1.Values = values1;
series1.Name = mySeries1;

series2.XValues = xValues;
series2.Values = values2;
series2.Name = mySeries2;

series3.XValues = xValues;
series3.Values = values3;
series3.Name = mySeries3;

series4.XValues = xValues;
series4.Values = values4;
series4.Name = mySeries4;







图表显示正常,图例出现在我想要的位置,但系列名称不会出现在传说。我不确定我做错了什么。



我尝试了什么:



我做了一些搜索并尝试了很多不同的东西。我尝试创建从R3到V5的整个范围,然后使用MyChart.SetSourceData例程,但这也不起作用。当前配置使我的线看起来像他们想象的那样(虽然我想让线从Y轴开始而不是通过数据点缩进)

解决方案

我没有在代码中添加的是我将图例放在图表区域内的事实。我设置了myChart.Legend.Height,myChart.Legend.Left和myChart.Legend.Top属性。我想我不需要设置Legend.Width属性,因为我认为图例会自动将其设置为适当的宽度。我错了。一旦我设置了myChart.Legend.Width属性,就会出现系列名称。

I'm trying to change the series names of my line chart in Excel. I'm using C# (interop) to create the chart. This is what I have so far:

Excel.Range xvalues = myWorkSheet.Range["R3", "R5"];
Excel.Range values1 = myWorkSheet.Range["S3", "S5"];
Excel.Range values2 = myWorkSheet.Range["T3", "T5"];
Excel.Range values3 = myWorkSheet.Range["U3", "U5"];
Excel.Range values4 = myWorkSheet.Range["V3", "V5"];

Excel.SeriesCollection sc = myChart.SeriesCollection();
myChart.ChartType = Excel.XlChartType.xlLine;

Excel.Series series1 = sc.NewSeries();
Excel.Series series2 = sc.NewSeries();
Excel.Series series3 = sc.NewSeries();
Excel.Series series4 = sc.NewSeries();

series1.XValues = xValues;
series1.Values = values1;
series1.Name = "mySeries1";

series2.XValues = xValues;
series2.Values = values2;
series2.Name = "mySeries2";    

series3.XValues = xValues;
series3.Values = values3;
series3.Name = "mySeries3";

series4.XValues = xValues;
series4.Values = values4;
series4.Name = "mySeries4";




The chart displays fine, and a legend appears where I want it, but the series names don't appear next to the line colors in the legend. I'm not sure what I'm doing wrong.

What I have tried:

I've done some searches and have tried a number of different things. I tried creating the entire range from R3 to V5 and then using the MyChart.SetSourceData routine but that didn't work either. The current configuration make my lines look the way their supposed to (although I would like to make the lines start from the Y axis and not indented by a data point)

解决方案

What I didn't put in the code, was the fact that I position the legend within the chart area. I had set the myChart.Legend.Height, myChart.Legend.Left and myChart.Legend.Top properties. I figured that I didn't need to set the Legend.Width property, as I thought that the legend would automatically set that to the proper width. I was wrong. Once I set the myChart.Legend.Width property, then the series names showed up.


这篇关于如何使用C#将我的系列名称显示在Excel中的折线图图例中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆