如何给图表控件的x轴提供自定义标签? [英] How to give custom labels to x axis of chart control?

查看:410
本文介绍了如何给图表控件的x轴提供自定义标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个windows项目,其中需要绘制一个图表,因为我使用图表控件。

I am creating a windows project in which there is a requirement to plot a graph, for that i am using chart control.

图表控件的X轴具有以下代码的从0到100的标签。

The X-Axis of chart control has label from 0 to 100 with following code.

chart1.ChartAreas[0].AxisX.Minimum = 0;
chart1.ChartAreas[0].AxisX.Maximum = 100;

chart1.ChartAreas[0].AxisY.Minimum = 0;
chart1.ChartAreas[0].AxisY.Maximum = 200;

chart1.ChartAreas[0].AxisX.Interval = 25;
chart1.ChartAreas[0].AxisY.Interval = 25;

但我想自定义X轴的标签从100到0。

But i want to customize the label of X-Axis from 100 to 0.

我试过以下的事情。

chart1.ChartAreas[0].AxisX.Minimum = 0;
chart1.ChartAreas[0].AxisX.Maximum = 100;

chart1.ChartAreas[0].AxisY.Minimum = 0;
chart1.ChartAreas[0].AxisY.Maximum = 200;

chart1.ChartAreas[0].AxisX.Interval = 25;
chart1.ChartAreas[0].AxisY.Interval = 25;



string[] xval = { "100", "75", "50", "25", "0" };
for (int i = 0; i < xval.Length; i++)
{
  chart1.ChartAreas[0].AxisX.CustomLabels.Add(i + 0.5, i + 1.5, xval[i]);
  //chart1.ChartAreas[0].AxisX.CustomLabels.Add(xval[i]);
}

Series S1 = new Series();
S1.Points.AddXY(184,10);
S1.Points.AddXY(100,10);
S1.ChartType = SeriesChartType.Line;
S1.Color = Color.Red;
S1.Name = "Steam Inlet Saturation Temp";
chart1.Series.Add(S1);

Series S2 = new Series();
S2.Points.AddXY(100, 10);
S2.Points.AddXY(0, 10);
S2.ChartType = SeriesChartType.Line;
S2.Color = Color.Blue;
S2.Name = "Back Pressure Temp";
chart1.Series.Add(S2);

但这件事不适合我。

有没有人做过这个?

推荐答案

以下是我的问题的解决方案。

Following is the solution of my question.

string[] monthNames = { "100", "75" , "50" , "25" ,"0"};
int startOffset = -2;
int endOffset = 2;
foreach (string monthName in monthNames)
{
 CustomLabel monthLabel = new CustomLabel(startOffset, endOffset, monthName, 0, LabelMarkStyle.None);                        
 chart1.ChartAreas[0].AxisX.CustomLabels.Add(monthLabel);
 startOffset = startOffset + 25;
 endOffset = endOffset + 25;
}

这篇关于如何给图表控件的x轴提供自定义标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆