8.多折线图(MuliLineChart)

愿你出走半生,归来仍是少年! 

环境:.NET 7、MAUI 

        话接上回(7.单折线图(SingleLineChart)),多个类型的折线图。

1.数据设置

        通过BasicSerieDto的ToSingleLine方法直接获取LineSeries,然后获取集合给它塞进去,其他的和单线的一样。

  private void InitValue(List dtos)
  {
      Series = new ISeries[dtos.Count];

      for (int i = 0; i < dtos.Count; i++)
      {
          var dto = dtos[i];
           
          Series[i] = dto.ToMuliLine();
      }
       

      chart.SetBinding(CartesianChart.SeriesProperty, new Binding("Series"));
  }

2.使用

public partial class MuliLineChartDemo : ContentPage
{

    private class Fruit
    {
        public string name { get; set; }

        public string WeekDay { get; set; }

        public double Money { get; set; }
    }

    public MuliLineChartDemo()
    {
        InitializeComponent();

        #region 原始数据

        List apples = new List();

        for (int i = 0; i < 7; i++)
        {
            apples.Add(new Fruit()
            {
                WeekDay = "周" + i,
                Money = (new Random()).NextDouble() * 10000,
                name = "苹果"
            });

        }

        List pears = new List();

        for (int i = 0; i < 7; i++)
        {
            pears.Add(new Fruit()
            {
                WeekDay = "周" + i,
                Money = (new Random()).NextDouble() * 10000,
                name = "梨子"
            });

        }

        List bananas = new List();

        for (int i = 0; i < 7; i++)
        {
            bananas.Add(new Fruit()
            {
                WeekDay = "周" + i,
                Money = (new Random()).NextDouble() * 10000,
                name = "香蕉"
            });

        }


        #endregion

        var appleDto = new BasicSerieDto(apples[0].name, apples.Select(p => p.Money).ToList(), apples.Select(p => p.WeekDay + "\n" + Math.Round(p.Money, 2)).ToList());
        

        var pearDto = new BasicSerieDto(pears[0].name, pears.Select(p => p.Money).ToList(), pears.Select(p => p.WeekDay + "\n" + Math.Round(p.Money, 2)).ToList());

        var bananaDto = new BasicSerieDto(bananas[0].name, bananas.Select(p => p.Money).ToList(), bananas.Select(p => p.WeekDay + "\n" + Math.Round(p.Money, 2)).ToList());


        chart.BindData("一周营业额", apples.Select(p => p.WeekDay).ToList(), new List()
                {
                    appleDto,pearDto,bananaDto
                }
            );
         

    }
}

 3.效果

多类别折线效果

你可能感兴趣的:(#,Maui基础开发,c#,.net,MAUI)