.NET開源、功能強大、跨平臺的圖表庫 - LiveCharts2

China Soft發表於2024-05-07

https://www.cnblogs.com/Can-daydayup/p/18166862

前言

今天大姚給大家分享一個.NET開源(MIT License)、功能強大、簡單、靈活、跨平臺的圖表、地圖和儀表庫:LiveCharts2。

專案介紹

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

專案原始碼

Blazor Wasm中快速使用

建立Blazor WebAssembly專案

安裝NuGet

NuGet包管理器中搜尋:LiveChartsCore.SkiaSharpView.Blazor 點選安裝。
注意:該包目前仍處於預發行階段,尚未有正式版,很多同學反饋說找不到,是因為沒有勾選:包括預發行版

Basic Bars

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

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();
}

專案更多圖表截圖

專案原始碼地址

更多專案實用功能和特性歡迎前往專案開源地址檢視👀,別忘了給專案一個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

DotNetGuide技術社群交流群

  • DotNetGuide技術社群是一個面向.NET開發者的開源技術社群,旨在為開發者們提供全面的C#/.NET/.NET Core相關學習資料、技術分享和諮詢、專案框架推薦、求職和招聘資訊、以及解決問題的平臺。
  • 在DotNetGuide技術社群中,開發者們可以分享自己的技術文章、專案經驗、學習心得、遇到的疑難技術問題以及解決方案,並且還有機會結識志同道合的開發者。
  • 我們致力於構建一個積極向上、和諧友善的.NET技術交流平臺。無論您是初學者還是有豐富經驗的開發者,我們都希望能為您提供更多的價值和成長機會。

相關文章