上篇文章介绍了如何使用折线图,这篇主要讲散点图,主要应用于数据瞄点
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;
}
}
}
}
}
效果图