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();
        }
    }
相关推荐
Poetinthedusk21 小时前
WPF获得当前电脑的储存和运存
wpf
unicrom_深圳市由你创科技21 小时前
Qt、MFC、WinForm、WPF,哪个做上位机界面更好?
qt·wpf·mfc
暮雪倾风2 天前
【WPF】使用Costura.Fody将工程打包为单个EXE文件
wpf·exe·windows原生开发
咖啡の猫2 天前
Jedis快速入门
wpf
Scout-leaf2 天前
WPF新手村教程(五)— 附魔教学(绑定)
c#·wpf
数据知道3 天前
MongoDB灾难恢复计划:RTO/RPO目标下的应急响应完整方案
数据库·mongodb·wpf
闻哥4 天前
深入剖析Redis数据类型与底层数据结构
java·jvm·数据结构·spring boot·redis·面试·wpf
yatum_20144 天前
Hadoop 三种核心运行模式(伪分布式/分布式/混合模式)全总结
hadoop·分布式·wpf
有技巧搬砖4 天前
基于WPF MVVM的流程编排状态机引擎上位机
wpf·状态机·流程步骤
Wiktok4 天前
WPF核心UI组件的功能、使用场景和基础示例
ui·wpf