WPF 控件库Live Charts 散点图

本文详细介绍了如何在WPF中使用LiveCharts库创建散点图,展示了XAML和后台CS代码示例,包括设置坐标轴范围、数据点及动态更新数据的方法。通过实例,读者可以了解到如何利用LiveCharts进行数据可视化,适用于数据展示和分析场景。
摘要由CSDN通过智能技术生成

上篇文章介绍了如何使用折线图,这篇主要讲散点图,主要应用于数据瞄点

xaml代码

<Window x:Class="WpfApplication3.PointWindow"
        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:WpfApplication3"
        mc:Ignorable="d"
        xmlns:lvc="clr-namespace:LiveCharts.Wpf;assembly=LiveCharts.Wpf"
        Title="PointWindow" Height="300" Width="300">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"></RowDefinition>
            <RowDefinition Height="*"></RowDefinition>
        </Grid.RowDefinitions>
        <Button Grid.Row="0" Click="UpdateAllOnClick">
            Move All
        </Button>
        <lvc:CartesianChart Grid.Row="1"  Series="{Binding SeriesCollection}" Height="200" Width="200">
            <lvc:CartesianChart.AxisY>
                <lvc:Axis Unit="1" MinValue="-5" MaxValue="5" Labels="{Binding labels}">
                    <lvc:Axis.Separator>
                        <lvc:Separator  Visibility="Collapsed" Step="1"></lvc:Separator>
                    </lvc:Axis.Separator>
                </lvc:Axis> 
            </lvc:CartesianChart.AxisY>
            <lvc:CartesianChart.AxisX>
                <lvc:Axis Unit="1" MinValue="-5"  MaxValue="5" IsEnabled="False" Labels="{Binding labels}">
                    <lvc:Axis.Separator>
                        <lvc:Separator  Visibility="Collapsed" Step="1"></lvc:Separator>
                    </lvc:Axis.Separator>
                </lvc:Axis>
            </lvc:CartesianChart.AxisX>
        </lvc:CartesianChart>
    </Grid>
</Window>

后台.cs代码

using LiveCharts;
using LiveCharts.Defaults;
using LiveCharts.Wpf;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace WpfApplication3
{
    /// <summary>
    /// PointWindow.xaml 的交互逻辑
    /// </summary>
    public partial class PointWindow : Window
    {
        public ObservableCollection<int> labels { get; set; }
        public PointWindow()
        {
            InitializeComponent();
            labels = new ObservableCollection<int>();
            for (int i = 0; i < 5; i++)
            {
                labels.Add(i);
            }
            SeriesCollection = new SeriesCollection
            {
                new ScatterSeries
                {
                    Values = new ChartValues<ScatterPoint>
                    {
                        new ScatterPoint(-1, 2, 1),
                        new ScatterPoint(3, -4, 1),
                        new ScatterPoint(2, 2, 1),
                        new ScatterPoint(-2,-3, 1),
                        new ScatterPoint(4, 2, 1)
                    },
                    MinPointShapeDiameter = 3,  //控制泡泡大小
                    MaxPointShapeDiameter = 3
                },
                new ScatterSeries
                {
                    Values = new ChartValues<ScatterPoint>
                    {
                        new ScatterPoint(-2, 2, 1),
                        new ScatterPoint(2, -1, 1),
                        new ScatterPoint(-1, 1, 1),
                        new ScatterPoint(4, -3, 1),
                        new ScatterPoint(-3, 5, 1)
                    }, 
                    MinPointShapeDiameter = 9,
                    MaxPointShapeDiameter = 9
                }
            };

            DataContext = this;
        }


        public SeriesCollection SeriesCollection { get; set; }

        private void UpdateAllOnClick(object sender, RoutedEventArgs e)
        {
            var r = new Random();
            foreach (var series in SeriesCollection)
            {
                foreach (var bubble in series.Values.Cast<ScatterPoint>())
                {
                    bubble.X = r.NextDouble() * 10;
                    bubble.Y = r.NextDouble() * 10;
                    bubble.Weight = r.NextDouble() * 10;
                }
            }
        }
    }
}

效果图

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

这个月太忙没时间看C++

你的鼓励将是我创作的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值