C#图表,x轴移位 [英] C# chart, x axis shifting

查看:138
本文介绍了C#图表,x轴移位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法尝试创建一个x轴移动的ac#图表。



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



我可以在右侧加点积分,



Im having trouble trying to create a c# chart with an x axis that shifts.

What i want is to add a new Y value and have the x axis shift to the left removing the first data point.

I can add points to the right hand side fine,

 DataPoint dp2 = new DataPoint(x, rnd.Next(100));

  chart1.Series [0 ] .Points.Add(dp2);

 chart1.Series[0].Points.Add(dp2);

  x ++;

 x++;




i可以删除第一个点左手边罚款




i can remove the first point on the left hand side fine

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




但如果我尝试两者都不起作用,它会在图表的开头添加空格并从右侧移出而不是留在图表区域?
$


       


but if i try to do both it does not work, it adds blank spaces at the beginning of the graph and goes off out the right hand side rather than staying inside the graph area ?

       

DataPoint dp2 = new DataPoint(x, rnd.Next(100));

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

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

chart1.Series [0] .Points.Add(dp2);

chart1.Series[0].Points.Add(dp2);

x ++;





我问题的完整演示: - (制作图表和3个按钮)





full demo of my problem:- (Make a chart and 3 buttons)

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()
        {
            Random rnd = new Random();
            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轴出现对齐问题,
显示图表顶部的日期。



Iv花费了多少年龄试图弄清楚这一点: - /所以任何帮助都非常感谢。



干杯。



It does work if i use series1.IsXValueIndexed = true;

but i cant use this as my project has a time X axis and when i use this option it does not produce a nice time line (the axis labels are not rounded to the nearest hour like they are normally) and it also causes alignment problems with my secondary X axis that shows the date at the top of the graph.

Iv spent ages trying to figure this one out :-/ so any help is much appreciated.

Cheers.

推荐答案

问候杰克。

当系列中的点数发生变化时,会触发自动重新计算的界限。重绘期间的图表。你的问题是,当重绘开始时,点的数量与你开始时相同(删除了
然后添加了一个),所以你需要手动强制图表重新计算。

When the number of points in the series changes, this triggers an automatic recalculation of the bounds of the chart during repaint. Your problem is that by the time the repaint begins, the number of points is the same that you started with (having removed one then added one), so you need to manually force the chart to do a recalculation.

        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);
            chart1.ResetAutoValues(); // Add this line.
            x++;
        }


这篇关于C#图表,x轴移位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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