根据群组数量从列表中均匀选择?

时间:2017-04-12 13:01:17

标签: c#

我有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,订购&amp;按双重值分组:

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

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

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

2 个答案:

答案 0 :(得分:2)

您有以下情况(适用于3,4和5组):

  

0 --- x i --- 19
  0 --- x i --- x ii --- 19
  0 --- x i --- x ii --- x iii --- 19
  等。

其中---是偶数(ish)间隙,x n 是您要查找的数字。

这样做,很明显,当有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;
}

答案 1 :(得分:1)

解决此问题的方法之一是计算组的预期颜色之间的距离。希望它有所帮助!

public SortedDictionary<Guid, Color> GetColors(IEnumerable<Tuple<Guid, double>> heightsAboveGroundByGuid)
{
        // Order providers by measurement height from ground
        var groups = heightsAboveGroundByGuid.OrderBy(h => h.Item2).GroupBy(x => x.Item2).ToList();

        var colorsByGuid = new SortedDictionary<Guid, Color>
        {
            [groups.First().First().Item1] = _colors.First(),
            [groups.Last().First().Item1] = _colors.Last()
        };

        if (groups.Count < 3) return colorsByGuid;

        var distance = _colors.Count / (groups.Count - 1);
        for (var step = 1; step < groups.Count - 1; step++)
        {
            var groupId = groups[step].First().Item1;
            colorsByGuid[groupId] = _colors.Skip(distance * step).First();
        }

        return colorsByGuid;
    }