gpt4 book ai didi

c# - 根据组数从列表中均匀选择?

转载 作者:太空宇宙 更新时间:2023-11-03 23:00:20 25 4
gpt4 key购买 nike

我有一个包含 20 个项目的颜色列表:

this.colors = new List<Color> {
(Color)ColorConverter.ConvertFromString("#DEEBF7"),
(Color)ColorConverter.ConvertFromString("#D2E1EF"),
(Color)ColorConverter.ConvertFromString("#C7D7E8"),
(Color)ColorConverter.ConvertFromString("#BCCDE0"),
(Color)ColorConverter.ConvertFromString("#B0C3D9"),
(Color)ColorConverter.ConvertFromString("#A5B9D2"),
(Color)ColorConverter.ConvertFromString("#9AAFCA"),
(Color)ColorConverter.ConvertFromString("#8FA6C3"),
(Color)ColorConverter.ConvertFromString("#839CBC"),
(Color)ColorConverter.ConvertFromString("#7892B4"),
(Color)ColorConverter.ConvertFromString("#6D88AD"),
(Color)ColorConverter.ConvertFromString("#627EA5"),
(Color)ColorConverter.ConvertFromString("#56749E"),
(Color)ColorConverter.ConvertFromString("#4B6B97"),
(Color)ColorConverter.ConvertFromString("#40618F"),
(Color)ColorConverter.ConvertFromString("#355788"),
(Color)ColorConverter.ConvertFromString("#294D81"),
(Color)ColorConverter.ConvertFromString("#1E4379"),
(Color)ColorConverter.ConvertFromString("#133972"),
(Color)ColorConverter.ConvertFromString("#08306B")
};

我将一个列表传递给 GetColors,按 double 值排序和分组:

public SortedDictionary<Guid, Color> GetColors(IList<Tuple<Guid, double>> heightsAboveGroundByGuid)
{
// Order providers by measurement height from ground
IEnumerable<IGrouping<double, Tuple<Guid, double>>> groupedOrderedList = heightsAboveGroundByGuid.OrderBy(h => h.Item2).GroupBy(o => o.Item2);

int numberOfGroups = groupedOrderedList.Count();

SortedDictionary<Guid, Color> seriesColorsByGuid = new SortedDictionary<Guid, Color>();

int index = 0;
foreach (IGrouping<double, Tuple<Guid, double>> item in groupedOrderedList)
{
if (index <= this.colors.Count() - 1)
{
foreach (Tuple<Guid, double> childItem in item)
{
seriesColorsByGuid.Add(childItem.Item1, this.colors[index]);
}
}

index = Not sure how to implement this!;
}

return seriesColorsByGuid;
}

例如,在运行时传入的 double 值是 40、40、40、50、50、60、60。因此,我有 3 个小组。在这种情况下,我希望第一组 (40) 返回索引为 0 的颜色,第二组 (50) 索引为 9,最后一组 (60) 索引为 19(列表中的最后一种颜色)。

如何根据传入的组数设置均匀间隔的颜色?

最佳答案

你有以下情况(3、4、5组):

0 --- xi ---19
0 --- xi ---xii --- 19
0 --- xi ---xii --- xiii --- 19
etc..

其中 --- 是偶数(左右)间隙,xn 是您要查找的数字。

这样布局,显然有 3 个组时有 2 个间隙,有 4 个组时有 3 个间隙,有 n 个组时有 n-1 个间隙。因此,您可以将颜色数除以组数,每次都将其添加到索引中。以下代码应该可以满足您的需求。我在每一行新代码之前添加了注释:

public SortedDictionary<Guid, Color> GetSeriesColors(IList<Tuple<Guid, double>> heightsAboveGroundByGuid)
{
IEnumerable<IGrouping<double, Tuple<Guid, double>>> groupedOrderedList = heightsAboveGroundByGuid.OrderBy(h => h.Item2).GroupBy(o => o.Item2);

int numberOfGroups = groupedOrderedList.Count();

SortedDictionary<Guid, Color> seriesColorsByGuid = new SortedDictionary<Guid, Color>();

int index = 0;

//calculate the size of the gaps.
float gap = (this.colors.Count() - 1) / (float)(numberofGroups - 1);
//keep track of how many multiples of gap we need
int gapsUsed = 1;

foreach (IGrouping<double, Tuple<Guid, double>> item in groupedOrderedList)
{
if (index <= this.colors.Count() - 1)
{
foreach (Tuple<Guid, double> childItem in item)
{
seriesColorsByGuid.Add(childItem.Item1, this.colors[index]);
}
}
//set the index to the gap * gapsUsed and cast it to an int
index = (int)(gap * gapsUsed);
//increment gaps used
gapsUsed++;
}

return seriesColorsByGuid;
}

关于c# - 根据组数从列表中均匀选择?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43370316/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com