如何为图表控件的 x 轴提供自定义标签?
How to give custom labels to x axis of chart control?
我正在创建一个 windows 项目,其中需要绘制图表,为此我正在使用图表控件。
图表控件的 X 轴具有从 0 到 100 的标签,代码如下。
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。
我尝试了以下内容。
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);
但这东西对我不起作用。
有人做过吗?非常感谢您的及时回复。
下面是我的问题的解答。
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;
}
我正在创建一个 windows 项目,其中需要绘制图表,为此我正在使用图表控件。
图表控件的 X 轴具有从 0 到 100 的标签,代码如下。
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。
我尝试了以下内容。
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);
但这东西对我不起作用。
有人做过吗?非常感谢您的及时回复。
下面是我的问题的解答。
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;
}