ThinkGeo.com    |     Documentation    |     Premium Support

Help with Improving this code

I am trying get get specific pieces of this layer to be colored with different colors.is there a better way than using a counter and a bunch of if statements?


private Collection<ValueItem> colorSelect(Collection<Feature> allFeatures, string layercode,Collection<ValueItem> valueItems)
       {
           
           List<string> values = new List<string>();
           foreach (Feature f in allFeatures)
           {
               if (f.ColumnValues.ContainsKey(layercode))
               {
                   if (!values.Contains(f.ColumnValues[layercode].ToString()))
                   {
                       values.Add(f.ColumnValues[layercode].ToString());
                      
                   }
               }
 
           }
 
           string json = JsonConvert.SerializeObject(values);
           if (values.Count > 0)
           {
                
               int count = 0;
               foreach (string esnCode in values)
               {
                   //int count = 0;
                   if (count == 0)
                   {
                       ValueItem item = new ValueItem(esnCode, AreaStyles.CreateSimpleAreaStyle(GeoColor.StandardColors.Green, GeoColor.StandardColors.Black, 3));
                       valueItems.Add(item);
 
                   }
                   if (count == 1)
                   {
                       ValueItem item = new ValueItem(esnCode, AreaStyles.CreateSimpleAreaStyle(GeoColor.StandardColors.Pink, GeoColor.StandardColors.Black, 3));
                       valueItems.Add(item);
 
                   }
                   if (count == 2)
                   {
                       ValueItem item = new ValueItem(esnCode, AreaStyles.CreateSimpleAreaStyle(GeoColor.StandardColors.Blue, GeoColor.StandardColors.Black, 3));
                       valueItems.Add(item);
 
                   }
                   if (count == 3)
                   {
                       ValueItem item = new ValueItem(esnCode, AreaStyles.CreateSimpleAreaStyle(GeoColor.StandardColors.Yellow, GeoColor.StandardColors.Black, 3));
                       valueItems.Add(item);
 
                   }
                   if (count == 4)
                   {
                       ValueItem item = new ValueItem(esnCode, AreaStyles.CreateSimpleAreaStyle(GeoColor.StandardColors.Violet, GeoColor.StandardColors.Black, 3));
                       valueItems.Add(item);
 
                   }
                   if (count == 5)
                   {
                       ValueItem item = new ValueItem(esnCode, AreaStyles.CreateSimpleAreaStyle(GeoColor.StandardColors.Red, GeoColor.StandardColors.Black, 3));
                       valueItems.Add(item);
 
                   }
                   if (count == 6)
                   {
                       ValueItem item = new ValueItem(esnCode, AreaStyles.CreateSimpleAreaStyle(GeoColor.StandardColors.Aqua, GeoColor.StandardColors.Black, 3));
                       valueItems.Add(item);
 
                   }
                   if (count == 7)
                   {
                       ValueItem item = new ValueItem(esnCode, AreaStyles.CreateSimpleAreaStyle(GeoColor.StandardColors.Black, GeoColor.StandardColors.Black, 3));
                       valueItems.Add(item);
 
                   }
                   if (count == 8)
                   {
                       ValueItem item = new ValueItem(esnCode, AreaStyles.CreateSimpleAreaStyle(GeoColor.StandardColors.Brown, GeoColor.StandardColors.Black, 3));
                       valueItems.Add(item);
 
                   }
                   if (count == 9)
                   {
                       ValueItem item = new ValueItem(esnCode, AreaStyles.CreateSimpleAreaStyle(GeoColor.StandardColors.Coral, GeoColor.StandardColors.Black, 3));
                       valueItems.Add(item);
 
                   }
                   if (count == 10)
                   {
                       ValueItem item = new ValueItem(esnCode, AreaStyles.CreateSimpleAreaStyle(GeoColor.StandardColors.DarkKhaki, GeoColor.StandardColors.Black, 3));
                       valueItems.Add(item);
 
                   }
                   count++;
               }
 
           }
           return (valueItems);
       }


Hi Gordon,



I didn’t find out some other alternative way but I noticed the only difference of those value items is its color, so, could we use random color for each value item to avoid the counter?



if (values.Count > 0)
           { 
               int count = 0;
               foreach (string esnCode in values)
               {
ValueItem item = new ValueItem(esnCode, AreaStyles.CreateSimpleAreaStyle(randomColor, GeoColor.StandardColors.Black, 3));
                       valueItems.Add(item);
}

Hope it helps.

Troy