WPF displays a data chart (LiveCharts2)

WPF displays a data chart (LiveCharts2)

LiveCharts is a data visualization library for. Net that runs across multiple devices and frameworks

最后更新 11/15/2023 9:58 AM
奔跑的皮卡峰
预计阅读 3 分钟
分类
WPF
标签
.NET C# WPF LiveChart

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版本较旧。

旧版本NuGet搜索结果

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

新版本NuGet搜索结果

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 渲染效果会不好,需要使用付费版。这个我还没有验证,读者可以自行尝试一下。

Keep Exploring

延伸阅读

更多文章
同分类 / 同标签 1/26/2025

WPF internationalizes with custom XML files

This article describes in detail the methods of using custom XML files to achieve internationalization in WPF programs, including installing the necessary NuGet package, dynamically obtaining language lists, dynamically switching languages, using translation strings in code and xaml interfaces, etc. It also provides source code links to help developers easily internationalize WPF applications.

继续阅读