WinUI3 Chart (chart) - pie chart

1. Install the package LiveChartCore.SkiaSharpView.WinUI, click the "Package Manager Console" in the picture below.
Insert image description here
2. Enter NuGet\Install-Package LiveChartsCore.SkiaSharpView.WinUI -Version 2.0.0-beta.911 after PM in the picture below
. Reference URL: https://www.lvcharts.com/docs/winui/2.0.0-beta.850/gallery
!Insert image description here

3.
Bind xmlns in Veiw
:lvc="using:LiveChartsCore.SkiaSharpView.WinUI"
<lvc:PieChart
Series="{Binding SeriesCartesian}"
>
In ViewModel: Note that msyh.ttc is downloaded from the font in the computer system Microsoft Yahei font, use it to support Chinese fonts, put it into the generated Debug folder
void ShowCartesianChart()
{ var typeface = SKTypeface.FromFile("msyh.ttc"); TextPaint = new SolidColorPaint() { Color = SKColors.DarkSlateGray, //SKTypeface = SKFontManager.Default.MatchCharacter('Chinese') SKTypeface = typeface,





    };
    SeriesCartesian = new ObservableCollection<ISeries> {
           new RowSeries<ObservableValue>
          {
            Values = new[] { new ObservableValue(43)},
            Name="张三",

            DataLabelsPaint=TextPaint,

            DataLabelsPosition=DataLabelsPosition.Middle,
            DataLabelsFormatter=value=>$"{value.Context.Series.Name}{value.Coordinate.PrimaryValue}"


           },
              new RowSeries<ObservableValue>
          {
          Values = new[] { new ObservableValue(23)},
          DataLabelsPaint=TextPaint,

            DataLabelsPosition=DataLabelsPosition.Middle,
            DataLabelsFormatter=value=>"李四"

           },
            };
            var xAxis = new Axis
    {
        Labeler = Labelers.Default,

        //Labels = new[] { "张三", "李四", "王五", "赵六", "测试" },
        //Name = "XXX",
        //LabelsPaint = TextPaint,
        //CrosshairLabelsPaint = TextPaint,
        //NamePaint = TextPaint,


    };


    XCartesianAxes = new List<Axis> { xAxis };

    //YCartesianAxes = new List<Axis> { new Axis { Labeler = Labelers.Currency, LabelsPaint = TextPaint } };
    YCartesianAxes = new List<Axis> { new Axis { IsVisible = false } };
    }

Guess you like

Origin blog.csdn.net/weixin_43780907/article/details/132302224