c# - 图表X轴移动

标签 c# charts visual-studio-2012

我在尝试创建 X 轴移动的 C# 图表时遇到了问题。

我想要的是添加一个新的 Y 值并让 x 轴向左移动以移除第一个数据点。

我可以在右侧添加点,

 DataPoint dp2 = new DataPoint(x, rnd.Next(100));
 chart1.Series[0].Points.Add(dp2);
 x++;

我可以删除左侧的第一个点

chart1.Series[0].Points.RemoveAt(0);

但如果我尝试同时执行这两种操作,它就不起作用,它会在图表的开头添加空格并从右侧消失,而不是停留在图表区域内?

DataPoint dp2 = new DataPoint(x, rnd.Next(100));
chart1.Series[0].Points.RemoveAt(0);
chart1.Series[0].Points.Add(dp2);
x++;

我的问题的完整演示:-(制作图表、3 个按钮和 3 个计时器,将我的代码粘贴到 form1.cs 的顶部,然后将按钮(鼠标单击)和计时器(滴答 1000 毫秒)事件连接到我的代码事件处理程序。)

using System;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;

namespace test_chart2
{
    public partial class Form1 : Form
    {
        double x = 16;
        Random rnd = new Random();
        public Form1()
        {
            InitializeComponent();


            DrawToGraph1();

        }

        private void DrawToGraph1()
        {
            chart1.Series[0].Points.Clear();
            for (int g = 5; g <= 15; g++)
            {
                DataPoint dp = new DataPoint(g, rnd.Next(100));
                chart1.Series[0].Points.Add(dp);
            }
        }
        private void timer1_Tick(object sender, EventArgs e)
        {
            DataPoint dp2 = new DataPoint(x, rnd.Next(100));
            chart1.Series[0].Points.Add(dp2);
            x++;
        }
        private void timer2_Tick(object sender, EventArgs e)
        {
            chart1.Series[0].Points.RemoveAt(0);
        }

        private void timer3_Tick(object sender, EventArgs e)
        {
            DataPoint dp2 = new DataPoint(x, rnd.Next(100));
            chart1.Series[0].Points.RemoveAt(0);
            chart1.Series[0].Points.Add(dp2);
            x++;
        }
        private void button2_MouseClick(object sender, MouseEventArgs e)
        {
            timer1.Enabled = false;
            timer2.Enabled = true;
            timer3.Enabled = false;
        }
        private void button3_MouseClick(object sender, MouseEventArgs e)
        {
            timer1.Enabled = false;
            timer2.Enabled = false;
            timer3.Enabled = true;
        }
        private void button1_MouseClick(object sender, MouseEventArgs e)
        {
            timer1.Enabled = true;
            timer2.Enabled = false;
            timer3.Enabled = false;
        }
    }
}

如果我使用它确实有效

series1.IsXValueIndexed = true;

但我不能使用它,因为我的项目有一个时间 X 轴,当我使用这个选项时,它不会产生一个很好的时间线(轴标签没有像通常那样四舍五入到最近的小时)并且它也会导致图表顶部显示日期的次 X 轴对齐问题。

最佳答案

请试试这个,然后告诉我会发生什么:

private void timer3_Tick(object sender, EventArgs e)
{
  DataPoint dp2 = new DataPoint(x, rnd.Next(100));
  Series mySeries = chart1.Series[0];
  chart1.Series.Clear();
  mySeries.Points.RemoveAt(0);
  mySeries.Points.Add(dp2);
  chart1.Series.Add(mySeries);
  x++;
}

也就是说,您可以尝试删除系列,修改它,然后将其添加回图表。 我无法自己运行它,因为我无法在 Visual Studio 中轻松重建您的项目。 希望对您有所帮助。

关于c# - 图表X轴移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13291517/

相关文章:

javascript - 使用 Charts.js 禁用动画

c# - 跟进 R# 警告 : Possible 'System.InvalidOperationException'

c# - .NET 的十进制算术实现的确切规范是什么?

c# - 如何找出 datagridview 中的最后一行并将其写入文本文件

C# 在运行时或使用 Mono.Cecil 向方法添加代码

excel - 如何使图表中的所有系列颜色相同?

python - 如何获得同一图表上两个曲面图的独立缩放比例

c# - 输出类型为类库的项目无法直接启动 ERROR

visual-studio-2012 - VS 2013数据库项目: Post-Deployment scripts to run based off of build configuration

asp.net - 安装 VS Express 时出现 Web 平台安装程序错误