简体   繁体   English

将轴名称添加到图表 C#

[英]Add axis name into chart c#

I'm working with winforms using C#.我正在使用 C# 处理winforms

I use chart and I want to set the titles of the X- and Y-axis in code.我使用图表,我想在代码中设置 X 轴和 Y 轴的标题。 I tried我试过

chart1.chartarea(0).axisX.title = "xxx" 

but it does't work and I don't know why.但它不起作用,我不知道为什么。

I am using the charts control on the web and setting the X and Y axis titles are done in the following way.我正在使用网络上的图表控件,并通过以下方式设置 X 和 Y 轴标题。

I assume the API would be the same for winforms.我认为 API 与 winforms 相同。

var chartArea = new ChartArea("MyChart");
...
chartArea.AxisX.Title = "xxx";
chartArea.AxisY.Title = "yyy";

None of the solutions worked for me.没有一个解决方案对我有用。 I used the following code which helped me to add Axis title on windows form chart.我使用以下代码帮助我在 Windows 窗体图表上添加轴标题。 I am adding a couple of useful properties so anyone who is working on it can have an idea how to use it.我正在添加一些有用的属性,以便任何正在使用它的人都可以知道如何使用它。 I searched a lot to find out all those properties.我搜索了很多以找出所有这些属性。 There are very few examples of this types.这种类型的例子很少。

chartESTOr.Titles.Add("Est OR Date " + " (" + Year + ")").Font = new Font("Arial", 10, FontStyle.Bold); // Chart Title
chartESTOr.ChartAreas["ChartArea1"].AxisX.Title = "Month";  // Chart X Axis Title
chartESTOr.ChartAreas["ChartArea1"].AxisX.TitleAlignment = StringAlignment.Center; // Chart X axis Text Alignment 
chartESTOr.ChartAreas["ChartArea1"].AxisX.TextOrientation = TextOrientation.Rotated270; // Chart X Axis Text Orientation 
chartESTOr.ChartAreas["ChartArea1"].AxisX.TitleFont = new Font("Arial", 8, FontStyle.Bold); // Chart X axis Title Font
chartESTOr.ChartAreas["ChartArea1"].AxisX.Interval = 1; // Chart X Axis Interval
chartESTOr.ChartAreas["ChartArea1"].AxisY.Title = "Quote Value (USD)"; // Chart Y Axis Title 
chartESTOr.ChartAreas["ChartArea1"].AxisY.TitleAlignment = StringAlignment.Center;  // Chart Y axis Text Alignment 
chartESTOr.ChartAreas["ChartArea1"].AxisY.TextOrientation = TextOrientation.Horizontal; // Chart Y Axis Text Orientation
chartESTOr.ChartAreas["ChartArea1"].AxisY.TitleFont = new Font("Arial", 8, FontStyle.Bold); // Chart Y axis Title Font
chartESTOr.ChartAreas["ChartArea1"].AxisY.LabelStyle.Format = "{0:0,}K"; // Chart Y Axis lable format

As suggested by @TaW in comments this code works as: chart1.ChartAreas[0].AxisX.Title = "xxx";正如@TaW 在评论中所建议的,这段代码的工作原理是: chart1.ChartAreas[0].AxisX.Title = "xxx";

for dynamic addition the code by @Mytroy2050 is what works对于动态添加,@Mytroy2050 的代码是有效的

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

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