.NET开源、功能强大、跨平台的图表库

本文介绍了LiveCharts2,一个功能丰富、易于使用的.NET图表库,适用于多种平台如BlazorWebAssembly。展示了如何在Blazor项目中快速集成基本柱状图和带有延迟动画的图表示例,以及如何通过MVVM模式管理数据和视图。
摘要由CSDN通过智能技术生成

ee7868f02d6957720349287caf902d47.png

cd0e0f32ec726bb0c4efa494a5a78d90.jpeg

前言

今天大姚给大家分享一个.NET开源(MIT License)、功能强大、简单、灵活、跨平台的图表、地图和仪表库:LiveCharts2。

1bb54db7283c0c76e2c94bf0f26ba1da.gif

项目介绍

LiveCharts2是一个.NET开源、简单、灵活、交互式且功能强大的.NET图表、地图和仪表,现在几乎可以在任何地方运行如:Maui、Uno Platform、Blazor-wasm、WPF、WinForms、Xamarin、Avalonia、WinUI、UWP。

  • 在线API文档:https://livecharts.dev/docs/blazor/2.0.0-rc2/gallery

bdcb26607331c8b935f3884d9f1ea269.png

项目源代码

cbfaa5855cba67dd3d3bb77666ed457d.png

Blazor Wasm中快速使用

创建Blazor WebAssembly项目

e88586af30dafb413bdd5a766087e141.png

安装NuGet

LiveChartsCore.SkiaSharpView.Blazor

Basic Bars

824a6c537f065600074eac3316468c8a.png
View Model
using CommunityToolkit.Mvvm.ComponentModel;
using LiveChartsCore;
using LiveChartsCore.SkiaSharpView;
using LiveChartsCore.SkiaSharpView.Painting;
using SkiaSharp;

namespace ViewModelsSamples.Bars.Basic;

public partial class ViewModel : ObservableObject
{
    public ISeries[] Series { get; set; } =
    {
        new ColumnSeries<double>
        {
            Name = "Mary",
            Values = new double[] { 2, 5, 4 }
        },
        new ColumnSeries<double>
        {
            Name = "Ana",
            Values = new double[] { 3, 1, 6 }
        }
    };

    public Axis[] XAxes { get; set; } =
    {
        new Axis
        {
            Labels = new string[] { "Category 1", "Category 2", "Category 3" },
            LabelsRotation = 0,
            SeparatorsPaint = new SolidColorPaint(new SKColor(200, 200, 200)),
            SeparatorsAtCenter = false,
            TicksPaint = new SolidColorPaint(new SKColor(35, 35, 35)),
            TicksAtCenter = true,
            // By default the axis tries to optimize the number of 
            // labels to fit the available space, 
            // when you need to force the axis to show all the labels then you must: 
            ForceStepToMin = true, 
            MinStep = 1 
        }
    };
}
HTML
@page "/Bars/Basic"
@using LiveChartsCore.SkiaSharpView.Blazor
@using ViewModelsSamples.Bars.Basic

<CartesianChart
 Series="ViewModel.Series"
 XAxes="ViewModel.XAxes"
 LegendPosition="LiveChartsCore.Measure.LegendPosition.Right">
</CartesianChart>

@code {
 public ViewModel ViewModel { get; set; } = new();
}

Delayed Animations

2db294b123bbacd20a4aebb103ce931f.png
View model
using System;
using System.Collections.Generic;
using CommunityToolkit.Mvvm.ComponentModel;
using LiveChartsCore;
using LiveChartsCore.Drawing;
using LiveChartsCore.Kernel;
using LiveChartsCore.SkiaSharpView;
using LiveChartsCore.SkiaSharpView.Drawing.Geometries;

namespace ViewModelsSamples.Bars.DelayedAnimation;

public partial class ViewModel : ObservableObject
{
    public ViewModel()
    {
        var values1 = new List<float>();
        var values2 = new List<float>();

        var fx = EasingFunctions.BounceInOut; // this is the function we are going to plot
        var x = 0f;

        while (x <= 1)
        {
            values1.Add(fx(x));
            values2.Add(fx(x - 0.15f));
            x += 0.025f;
        }

        var columnSeries1 = new ColumnSeries<float>
        {
            Values = values1,
            Stroke = null,
            Padding = 2
        };

        var columnSeries2 = new ColumnSeries<float>
        {
            Values = values2,
            Stroke = null,
            Padding = 2
        };

        columnSeries1.PointMeasured += OnPointMeasured;
        columnSeries2.PointMeasured += OnPointMeasured;

        Series = new List<ISeries> { columnSeries1, columnSeries2 };
    }

    private void OnPointMeasured(ChartPoint<float, RoundedRectangleGeometry, LabelGeometry> point)
    {
        var perPointDelay = 100; // milliseconds
        var delay = point.Context.Entity.MetaData!.EntityIndex * perPointDelay;
        var speed = (float)point.Context.Chart.AnimationsSpeed.TotalMilliseconds + delay;

        point.Visual?.SetTransition(
            new Animation(progress =>
            {
                var d = delay / speed;

                return progress <= d
                    ? 0
                    : EasingFunctions.BuildCustomElasticOut(1.5f, 0.60f)((progress - d) / (1 - d));
            },
            TimeSpan.FromMilliseconds(speed)));
    }

    public List<ISeries> Series { get; set; }
}
HTML
@page "/Bars/DelayedAnimation"
@using LiveChartsCore.SkiaSharpView.Blazor
@using ViewModelsSamples.Bars.DelayedAnimation

<CartesianChart
 Series="ViewModel.Series">
</CartesianChart>

@code {
 public ViewModel ViewModel { get; set; } = new();
}

项目更多图表截图

507277fc25816e6e54f01cc21e7cdad6.png e008d8c365adbad6a8bee6d55ca2f26e.png 7c70bce5b681e825ade1aefb694b5185.png e5841f90c470f8b68f3df868df6fe904.png e582c1deef8d482ae9877546fabbec74.png 3f7f3f9f65449f7972f1c3533c45b9a9.png 6eda9b25ecf2ac2385af2dd2285526b4.png 6eb62d3b3a1515873eb7bb5c3bef99c7.png

项目源码地址

更多项目实用功能和特性欢迎前往项目开源地址查看👀,别忘了给项目一个Star支持💖。

  • https://github.com/beto-rodriguez/LiveCharts2

优秀项目和框架精选

该项目已收录到C#/.NET/.NET Core优秀项目和框架精选中,关注优秀项目和框架精选能让你及时了解C#、.NET和.NET Core领域的最新动态和最佳实践,提高开发工作效率和质量。坑已挖,欢迎大家踊跃提交PR推荐或自荐(让优秀的项目和框架不被埋没🤞)。

  • https://github.com/YSGStudyHards/DotNetGuide/blob/main/docs/DotNet/DotNetProjectPicks.md

71d29e49ece9f647f5387d3076e16f74.gif

6995975d0e8bfaa121660221c971f700.jpeg


学习是一个永无止境的过程,你知道的越多,你不知道的也会越多,在有限的时间内坚持每天多学一点,你一定能成为你想要成为的那个人。不积跬步无以至千里,不积小流无以成江河!!!

bac5a4349b9ff08b24c1365e5846ead3.gif

See you next good day

5e933ad2a75d9c7a37b6eb616585459f.gif

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值