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();
        }
    }
相关推荐
Magnum Lehar1 小时前
wpf游戏引擎的Components的entity组件实现
游戏引擎·wpf
Magnum Lehar8 小时前
wpf游戏引擎content/Asset.cs
游戏引擎·wpf
Magnum Lehar9 小时前
wpf游戏引擎下的Geometry实现
java·游戏引擎·wpf
Java Fans9 小时前
WPF调用Python心率监测脚本解决方案
开发语言·python·wpf
Magnum Lehar1 天前
wpf游戏引擎的script实现
游戏引擎·wpf
CPU不够了2 天前
window7 wpf程序打不开问题排查及处理
windows·.net·wpf
界面开发小八哥2 天前
界面组件DevExpress WPF中文教程:Grid - 如何获取行句柄?
wpf·界面控件·devexpress·ui开发·用户界面
YUNYINGXIA3 天前
Redis集群
redis·wpf
大霸王龙4 天前
系统模块与功能设计框架
人工智能·wpf
明耀5 天前
WPF DataGrid 默认显示行号
wpf