private void SetRangeValues()
{
Style smallStyle = Application.Current.Resources["PhoneTextSmallStyle"] as Style;
GraphicsLayer graphicsLayer = MyMap.Layers["MyGraphicsLayer"] as GraphicsLayer;
// if necessary, update ColorList based on current number of classes.
if (_lastGeneratedClassCount != _classCount) CreateColorList();
// Field on which to generate a classification scheme.
ThematicItem thematicItem = ThematicItemList[_thematicListIndex];
// Calculate value for classification scheme
bool useCalculatedValue = !string.IsNullOrEmpty(thematicItem.CalcField);
// Store a list of values to classify
List<double> valueList = new List<double>();
// Get range, min, max, etc. from features
for (int i = 0; i < _featureSet.Features.Count; i++)
{
Graphic graphicFeature = _featureSet.Features[i];
double graphicValue = Convert.ToDouble(graphicFeature.Attributes[thematicItem.Name]);
string graphicName = graphicFeature.Attributes["STATE_NAME"].ToString();
if (useCalculatedValue)
{
double calcVal = Convert.ToDouble(graphicFeature.Attributes[thematicItem.CalcField]);
graphicValue = Math.Round(graphicValue / calcVal * 100, 2);
}
if (i == 0)
{
thematicItem.Min = graphicValue;
thematicItem.Max = graphicValue;
thematicItem.MinName = graphicName;
thematicItem.MaxName = graphicName;
}
else
{
if (graphicValue < thematicItem.Min) { thematicItem.Min = graphicValue; thematicItem.MinName = graphicName; }
if (graphicValue > thematicItem.Max) { thematicItem.Max = graphicValue; thematicItem.MaxName = graphicName; }
}
valueList.Add(graphicValue);
}
// Set up range start values
thematicItem.RangeStarts = new List<double>();
double totalRange = thematicItem.Max - thematicItem.Min;
double portion = totalRange / _classCount;
thematicItem.RangeStarts.Add(thematicItem.Min);
double startRangeValue = thematicItem.Min;
// Equal Interval
if (_classType == 1)
{
for (int i = 1; i < _classCount; i++)
{
startRangeValue += portion;
thematicItem.RangeStarts.Add(startRangeValue);
}
}
// Quantile
else
{
// Enumerator of all values in ascending order
IEnumerable<double> valueEnumerator =
from aValue in valueList
orderby aValue //"ascending" is default
select aValue;
int increment = Convert.ToInt32(Math.Ceiling((double)_featureSet.Features.Count / _classCount));
for (int i = increment; i < valueList.Count; i += increment)
{
double value = valueEnumerator.ElementAt(i);
thematicItem.RangeStarts.Add(value);
}
}
// Create graphic features and set symbol using the class range which contains the value
List<SolidColorBrush> brushList = ColorList[_colorShadeIndex];
if (_featureSet != null && _featureSet.Features.Count > 0)
{
// Clear previous graphic features
graphicsLayer.ClearGraphics();
for (int i = 0; i < _featureSet.Features.Count; i++)
{
Graphic graphicFeature = _featureSet.Features[i];
double graphicValue = Convert.ToDouble(graphicFeature.Attributes[thematicItem.Name]);
if (useCalculatedValue)
{
double calcVal = Convert.ToDouble(graphicFeature.Attributes[thematicItem.CalcField]);
graphicValue = Math.Round(graphicValue / calcVal * 100, 2);
}
int brushIndex = GetRangeIndex(graphicValue, thematicItem.RangeStarts);
SimpleFillSymbol symbol = new SimpleFillSymbol()
{
Fill = brushList[brushIndex],
BorderBrush = new SolidColorBrush(Colors.Transparent),
BorderThickness = 1
};
Graphic graphic = new Graphic();
graphic.Geometry = graphicFeature.Geometry;
graphic.Attributes.Add("Name", graphicFeature.Attributes["STATE_NAME"].ToString());
graphic.Attributes.Add("Description", thematicItem.Description);
graphic.Attributes.Add("Value", graphicValue.ToString());
graphic.Symbol = symbol;
graphicsLayer.Graphics.Add(graphic);
}
// Create new legend with ranges and swatches.
LegendStackPanel.Children.Clear();
ListBox legendList = new ListBox();
LegendTitle.Text = thematicItem.Description;
for (int c = 0; c < _classCount; c++)
{
Rectangle swatchRect = new Rectangle()
{
Width = 20,
Height = 20,
Stroke = new SolidColorBrush(Colors.Black),
Fill = brushList[c]
};
TextBlock classTextBlock = new TextBlock() { Style = smallStyle };
// First classification
if (c == 0)
classTextBlock.Text = String.Format(" Less than {0}", Math.Round(thematicItem.RangeStarts[1], 2));
// Last classification
else if (c == _classCount - 1)
classTextBlock.Text = String.Format(" {0} and above", Math.Round(thematicItem.RangeStarts[c], 2));
// Middle classifications
else
classTextBlock.Text = String.Format(" {0} to {1}", Math.Round(thematicItem.RangeStarts[c], 2), Math.Round(thematicItem.RangeStarts[c + 1], 2));
StackPanel classStackPanel = new StackPanel();
classStackPanel.Orientation = System.Windows.Controls.Orientation.Horizontal;
classStackPanel.Children.Add(swatchRect);
classStackPanel.Children.Add(classTextBlock);
legendList.Items.Add(classStackPanel);
}
TextBlock minTextBlock = new TextBlock() { Style = smallStyle };
StackPanel minStackPanel = new StackPanel();
minStackPanel.Orientation = System.Windows.Controls.Orientation.Horizontal;
minTextBlock.Text = String.Format("Min: {0} ({1})", thematicItem.Min, thematicItem.MinName);
minStackPanel.Children.Add(minTextBlock);
legendList.Items.Add(minStackPanel);
TextBlock maxTextBlock = new TextBlock() { Style = smallStyle };
StackPanel maxStackPanel = new StackPanel();
maxStackPanel.Orientation = System.Windows.Controls.Orientation.Horizontal;
maxTextBlock.Text = String.Format("Max: {0} ({1})", thematicItem.Max, thematicItem.MaxName);
maxStackPanel.Children.Add(maxTextBlock);
legendList.Items.Add(maxStackPanel);
legendList.IsHitTestVisible = false;
LegendStackPanel.Children.Add(legendList);
}
}