Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:

C#
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)
Posted
Updated 7-Dec-18 4:30am
v4

1 solution

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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900