在Excel图表更改轴标签在C#创建 [英] Changing Axis Labels on Excel Chart created in C#
问题描述
所以,我一直在努力,现在到这出一段时间,但似乎无法取得任何进展。我创建使用C#的Excel电子表格。我的电子表格包含图表。我能够做的一切与图表EXCEPT改变X轴标签。我尝试过,我可以找到,但没有工作的一切。
So I've been trying to figure this out for a while now but can't seem to get anywhere. I'm creating an Excel spreadsheet using C#. My spreadsheet contains a chart. I'm able to do everything with the chart EXCEPT change the X-Axis labels. I've tried just about everything that i can find but nothing works.
Excel.ChartObjects xlChart = (Excel.ChartObjects)xlWorkSheet.ChartObjects(Type.Missing);
Excel.ChartObject myChart = (Excel.ChartObject)xlChart.Add(1420, 660, 320, 180);
Excel.Chart chartPage = myChart.Chart;
chartPage.ChartType = Excel.XlChartType.xlXYScatterLines;
chartPage.HasTitle = true;
chartPage.ChartTitle.Text = "Title Text";
chartPage.HasLegend = false;
var yAxis = (Excel.Axis)chartPage.Axes(Excel.XlAxisType.xlValue,Excel.XlAxisGroup.xlPrimary);
yAxis.HasTitle = true;
yAxis.AxisTitle.Text = "Y-Axis Title text";
yAxis.MaximumScale = 20;
yAxis.AxisTitle.Orientation = Excel.XlOrientation.xlUpward;
Excel.Range Data_Range = xlWorkSheet.get_Range("A10", "C10");//Data to be plotted in chart
Excel.Range XVal_Range = xlWorkSheet.get_Range("A1", "C1");//Catagory Names I want on X-Axis as range
var x_labels = new List<string>() { "Val1", "Val2", "Val3" }; //Catagory Names I want on X-Axis as text array
Excel.SeriesCollection oSeriesCollection = (Excel.SeriesCollection)myChart.Chart.SeriesCollection(misValue);
Excel.Series Data = oSeriesCollection.NewSeries();
Data.Values = Data_Range;
Data.Name = "Plot Data";
Excel.Axis valueAxis = (Excel.Axis)chartPage.Axes(Excel.XlAxisType.xlCategory, Excel.XlAxisGroup.xlPrimary);
/*
Methods I've tried with no luck:
1) Data.XValues = XVal_Range;
2) Data.XValues = x_labels.ToArray();
3) valueAxis.CategoryNames = x_labels.ToArray();
4) valueAxis.CategoryNames = XVal_Range;
*/
每次只使用默认数值显示的时间......我m,在亏损
each time it just displays with the default number values... I'm at a loss
推荐答案
行,所以我想通了这个问题...你不能改变X轴上分散型图。我改变图形的类型 Excel.XlChartType.xlLineMarkers;
它工作得很好。
Ok so i figured out the problem... You can't change the X-Axis on a Scatter type graph. I changed the type of graph to Excel.XlChartType.xlLineMarkers;
and it worked fine.
下面是整个片段:
Excel.ChartObjects xlChart = (Excel.ChartObjects)xlWorkSheet.ChartObjects(Type.Missing);
Excel.ChartObject myChart = (Excel.ChartObject)xlChart.Add(1420, 660, 320, 180);
Excel.Chart chartPage = myChart.Chart;
chartPage.ChartType = Excel.XlChartType.xlLineMarkers;
chartPage.HasTitle = true;
chartPage.ChartTitle.Text = "Title Text";
chartPage.HasLegend = false;
var yAxis = (Excel.Axis)chartPage.Axes(Excel.XlAxisType.xlValue,Excel.XlAxisGroup.xlPrimary);
yAxis.HasTitle = true;
yAxis.AxisTitle.Text = "Y-Axis Title text";
yAxis.MaximumScale = 20;
yAxis.AxisTitle.Orientation = Excel.XlOrientation.xlUpward;
Excel.Range Data_Range = xlWorkSheet.get_Range("A10", "C10");//Data to be plotted in chart
Excel.Range XVal_Range = xlWorkSheet.get_Range("A1", "C1");//Catagory Names I want on X-Axis as range
Excel.SeriesCollection oSeriesCollection = (Excel.SeriesCollection)myChart.Chart.SeriesCollection(misValue);
Excel.Series Data = oSeriesCollection.NewSeries();
Data.Values = Data_Range;
Data.Name = "Plot Data";
Excel.Axis xAxis = (Excel.Axis)chartPage.Axes(Excel.XlAxisType.xlCategory, Excel.XlAxisGroup.xlPrimary);
xAxis.CategoryNames = XVal_Range;
这篇关于在Excel图表更改轴标签在C#创建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!