Recently, I took over a small project and needed to make a large data display screen. It should be able to display curves and the volume of imports and exports. The key point is to look good. I don't know what looks good, so I just do it casually.

数据都是假的,只是做出一个效果。显示这种数据图,使用的是LiveCharts2。这是一个非常漂亮的 Charts 模块。官网如下:


LiveCharts2支持的还挺多,我们在这里选择WPF。
如果我们直接通过NuGet进行搜索,会发现找到的LiveCharts版本较旧。

LiveCharts2相对V0版本做了很多改进和修复,通过NuGet安装LiveChartsCore.SkiaSharpView.WPF。

Or open the Package Manager console and enter:
Install-Package LiveChartsCore.SkiaSharpView.WPF -Version 2.0.0-rc2

Wait until the installation is complete and you can find it.

顺便安装一下CommunityToolkit.Mvvm,这个可以通过NuGet找到,这个包是因为GalaSoft.MVVMLight已经不再维护,所以官方做的新模型,可以很好的迁移进来。
新建一个MainViewModel,添加引用,增加代码。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using LiveChartsCore;
using LiveChartsCore.SkiaSharpView;
using CommunityToolkit.Mvvm.ComponentModel;
namespace LiveChartsDemo
{
[ObservableObject]
public partial class MainViewModel
{
public ISeries[] Series { get; set; }
= new ISeries[]
{
new LineSeries<double>
{
Values = new double[] { 2, 1, 3, 5, 3, 4, 6 },
Fill = null
}
};
}
}
然后到MainWindow.xaml里,引用LiveCharts,设置DataContext。
xmlns:lvc="clr-namespace:LiveChartsCore.SkiaSharpView.WPF;assembly=LiveChartsCore.SkiaSharpView.WPF"
<Window.DataContext>
<local:MainViewModel />
</Window.DataContext>
然后增加CartesianChart
<Grid>
<lvc:CartesianChart Series="{Binding Series}" />
</Grid>

LineSeries为设置的线形图,当然还能有柱状图。
public ISeries[] Series { get; set; }
= new ISeries[]
{
new ColumnSeries<double>
{
Values = new double[] { 2, 1, 3, 5, 3, 4, 6 },
},
new LineSeries<double>
{
Values = new double[] { 2, 1, 3, 5, 3, 4, 6 },
Fill = null
},
};

You can also add a Title to explain the role of the icon.
public LabelVisual Title { get; set; }
= new LabelVisual
{
Text = "Title",
TextSize=20,
Paint = new SolidColorPaint(SKColors.Black)
};
<lvc:CartesianChart Series="{Binding Series}" Title="{Binding Title}"/>

There is a problem here. If your Title is set to Chinese,
public LabelVisual Title { get; set; }
= new LabelVisual
{
Text = "标题",
TextSize=20,
Paint = new SolidColorPaint(SKColors.Black)
};
it won't be displayed.

We need to modify the settings to support Chinese display.
public LabelVisual Title { get; set; }
= new LabelVisual
{
Text = "标题",
TextSize=20,
Paint = new SolidColorPaint {
Color = SKColors.Black,
SKTypeface = SKFontManager.Default.MatchCharacter('汉')
},
};

The official documents are very rich and there are corresponding examples, and the results are still very good.
** Partial effect 1: **

** Partial effect 2: **

** Partial effect 3: **

** Partial effect 4: **

** Partial effect 5: **

** Partial effect 6: **

据说免费的LiveCharts在数据量超过 10000 渲染效果会不好,需要使用付费版。这个我还没有验证,读者可以自行尝试一下。