WPF LiveChart使用案例
首先使用Nuget下载LiveChart,然后添加引用;
效果:(其中右侧引用了LiveCharts.Geared.dll,效率更高)
xaml代码:
<Window x:Class="WPFChart.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:lvc="clr-namespace:LiveCharts.Wpf;assembly=LiveCharts.Wpf"
Title="MainWindow" Height="700" Width="1000">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="80"></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
<Button Width="120" Height="30" Content="Chart 1" Click="Button_Click"></Button>
<Button Width="120" Height="30" Content="Chart 2" Click="Button_Click_1"></Button>
</StackPanel>
<Grid Grid.Row="1">
<Grid.ColumnDefinitions>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions>
<lvc:CartesianChart x:Name="c1">
</lvc:CartesianChart>
<lvc:CartesianChart x:Name="c2" Grid.Column="1">
</lvc:CartesianChart>
</Grid>
</Grid>
</Window>
后台主程序代码:
using LiveCharts;
using LiveCharts.Geared;
using LiveCharts.Wpf;
using System;
using System.Collections.Generic;
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.Navigation;
using System.Windows.Shapes;
namespace WPFChart
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
public SeriesCollection series1 { get; set; }
public SeriesCollection series2 { get; set; }
private void Button_Click(object sender, RoutedEventArgs e)
{
ChartValues<double> arr = new ChartValues<double>();
for (int i = 0; i < 1000;i++ )
{
arr.Add(i);
}
series1 = new SeriesCollection();
series1.Add(new LineSeries()
{
Values = arr
});
c1.Series = series1;
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
GearedValues<double> arr = new GearedValues<double>();
for (int i = 0; i < 1000; i++)
{
arr.Add(i);
}
series2 = new SeriesCollection();
series2.Add(new GLineSeries()
{
Values = arr
});
c2.Series = series2;
}
}
}