none
c# 三个chart控件曲线一起动 RRS feed

  • 问题

  • this.BeginInvoke(new MethodInvoker(() => { DrawSpline(CommonHelper.MyList.Where(p => p != null).Select(p => p.Date).ToList(), CommonHelper.MyList.Where(p => p != null).Select(p => p.Value).ToList(), chart1); DrawSpline(CommonHelper.MyList2.Where(p => p != null).Select(p => p.Date).ToList(), CommonHelper.MyList2.Where(p => p != null).Select(p => p.Value).ToList(), chart2); DrawSpline(CommonHelper.MyList3.Where(p => p != null).Select(p => p.Date).ToList(), CommonHelper.MyList3.Where(p => p != null).Select(p => p.Value).ToList(), chart3);

    }));

    把数据绑定到三个chart控件绘制曲线,想让曲线一起动,怎么修改?


    please verify my account

    2017年4月18日 1:41

答案

  • Hi,

    如下代码供你参考:

      public partial class Form2 : Form
        {
            public Form2()
            {
                InitializeComponent();
                //设置窗体背景透明
                //this.BackColor = Color.White;
                //this.TransparencyKey = Color.White;
                //设置窗体透明度
                this.Opacity = 0.5;
            }
            Chart chart1;
            Series series1;
            Series series2;
            Series series3;
            private void Form2_Load(object sender, EventArgs e)
            {
                CreateChart();
                createSeries1();
                createSeries2();
                createSeries3();
                bindingSeries();
                System.Windows.Forms.Timer t = new System.Windows.Forms.Timer();
                t.Enabled = true;
                t.Interval = 100;
                t.Tick += T_Tick;
                t.Start();
            }
    
            private void T_Tick(object sender, EventArgs e)
            {
                chart1.Series["series1"].Points.Clear();
                chart1.Series["series2"].Points.Clear();
                chart1.Series["series3"].Points.Clear();
                bindingSeries();
            }
    
            static int range = 0;
            Random r = new Random(6);
            //模拟数据
            private DataTable getData()
            {
                DataTable dt = new DataTable();
                dt.Columns.Add("xValue", typeof(string));
                dt.Columns.Add("y1Value", typeof(float));
                dt.Columns.Add("y2Value", typeof(float));
                dt.Columns.Add("y3Value", typeof(float));
                dt.Clear();
    
                for (int i = 0; i < 50; i++)
                {
                    range = r.Next(1, 20);
                    dt.Rows.Add(new object[] { i, 5 + range, 30 + range, 70 + range });
                }
                return dt;
            }
    
            private void bindingSeries()
            {
                //binding
                chart1.DataSource = getData();
                series1.XValueMember = "xValue";
                series1.YValueMembers = "y1Value";
    
                series2.XValueMember = "xValue";
                series2.YValueMembers = "y2Value";
    
                series3.XValueMember = "xValue";
                series3.YValueMembers = "y3Value";
            }
    
            private void CreateChart()
            {
                chart1 = new Chart();
                chart1.Location = new Point(10, 10);
                chart1.Width = 800;
                chart1.Height = 400;
                chart1.Anchor = AnchorStyles.Bottom | AnchorStyles.Right | AnchorStyles.Top;
                this.Controls.Add(chart1);
    
                // chartArea
                ChartArea chartArea = new ChartArea();
                chartArea.Name = "FirstArea";
                chart1.ChartAreas.Add(chartArea);
                chartArea.BackColor = Color.White;
                chartArea.BackGradientStyle = GradientStyle.None;
                chartArea.BackHatchStyle = ChartHatchStyle.None;
                chartArea.BorderDashStyle = ChartDashStyle.NotSet;
                chartArea.BorderWidth = 1;
                chartArea.BorderColor = Color.Black;
                chart1.ChartAreas[0].Axes[0].MajorGrid.Enabled = false;//x axis
                chart1.ChartAreas[0].Axes[1].MajorGrid.Enabled = false;//y axis
    
                ////调整区域位置
                //chart1.ChartAreas["First Area"].Position = new ElementPosition(20, 9, 80, 85);
                //chart1.ChartAreas["First Area"].InnerPlotPosition = new ElementPosition(7, 0, 90, 90);
    
                //Cursor:only apply the top area
                chartArea.CursorX.IsUserEnabled = true;
                chartArea.CursorX.AxisType = AxisType.Primary;//act on primary x axis
                chartArea.CursorX.Interval = 1;
                chartArea.CursorX.LineWidth = 1;
                chartArea.CursorX.LineDashStyle = ChartDashStyle.Dash;
                chartArea.CursorX.IsUserSelectionEnabled = true;
                chartArea.CursorX.SelectionColor = Color.Yellow;
                chartArea.CursorX.AutoScroll = true;
    
                chartArea.CursorY.IsUserEnabled = true;
                chartArea.CursorY.AxisType = AxisType.Primary;//act on primary y axis
                chartArea.CursorY.Interval = 1;
                chartArea.CursorY.LineWidth = 1;
                chartArea.CursorY.LineDashStyle = ChartDashStyle.Dash;
                chartArea.CursorY.IsUserSelectionEnabled = true;
                chartArea.CursorY.SelectionColor = Color.Yellow;
                chartArea.CursorY.AutoScroll = true;
    
                // Axis
                chartArea.AxisY.Title = @"Value";
                chartArea.AxisY.Minimum = 0d;//Y axis Minimum value
                chartArea.AxisY.Maximum = 200;//Y axis Maximum value
                chartArea.AxisY.Interval = 20;
                chartArea.AxisY.LabelAutoFitMinFontSize = 5;
                chartArea.AxisY.LineWidth = 2;
                chartArea.AxisY.LineColor = Color.Black;
                chartArea.AxisY.Enabled = AxisEnabled.True;
    
                chartArea.AxisX.Title = @"X axis";
                chartArea.AxisX.Minimum = 0d; //X axis Minimum value
                chartArea.AxisX.Maximum = 50d;
                chartArea.AxisX.IsLabelAutoFit = true;
                chartArea.AxisX.LabelAutoFitMinFontSize = 5;
                chartArea.AxisX.LabelStyle.Angle = -30;
                //chartArea.AxisX.LabelStyle.IntervalOffset = 3;
                // chartArea.AxisX.LineDashStyle = ChartDashStyle.DashDotDot;
                //chartArea.AxisX.LabelStyle.IsStaggered = true;
                //chartArea.AxisX.LabelStyle.TruncatedLabels = true;
    
                chartArea.AxisX.LabelStyle.IsEndLabelVisible = true;//show the last label
                chartArea.AxisX.Interval = 10;
                chartArea.AxisX.IntervalAutoMode = IntervalAutoMode.FixedCount;
                chartArea.AxisX.IntervalType = DateTimeIntervalType.NotSet;
                chartArea.AxisX.TextOrientation = TextOrientation.Auto;
                chartArea.AxisX.LineWidth = 2;
                chartArea.AxisX.LineColor = Color.Black;
                chartArea.AxisX.Enabled = AxisEnabled.True;
                chartArea.AxisX.ScaleView.MinSizeType = DateTimeIntervalType.Months;
                chartArea.AxisX.Crossing = 0;
    
    
            }
    
            private void createSeries1()
            {
                //Series1
                series1 = new Series();
                series1.ChartArea = "FirstArea";
                chart1.Series.Add(series1);
    
                //Series1 style
                series1.Name = @"series1";
                series1.ChartType = SeriesChartType.Spline;  // type
                series1.BorderWidth = 2;
                series1.Color = Color.Green;
                series1.XValueType = ChartValueType.Int32;//x axis type
                series1.YValueType = ChartValueType.Int32;//y axis type
                series1.YValuesPerPoint = 6;
    
                //Marker
                series1.MarkerStyle = MarkerStyle.Circle;
                series1.MarkerSize = 4;
                series1.MarkerStep = 1;
                series1.MarkerColor = Color.Red;
                series1.ToolTip = @"ToolTip";
    
                //Label
                series1.IsValueShownAsLabel = true;
                series1.SmartLabelStyle.Enabled = false;
                series1.SmartLabelStyle.AllowOutsidePlotArea = LabelOutsidePlotAreaStyle.Yes;
                series1.LabelForeColor = Color.Gray;
                series1.LabelToolTip = @"LabelToolTip";
    
                //Legend
                series1.IsVisibleInLegend = true;
                series1.LegendText = "series1";
                chart1.Legends.Add(series1.Legend);
            }
            private void createSeries2()
            {
                //Series1
                series2 = new Series();
                series2.ChartArea = "FirstArea";
                chart1.Series.Add(series2);
    
                //Series1 style
                series2.Name = @"series2";
                series2.ChartType = SeriesChartType.Spline;  // type
                series2.BorderWidth = 2;
                series2.Color = Color.Green;
                series2.XValueType = ChartValueType.Int32;//x axis type
                series2.YValueType = ChartValueType.Int32;//y axis type
                series2.YValuesPerPoint = 6;
    
                //Marker
                series2.MarkerStyle = MarkerStyle.Circle;
                series2.MarkerSize = 4;
                series2.MarkerStep = 1;
                series2.MarkerColor = Color.Red;
                series2.ToolTip = @"ToolTip";
    
                //Label
                series2.IsValueShownAsLabel = true;
                series2.SmartLabelStyle.Enabled = false;
                series2.SmartLabelStyle.AllowOutsidePlotArea = LabelOutsidePlotAreaStyle.Yes;
                series2.LabelForeColor = Color.Gray;
                series2.LabelToolTip = @"LabelToolTip";
    
                //Legend
                series2.IsVisibleInLegend = true;
            }
            private void createSeries3()
            {
                //Series1
                series3 = new Series();
                series3.ChartArea = "FirstArea";
                chart1.Series.Add(series3);
    
                //Series1 style
                series3.Name = @"series3";
                series3.ChartType = SeriesChartType.Spline;  // type
                series3.BorderWidth = 2;
                series3.Color = Color.Green;
                series3.XValueType = ChartValueType.Int32;//x axis type
                series3.YValueType = ChartValueType.Int32;//y axis type
                series3.YValuesPerPoint = 6;
    
                //Marker
                series3.MarkerStyle = MarkerStyle.Circle;
                series3.MarkerSize = 4;
                series3.MarkerStep = 1;
                series3.MarkerColor = Color.Red;
                series3.ToolTip = @"ToolTip";
    
                //Label
                series3.IsValueShownAsLabel = true;
                series3.SmartLabelStyle.Enabled = false;
                series3.SmartLabelStyle.AllowOutsidePlotArea = LabelOutsidePlotAreaStyle.Yes;
                series3.LabelForeColor = Color.Gray;
                series3.LabelToolTip = @"LabelToolTip";
    
                series3.IsVisibleInLegend = true;
            }
        }

    Hope it helps.

    Best Regards,

    Bob


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • 已标记为答案 lctk 2017年5月4日 0:14
    2017年5月3日 1:28

全部回复

  • Hi lctk,

    你创建的工程是什么类型的? winform的吗? 还是其他类型?

     对于UWP工程,再绑定数据和界面的时候,类型的绑定类型one way 和two way。

    如果是two way,后台数据变动,就会影响到界面。

    Best Regards,

    Hart


    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    2017年4月18日 7:52
    版主
  • winfrom

    please verify my account

    2017年4月18日 10:03
  • Hi,

    不知你指的曲线一起动是什么意思,你的数据源是什么? 我猜测你指的是Chart控件的实时加载。

    那么, 你需要使用定时器来实时的刷新Chart控件. 我做了一个Demo供你参考,希望有帮助。

    public partial class Form2 : Form
        {
            public Form2()
            {
                InitializeComponent();
            }
            Chart chart1;
            Series series1;
            private void Form2_Load(object sender, EventArgs e)
            {
                CreateChart();
                createSeries();
                bindingSeries();
                System.Windows.Forms.Timer t = new System.Windows.Forms.Timer();
                t.Enabled = true;
                t.Interval = 100;
                t.Tick += T_Tick;
                t.Start();
            }
    
            private void T_Tick(object sender, EventArgs e)
            {
                chart1.Series["series1"].Points.Clear();
                bindingSeries();
            }
    
            static int range = 0;
            Random r = new Random(6);
            //模拟数据
            private DataTable getData()
            {
                DataTable dt = new DataTable();
                dt.Columns.Add("xValue", typeof(string));
                dt.Columns.Add("yValue", typeof(float));
                dt.Clear();
               
                for (int i = 0; i < 50; i++)
                {
                    range = r.Next(1,60);
                    dt.Rows.Add(new object[] { i, 5 + range });
                }
                return dt;
            }
    
            private void bindingSeries()
            {
                //binding
                chart1.DataSource = getData();
                series1.XValueMember = "xValue";
                series1.YValueMembers = "yValue";
            }
    
            private void CreateChart()
            {
                chart1 = new Chart();
                chart1.Location = new Point(10, 10);
                chart1.Width = 800;
                chart1.Height = 400;
                chart1.Anchor = AnchorStyles.Bottom | AnchorStyles.Right | AnchorStyles.Top;
                this.Controls.Add(chart1);
    
                // chartArea
                ChartArea chartArea = new ChartArea();
                chartArea.Name = "FirstArea";
                chart1.ChartAreas.Add(chartArea);
                chartArea.BackColor = Color.White;
                chartArea.BackGradientStyle = GradientStyle.None;
                chartArea.BackHatchStyle = ChartHatchStyle.None;
                chartArea.BorderDashStyle = ChartDashStyle.NotSet;
                chartArea.BorderWidth = 1;
                chartArea.BorderColor = Color.Black;
                chart1.ChartAreas[0].Axes[0].MajorGrid.Enabled = false;//x axis
                chart1.ChartAreas[0].Axes[1].MajorGrid.Enabled = false;//y axis
    
                ////调整区域位置
                //chart1.ChartAreas["FirstArea"].Position = new ElementPosition(20, 9, 80, 85);
                //chart1.ChartAreas["FirstArea"].InnerPlotPosition = new ElementPosition(7, 0, 90, 90);
    
                //Cursor:only apply the top area
                chartArea.CursorX.IsUserEnabled = true;
                chartArea.CursorX.AxisType = AxisType.Primary;//act on primary x axis
                chartArea.CursorX.Interval = 1;
                chartArea.CursorX.LineWidth = 1;
                chartArea.CursorX.LineDashStyle = ChartDashStyle.Dash;
                chartArea.CursorX.IsUserSelectionEnabled = true;
                chartArea.CursorX.SelectionColor = Color.Yellow;
                chartArea.CursorX.AutoScroll = true;
    
                chartArea.CursorY.IsUserEnabled = true;
                chartArea.CursorY.AxisType = AxisType.Primary;//act on primary y axis
                chartArea.CursorY.Interval = 1;
                chartArea.CursorY.LineWidth = 1;
                chartArea.CursorY.LineDashStyle = ChartDashStyle.Dash;
                chartArea.CursorY.IsUserSelectionEnabled = true;
                chartArea.CursorY.SelectionColor = Color.Yellow;
                chartArea.CursorY.AutoScroll = true;
    
                // Axis
                chartArea.AxisY.Title = @"Value";
                chartArea.AxisY.Minimum = 0d;//Y axis Minimum value
                chartArea.AxisY.Maximum = 100d;//Y axis Maximum value
                chartArea.AxisY.Interval = 10;
                chartArea.AxisY.LabelAutoFitMinFontSize = 5;
                chartArea.AxisY.LineWidth = 2;
                chartArea.AxisY.LineColor = Color.Black;
                chartArea.AxisY.Enabled = AxisEnabled.True;
    
                chartArea.AxisX.Title = @"X axis";
                chartArea.AxisX.Minimum = 0d; //X axis Minimum value
                chartArea.AxisX.Maximum = 50d;
                chartArea.AxisX.IsLabelAutoFit = true;
                chartArea.AxisX.LabelAutoFitMinFontSize = 5;
                chartArea.AxisX.LabelStyle.Angle = -30;
                //chartArea.AxisX.LabelStyle.IntervalOffset = 3;
                // chartArea.AxisX.LineDashStyle = ChartDashStyle.DashDotDot;
                //chartArea.AxisX.LabelStyle.IsStaggered = true;
                //chartArea.AxisX.LabelStyle.TruncatedLabels = true;
    
                chartArea.AxisX.LabelStyle.IsEndLabelVisible = true;//show the last label
                chartArea.AxisX.Interval = 10;
                chartArea.AxisX.IntervalAutoMode = IntervalAutoMode.FixedCount;
                chartArea.AxisX.IntervalType = DateTimeIntervalType.NotSet;
                chartArea.AxisX.TextOrientation = TextOrientation.Auto;
                chartArea.AxisX.LineWidth = 2;
                chartArea.AxisX.LineColor = Color.Black;
                chartArea.AxisX.Enabled = AxisEnabled.True;
                chartArea.AxisX.ScaleView.MinSizeType = DateTimeIntervalType.Months;
                chartArea.AxisX.Crossing = 0;
            }
           
            private void createSeries()
            {
                //Series1
                series1 = new Series();
                series1.ChartArea = "FirstArea";
                chart1.Series.Add(series1);
    
                //Series1 style
                series1.Name = @"series1";
                series1.ChartType = SeriesChartType.Spline;  // type
                series1.BorderWidth = 2;
                series1.Color = Color.Green;
                series1.XValueType = ChartValueType.Int32;//x axis type
                series1.YValueType = ChartValueType.Int32;//y axis type
                series1.YValuesPerPoint = 6;
    
                //Marker
                series1.MarkerStyle = MarkerStyle.Circle;
                series1.MarkerSize = 4;
                series1.MarkerStep = 1;
                series1.MarkerColor = Color.Red;
                series1.ToolTip = @"ToolTip";
    
                //Label
                series1.IsValueShownAsLabel = true;
                series1.SmartLabelStyle.Enabled = false;
                series1.SmartLabelStyle.AllowOutsidePlotArea = LabelOutsidePlotAreaStyle.Yes;
                series1.LabelForeColor = Color.Gray;
                series1.LabelToolTip = @"LabelToolTip";
    
                //Legend
                series1.IsVisibleInLegend = true;
                series1.LegendText = "series1";
                chart1.Legends.Add(series1.Legend);         
            }
        }

    另外,请参考下列文章:

    C# chart控件实时动态显示数据

    http://blog.csdn.net/lj22377/article/details/38373609

    MS chart 总结

    http://blog.csdn.net/g1036583997/article/details/50443673

    c# winform 数据实时刷新问题

    http://bbs.csdn.net/topics/390912183

    注意:此回复包含对第三方万维网站点的引用。 Microsoft提供此信息为您提供方便。Microsoft不控制这些网站,并且未测试在这些网站上找到的任何软件或信息;因此,Microsoft不能对任何软件或信息的质量,安全性或适用性做任何声明。使用互联网上发现的任何软件都存在固有的危险,Microsoft警告您,在从互联网检索任何软件之前,请确保您完全理解风险。

    Best Regards,

    Bob


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.



    • 已编辑 Bob Ding 2017年4月19日 3:36
    2017年4月19日 2:10
  • 我的意思是让这三个chart的曲线一起动,同步走

    我的数据是从服务器接收过来的,一包大概10个

    如果满屏幕了,

    假如x轴是1-100,

    那么把1-10替换掉,然后10-20,。。。。。

    就是这个效果


    please verify my account

    2017年4月19日 5:42
  • Hi Lctk,

    每次从服务器取得数据保存到集合中,对数据做相应的处理,然后重新绑定数据和刷新chart即可。我的demo可以做到你的需求,只需在加两条series,至于怎么处理数据你可能要自己写逻辑,因为我实在不知道你的需求是什么。

    Best Regards,

    Bob


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    2017年4月19日 5:59
  • 你好,三个series,曲线不是显示在一个chart上了吗?不想让曲线显示在一起,要分开显示,



    please verify my account

    2017年4月19日 6:03
  • 我是用的tcp客户端接收服务器端的数据,然后收到的数据包里面包含三个data,一个data绑定到一个chart,

    一个数据包来的时候有一个时间,这一个数据包代表0.5秒的数据,请问用时间做横坐标,可以吗?每条数据相差10/1000秒,

    我没用时间做chart横坐标,我写了一个int i,把i的值累加作为横坐标,这样做可以吗?

    当我收到数据并解析之后,我写了一个委托,调用了一个方法,把解析的数据传到chart控件的窗体的方法里,

    在方法里面处理集合,

    然后处理集合,绑定到chart上



    please verify my account


    • 已编辑 lctk 2017年4月19日 6:12
    2017年4月19日 6:10
  • Hi,

    >>我是用的tcp客户端接收服务器端的数据,然后收到的数据包里面包含三个data,一个data绑定到一个chart,

    你可以把接收到数据放到一个集合中,在绑定chart。

    >>一个数据包来的时候有一个时间,这一个数据包代表0.5秒的数据,请问用时间做横坐标,可以吗?每条数据相差10/1000秒,

    时间做横坐标是没有问题的,建议不要刷新的太快,想要显示的更平滑一点,还是刷新的慢点比较好。

    >>我没用时间做chart横坐标,我写了一个int i,把i的值累加作为横坐标,这样做可以吗?

    可以的。

    >> 你好,三个series,曲线不是显示在一个chart上了吗?不想让曲线显示在一起,要分开显示,

    使用三个series和使用三个chart原理都是一样的,数据绑定很简单,关键在于你打算怎么处理数据。据我所知,一个chart 可以有多个chartArea,一个chart可以有多个serie.

    由于你的问题太多,我建议你好好学习和研究一下Chart控件,自己做几个小例子。

    Best Regard,

    Bob


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    • 已编辑 Bob Ding 2017年4月19日 6:31
    • 已建议为答案 Angie Xu 2017年4月25日 1:05
    2017年4月19日 6:27
  • Hi,

    >>我是用的tcp客户端接收服务器端的数据,然后收到的数据包里面包含三个data,一个data绑定到一个chart,

    你可以把接收到数据放到一个集合中,在绑定chart。

    >>一个数据包来的时候有一个时间,这一个数据包代表0.5秒的数据,请问用时间做横坐标,可以吗?每条数据相差10/1000秒,

    时间做横坐标是没有问题的,建议不要刷新的太快,想要显示的更平滑一点,还是刷新的慢点比较好。

    >>我没用时间做chart横坐标,我写了一个int i,把i的值累加作为横坐标,这样做可以吗?

    可以的。

    >> 你好,三个series,曲线不是显示在一个chart上了吗?不想让曲线显示在一起,要分开显示,

    使用三个series和使用三个chart原理都是一样的,数据绑定很简单,关键在于你打算怎么处理数据。据我所知,一个chart 可以有多个chartArea,一个chart可以有多个serie.

    由于你的问题太多,我建议你好好学习和研究一下Chart控件,自己做几个小例子。

    Best Regard,

    Bob


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    请问一个chart里面分开显示三条曲线,代码是怎么写的?给我参考参考啊

    please verify my account

    2017年5月3日 0:07
  • Hi,

    如下代码供你参考:

      public partial class Form2 : Form
        {
            public Form2()
            {
                InitializeComponent();
                //设置窗体背景透明
                //this.BackColor = Color.White;
                //this.TransparencyKey = Color.White;
                //设置窗体透明度
                this.Opacity = 0.5;
            }
            Chart chart1;
            Series series1;
            Series series2;
            Series series3;
            private void Form2_Load(object sender, EventArgs e)
            {
                CreateChart();
                createSeries1();
                createSeries2();
                createSeries3();
                bindingSeries();
                System.Windows.Forms.Timer t = new System.Windows.Forms.Timer();
                t.Enabled = true;
                t.Interval = 100;
                t.Tick += T_Tick;
                t.Start();
            }
    
            private void T_Tick(object sender, EventArgs e)
            {
                chart1.Series["series1"].Points.Clear();
                chart1.Series["series2"].Points.Clear();
                chart1.Series["series3"].Points.Clear();
                bindingSeries();
            }
    
            static int range = 0;
            Random r = new Random(6);
            //模拟数据
            private DataTable getData()
            {
                DataTable dt = new DataTable();
                dt.Columns.Add("xValue", typeof(string));
                dt.Columns.Add("y1Value", typeof(float));
                dt.Columns.Add("y2Value", typeof(float));
                dt.Columns.Add("y3Value", typeof(float));
                dt.Clear();
    
                for (int i = 0; i < 50; i++)
                {
                    range = r.Next(1, 20);
                    dt.Rows.Add(new object[] { i, 5 + range, 30 + range, 70 + range });
                }
                return dt;
            }
    
            private void bindingSeries()
            {
                //binding
                chart1.DataSource = getData();
                series1.XValueMember = "xValue";
                series1.YValueMembers = "y1Value";
    
                series2.XValueMember = "xValue";
                series2.YValueMembers = "y2Value";
    
                series3.XValueMember = "xValue";
                series3.YValueMembers = "y3Value";
            }
    
            private void CreateChart()
            {
                chart1 = new Chart();
                chart1.Location = new Point(10, 10);
                chart1.Width = 800;
                chart1.Height = 400;
                chart1.Anchor = AnchorStyles.Bottom | AnchorStyles.Right | AnchorStyles.Top;
                this.Controls.Add(chart1);
    
                // chartArea
                ChartArea chartArea = new ChartArea();
                chartArea.Name = "FirstArea";
                chart1.ChartAreas.Add(chartArea);
                chartArea.BackColor = Color.White;
                chartArea.BackGradientStyle = GradientStyle.None;
                chartArea.BackHatchStyle = ChartHatchStyle.None;
                chartArea.BorderDashStyle = ChartDashStyle.NotSet;
                chartArea.BorderWidth = 1;
                chartArea.BorderColor = Color.Black;
                chart1.ChartAreas[0].Axes[0].MajorGrid.Enabled = false;//x axis
                chart1.ChartAreas[0].Axes[1].MajorGrid.Enabled = false;//y axis
    
                ////调整区域位置
                //chart1.ChartAreas["First Area"].Position = new ElementPosition(20, 9, 80, 85);
                //chart1.ChartAreas["First Area"].InnerPlotPosition = new ElementPosition(7, 0, 90, 90);
    
                //Cursor:only apply the top area
                chartArea.CursorX.IsUserEnabled = true;
                chartArea.CursorX.AxisType = AxisType.Primary;//act on primary x axis
                chartArea.CursorX.Interval = 1;
                chartArea.CursorX.LineWidth = 1;
                chartArea.CursorX.LineDashStyle = ChartDashStyle.Dash;
                chartArea.CursorX.IsUserSelectionEnabled = true;
                chartArea.CursorX.SelectionColor = Color.Yellow;
                chartArea.CursorX.AutoScroll = true;
    
                chartArea.CursorY.IsUserEnabled = true;
                chartArea.CursorY.AxisType = AxisType.Primary;//act on primary y axis
                chartArea.CursorY.Interval = 1;
                chartArea.CursorY.LineWidth = 1;
                chartArea.CursorY.LineDashStyle = ChartDashStyle.Dash;
                chartArea.CursorY.IsUserSelectionEnabled = true;
                chartArea.CursorY.SelectionColor = Color.Yellow;
                chartArea.CursorY.AutoScroll = true;
    
                // Axis
                chartArea.AxisY.Title = @"Value";
                chartArea.AxisY.Minimum = 0d;//Y axis Minimum value
                chartArea.AxisY.Maximum = 200;//Y axis Maximum value
                chartArea.AxisY.Interval = 20;
                chartArea.AxisY.LabelAutoFitMinFontSize = 5;
                chartArea.AxisY.LineWidth = 2;
                chartArea.AxisY.LineColor = Color.Black;
                chartArea.AxisY.Enabled = AxisEnabled.True;
    
                chartArea.AxisX.Title = @"X axis";
                chartArea.AxisX.Minimum = 0d; //X axis Minimum value
                chartArea.AxisX.Maximum = 50d;
                chartArea.AxisX.IsLabelAutoFit = true;
                chartArea.AxisX.LabelAutoFitMinFontSize = 5;
                chartArea.AxisX.LabelStyle.Angle = -30;
                //chartArea.AxisX.LabelStyle.IntervalOffset = 3;
                // chartArea.AxisX.LineDashStyle = ChartDashStyle.DashDotDot;
                //chartArea.AxisX.LabelStyle.IsStaggered = true;
                //chartArea.AxisX.LabelStyle.TruncatedLabels = true;
    
                chartArea.AxisX.LabelStyle.IsEndLabelVisible = true;//show the last label
                chartArea.AxisX.Interval = 10;
                chartArea.AxisX.IntervalAutoMode = IntervalAutoMode.FixedCount;
                chartArea.AxisX.IntervalType = DateTimeIntervalType.NotSet;
                chartArea.AxisX.TextOrientation = TextOrientation.Auto;
                chartArea.AxisX.LineWidth = 2;
                chartArea.AxisX.LineColor = Color.Black;
                chartArea.AxisX.Enabled = AxisEnabled.True;
                chartArea.AxisX.ScaleView.MinSizeType = DateTimeIntervalType.Months;
                chartArea.AxisX.Crossing = 0;
    
    
            }
    
            private void createSeries1()
            {
                //Series1
                series1 = new Series();
                series1.ChartArea = "FirstArea";
                chart1.Series.Add(series1);
    
                //Series1 style
                series1.Name = @"series1";
                series1.ChartType = SeriesChartType.Spline;  // type
                series1.BorderWidth = 2;
                series1.Color = Color.Green;
                series1.XValueType = ChartValueType.Int32;//x axis type
                series1.YValueType = ChartValueType.Int32;//y axis type
                series1.YValuesPerPoint = 6;
    
                //Marker
                series1.MarkerStyle = MarkerStyle.Circle;
                series1.MarkerSize = 4;
                series1.MarkerStep = 1;
                series1.MarkerColor = Color.Red;
                series1.ToolTip = @"ToolTip";
    
                //Label
                series1.IsValueShownAsLabel = true;
                series1.SmartLabelStyle.Enabled = false;
                series1.SmartLabelStyle.AllowOutsidePlotArea = LabelOutsidePlotAreaStyle.Yes;
                series1.LabelForeColor = Color.Gray;
                series1.LabelToolTip = @"LabelToolTip";
    
                //Legend
                series1.IsVisibleInLegend = true;
                series1.LegendText = "series1";
                chart1.Legends.Add(series1.Legend);
            }
            private void createSeries2()
            {
                //Series1
                series2 = new Series();
                series2.ChartArea = "FirstArea";
                chart1.Series.Add(series2);
    
                //Series1 style
                series2.Name = @"series2";
                series2.ChartType = SeriesChartType.Spline;  // type
                series2.BorderWidth = 2;
                series2.Color = Color.Green;
                series2.XValueType = ChartValueType.Int32;//x axis type
                series2.YValueType = ChartValueType.Int32;//y axis type
                series2.YValuesPerPoint = 6;
    
                //Marker
                series2.MarkerStyle = MarkerStyle.Circle;
                series2.MarkerSize = 4;
                series2.MarkerStep = 1;
                series2.MarkerColor = Color.Red;
                series2.ToolTip = @"ToolTip";
    
                //Label
                series2.IsValueShownAsLabel = true;
                series2.SmartLabelStyle.Enabled = false;
                series2.SmartLabelStyle.AllowOutsidePlotArea = LabelOutsidePlotAreaStyle.Yes;
                series2.LabelForeColor = Color.Gray;
                series2.LabelToolTip = @"LabelToolTip";
    
                //Legend
                series2.IsVisibleInLegend = true;
            }
            private void createSeries3()
            {
                //Series1
                series3 = new Series();
                series3.ChartArea = "FirstArea";
                chart1.Series.Add(series3);
    
                //Series1 style
                series3.Name = @"series3";
                series3.ChartType = SeriesChartType.Spline;  // type
                series3.BorderWidth = 2;
                series3.Color = Color.Green;
                series3.XValueType = ChartValueType.Int32;//x axis type
                series3.YValueType = ChartValueType.Int32;//y axis type
                series3.YValuesPerPoint = 6;
    
                //Marker
                series3.MarkerStyle = MarkerStyle.Circle;
                series3.MarkerSize = 4;
                series3.MarkerStep = 1;
                series3.MarkerColor = Color.Red;
                series3.ToolTip = @"ToolTip";
    
                //Label
                series3.IsValueShownAsLabel = true;
                series3.SmartLabelStyle.Enabled = false;
                series3.SmartLabelStyle.AllowOutsidePlotArea = LabelOutsidePlotAreaStyle.Yes;
                series3.LabelForeColor = Color.Gray;
                series3.LabelToolTip = @"LabelToolTip";
    
                series3.IsVisibleInLegend = true;
            }
        }

    Hope it helps.

    Best Regards,

    Bob


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • 已标记为答案 lctk 2017年5月4日 0:14
    2017年5月3日 1:28