Avalonia常用小控件Charts

1.项目下载地址:https://gitee.com/confusedkitten/avalonia-demo
2.UI库Semi.Avalonia,项目地址  https://github.com/irihitech/Semi.Avalonia

3.Charts库,LiveChartsCore.SkiaSharpView.Avalonia,Nuget获取只有预览库,也没找到别的啥好用的库

4.样式预览:

Avalonia常用小控件Charts_第1张图片

5.Charts.axaml


	
		
		
		
			
				
			
		
	

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

    /// 
    /// 改图例字体,默认不支持中文
    /// 
    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)_VictorySeries.ElementAt(0)).Fill = _solidColor;
        }

        public StatisticsViewModel()
        {
        }
        /// 
        /// 饼图内径
        /// 
        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 _VictorySeries = new ObservableCollection
         {
             new ColumnSeries { Values = new int[] { 5,8,1,0,9 },Fill=  new SolidColorPaint(SKColor.Parse("#4992FF"))},
         };

        private ISeries[] _DamageSeries = new ISeries[]
         {
             new PieSeries { Values = new int[] { 2 }, Name = "正常" ,InnerRadius=PieInnerRadius, Fill=  new SolidColorPaint(SKColor.Parse("#008000"))},
             new PieSeries { Values = new int[] { 4 }, Name = "轻伤" ,InnerRadius=PieInnerRadius, Fill=  new SolidColorPaint(SKColor.Parse("#D2C86B")) },
             new PieSeries { Values = new int[] { 1 }, Name = "中伤" ,InnerRadius=PieInnerRadius, Fill=  new SolidColorPaint(SKColor.Parse("#03AEDE")) },
             new PieSeries { Values = new int[] { 4 }, Name = "重伤" ,InnerRadius=PieInnerRadius, Fill=  new SolidColorPaint(SKColor.Parse("#C13530")) },
             new PieSeries { Values = new int[] { 3 }, Name = "死亡" ,InnerRadius=PieInnerRadius, Fill=  new SolidColorPaint(SKColor.Parse("#696969")) }
         };

        public ObservableCollection VictorySeries
        {
            get => _VictorySeries;
            set => RaiseAndSetIfChanged(ref _VictorySeries, value);
        }

        public ISeries[] DamageSeries
        {
            get => _DamageSeries;
            set => RaiseAndSetIfChanged(ref _DamageSeries, value);
        }

        #endregion
    }
}

你可能感兴趣的:(Avalonia,Avalonia,Chart)