精华内容
下载资源
问答
  • VS2015,在项目的引用点击右键,选择“管理NuGet程序包”,搜索OxyPlot,安装OxyPlot.WPF;安装完成后,参考该资源代码,可以进行实时绘图
  • oxyplot_oxyplot文件_源码

    2021-10-04 09:21:13
    c#下的绘图控件源代码,oxy编制,供大家开源使用,基于mit协议
  • oxyplot跨平台绘图库官方说明文档,适合.NET跨平台数据可视化,在OxyPlot内部可以通过Serial进行定义图表,包含:LineSerial(线条--可以通过定义点进行画线条)、FunctionSerial(函数曲线)、AreaSerial(区域)、...
  • 这是一个WPF应用程序,可在图表中显示(模拟)实时数据。
  • OxyPlotWinform.rar

    2020-03-20 14:55:36
    Oxyplot是一个.NET跨平台的绘图组件,适用于WPF, Windows 8, Windows Phone, Windows Phone Silverlight, Windows Forms, Silverlight, GTK#, Xwt, Xamarin.iOS, Xamarin.Android, Xamarin.Forms and Xamarin.Mac....
  • 使用OxyPlot、DynamicDataDisplay绘制折线图,两个分开的Demo。下载之前可以先参考博客“折线图的绘制”,确认是否是自己需要的。
  • 本例是使用 oxyplot绘制常用的统计图。包括柱状图、折线图、饼状图等。
  • Oxyplot.rar

    2020-06-07 13:18:56
    非常好用的Oxyplot插件,功能强大,有喜欢的朋友一起学习,相互交流。
  • oxyplot-master.zip

    2021-09-27 18:21:20
    完全开源的OxyPlot插件代码、不收费、好用,自定义程度高,实例代码详细。
  • oxyplot 扩展 OxyPlot 的一组扩展,可减少绘制数据所需的代码量。 用法 void Main() { var model = new PlotModel { Title = "Test" }; var xseries = new double[] { 1, 2, 3 }; var yseries1 = new double...
  • OxyPlot绘图库文件以及说明文档,主要适用于C# WPF应用的开发,能够快速简单地绘制图标,具体效果请参见博客“折线图的绘制”。
  • OxyPlotWinform-Demo.zip

    2020-06-08 14:25:09
    oxyPlot能够简易的创建图表,支持Winform、WPF等,并且该库也在Github上面开源,可随时查看更新的内容。附件Demo为利用OxyPlot库绘制的一个动态曲线,曲线数据为随机值,可根据自己的需要进行修改。
  • Oxyplot绘图插件.rar

    2021-09-16 15:56:37
    直接扔到VS工程里即可使用
  • OxyPlot是.NET的跨平台绘图库 入门 使用NuGet程序包管理器添加对OxyPlot的引用(如果要使用预发行版程序包,请参阅下面的详细信息) 将PlotView添加到您的用户界面 在代码中创建一个PlotModel 绑定PlotModel到...
  • 兽人情节 名称 徽章 聊天室 资料下载 稳定版 不稳定版本 扩展了的功能。 有关文档,请访问
  • OxyPlot 是 一个 .NET 跨平台的绘图组件。  标签:OxyPlot
  • OxyPlot是.NET下一款非常强大的图表库,几乎可以涵盖各种图表的制作,且支持.net下各种平台和运行库,本文通过示例介绍该如何在Wpf中使用OxyPlot。 本文使用的开发工具是Vs2019,sdk使用的是.net5 首先,使用Vs...

    OxyPlot是.NET下一款非常强大的图表库,几乎可以涵盖各种图表的制作,且支持.net下各种平台和运行库,本文通过示例介绍该如何在Wpf中使用OxyPlot。

    本文使用的开发工具是Vs2019,sdk使用的是.net5

    首先,使用Vs2019创建一个wpf项目,通过包管理器添加OxyPlot.Wpf依赖或者通过命令行添加依赖:

    dotnet add package OxyPlot.Wpf

    然后添加Prism.Core依赖,添加此依赖主要是为了mvvm的支持

    dotnet add package Prism.Core

    在项目中添加MainWindowViewModel.cs类,使其继承自BindableBase类,BindableBase是mvvm模式的viewmodel基类,它实现了INotifyPropertyChanged接口,可以通过数据绑定让UI响应viewmodel的变化

    在MainWindowViewModel添加如下代码:

    using OxyPlot;
    using OxyPlot.Axes;
    using OxyPlot.Legends;
    using OxyPlot.Series;
    using Prism.Mvvm;
    using System;
    using System.Collections.Generic;
    using System.Threading.Tasks;
    
    namespace WpfChart.Test
    {    
        public class MainWindowViewModel : BindableBase
        {
            public MainWindowViewModel()
            {
                // 构造函数中同步生成数据,无需设置mvvm属性
                // _ChartModel = CreateChartModel(data);
    
                // 异步获取图表数据
                GetData().ContinueWith(x => {
                    // 设置mvvm属性更新图表Model
                    ChartModel = CreateChartModel(x.Result);
                    // 不使用mvvm属性,使用下面方法通知UI
                    // RaisePropertyChanged(nameof(ChartModel));
                });
            }
    
    
            private PlotModel _ChartModel;
            /// <summary>
            /// 图表Model的mvvm属性,可通知UI更新
            /// </summary>
            public PlotModel ChartModel
            {
                get { return _ChartModel; }
                set { SetProperty(ref _ChartModel, value); }
            }
    
            ...
            ...
        }

    在MainWindow.xaml中添加如下内容:

    <Window ...
            xmlns:oxyWpf="http://oxyplot.org/wpf"
            ...
            >
    
        <Grid>
            <oxyWpf:PlotView Foreground="Black" Margin="5 5 5 0" Background="Transparent" Model="{Binding ChartModel}" />
        </Grid>
    </Window>
    

    在MainWindow.xaml.cs中为数据绑定添加数据上下文

        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
                // 添加数据绑定上下文
                DataContext = new MainWindowViewModel();
            }
        }

    这里我们通过数据绑定的方法显示图表,需要添加实体对象类:

        public class ChartData
        {
            public DateTime Date { get; set; }
    
            public double Total { get; set; }
    
            public double PassRate { get; set; }
        }

    添加测试数据,模拟后台查询方法:

            /// <summary>
            /// 模拟后台异步查询表格数据
            /// </summary>
            /// <returns></returns>
            private Task<List<ChartData>> GetData()
            {
                var data = new List<ChartData>()
                {
                    new ChartData { Date = DateTime.Now.Date.AddDays(-15), Total = 121, PassRate = .84 },
                    new ChartData { Date = DateTime.Now.Date.AddDays(-14), Total = 88, PassRate = .92 },
                    new ChartData { Date = DateTime.Now.Date.AddDays(-13), Total = 180, PassRate = .35 },
                    new ChartData { Date = DateTime.Now.Date.AddDays(-12), Total = 150, PassRate = .46 },
                    new ChartData { Date = DateTime.Now.Date.AddDays(-11), Total = 78, PassRate = .58 },
                    new ChartData { Date = DateTime.Now.Date.AddDays(-10), Total = 99, PassRate = .71 },
                    new ChartData { Date = DateTime.Now.Date.AddDays(-9), Total = 143, PassRate = .81 },
                    new ChartData { Date = DateTime.Now.Date.AddDays(-8), Total = 56, PassRate = .85 },
                    new ChartData { Date = DateTime.Now.Date.AddDays(-7), Total = 108, PassRate = .95 },
                    new ChartData { Date = DateTime.Now.Date.AddDays(-6), Total = 79, PassRate = .78 },
                    new ChartData { Date = DateTime.Now.Date.AddDays(-5), Total = 63, PassRate = .65 },
                    new ChartData { Date = DateTime.Now.Date.AddDays(-4), Total = 157, PassRate = .58 },
                    new ChartData { Date = DateTime.Now.Date.AddDays(-3), Total = 148, PassRate = .36 },
                    new ChartData { Date = DateTime.Now.Date.AddDays(-2), Total = 115, PassRate = .48 },
                    new ChartData { Date = DateTime.Now.Date.AddDays(-1), Total = 89, PassRate = .63 },
                    new ChartData { Date = DateTime.Now.Date, Total = 121, PassRate = .90 },
                };
                return Task.FromResult(data);
            }
    

    添加生成图表模型的方法,方法中创建一个图表模型,在其中添加一个日期x轴,一个数字y轴,一个百分比y轴,还添加了柱状图和折线图两个不同的图表系列,并将测试数据绑定到这两个图表系列中,代码如下:

            /// <summary>
            /// 根据数据生成图表模型
            /// </summary>
            /// <param name="list"></param>
            /// <returns></returns>
            private PlotModel CreateChartModel(List<ChartData> list)
            {
                var model = new PlotModel() { Title = "测试"};
    
                // 添加图例说明
                model.Legends.Add(new Legend
                {
                    LegendPlacement = LegendPlacement.Outside,
                    LegendPosition = LegendPosition.BottomCenter,
                    LegendOrientation = LegendOrientation.Horizontal,
                    LegendBorderThickness = 0,
                    LegendTextColor = OxyColors.LightGray
                });
    
                // 定义第一个Y轴y1,显示数量
                var ay1 = new LinearAxis()
                {
                    Key = "y1",
                    Position = AxisPosition.Left,
                };
    
    
                // 定义第二个Y轴y2,显示百分比
                var ay2 = new LinearAxis()
                {
                    Key = "y2",
                    Position = AxisPosition.Right,
                    Minimum = 0.1,
                    MajorStep = .1,
                    Maximum = 1,
                    LabelFormatter = v => $"{v:P1}"
                };
                // 在第二Y轴坐标50%和80%处显示网格线
                ay2.ExtraGridlines = new double[2] { 0.5, 0.8 };
                ay2.ExtraGridlineStyle = LineStyle.DashDashDot; // 网格线样式
    
                // 定义X轴为日期轴,从15天前到现在
                var minValue = DateTimeAxis.ToDouble(DateTime.Now.Date.AddDays(-15));
                var maxValue = DateTimeAxis.ToDouble(DateTime.Now.Date);
                var ax = new DateTimeAxis()
                {
                    Minimum = minValue,
                    Maximum = maxValue,
                    StringFormat = "yyyy-MM-dd日",
                    MajorStep = 2,
                    Position = AxisPosition.Bottom,
                    Angle = 45,
                    IsZoomEnabled = false
                };
    
                // 定义柱形图序列,指定数据轴为Y1轴
                var totalBarSeries = new LinearBarSeries();
                totalBarSeries.YAxisKey = "y1";
                totalBarSeries.BarWidth = 10;
                //totalBarSeries.FillColor = OxyColor.FromArgb(69, 76, 175, 80);
                //totalBarSeries.StrokeThickness = 1;
                //totalBarSeries.StrokeColor = OxyColor.FromArgb(255, 76, 175, 80);
                totalBarSeries.Title = "总数";
                // 点击时弹出的label内容
                totalBarSeries.TrackerFormatString = "{0}\r\n{2:dd}日: {4:0}";
                // 设置数据绑定源和字段
                totalBarSeries.ItemsSource = list;
                totalBarSeries.DataFieldX = "Date";
                totalBarSeries.DataFieldY = "Total";
                // 下面为手动添加数据方式
                //totalBarSeries.Points.Add(new DataPoint(DateTimeAxis.ToDouble(DateTime.Now.Date.AddDays(-15)), 333));
    
                // 定义三色折线图序列,指定数据轴为Y2轴
                var passedRateSeries = new ThreeColorLineSeries();
                passedRateSeries.Title = "通过率";
                passedRateSeries.YAxisKey = "y2";
                // 点击时弹出的label内容
                passedRateSeries.TrackerFormatString = "{0}\r\n{2:dd}日: {4:P1}";
                // 设置颜色阈值范围
                passedRateSeries.LimitHi = .8;
                passedRateSeries.LimitLo = .5;
                // 设置数据绑定源和字段
                passedRateSeries.ItemsSource = list;
                passedRateSeries.DataFieldX = "Date";
                passedRateSeries.DataFieldY = "PassRate";
                // 下面为手动添加数据方式
                //passedRateSeries.Points.Add(new DataPoint(DateTimeAxis.ToDouble(DateTime.Now.Date.AddDays(-15)), .750));
                // 添加图标资源
                model.Series.Add(totalBarSeries);
                model.Series.Add(passedRateSeries);
                model.Axes.Add(ay1);
                model.Axes.Add(ay2);
                model.Axes.Add(ax);
                // 设置图形边框
                model.PlotAreaBorderThickness = new OxyThickness(1, 0, 1, 1);
                return model;
            }

    编译运行后效果如下:

    官方源码地址,里面包含上百个示例图表 GitHub - oxyplot/oxyplot: A cross-platform plotting library for .NET 

    本文源码下载

    展开全文
  • 程序下载 ... 官方网站OxyPlot https://oxyplot.github.io/ 官方帮助文档 WPF — OxyPlot 2015.1 documentationhttps://oxyplot.readthedocs.io/en/latest/getting-sta...

    程序下载

    https://download.csdn.net/download/g313105910/31986447icon-default.png?t=L892https://download.csdn.net/download/g313105910/31986447

    官方网站  OxyPlot https://oxyplot.github.io/

      

    官方帮助文档

    WPF — OxyPlot 2015.1 documentationicon-default.png?t=L892https://oxyplot.readthedocs.io/en/latest/getting-started/hello-wpf.html

    简单示例打开串口,解析x,y,z数据并显示

    协议字符型X:11,Y:111.01,Z:21.5\r\n

    第一步通过Nuget安装OxyPlot.Wpf

    新建Bootstrapper.cs类,引入Caliburn.Micro

    using Caliburn.Micro;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows;
    
    namespace DisplayToolCollection
    {
        public class Bootstrapper : BootstrapperBase
        {
            SimpleContainer _container;
    
            public Bootstrapper()
            {
                System.Windows.FrameworkCompatibilityPreferences.KeepTextBoxDisplaySynchronizedWithTextProperty = false;
    
                Initialize();
            }
    
            protected override void Configure()
            {
                base.Configure();
    
                _container = new SimpleContainer();
                _container.Singleton<IWindowManager, WindowManager>();
                _container.Singleton<MainWindowView>();
                _container.Singleton<MainWindowViewModel>();
            }
    
            protected override object GetInstance(Type service, string key)
            {
                return _container.GetInstance(service, key);
            }
    
            protected override IEnumerable<object> GetAllInstances(Type service)
            {
                return _container.GetAllInstances(service);
            }
    
            protected override void BuildUp(object instance)
            {
                _container.BuildUp(instance);
            }
    
            protected override void OnStartup(object sender, StartupEventArgs e)
            {
                DisplayRootViewFor<MainWindowViewModel>();
            }
        }
    }
    

    MainWindowView.xaml

    <Window x:Class="DisplayToolCollection.MainWindowView"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
            xmlns:oxy="http://oxyplot.org/wpf"
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
            xmlns:local="clr-namespace:DisplayToolCollection"
            mc:Ignorable="d"
            Title="数据可视化显示" Height="768" Width="1024">
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="1*"/>
                <ColumnDefinition Width="240"/>
            </Grid.ColumnDefinitions>
            <!--<Grid.ContextMenu >
                <ContextMenu>
                    <MenuItem Header="复位" x:Name="Reset"/>
                </ContextMenu>
            </Grid.ContextMenu>-->
            <oxy:PlotView Model="{Binding _model}"></oxy:PlotView>
    
            <StackPanel Margin="2,20,2,2" Grid.Column="1">
                <GroupBox Header="常用设置">
                    <StackPanel>
                        <StackPanel Orientation="Horizontal" Margin="5">
                            <TextBlock Text="串口号:" Width="80" VerticalAlignment="Center"/>
                            <TextBox Text="{Binding PortName}" Width="120" VerticalAlignment="Center">
                            </TextBox>
                        </StackPanel>
                        <StackPanel Orientation="Horizontal" Margin="5">
                            <TextBlock Text="波特率:" Width="80" VerticalAlignment="Center"/>
                            <TextBox Text="{Binding BaudRate}" Width="120" VerticalAlignment="Center">
                            </TextBox>
                        </StackPanel>
                        <!--<StackPanel Orientation="Horizontal" Margin="5">
                            <TextBlock Text="数据位数:" Width="80" VerticalAlignment="Center"/>
                            <TextBox Width="120" VerticalAlignment="Center">
                            </TextBox>
                        </StackPanel>
                        <StackPanel Orientation="Horizontal" Margin="5">
                            <TextBlock Text="停止位:" Width="80" VerticalAlignment="Center"/>
                            <TextBox Width="120" VerticalAlignment="Center">
                            </TextBox>
                        </StackPanel>
                        <StackPanel Orientation="Horizontal" Margin="5">
                            <TextBlock Text="奇偶校验位:" Width="80" VerticalAlignment="Center"/>
                            <TextBox Width="120" VerticalAlignment="Center">
                            </TextBox>
                        </StackPanel>
                        <StackPanel Orientation="Horizontal" Margin="5">
                            <TextBlock Text="流控:" Width="80" VerticalAlignment="Center"/>
                            <TextBox Width="120" VerticalAlignment="Center">
                            </TextBox>
                        </StackPanel>-->
                        <StackPanel Orientation="Horizontal" Margin="5">
                            <TextBlock Text="窗口数据个数:" Width="80" VerticalAlignment="Center"/>
                            <TextBox Text="{Binding WindowsCount}" Width="120" VerticalAlignment="Center">
                            </TextBox>
                        </StackPanel>
                        <StackPanel Orientation="Horizontal" Margin="5">
                            <TextBlock Text="操作:" Width="80" VerticalAlignment="Center"/>
                            <Button Content="打开串口"  Width="55" x:Name="OpenSerial" IsEnabled="{Binding IsOpenSerial}"/>
                            <TextBlock Text="" Width="10" VerticalAlignment="Center"/>
                            <Button Content="关闭串口"  Width="55" x:Name="CloseSerial" IsEnabled="{Binding IsCloseSerial}"/>
                        </StackPanel>
                        <StackPanel Orientation="Horizontal" Margin="5">
                            <TextBlock Text="图表:" Width="80" VerticalAlignment="Center"/>
                            <Button Content="复位"  Width="55" x:Name="Reset"/>
                            <TextBlock Text="" Width="10" VerticalAlignment="Center"/>
                            <Button Content="清空"  Width="55" x:Name="Clear"/>
                        </StackPanel>
                        <StackPanel Orientation="Horizontal" Margin="5">
                            <TextBlock Text="扩展:" Width="80" VerticalAlignment="Center"/>
                            <Button Content="截图"  Width="55" x:Name="Snap"/>
                        </StackPanel>
                        <StackPanel Orientation="Horizontal" Margin="5">
                            <TextBlock Text="信息:" Width="80" VerticalAlignment="Center"/>
                            <TextBox Text="{Binding Info}" Width="120" Height="200" VerticalScrollBarVisibility="Visible" />
                        </StackPanel>
                    </StackPanel>
                </GroupBox>
            </StackPanel>
        </Grid>
    </Window>
    

     MainWindowViewModel.cs

    using Caliburn.Micro;
    using DisplayToolCollection.Properties;
    using Microsoft.Win32;
    using OxyPlot;
    using OxyPlot.Axes;
    using OxyPlot.Series;
    using System;
    using System.Collections.Generic;
    using System.IO.Ports;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows;
    
    namespace DisplayToolCollection
    {
        public class MainWindowViewModel : PropertyChangedBase
        {
    
            private string _portName;
            public string PortName
            {
                get { return _portName; }
                set { _portName = value; Settings.Default.PortName = value; Settings.Default.Save(); NotifyOfPropertyChange(() => PortName); }
            }
    
            private int _baudRate;
            public int BaudRate
            {
                get { return _baudRate; }
                set { _baudRate = value; Settings.Default.BaudRate = value; Settings.Default.Save(); NotifyOfPropertyChange(() => BaudRate); }
            }
    
            private int _windowsCount;
            public int WindowsCount
            {
                get { return _windowsCount; }
                set { _windowsCount = value; Settings.Default.WindowsCount = value; Settings.Default.Save(); NotifyOfPropertyChange(() => WindowsCount); }
            }
    
            private string _info;
            public string Info
            {
                get { return _info; }
                set { _info = value;  NotifyOfPropertyChange(() => Info); }
            }
    
            private bool _isOpenSerial = true;
            public bool IsOpenSerial
            {
                get { return _isOpenSerial; }
                set { _isOpenSerial = value; NotifyOfPropertyChange(() => IsOpenSerial); }
            }
    
            private bool _isCloseSerial = false;
            public bool IsCloseSerial
            {
                get { return _isCloseSerial; }
                set { _isCloseSerial = value; NotifyOfPropertyChange(() => IsCloseSerial); }
            }
    
            private SerialPort _serialPort = new SerialPort();
            public PlotModel _model { get; private set; }
            private LineSeries _oneLine;
            private LineSeries _twoLine;
            private LineSeries _threeLine;
            //private LineSeries _fourLine;
            //private LineSeries _fiveLine;
            //private LineSeries _sixLine;
    #if ShowTime
            List<VoltagePoint> _oneData = new List<VoltagePoint>();
            List<VoltagePoint> _twoData = new List<VoltagePoint>();
            List<VoltagePoint> _threeData = new List<VoltagePoint>();
    #else
            List<DataPoint> _oneData = new List<DataPoint>();
            List<DataPoint> _twoData = new List<DataPoint>();
            List<DataPoint> _threeData = new List<DataPoint>();
    #endif
            //List<VoltagePoint> _fourData = new List<VoltagePoint>();
            //List<VoltagePoint> _fiveData = new List<VoltagePoint>();
            //List<VoltagePoint> _sixData = new List<VoltagePoint>();
    
            public MainWindowViewModel()
            {
                PortName = Settings.Default.PortName;
                BaudRate = Settings.Default.BaudRate;
                WindowsCount = Settings.Default.WindowsCount;
                this._model = new PlotModel { Title = "数据可视化显示" };
    #if ShowTime
                this._model.Axes.Add(new DateTimeAxis { Position = AxisPosition.Bottom, StringFormat = "HH:mm:ss" });
                _oneLine = new LineSeries
                {
                    StrokeThickness = 1,
                    Title = $"一",
                    ItemsSource = _oneData,
                    Color = OxyColors.Red,
                    MarkerType = MarkerType.None,
                    DataFieldX = "Time",
                    DataFieldY = "Voltage",
                    TrackerFormatString = "Time: {2:yyyy/MM/dd HH:mm:ss.fff}\n值: {4}"
                };
    
                _twoLine = new LineSeries
                {
                    StrokeThickness = 1,
                    Title = $"二",
                    ItemsSource = _twoData,
                    Color = OxyColors.Blue,
                    DataFieldX = "Time",
                    DataFieldY = "Voltage",
                    TrackerFormatString = "Time: {2:yyyy/MM/dd HH:mm:ss.fff}\n值: {4}"
                };
                _threeLine = new LineSeries
                {
                    StrokeThickness = 1,
                    Title = $"三",
                    ItemsSource = _threeData,
                    Color = OxyColors.Green,
                    DataFieldX = "Time",
                    DataFieldY = "Voltage",
                    TrackerFormatString = "Time: {2:yyyy/MM/dd HH:mm:ss.fff}\n值: {4}"
                };
    #else
                _oneLine = new LineSeries
                {
                    StrokeThickness = 1,
                    Title = $"x轴数据",
                    ItemsSource = _oneData,
                    Color = OxyColors.Red,
                    MarkerType = MarkerType.None,
                };
    
                _twoLine = new LineSeries
                {
                    StrokeThickness = 1,
                    Title = $"y轴数据",
                    ItemsSource = _twoData,
                    Color = OxyColors.Blue
                };
    
                _threeLine = new LineSeries
                {
                    StrokeThickness = 1,
                    Title = $"z轴数据",
                    ItemsSource = _threeData,
                    Color = OxyColors.Green
                };
    #endif
                _model.Series.Add(_oneLine);
                _model.Series.Add(_twoLine);
                _model.Series.Add(_threeLine);
                //for (int i = 0; i < WindowsCount; i++)
                //{
                //    _oneData.Add(new DataPoint(_oneData.Count, 0));
                //    _twoData.Add(new DataPoint(_twoData.Count, 0));
                //    _threeData.Add(new DataPoint(_threeData.Count, 0));
                //}
                _model.InvalidatePlot(true);
            }
    
            public void OpenSerial()
            {
                try
                {
                    if (_serialPort.IsOpen == false)
                    {
                        _serialPort.BaudRate = BaudRate;          //波特率
                        _serialPort.PortName = PortName;          //串口号
                        _serialPort.StopBits = StopBits.One;    //停止位
                        _serialPort.DataBits = 8;               //数据位
                        _serialPort.Parity = Parity.None;       //奇偶校验位
                        _serialPort.Handshake = Handshake.None; //流控
                        _serialPort.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(this.SerialPortDataReceived);
                        _serialPort.Open();
                        Info = $"打开成功!";
                        IsOpenSerial = false;
                        IsCloseSerial = !IsOpenSerial;
                    }
                }
                catch(Exception ex)
                {
                    Info = $"打开失败!错误信息:{ex.Message}!";
                }
            }
    
            public void CloseSerial()
            {
                try
                {
                    if (_serialPort.IsOpen == true)
                    {
                        _serialPort.Close();
                        Info = $"关闭成功!";
                    }
                }
                catch (Exception ex)
                {
                    Info = $"关闭失败!错误信息:{ex.Message}!";
                }
                IsOpenSerial = true;
                IsCloseSerial = !IsOpenSerial;
            }
    
            public void Reset()
            {
                _model.ResetAllAxes();
                _model.InvalidatePlot(true);
            }
    
            public void Clear()
            {
                _oneData.Clear();
                _twoData.Clear();
                _threeData.Clear();
                _model.InvalidatePlot(true);
            }
    
            private void SerialPortDataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
            {
                try
                {
                    System.IO.Ports.SerialPort com = (System.IO.Ports.SerialPort)sender;
                    int n = com.BytesToRead;//先记录下来,避免丢失
                    byte[] buf = new byte[n];
                    com.Read(buf, 0, n);//读取缓冲区数据
                    DataLine dataLine = null;
                    Buffs.Protocol(buf, buf.Length, out dataLine);
                    if (dataLine != null)
                    {
                        Application.Current.Dispatcher.Invoke(() =>
                        {
                            if (_oneData.Count >= WindowsCount)
                            {
                                _oneData.RemoveAt(0);
                            }
                            if (_twoData.Count >= WindowsCount)
                            {
                                _twoData.RemoveAt(0);
                            }
                            if (_threeData.Count >= WindowsCount)
                            {
                                _threeData.RemoveAt(0);
                            }
        #if ShowTime
                            _oneData.Add(new VoltagePoint() { Time = DateTime.Now, Voltage = dataLine.X });
                            _twoData.Add(new VoltagePoint() { Time = DateTime.Now, Voltage = dataLine.Y });
                            _threeData.Add(new VoltagePoint() { Time = DateTime.Now, Voltage = dataLine.Z });
        #else
                            _oneData.Add(new DataPoint(_oneData.Count, dataLine.X));
                            for (int i=0;i< _oneData.Count();i++)
                            {
                                _oneData[i] = new DataPoint(i, _oneData[i].Y);
                            }
    
                            _twoData.Add(new DataPoint(_twoData.Count, dataLine.Y));
                            for (int i = 0; i < _twoData.Count(); i++)
                            {
                                _twoData[i] = new DataPoint(i, _twoData[i].Y);
                            }
    
                            _threeData.Add(new DataPoint(_threeData.Count, dataLine.Z));
                            for (int i = 0; i < _threeData.Count(); i++)
                            {
                                _threeData[i] = new DataPoint(i, _threeData[i].Y);
                            }
    #endif
                            _model.InvalidatePlot(true);
                        });
                    }
                }
                catch(Exception ex)
                {
                    Info = ex.Message;
                }
            }
    
            /// <summary>
            /// 界面截图
            /// </summary>
            public void Snap()
            {
                var mainWindow = System.Windows.Application.Current.Windows.OfType<MainWindowView>().FirstOrDefault();
                if (mainWindow != null)
                {
                    //弹出保存对话框
                    SaveFileDialog sfd = new SaveFileDialog();
                    sfd.Filter = "jpg files(*.jpg)|*.jpg|All files(*.*)|*.*";
                    if (sfd.ShowDialog() == true)
                    {
                        string fileName = sfd.FileName;
                        SaveFrameworkElementToImage(mainWindow, fileName);
                    }
                }
            }
    
            /// <summary>
            /// 保存截图
            /// </summary>
            /// <param name="ui">控件名称</param>
            /// <param name="filename">图片文件名</param>
            public void SaveFrameworkElementToImage(FrameworkElement ui, string filename)
            {
                try
                {
                    System.IO.FileStream ms = new System.IO.FileStream(filename, System.IO.FileMode.Create);
                    System.Windows.Media.Imaging.RenderTargetBitmap bmp = new System.Windows.Media.Imaging.RenderTargetBitmap((int)ui.ActualWidth, (int)ui.ActualHeight, 96d, 96d, System.Windows.Media.PixelFormats.Pbgra32);
                    bmp.Render(ui);
                    System.Windows.Media.Imaging.JpegBitmapEncoder encoder = new System.Windows.Media.Imaging.JpegBitmapEncoder();
                    encoder.Frames.Add(System.Windows.Media.Imaging.BitmapFrame.Create(bmp));
                    encoder.Save(ms);
                    ms.Close();
                }
                catch (Exception ex)
                {
                    Info = ex.Message;
                }
            }
        }
    }
    

    协议解析ProtocalBasic.cs

    using System;
    using System.Collections.Concurrent;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading;
    using System.Threading.Tasks;
    
    namespace DisplayToolCollection
    {
        public static class Buffs
        {
            static Byte[] _resolverBuf = new Byte[8192000];
            static int _resolverBufLen = 0;
    
            public static void Protocol(Byte[] buf, int len , out DataLine dataLine)
            {
                AddResolverBuf(buf, len);
                var dataBuf = LookResolverBuf();
                var deleteLen = DeleteResolver(dataBuf, out dataLine);
                DeleteResolverBuf(deleteLen);
            }
    
            private static int DeleteResolver(Byte[] buf,out DataLine dataLine)
            {
                dataLine = null;
                int byteCount = 0;
                int len = 0;
                if (buf == null)
                {
                    return byteCount;
                }
                if (buf.Length >= 8)
                {
                    for (int i = 0; i < buf.Length - 1; i++)
                    {
                        if (buf[i] == '\r' && buf[i + 1] == '\n')
                        {
                            len = i + 1 + 1;
                            dataLine = ProtocalBasic.Protocol(buf, len);
                            return len;
                        }
                    }
                }
                return byteCount;
            }
    
            private static Byte[] LookResolverBuf()
            {
                Byte[] tempByte = new Byte[_resolverBufLen];
                Buffer.BlockCopy(_resolverBuf, 0, tempByte, 0, _resolverBufLen);
                return tempByte;
            }
    
            private static bool AddResolverBuf(Byte[] buf, int len)
            {
                bool isOk = false;
                if (len < 0)
                {
                    return isOk;
                }
                if ((len + _resolverBufLen) < _resolverBuf.Length)
                {
                    Buffer.BlockCopy(buf, 0, _resolverBuf, _resolverBufLen, len);
                    _resolverBufLen += len;
                    isOk = true;
                }
                return isOk;
            }
    
            private static bool DeleteResolverBuf(int len)
            {
                bool isOk = false;
                if (len < 0)
                {
                    return isOk;
                }
                if (len == 0)
                {
                    isOk = true;
                    return isOk;
                }
                if (len <= _resolverBufLen)
                {
                    Buffer.BlockCopy(_resolverBuf, len, _resolverBuf, 0, _resolverBufLen - len);
                    _resolverBufLen = _resolverBufLen - len;
                    isOk = true;
                }
                return isOk;
            }
        }
    
    }
    
    展开全文
  • C#oxyPlot用法记录

    2022-01-10 20:12:33
    窗口应用程序添加OxyPlot的Nuget包。 添加PlotView控件“plotView_PlotCurve” plotView_PlotCurve.Model = new PlotModel();//实例化绘图模块 plotView_PlotCurve.Model.Title = "曲线名称";//图名 plotView_...

    窗口应用程序添加OxyPlot的Nuget包。

    添加PlotView控件“plotView_PlotCurve”

    plotView_PlotCurve.Model = new PlotModel();//实例化绘图模块
    plotView_PlotCurve.Model.Title = "曲线名称";//图名
    plotView_PlotCurve.Model.TitleColor = OxyColors.Automatic;//标题字体颜色
    OxyPlot.Axes.Axis xa = new OxyPlot.Axes.LogarithmicAxis();//创建x轴,对数刻度
    xa.Position = OxyPlot.Axes.AxisPosition.Bottom;//坐标轴位置,底部
    xa.Title = "曲线值";//坐标轴名称
    xa.Minimum = 0.01; //坐标轴最小值
    xa.Maximum = 10000;//坐标轴最大值          
    xa.MinorGridlineStyle = LineStyle.Solid;  //x轴网格线,线类型为实线                 
    plotView_PlotCurve.Model.Axes.Add(xa);//绘图模块添加坐标轴
    OxyPlot.Axes.Axis xb = new OxyPlot.Axes.LinearAxis(); //实例化y轴
    xb.Position = OxyPlot.Axes.AxisPosition.Left;//y轴位置
    xb.Title = "曲线幅度";//坐标轴名称
    plotView_PlotCurve.Model.Axes.Add(xb);//绘图模块添加坐标轴
    LineSeries lineSeries = new LineSeries();//实例化绘图线
    for (int i = 0; i <count; i++)
    {
      
      lineSeries.Points.Add(new DataPoint(valuenow[], value[]));//将valuenow为x值,value为y值
     }
     plotView_PlotCurve.Model.Series.Add(lineSeries);//绘图模块条件绘制线条
     plotView_PlotCurve.InvalidatePlot(true);//刷新绘图区域

    展开全文
  • 最终我换了oxyplot,但还是感觉emmm资料不够(文档好烂啊,也可能是我读得不够细致)。一些内容还是去扒了官方示例才搞清楚的。不过总算折腾得差不多了,所以记录一下注意事项。 基础 安装 NuGet安装 文档和参考...

    2021/10/18更新-Debug: System.NullReferenceException: 未将对象引用设置到对象的实例

    这个错误我出过好几次,完全不长记性。。

    它的神奇之处在于,报错的位置跟实际出错的地方不是同一行:)
    在这里插入图片描述
    在这里插入图片描述
    经过排查,我是1155行出错了,并不是1154行,也不是那之前的某行(当然如果你前面有错,还是要改的!)。前面是这样的:
    在这里插入图片描述
    因为我在1154行以后才将对象放到this.DataContext中,所以在那之前,eptPlot.Model肯定是空的,所以一定会报错:)

    改正方法就是不用前台的对象,用后台的。比如:

    _viewModel.EptPlotModel.LegendOrientation = LegendOrientation.Horizontal;
    

    关于折线图,最终我还是放弃了InteractiveDataDisplay.WPF。原因是:1.资料好少,我不会改;2.自定义坐标轴始终搞不出来。

    最终我换了oxyplot,但还是感觉emmm资料不够(文档好烂啊,也可能是我读得不够细致)。一些内容还是去扒了官方示例才搞清楚的。不过总算折腾得差不多了,所以记录一下注意事项。

    基础

    安装

    NuGet安装
    在这里插入图片描述

    文档和参考资料

    官方文档:
    https://oxyplot.readthedocs.io/en/latest/introduction/index.html

    github(这个很有用!泪目):
    https://github.com/oxyplot/oxyplot
    在这里插入图片描述
    在这里插入图片描述

    入门:
    https://zhuanlan.zhihu.com/p/374211133

    公共属性介绍(坐标轴设置!很有用!):
    http://dlgcy.com/oxyplot-wpf-public-props/

    入门实例

    在这里插入图片描述
    后台

        public partial class WavePainter : Window 
        {
            internal class PlotViewModel
            {
                public PlotModel SimplePlotModel { set; get; }
                public PlotViewModel()
                {
                    SimplePlotModel = new PlotModel();
                    //线条
                    var lineSerial = new LineSeries() { Title = "直线实例" };
                    lineSerial.Points.Add(new DataPoint(0, 0));
                    lineSerial.Points.Add(new DataPoint(10, 10));
                    SimplePlotModel.Series.Add(lineSerial);
    
                    //函数sin(x)
                    var funcSerial = new FunctionSeries((x) => { return Math.Sin(x); }, 0, 10, 0.1, "y=sin(x)");
                    SimplePlotModel.Series.Add(funcSerial);
                    //return SimplePlotModel;
                }
            }
       
            private PlotViewModel _viewModel;
    
    		public WavePainter()
            {
                InitializeComponent();   
                PlotViewModel _viewModel = new PlotViewModel();
                //画直线
                this.DataContext = _viewModel;
                ...
            }
        }
    

    前台

    <Window ...
    		...
            xmlns:oxy="http://oxyplot.org/wpf">
    	<Grid>
    		<oxy:PlotView Model="{Binding Path= SimplePlotModel}"></oxy:PlotView>
    	</Grid>
    </Window>
    

    正式绘图

    xaml中的设置:加拖动条

        <Grid x:Name="myGrid">
        	<!--加拖动条-->
            <ScrollViewer HorizontalScrollBarVisibility="Visible" 
    				VerticalScrollBarVisibility="Visible" Margin="10,0,10,10">
                <oxy:PlotView x:Name="myPlot" Model="{Binding Path= SimplePlotModel}" 
    					  HorizontalAlignment="Left" VerticalAlignment="Center">
                </oxy:PlotView>
            </ScrollViewer>
        </Grid>    
    

    xaml中的设置:定义颜色

        <Window.Resources>
            <!-- 定义几种颜色 -->
            <SolidColorBrush x:Key="LineColorA" Color="#60D257" />
        </Window.Resources>
        ...
    	Background="{StaticResource LineColorA}"
    

    xaml中的设置:定义tracker

    一条线上有好多点,点击上面的某个点,出现的东西就是tracker。如果有多条线,需要多个tracker,那么给每个tracker以不同的TrackerKey即可。

    <!--标记-->
    <oxy:PlotView.TrackerDefinitions>
        <oxy:TrackerDefinition TrackerKey="TrackerA">
            <oxy:TrackerDefinition.TrackerTemplate>
                <ControlTemplate>
                    <Canvas>
                        <StackPanel Orientation="Horizontal" Canvas.Left="{Binding Position.X}" Canvas.Top="{Binding Position.Y}">
                            <TextBlock Foreground="White" Text="{Binding Item}"/>
                        </StackPanel>
                    </Canvas>
                </ControlTemplate>
            </oxy:TrackerDefinition.TrackerTemplate>
        </oxy:TrackerDefinition>
        ...
    </oxy:PlotView.TrackerDefinitions>
    <!--标记结束-->
    

    c#的设置为:

    var AreaSeriala = new AreaSeries() { 
        Title = "A",
        ItemsSource = pa,
        DataFieldX = "X",
        DataFieldY = "Y",
        Color = OxyColor.Parse("#4169E8"),
        Fill = OxyColor.FromArgb(30,0,0,255),
        StrokeThickness = 1,
        TrackerKey = "TrackerA",
        TrackerFormatString = "{0} {2} {4}",
        CanTrackerInterpolatePoints = false,
    };
    

    说明:

    1. AreaSeries就是线条下带填充色块的那种;
    2. pa是一个List,T类里有成员变量X和Y,以此指定x和y轴的数据来源;
    3. OxyColor.Parse和FromArgb是两种获取颜色的方法,后者第一个参数是透明度;
    4. TrackerFormatString设置的是tracker中字符串的格式。其中{0}是Series的Title,{1}是x轴的Title,{2}是x值,{3}是y轴的Title,{4}是y值;
    5. CanTrackerInterpolatePoints设为false,表示在没有数据点的位置,不另外计算数值。
    展开全文
  • WPF深度使用OxyPlot

    2021-09-27 17:49:24
    WPF深度使用OxyPlot 说明 OxyPlot是开源的Chart插件,支持WPF和WinForm,相比于Devpress、SCIChart、Toolkit、InteractiveDataDisplay.WPF、Lightningchart个人感觉更加完善和清楚,也不收费。 OxyPlot的实例Demo、...
  • OxyPlot.Wpf 图表控件使用备忘目录OxyPlot.Wpf 图表控件使用备忘一、OxyPlot.Wpf 控件信息二、基本概念(一) PlotView 和 Plot(二) PlotM...
  • 初识OxyPlot插件

    2020-06-07 13:19:27
    刚接触Oxyplot插件,觉得有点儿意思。第一次试验的一个小程序,分享一下。 private void button1_Click(object sender, EventArgs e) { var model = new PlotModel { Title = "图的标题" }; var scatterSeries =...
  • silverlight图表oxyplot例子--动态加载XAML
  • 一篇关于OxyPlot的使用记录

    千次阅读 2018-04-13 15:01:34
    官网链接1:点击打开链接(www.oxyplot.org)官网链接2:http://docs.oxyplot.org/en/latest/引用资源共享: 链接:https://pan.baidu.com/s/1r9ctsyEYW7_E7x-L99Op8w 密码:dfjm ps:项目添加引用后可直接使用1....
  • Install-Package Oxyplot Install-Package Oxyplot.Wpf Install-Package Oxyplot.Winform 官方文档:https://oxyplot.readthedocs.io/en/latest/index.html
  • Oxyplot实时绘图学习笔记(上)

    千次阅读 多人点赞 2020-06-21 21:57:26
    Oxyplot实时绘图学习笔记(上) (本项目来源于嵌入式系统实践课程项目) 目标 作为嵌入式系统的考核项目之一,我们小组选择项目是“基于串行通信的智能传感器和数据采集”,我们拟开发的项目是“基于智能传感器的...
  • 上一篇在:wpf-折线图绘制2-oxyplot-1 设置图本身 IsLegendVisible = true; // 是否显示图例 PlotAreaBorderColor = OxyColors.Transparent; // 图边界的颜色 这些都是model层的设置,对应上一篇,就是_viewModel....
  • 第一篇:基础、参考、简单设置 wpf-折线图绘制2-oxyplot-1 第二篇:图和轴设置、简单控制 wpf-折线图绘制2-oxyplot-2 这篇主要想写Annotation。 比如下图这种,浅蓝色bar就是RectangleAnnotation,可以为每个点都...
  • 最近公司要求展示一个曲线图的控件,网上查了一下,都比较推荐OxyPlot 开源的这个库,所以就研究了一下。记录一下 1.首先需要从NuGet上下载相关的NuGut包,搜索OxyPlot.Wpf,进行安装,并且我采用的mvvvmlight,顺便...

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 401
精华内容 160
关键字:

OxyPlot