愿你出走半生,归来仍是少年!
环境:.NET 7、MAUI
话接上回(9.单柱状图(SingleBarChart)),从单柱拓展到多柱状图。
1.数据设置
private void InitValue(List<BasicSerieDto> dtos)
{
Series = new ISeries[dtos.Count];
for (int i = 0; i < dtos.Count; i++)
{
Series[i] = dtos[i].ToMuliBar();
}
chart.SetBinding(CartesianChart.SeriesProperty, new Binding("Series"));
}
2.使用
public partial class MuliBarChartDemo : ContentPage
{
private class Fruit
{
public string name { get; set; }
public string WeekDay { get; set; }
public double Money { get; set; }
}
public MuliBarChartDemo()
{
InitializeComponent();
#region 原始数据
List<Fruit> apples = new List<Fruit>();
for (int i = 0; i < 7; i++)
{
apples.Add(new Fruit()
{
WeekDay = "周" + i,
Money = (new Random()).NextDouble() * 10000,
name = "苹果"
});
}
List<Fruit> pears = new List<Fruit>();
for (int i = 0; i < 7; i++)
{
pears.Add(new Fruit()
{
WeekDay = "周" + i,
Money = (new Random()).NextDouble() * 10000,
name = "梨子"
});
}
List<Fruit> bananas = new List<Fruit>();
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<BasicSerieDto>()
{
appleDto,pearDto,bananaDto
}
);
}
}