问题
I am creating an excel chart using C# (interop). I am using get_Range function to get it from the data sheet in excel. I am getting the graph properly. But I am not getting the Legend names next to graph, it is coming as series0, series1. I am using folowing code.
Range chartRange;
Object misValue = System.Reflection.Missing.Value;
ChartObjects xlCharts = (ChartObjects)sheet0.ChartObjects(Type.Missing);
ChartObject myChart = (ChartObject)xlCharts.Add(10, 70, 250, 250);
Chart chartPage = myChart.Chart;
Chart chartPage1 = myChart.Chart;
chartRange = sheet0.get_Range("$G$45:$G$54,$AT$45:$AT$54", misValue);
chartPage.SetSourceData(chartRange, misValue);
chartPage.ChartType = XlChartType.xlColumnClustered;
chartPage.Location(XlChartLocation.xlLocationAsNewSheet, "Chart");
What shoud I change in get_Range method to get the Legend names in chart?
Thank you in advance.
回答1:
Try to iterate over series collection and set the names one by one. Something like this:
foreach (Series series in myChart.SeriesCollection()){
series.Name = "MyName";
}
回答2:
Generally the data is arranged so that if the series are in columns, the first column of the data has the X values for all series, the rest of the columns are the Y values, and the first row of the data has the series names, which are shown in the legend. To make sure Excel is able to parse the data this way, make sure the top left cell is blank. Use the entire range, including X values in the first column and the series names in the first row, as the chart range in SetSourceData.
来源:https://stackoverflow.com/questions/14806497/excel-chart-not-getting-legends