首页 > 开发 > 综合 > 正文

Mschart应用之曲线图表spline

2024-07-21 02:48:47
字体:
来源:转载
供稿:网友
Mschart应用之曲线图表spline

本文主要是Mschart应用之曲线图表spline,实现6个模拟数据的图表,其中数据源X轴为当前系统时间,Y轴是由随机函数产生的不同范围的随机数。

首先是自定义一个数据表,然后产生的数据添加到该数据表中,最后通过chart1.Series[0].Points.AddXY()函数把数据绘制到图表上。

在开始运行界面主要是通过Timer函数不断调用绘制图表,具体图表绘制函数在StartRun类之中,下面是开始运行界面。

开始运行界面后台代码:

using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;using ComponentFactory.Krypton.Toolkit;//项目需要命名空间//using System.Data.SqlClient;using KryptonFormAirComPRessor.DAL;using KryptonFormAirCompressor.BLL;using System.Windows.Forms.DataVisualization.Charting;namespace KryptonFormAirCompressor.UI{    public partial class 开始运行 : ComponentFactory.Krypton.Toolkit.KryptonForm    {        //定义数据采集类        StartRun run = new StartRun();        DataTable dt = new DataTable();        //定义一种行样式        private DataGridViewCellStyle m_RowStyleNormal;        //定义测试数据逻辑类        testdData td = new testdData();        public 开始运行()        {            InitializeComponent();        }        private void 开始运行_Resize(object sender, EventArgs e)        {            Split1.Height = this.tabPage2.Height / 3 - 1;            Split2.Height = this.tabPage2.Height / 3 - 1;            Split3.Height = this.tabPage2.Height - Split1.Height - Split2.Height;            tabRun.Refresh();            // run.chartInit(chart1, tabPage2);        }        private void 开始运行_Load(object sender, EventArgs e)        {            refeshData();            run.chartInit(chart1, tabPage2);            run.chartInit(chart2, tabPage2);            run.chartInit(chart3, tabPage2);            run.chartInit(chart4, tabPage2);            run.chartInit(chart5, tabPage2);            run.chartInit(chart6, tabPage2);            //添加数据表两列            dt.Columns.Add("XTime", System.Type.GetType("System.String"));            dt.Columns.Add("YTemp", System.Type.GetType("System.String"));        }        //刷新数据        private void refeshData()        {            kptGridRun.DataSource = td.GetDataTable();            kptGridRun.AllowUserToAddRows = false;        }        /// <summary>        /// 设置单元格的格式        /// </summary>        /// <param name="sender"></param>        /// <param name="e"></param>        private void kptGridRun_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)        {            //可根据需要设置更多样式属性,如字体、对齐、前景色、背景色等            this.m_RowStyleNormal = new DataGridViewCellStyle();            //this.m_RowStyleNormal.BackColor = Color.LightBlue;            kptGridRun.Columns[0].DefaultCellStyle.BackColor = Color.LightGray;            kptGridRun.Columns[1].DefaultCellStyle.BackColor = Color.LightGray; ;            kptGridRun.Columns[2].DefaultCellStyle.BackColor = Color.LightGray; ;            //this.m_RowStyleNormal.SelectionBackColor = Color.LightSteelBlue;            //this.m_RowStyleNormal.SelectionForeColor = Color.Red;            kptGridRun.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;            kptGridRun.Columns[0].FillWeight = 60;            kptGridRun.Columns[1].FillWeight = 100;            kptGridRun.Columns[2].FillWeight = 80;            kptGridRun.Columns[3].FillWeight = 50;            kptGridRun.Columns[4].FillWeight = 50;            kptGridRun.Columns[5].FillWeight = 50;            kptGridRun.Columns[0].ReadOnly = true;            kptGridRun.Columns[1].ReadOnly = true;            kptGridRun.Columns[2].ReadOnly = true;            kptGridRun.Columns[3].ReadOnly = true;            kptGridRun.Columns[4].ReadOnly = true;            kptGridRun.Columns[5].ReadOnly = true;            kptGridRun.RowsDefaultCellStyle = m_RowStyleNormal;        }        private void timer1_Tick(object sender, EventArgs e)        {            if (Convert.ToDouble(chart1TxtMin.Text) < Convert.ToDouble(chart1TxtMax.Text))            {                chart1TxtNow.Text = run.chart(chart1, chart1ComboBox.SelectedIndex, dt, Convert.ToDouble(chart1TxtMin.Text), Convert.ToDouble(chart1TxtMax.Text), 0).ToString();            }            if (Convert.ToDouble(chart2TxtMin.Text) < Convert.ToDouble(chart2TxtMax.Text))            {                chart2TxtNow.Text = run.chart(chart2, chart2ComboBox.SelectedIndex, dt, Convert.ToDouble(chart2TxtMin.Text), Convert.ToDouble(chart2TxtMax.Text), 0).ToString();            }            if (Convert.ToDouble(chart3TxtMin.Text) < Convert.ToDouble(chart3TxtMax.Text))            {                chart3TxtNow.Text = run.chart(chart3, chart3ComboBox.SelectedIndex, dt, Convert.ToDouble(chart3TxtMin.Text), Convert.ToDouble(chart3TxtMax.Text), 0).ToString();            }            if (Convert.ToDouble(chart4TxtMin.Text) < Convert.ToDouble(chart4TxtMax.Text))            {                chart4TxtNow.Text = run.chart(chart4, chart4ComboBox.SelectedIndex, dt, Convert.ToDouble(chart4TxtMin.Text), Convert.ToDouble(chart4TxtMax.Text), 0).ToString();            }            if (Convert.ToDouble(chart5TxtMin.Text) < Convert.ToDouble(chart5TxtMax.Text))            {                chart5TxtNow.Text = run.chart(chart5, chart5ComboBox.SelectedIndex, dt, Convert.ToDouble(chart5TxtMin.Text), Convert.ToDouble(chart5TxtMax.Text), 0).ToString();            }            if (Convert.ToDouble(chart6TxtMin.Text) < Convert.ToDouble(chart6TxtMax.Text))            {                chart6TxtNow.Text = run.chart(chart6, chart6ComboBox.SelectedIndex, dt, Convert.ToDouble(chart6TxtMin.Text), Convert.ToDouble(chart6TxtMax.Text), 0).ToString();            }        }    }}

  

StartRun类:

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Data;using System.Windows.Forms.DataVisualization.Charting;using System.Drawing;using System.Windows.Forms;namespace KryptonFormAirCompressor.BLL{    class StartRun    {        static DateTime startTime = DateTime.Now.AddSeconds(-1);        DateTime endTime = startTime.AddSeconds(6);        DateTime kd = DateTime.Parse("00:00:01");        Random rd = new Random();       //随机函数,产生Y轴数据        DataTable dt = new DataTable(); //创建数据表,存储数据        int flag = 0;   //设置Timer控件是否生效        int i = 0;      //显示数据表中的数据行数        double yMax = 400;        double yMin = -400;        double yStep = 200;        Chart mychart = new Chart();        //图表初始化        public void chartInit(Chart ch, TabPage tp)        {            TabPage tabPage2 = tp;            Chart chInit = ch;            ch.Series["Series1"].Points.Clear();            chInit.Width = tabPage2.Width / 2 - 1;            chInit.Height = tabPage2.Height / 3 - 25;            chInit.BackColor = Color.Azure;            //设置是否在内部绘图区域中自动设置合适的图表元素            chInit.ChartAreas["ChartArea1"].Position.Auto = false;            chInit.ChartAreas["ChartArea1"].Position.X = 0;            chInit.ChartAreas["ChartArea1"].Position.Y = 0;            chInit.ChartAreas["ChartArea1"].Position.Width = 100;            chInit.ChartAreas["ChartArea1"].Position.Height = 100;            chInit.ChartAreas["ChartArea1"].InnerPlotPosition.Auto = false;            chInit.ChartAreas["ChartArea1"].InnerPlotPosition.X = 7;            chInit.ChartAreas["ChartArea1"].InnerPlotPosition.Y = 4;            chInit.ChartAreas["ChartArea1"].InnerPlotPosition.Width = 88;            chInit.ChartAreas["ChartArea1"].InnerPlotPosition.Height = 85;            ////设置X轴字体类型、大小、颜色            //chInit.ChartAreas["ChartArea1"].AxisX.LabelStyle.Font = new System.Drawing.Font("宋体", 10f);            //chInit.ChartAreas["ChartArea1"].AxisX.LabelStyle.ForeColor = Color.Black;            ////设置Y轴字体类型、大小、颜色            //chInit.ChartAreas["ChartArea1"].AxisY.LabelStyle.Font = new System.Drawing.Font("宋体", 10f);            //chInit.ChartAreas["ChartArea1"].AxisY.LabelStyle.ForeColor = Color.Black;        }        public double chart(Chart ch, int category, DataTable table, double min, double max, double now)        {            mychart = ch;            dt = table;            yMin = min;            yMax = max;
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表