1.项目下载地址:https://gitee.com/confusedkitten/avalonia-demo
3.Charts库,LiveChartsCore.SkiaSharpView.Avalonia,Nuget获取只有预览库,也没找到别的啥好用的库
4.样式预览:
5.Charts.axaml
<UserControl xmlns="https://github.com/avaloniaui"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft.com/expression/blend/2008"xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"xmlns:lvc="using:LiveChartsCore.SkiaSharpView.Avalonia"xmlns:vm="using:AvaloniaDemo.ViewModels"x:Class="AvaloniaDemo.Pages.Charts"x:DataType="vm:StatisticsViewModel"><Grid RowDefinitions="*,*"><lvc:PieChart Name="ryredpie" FontFamily="Microsoft YaHei" Series="{Binding DamageSeries}" LegendPosition="Right" ></lvc:PieChart><lvc:CartesianChart Name="ryredaxisx" Grid.Row="1" Series="{Binding VictorySeries}" XAxes="{Binding VictoryXAxes}"><lvc:CartesianChart.Tooltip><vm:CustomTooltip></vm:CustomTooltip></lvc:CartesianChart.Tooltip></lvc:CartesianChart></Grid>
</UserControl>
6.Charts.axaml.cs
using Avalonia;
using Avalonia.Controls;
using Avalonia.Markup.Xaml;
using AvaloniaDemo.ViewModels;
using LiveChartsCore.Measure;
using LiveChartsCore.SkiaSharpView.Painting;
using LiveChartsCore.SkiaSharpView.SKCharts;
using SkiaSharp;
using System.Reflection.Metadata;namespace AvaloniaDemo.Pages;public partial class Charts : UserControl
{public const string DefaultFontFamily = "Microsoft YaHei";private StatisticsViewModel viewModel = new StatisticsViewModel(new SolidColorPaint(SKColor.Parse("#4992FF")));public Charts(){InitializeComponent();DataContext = viewModel;InitLegend();}/// <summary>/// 改图例字体,默认不支持中文/// </summary>private void InitLegend(){try{if (ryredpie.Legend is SKDefaultLegend skDefaultLegend){skDefaultLegend.TextSize = 13;if (skDefaultLegend.FontPaint is Paint paint){paint.FontFamily = Constant.DefaultFontFamily;paint.Color = new SKColor(255, 255, 255);}}if (ryredpie.Tooltip is SKDefaultTooltip skDefaultTooltip){if (skDefaultTooltip.FontPaint is Paint paint){paint.FontFamily = Constant.DefaultFontFamily;paint.Color = new SKColor(255, 255, 255);}}ryredpie.TooltipPosition = TooltipPosition.Right;}catch { }}
}
7.StatisticsViewModel.cs
using LiveChartsCore.Drawing;
using LiveChartsCore.SkiaSharpView.Painting;
using LiveChartsCore.SkiaSharpView;
using LiveChartsCore;
using SkiaSharp;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Reflection.Metadata;
using System.Text;
using System.Threading.Tasks;namespace AvaloniaDemo.ViewModels
{public class StatisticsViewModel : ViewModelBase{SolidColorPaint _solidColor { get; set; } = new SolidColorPaint(SKColor.Parse("#4992FF"));public StatisticsViewModel(SolidColorPaint solidColor){_solidColor = solidColor;((ColumnSeries<int>)_VictorySeries.ElementAt(0)).Fill = _solidColor;}public StatisticsViewModel(){}/// <summary>/// 饼图内径/// </summary>public static int PieInnerRadius = 35;#region 统计public Axis[] VictoryXAxes { get; set; } ={new Axis{Labels = new[] { "正常", "轻伤", "中伤", "重伤", "死亡" },LabelsRotation = 0,LabelsPaint=new SolidColorPaint(new SKColor(255, 255, 255)){FontFamily="Microsoft YaHei",},TextSize=12,LabelsAlignment=Align.Middle,TicksPaint = new SolidColorPaint(new SKColor(35, 35, 35)),TicksAtCenter = true,MinStep = 1,ForceStepToMin = true,}};private ObservableCollection<ISeries> _VictorySeries = new ObservableCollection<ISeries>{new ColumnSeries<int> { Values = new int[] { 5,8,1,0,9 },Fill= new SolidColorPaint(SKColor.Parse("#4992FF"))},};private ISeries[] _DamageSeries = new ISeries[]{new PieSeries<int> { Values = new int[] { 2 }, Name = "正常" ,InnerRadius=PieInnerRadius, Fill= new SolidColorPaint(SKColor.Parse("#008000"))},new PieSeries<int> { Values = new int[] { 4 }, Name = "轻伤" ,InnerRadius=PieInnerRadius, Fill= new SolidColorPaint(SKColor.Parse("#D2C86B")) },new PieSeries<int> { Values = new int[] { 1 }, Name = "中伤" ,InnerRadius=PieInnerRadius, Fill= new SolidColorPaint(SKColor.Parse("#03AEDE")) },new PieSeries<int> { Values = new int[] { 4 }, Name = "重伤" ,InnerRadius=PieInnerRadius, Fill= new SolidColorPaint(SKColor.Parse("#C13530")) },new PieSeries<int> { Values = new int[] { 3 }, Name = "死亡" ,InnerRadius=PieInnerRadius, Fill= new SolidColorPaint(SKColor.Parse("#696969")) }};public ObservableCollection<ISeries> VictorySeries{get => _VictorySeries;set => RaiseAndSetIfChanged(ref _VictorySeries, value);}public ISeries[] DamageSeries{get => _DamageSeries;set => RaiseAndSetIfChanged(ref _DamageSeries, value);}#endregion}
}