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();
        }
    }
相关推荐
zzyzxb11 小时前
WPF中Adorner和Style异同
wpf
棉晗榜13 小时前
WPF锚点页面,点击跳转到指定区域
wpf
zzyzxb13 小时前
Style/Setter、Template 属性、ControlTemplate 三者的关系
wpf
要记得喝水13 小时前
某公司WPF面试题(含答案和解析)--2
wpf
zzyzxb14 小时前
WPF中Template、Style、Adorner异同
wpf
小股虫1 天前
数据一致性保障:从理论深度到架构实践的十年沉淀
架构·wpf
廋到被风吹走1 天前
【Spring】PlatformTransactionManager详解
java·spring·wpf
源之缘-OFD先行者1 天前
全栈开发实战:WPF+FFmpeg+GIS,打造工业级雷达探测终端
ffmpeg·wpf
Poetinthedusk1 天前
WPF动画制作分享
wpf·动画
张人玉2 天前
WPF HTTPS 通信示例使用说明
数据库·网络协议·http·c#·wpf