WPF中使用LiveCharts绘制散点图

一、背景

这里的代码使用MVVM模式进行编写

二、Model

复制代码
public class DataPoint
    {
        public double X { get; set; }
        public double Y { get; set; }
    }

三、ViewModel

cs 复制代码
public class ScatterChartViewModel
    {


        public SeriesCollection Series { get; set; }


        public ScatterChartViewModel()
        {
            //初始化数据
            var dataPoints = new List<DataPoint>
            {
                new DataPoint { X= 1, Y= 10 },
                new DataPoint { X= 2, Y= 20 },
                new DataPoint { X= 3, Y= 15 },
            };


            Series = new SeriesCollection()
            {
                new ScatterSeries
                {
                    Title = "Data",
                    Values = new ChartValues<ObservablePoint>(posPoints.Select(dp => new ObservablePoint(dp.X, dp.Y)))
                }
            };        
        }  

    }

四、View

cs 复制代码
<Window x:Class="DisplayData.Views.MainWindow"
        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:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:DisplayData.Views"
        mc:Ignorable="d"
        xmlns:lvc="clr-namespace:LiveCharts.Wpf;assembly=LiveCharts.Wpf"
        Title="Data" Height="900" Width="1200" WindowStartupLocation="CenterScreen">
    <Grid>          
            <lvc:CartesianChart Series="{Binding Series}" BorderBrush="#7ADA95" BorderThickness="1">
            </lvc:CartesianChart>       
    </Grid>
</Window>
cs 复制代码
public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            this.DataContext = new ScatterChartViewModel();
        }
    }
相关推荐
Chris _data13 小时前
WPF 上位机开发学习笔记 - 第四天
笔记·学习·wpf
李高钢1 天前
用 JetBrains Rider 写 WPF 的 15 个高效技巧:从 VS 转过来也值得看
wpf
李高钢2 天前
【WPF】MVVM 与数据绑定实战:从原理到完整项目
wpf
李高钢2 天前
【WPF】从 Avalon 到 .NET 10:WPF 平台演进史与特性全览
.net·wpf
李高钢3 天前
WPF MVVM Light 入门:从零到Hello World
c#·wpf
李高钢3 天前
WPF MVVM Light(三):RelayCommand 命令与 Messenger 消息
前端·c#·wpf
SamChan904 天前
FastAPI+Celery+Redis搭建企业级PDF翻译异步任务系统
redis·后端·python·pdf·wpf·fastapi
李高钢4 天前
C# WPF Prism 进阶(三):导航(Navigation)深入
开发语言·c#·wpf
李高钢5 天前
C# WPF 的 Prism 框架入门:核心概念与一个完整示例
开发语言·c#·wpf
SamChan905 天前
用Playwright端到端测试PDF翻译功能:Web自动化测试实战
前端·python·ai·pdf·wpf