如何更改 Windows Forms Chart 控件数据?
How can I change Windows Forms Chart control data?
这是我现在使用的代码:
private void button1_Click(object sender, EventArgs e)
{
Random rdn = new Random();
for (int i = 116; i > 0; i--)
{
chart1.Series["Series1"].Points.AddXY
(rdn.Next(0, 10), rdn.Next(0, 10));
}
chart1.Series["Series1"].ChartType = SeriesChartType.FastLine;
chart1.Series["Series1"].Color = Color.Red;
chart1.Series["Series1"].ChartType = SeriesChartType.FastLine;
}
我得到的是:
但现在我想改变它
在左侧而不是数字 0 到 10 看到数字 1 到 116
而在底部而不是 -1 到 10 看到 1 到 30。
并根据 1 到 30 画一条从 116 到 1 的线。
例如在 1 中它是 116 然后在 2 中它会是 105 然后 100...它只是为了测试所以它可以是随机的或者只是从 116 到 30 的直线。
但是我想知道如何使用它如何画一条从116开始并根据1到30向下移动的线。
您的图表显示从 0/-1 到 10 的数字,因为这是您提供的值的范围。当数字更高时,图表将更改标签以包括它们。至于你的代码,你提供了一个随机数,因此你的结果。要获得您描述的内容,您的数据点必须包含指定的值:{1, 116} {2, 105} {100, 3} 等。我不明白这个系列背后的规则是什么,所以我无法继续那个。但是举个例子,我生成了以下内容:{116, 1} {112.13, 2} {108.26, 3} 等等 - 每一步 116 都会减少 116 的三分之一:
private void button1_Click(object sender, EventArgs e)
{
for (var i = 1.0; i < 30.0; ++i)
{
chart1.Series["Series1"].Points.AddXY(i, (116.0 / 30.0) * (31.0 - i));
}
chart1.Series["Series1"].ChartType = SeriesChartType.FastLine;
chart1.Series["Series1"].Color = Color.Red;
chart1.Series["Series1"].ChartType = SeriesChartType.FastLine;
}
结果如下图:
如果您需要比使用 CustomLabel
和 ChartAreas
更好地控制标签,但这可能会很棘手。 This post 有一些细节
我不太清楚你要做什么。但这就是我解释你的问题的方式:
On the left side instead the numbers 0 to 10 to see the numbers 1 to 116 And on the bottom instead -1 to 10 to see 1 to 30.
我的意思是你想改变每个轴的范围,比如X轴从1开始到30结束,Y轴从1开始到116结束
But I want to know how to use it how to draw a line starting 116 and moving down according to the 1 to 30.
我的意思是,除了实际的数据系列(即在您的示例中绘制为 XY 散点图的随机点)之外,您还希望在图表区域上覆盖一条线,其中线从图形坐标 (1, 116) 开始,连接到图形坐标 (30, 1)。
考虑到这一点,这里是您的原始代码,添加了一些内容以实现上述目标:
private void button1_Click(object sender, EventArgs e)
{
Random rdn = new Random();
for (int i = 116; i > 0; i--)
{
chart1.Series["Series1"].Points.AddXY
(rdn.Next(0, 10), rdn.Next(0, 10));
}
chart1.Series["Series1"].ChartType = SeriesChartType.FastLine;
chart1.Series["Series1"].Color = Color.Red;
ChartArea area = chart1.ChartAreas[0];
// Set the min and max for each axis
area.AxisX.Minimum = 1;
area.AxisX.Maximum = 30;
area.AxisY.Minimum = 1;
area.AxisY.Maximum = 116;
// Add a line on top of the chart
LineAnnotation line = new LineAnnotation();
chart1.Annotations.Add(line);
// Set the annotation positioning to be relative to the X and Y axes
line.AxisX = area.AxisX;
line.AxisY = area.AxisY;
// Set the actual annotation position and boundary. Disable
// IsSizeAlwaysRelative so that the annotation's size is
// determined by the absolute positioning of the boundary.
line.IsSizeAlwaysRelative = false;
line.X = 1; line.Y = 116;
line.Right = 30; line.Bottom = 1;
// Format the line so it shows up better
line.LineColor = Color.Blue;
line.LineWidth = 3;
}
这会生成如下所示的图表:
(我没有更改随机数生成的范围,您已将其设置为 select X 和 Y 坐标的随机整数点在 0 到 10 之间,因此数据系列本身是当然集中在图表的左下方)。
这是我现在使用的代码:
private void button1_Click(object sender, EventArgs e)
{
Random rdn = new Random();
for (int i = 116; i > 0; i--)
{
chart1.Series["Series1"].Points.AddXY
(rdn.Next(0, 10), rdn.Next(0, 10));
}
chart1.Series["Series1"].ChartType = SeriesChartType.FastLine;
chart1.Series["Series1"].Color = Color.Red;
chart1.Series["Series1"].ChartType = SeriesChartType.FastLine;
}
我得到的是:
但现在我想改变它
在左侧而不是数字 0 到 10 看到数字 1 到 116 而在底部而不是 -1 到 10 看到 1 到 30。
并根据 1 到 30 画一条从 116 到 1 的线。 例如在 1 中它是 116 然后在 2 中它会是 105 然后 100...它只是为了测试所以它可以是随机的或者只是从 116 到 30 的直线。
但是我想知道如何使用它如何画一条从116开始并根据1到30向下移动的线。
您的图表显示从 0/-1 到 10 的数字,因为这是您提供的值的范围。当数字更高时,图表将更改标签以包括它们。至于你的代码,你提供了一个随机数,因此你的结果。要获得您描述的内容,您的数据点必须包含指定的值:{1, 116} {2, 105} {100, 3} 等。我不明白这个系列背后的规则是什么,所以我无法继续那个。但是举个例子,我生成了以下内容:{116, 1} {112.13, 2} {108.26, 3} 等等 - 每一步 116 都会减少 116 的三分之一:
private void button1_Click(object sender, EventArgs e)
{
for (var i = 1.0; i < 30.0; ++i)
{
chart1.Series["Series1"].Points.AddXY(i, (116.0 / 30.0) * (31.0 - i));
}
chart1.Series["Series1"].ChartType = SeriesChartType.FastLine;
chart1.Series["Series1"].Color = Color.Red;
chart1.Series["Series1"].ChartType = SeriesChartType.FastLine;
}
结果如下图:
如果您需要比使用 CustomLabel
和 ChartAreas
更好地控制标签,但这可能会很棘手。 This post 有一些细节
我不太清楚你要做什么。但这就是我解释你的问题的方式:
On the left side instead the numbers 0 to 10 to see the numbers 1 to 116 And on the bottom instead -1 to 10 to see 1 to 30.
我的意思是你想改变每个轴的范围,比如X轴从1开始到30结束,Y轴从1开始到116结束
But I want to know how to use it how to draw a line starting 116 and moving down according to the 1 to 30.
我的意思是,除了实际的数据系列(即在您的示例中绘制为 XY 散点图的随机点)之外,您还希望在图表区域上覆盖一条线,其中线从图形坐标 (1, 116) 开始,连接到图形坐标 (30, 1)。
考虑到这一点,这里是您的原始代码,添加了一些内容以实现上述目标:
private void button1_Click(object sender, EventArgs e)
{
Random rdn = new Random();
for (int i = 116; i > 0; i--)
{
chart1.Series["Series1"].Points.AddXY
(rdn.Next(0, 10), rdn.Next(0, 10));
}
chart1.Series["Series1"].ChartType = SeriesChartType.FastLine;
chart1.Series["Series1"].Color = Color.Red;
ChartArea area = chart1.ChartAreas[0];
// Set the min and max for each axis
area.AxisX.Minimum = 1;
area.AxisX.Maximum = 30;
area.AxisY.Minimum = 1;
area.AxisY.Maximum = 116;
// Add a line on top of the chart
LineAnnotation line = new LineAnnotation();
chart1.Annotations.Add(line);
// Set the annotation positioning to be relative to the X and Y axes
line.AxisX = area.AxisX;
line.AxisY = area.AxisY;
// Set the actual annotation position and boundary. Disable
// IsSizeAlwaysRelative so that the annotation's size is
// determined by the absolute positioning of the boundary.
line.IsSizeAlwaysRelative = false;
line.X = 1; line.Y = 116;
line.Right = 30; line.Bottom = 1;
// Format the line so it shows up better
line.LineColor = Color.Blue;
line.LineWidth = 3;
}
这会生成如下所示的图表:
(我没有更改随机数生成的范围,您已将其设置为 select X 和 Y 坐标的随机整数点在 0 到 10 之间,因此数据系列本身是当然集中在图表的左下方)。