WPF编程,Live Charts使用说明(17)——散点图

LiveCharts中的每个散点图系列点都包含3个属性,即X,Y和权重,每个点的大小将根据该点的权重而变化,在这种情况下,所有点都具有相同的权重,请参见Bubbles示例以了解如何应用weight属性

在这里插入图片描述

后台:

using System;
using System.Windows;
using System.Windows.Controls;
using LiveCharts;
using LiveCharts.Defaults;
 
namespace Wpf.CartesianChart.ScatterPlot
{
    /// <summary>
    /// Interaction logic for ScatterExample.xaml
    /// </summary>
    public partial class ScatterExample : UserControl
    {
        public ScatterExample()
        {
            InitializeComponent();
 
            var r = new Random();
            ValuesA = new ChartValues<ObservablePoint>();
            ValuesB = new ChartValues<ObservablePoint>();
            ValuesC = new ChartValues<ObservablePoint>();
 
            for (var i = 0; i < 20; i++)
            {
                ValuesA.Add(new ObservablePoint(r.NextDouble()*10, r.NextDouble()*10));
                ValuesB.Add(new ObservablePoint(r.NextDouble()*10, r.NextDouble()*10));
                ValuesC.Add(new ObservablePoint(r.NextDouble()*10, r.NextDouble()*10));
            }
 
            DataContext = this;
        }
 
        public ChartValues<ObservablePoint> ValuesA { get; set; }
        public ChartValues<ObservablePoint> ValuesB { get; set; }
        public ChartValues<ObservablePoint> ValuesC { get; set; }
 
        private void RandomizeOnClick(object sender, RoutedEventArgs e)
        {
            var r = new Random();
            for (var i = 0; i < 20; i++)
            {
                ValuesA[i].X = r.NextDouble()*10;
                ValuesA[i].Y = r.NextDouble()*10;
                ValuesB[i].X = r.NextDouble()*10;
                ValuesB[i].Y = r.NextDouble()*10;
                ValuesC[i].X = r.NextDouble()*10;
                ValuesC[i].Y = r.NextDouble()*10;
            }
        }
    }
}

前台:

<UserControl x:Class="Wpf.CartesianChart.ScatterPlot.ScatterExample"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:Wpf.CartesianChart.ScatterPlot"
             xmlns:lvc="clr-namespace:LiveCharts.Wpf;assembly=LiveCharts.Wpf"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300" d:DataContext="{d:DesignInstance local:ScatterExample}">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"></RowDefinition>
            <RowDefinition Height="*"></RowDefinition>
        </Grid.RowDefinitions>
        <Button Grid.Row="0" Margin="10" Click="RandomizeOnClick">Randomize</Button>
        <lvc:CartesianChart Grid.Row="1" LegendLocation="Bottom">
            <lvc:CartesianChart.Series>
                <lvc:ScatterSeries Title="Series A" Values="{Binding ValuesA}" />
                <lvc:ScatterSeries Title="Series B" Values="{Binding ValuesB}"
                                   PointGeometry="{x:Static lvc:DefaultGeometries.Diamond}" />
                <lvc:ScatterSeries Title="Series C" Values="{Binding ValuesC}"
                                   PointGeometry="{x:Static lvc:DefaultGeometries.Triangle}"
                                   StrokeThickness="2" Fill="Transparent"/>
            </lvc:CartesianChart.Series>
            <lvc:CartesianChart.AxisY>
                <!--setting the axis unit improved the labels rounding rule-->
                <lvc:Axis Unit="1"></lvc:Axis>
            </lvc:CartesianChart.AxisY>
        </lvc:CartesianChart>
    </Grid>
</UserControl>

版权声明:本文为qq_43307934原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。