C# .NetCore3.1下 LiveCharts 3条实时曲线

using System;
using System.Windows.Forms;
using LiveCharts;
using LiveCharts.Configurations;
using LiveCharts.Wpf;

namespace Winforms.Cartesian.ConstantChanges
{
    public partial class ConstantChanges : Form
    {
        public ConstantChanges()
        {
            InitializeComponent();

            //To handle live data easily, in this case we built a specialized type
            //the MeasureModel class, it only contains 2 properties
            //DateTime and Value
            //We need to configure LiveCharts to handle MeasureModel class
            //The next code configures MEasureModel  globally, this means
            //that livecharts learns to plot MeasureModel and will use this config every time
            //a ChartValues instance uses this type.
            //this code ideally should only run once, when application starts is reccomended.
            //you can configure series in many ways, learn more at http://lvcharts.net/App/examples/v1/wpf/Types%20and%20Configuration

            var mapper = Mappers.Xy<MeasureModel>()
                .X(model => model.DateTime.Ticks)   //use DateTime.Ticks as X
                .Y(model => model.Value);           //use the value property as Y

            //lets save the mapper globally.
            Charting.For<MeasureModel>(mapper);

            //the ChartValues property will store our values array
            ChartValues = new ChartValues<MeasureModel>();
            #region 添加两个实时曲线值
            ChartValues2 = new ChartValues<MeasureModel>();
            ChartValues3 = new ChartValues<MeasureModel>(); 
            #endregion
            //Servies 添加2个实时曲线
            cartesianChart1.Series = new SeriesCollection
            {
                new LineSeries
                {
                    Values = ChartValues,
                    PointGeometrySize = 18,
                    StrokeThickness = 4
                },
                  new LineSeries
                {
                    Values = ChartValues2,
                    PointGeometrySize = 18,
                    StrokeThickness = 4
                },
                    new LineSeries
                {
                    Values = ChartValues3,
                    PointGeometrySize = 18,
                    StrokeThickness = 4
                }
            };
            cartesianChart1.AxisX.Add(new Axis
            {
                DisableAnimations = true,
                LabelFormatter = value => new System.DateTime((long) value).ToString("mm:ss"),
                Separator = new Separator
                {
                    Step = TimeSpan.FromSeconds(1).Ticks
                }
            });

            SetAxisLimits(System.DateTime.Now);

            //The next code simulates data changes every 500 ms
            Timer = new Timer
            {
                Interval = 500
            };
            Timer.Tick += TimerOnTick;
            R = new Random();
            Timer.Start();
        }

        public ChartValues<MeasureModel> ChartValues { get; set; }
        #region 添加2个实时曲线值
        public ChartValues<MeasureModel> ChartValues2 { get; set; }
        public ChartValues<MeasureModel> ChartValues3 { get; set; } 
        #endregion
        public Timer Timer { get; set; }
        public Random R { get; set; }

        private void SetAxisLimits(System.DateTime now)
        {
            cartesianChart1.AxisX[0].MaxValue = now.Ticks + TimeSpan.FromSeconds(1).Ticks; // lets force the axis to be 100ms ahead
            cartesianChart1.AxisX[0].MinValue = now.Ticks - TimeSpan.FromSeconds(8).Ticks; //we only care about the last 8 seconds
        }

        private void TimerOnTick(object sender, EventArgs eventArgs)
        {
            var now = System.DateTime.Now;

            ChartValues.Add(new MeasureModel
            {
                DateTime = now,
                Value = R.Next(0, 10)
            });
            #region 修改添加两个实时曲线值
            ChartValues2.Add(new MeasureModel
            {
                DateTime = now,
                Value = R.Next(0, 10)
            });
            ChartValues3.Add(new MeasureModel
            {
                DateTime = now,
                Value = R.Next(0, 10)
            }); 
            #endregion

            SetAxisLimits(now);

            //lets only use the last 30 values
            if (ChartValues.Count > 30) ChartValues.RemoveAt(0);
        }
    }
}

添加效果图

 

  • 4
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
.NET Core 3.1是微软开发的一种跨平台的开发框架,可以用于构建各种应用程序,包括Web应用、移动应用、桌面应用等。它具有高性能、可扩展、易于维护等特点,被广泛应用于各个领域。 API(Application Programming Interface)是一种软件架构,用于不同系统之间的互操作和通信。通过使用API,我们可以将不同的应用程序和服务连接起来,实现数据交互和功能共享。 MySQL是一种常用的开源关系型数据库管理系统。它具有高性能、稳定性和安全性,并支持多平台。在.NET Core 3.1中,我们可以通过使用MySQL数据库提供程序来操作MySQL数据库。 在.NET Core 3.1 API中使用MySQL数据库,我们可以按照以下步骤进行: 1. 首先,我们需要安装MySQL数据库,并创建一个数据库,用于存储数据。 2. 接着,我们需要在.NET Core 3.1项目中安装MySQL数据库提供程序。可以使用NuGet包管理器或在项目文件中添加对MySQL提供程序的引用。 3. 在项目中添加对MySQL数据库的连接字符串的配置,包括服务器地址、用户名、密码和数据库名称等。 4. 创建一个数据模型,用于定义数据库中的表和字段。 5. 使用Entity Framework Core来执行数据库操作,包括查询、插入、更新和删除等。 6. 在API的控制器中编写相应的接口,用于处理客户端的请求并与数据库进行交互。 7. 在启动文件中配置Web API的路由和Authentication。 通过以上步骤,我们就可以在.NET Core 3.1的API中使用MySQL数据库。使用MySQL数据库可以提供数据存储和检索的功能,使我们的API可以处理和管理数据。同时,由于.NET Core 3.1的跨平台特性,我们可以在不同的操作系统上部署和运行我们的API,实现应用程序的跨平台和多设备支持。
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值