简体   繁体   English

在C#中创建的Excel图表上更改轴标签

[英]Changing Axis Labels on Excel Chart created in C#

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#. 我正在使用C#创建Excel电子表格。 My spreadsheet contains a chart. 我的电子表格包含一个图表。 I'm able to do everything with the chart EXCEPT change the X-Axis labels. 我可以用图表做任何事情除了更改X轴标签。 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;
*/ 

each time it just displays with the default number values... I'm at a loss 每次它只显示默认数值...我不知所措

Ok so i figured out the problem... You can't change the X-Axis on a Scatter type graph. 好的,我找出了问题...你不能改变Scatter类型图上的X轴。 I changed the type of graph to Excel.XlChartType.xlLineMarkers; 我将图形类型更改为Excel.XlChartType.xlLineMarkers; and it worked fine. 它工作得很好。

Here's the entire snippet: 这是整个代码段:

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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM