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();
        }
    }
相关推荐
没有bug.的程序员11 小时前
SOA、微服务、分布式系统的区别与联系
java·jvm·微服务·架构·wpf·日志·gc
Macbethad12 小时前
基于WPF的半导体设备配方管理程序技术方案
wpf
FuckPatience12 小时前
WPF Geometry
wpf
武藤一雄1 天前
.NET 中常见计时器大全
microsoft·微软·c#·.net·wpf·.netcore
MarkHD2 天前
车辆TBOX科普 第70次 AUTOSAR Adaptive、容器化与云原生的融合革命
云原生·wpf
极客智造2 天前
WPF Behavior 实战:自定义 InvokeCommandAction 实现事件与命令解耦
wpf
L、2182 天前
Flutter 与 OpenHarmony 深度集成:构建分布式多端协同应用
分布式·flutter·wpf
布伦鸽2 天前
C# WPF -MaterialDesignTheme 找不到资源“xxx“问题记录
开发语言·c#·wpf
小二·2 天前
MyBatis基础入门《十五》分布式事务实战:Seata + MyBatis 实现跨服务数据一致性
分布式·wpf·mybatis
helloworddm2 天前
UnregisterManyAsync
wpf