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();
        }
    }
相关推荐
△曉風殘月〆20 小时前
WPF Prism中的MVVM实现
wpf·mvvm
量子物理学20 小时前
.NET8 中 WPF与ScottPlot 报表 的完美结合
.net·wpf
△曉風殘月〆21 小时前
WPF Prism区域导航功能详解
wpf·mvvm
星河Cynthia2 天前
WPF基于resx资源文件的多语言实现
c#·wpf
量子物理学2 天前
WPF 标签预览可以显示图片运行后不显示
c#·wpf
△曉風殘月〆2 天前
WPF Prism中的依赖注入详解
wpf·mvvm
△曉風殘月〆2 天前
WPF Prism创建Bootstrapper/启动器
wpf·mvvm
小曹要微笑2 天前
WPF的依赖与附加属性
wpf·依赖属性·附加属性
bugcome_com3 天前
WPF 命令 ICommand 从原理到实战
后端·wpf·icommand
武藤一雄4 天前
WPF处理耗时操作的7种方法
microsoft·c#·.net·wpf