赞
踩
1、拖3个chart到新建的窗口中,分别命名为chart1,chart2,chart3,再放一个Button,如下图
2、对chart1 的ChartAreas和Series属性进行相应的设置
ChartAreas : 主要对游标和坐标轴进行设置,我们保持默认就可以。
其实大部。分的设置,都在Series属性进行,比如设置图标类型(样条曲线、散点、饼图...)、曲线数量、数据点标记样式、图例的名字等等
分别在chart1和chart2添加多一个Series,并在ChartType里设置图形样式,然后我们就可以分别往Series[0]和Series[1]添加数据了。
chart1.Series[0].Points.AddXY(i + 1, List_ia[i]); //添加数据
chart1.Series[1].Points.AddXY(i + 1, List_ic[i]); //添加数据
chart2相同的操作,只是设置图形样式都为Column分别添加数据后,效果如下
完整代码
-
-
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- using System.IO.Ports; //使用串口
- using System.Threading.Tasks; //线程
- using System.Runtime.InteropServices;
- using System.Windows.Forms.DataVisualization.Charting;
-
-
- namespace 多个chart动态曲线绘制
- {
- public partial class Form1 : Form
- {
- /*泛型集合用于图表显示*/
- private List<int> List_ia = new List<int>();
- private List<int> List_ic = new List<int>();
- private List<int> List_udc = new List<int>();
- private List<int> List_uAdc = new List<int>();
- private List<int> List_vel = new List<int>();
-
-
- Random random = new Random();
-
-
-
- public Form1()
- {
- InitializeComponent();
- }
-
- private void Form1_Load(object sender, EventArgs e)
- {
-
- }
-
- private void timer1_Tick(object sender, EventArgs e)
- {
- int num1 = random.Next(0, 99); //随机数
- int num2 = random.Next(0, 99);
- int num3 = random.Next(0, 99);
- int num5 = random.Next(0, 99);
- int num4 = random.Next(0, 99);
-
- List_ia.Add(num1); ListRemove(List_ia);
- List_ic.Add(num2); ListRemove(List_ic);
- List_udc.Add(num3); ListRemove(List_udc);
- List_uAdc.Add(num5);ListRemove(List_uAdc);
- List_vel.Add(num4); ListRemove(List_vel);
-
- DrawChart(List_ia, List_ic, List_udc, List_uAdc, List_vel);
- }
-
- private void DrawChart(List<int> List_ia, List<int> List_ic, List<int> List_udc, List<int> List_uAdc, List<int> List_vel)
- {
- chart1.Series[0].Points.Clear(); //清除所有点 ,如果不清除,将会出现所有数据叠加
- chart1.Series[1].Points.Clear(); //清除所有点
- chart2.Series[0].Points.Clear(); //清除所有点
- chart2.Series[1].Points.Clear(); //清除所有点
- chart3.Series[0].Points.Clear(); //清除所有点
-
- for (int i = 0; i < List_ia.Count; i++)
- {
- chart1.Series[0].Points.AddXY(i + 1, List_ia[i]); //添加点
- }
- for (int i = 0; i < List_ic.Count; i++)
- {
- chart1.Series[1].Points.AddXY(i + 1, List_ic[i]);
- }
- for (int i = 0; i < List_udc.Count; i++)
- {
- chart2.Series[0].Points.AddXY(i + 1, List_udc[i]);
- }
- for (int i = 0; i < List_uAdc.Count; i++)
- {
- chart2.Series[1].Points.AddXY(i + 1, List_uAdc[i]);
- }
- for (int i = 0; i < List_vel.Count; i++)
- {
- chart3.Series[0].Points.AddXY(i + 1, List_vel[i]);
- }
- }
- private void button1_Click(object sender, EventArgs e)
- {
- if (button1.Text == "开始")
- {
- timer1.Start();
- button1.Text = "暂停";
- }
- else
- {
- timer1.Stop();
- button1.Text = "开始";
- }
- }
-
- //当大于80时移除list头部
- private void ListRemove(List<int> list)
- {
- if (list.Count >= 80)
- {
- list.RemoveAt(0);
- }
- }
- //清除所有的列表
- private void ClearAllList()
- {
- List_ia.Clear();
- List_ic.Clear();
- List_udc.Clear();
- List_vel.Clear();
- List_uAdc.Clear();
- }
- }
- }
-
-
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。