WPF livecharts2 LineSeries binding to type struct contains x and y

public struct XYPoint
{
    public double X { get; set; }
    public double Y { get; set; }
}

public MainVM()
{
    LiveCharts.Configure(config =>
    {
        config.HasMap<XYPoint>((point, chartPt) =>
        {
            chartPt.PrimaryValue=point.Y;
            chartPt.SecondaryValue=point.X;
        });
    });
    InitData();
}

private void InitData()
{
    Random rnd = new Random();

    for (int i = 0; i<50; i++)
    {
        XYPointsCollection.Add(new XYPoint()
        {
            X = i+1,
            Y=rnd.Next(0, 100)
        });
    }

    SolidColorBrush blueBrush = Brushes.Blue;
    var lvcPaint = new SolidColorPaint(new SKColor(blueBrush.Color.R, blueBrush.Color.G, blueBrush.Color.B, blueBrush.Color.A));

    Series = new ISeries[]
    {
        new LineSeries<XYPoint>
        {
            Values = XYPointsCollection,
            Name = "XY Series",
            GeometrySize=30,
            GeometryFill=new SolidColorPaint(SKColors.Red),
            Stroke=new SolidColorPaint(SKColors.DarkBlue,10),                    
            GeometryStroke=new SolidColorPaint(SKColors.Yellow,2)                  
        }
    };
}

 

 

 

 

 

 

//xaml
<Window x:Class="WpfApp4.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:lvc="clr-namespace:LiveChartsCore.SkiaSharpView.WPF;assembly=LiveChartsCore.SkiaSharpView.WPF"
        xmlns:local="clr-namespace:WpfApp4"
        mc:Ignorable="d"
        Title="LiveCharts2" Height="450" Width="800"
        WindowState="Maximized">
    <Grid>
        <lvc:CartesianChart Series="{Binding Series}">
            
        </lvc:CartesianChart>
    </Grid>
</Window>



//cs
using LiveChartsCore;
using LiveChartsCore.SkiaSharpView;
using LiveChartsCore.SkiaSharpView.Painting;
using SkiaSharp;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Text;
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.Navigation;
using System.Windows.Shapes;

namespace WpfApp4
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            var vm = new MainVM();
            this.DataContext=vm;
        }
    }

    public class MainVM : INotifyPropertyChanged
    {
        public ObservableCollection<XYPoint> XYPointsCollection { get; set; } = new ObservableCollection<XYPoint>();

        private ISeries[] series;
        public ISeries[] Series
        {
            get
            {
                return series;
            }
            set
            {
                series = value;
                OnPropertyChanged();
            }
        }

        public MainVM()
        {
            LiveCharts.Configure(config =>
            {
                config.HasMap<XYPoint>((point, chartPt) =>
                {
                    chartPt.PrimaryValue=point.Y;
                    chartPt.SecondaryValue=point.X;
                });
            });
            InitData();
        }
        
        private void InitData()
        {
            Random rnd = new Random();

            for (int i = 0; i<50; i++)
            {
                XYPointsCollection.Add(new XYPoint()
                {
                    X = i+1,
                    Y=rnd.Next(0, 100)
                });
            }

            SolidColorBrush blueBrush = Brushes.Blue;
            var lvcPaint = new SolidColorPaint(new SKColor(blueBrush.Color.R, blueBrush.Color.G, blueBrush.Color.B, blueBrush.Color.A));

            Series = new ISeries[]
            {
                new LineSeries<XYPoint>
                {
                    Values = XYPointsCollection,
                    Name = "XY Series",
                    GeometrySize=30,
                    GeometryFill=new SolidColorPaint(SKColors.Red),
                    Stroke=new SolidColorPaint(SKColors.DarkBlue,10),                    
                    GeometryStroke=new SolidColorPaint(SKColors.Yellow,2)                  
                }
            };
        }

        public event PropertyChangedEventHandler? PropertyChanged;
        private void OnPropertyChanged([CallerMemberName] string propName = "")
        {
            var handler = PropertyChanged;
            if (handler!=null)
            {
                handler?.Invoke(this, new PropertyChangedEventArgs(propName));
            }
        }

    }

    public struct XYPoint
    {
        public double X { get; set; }
        public double Y { get; set; }
    }
}

 

 

 

 

 

 

 

posted @ 2025-07-15 23:13  FredGrit  阅读(28)  评论(0)    收藏  举报