Exemple #1
0
        public List <double> CalculateRangeStarts(int classCount, ThematicItem thematicItem, List <double> valueList)
        {
            List <double> uniqueValues = valueList
                                         .Select(e => e)
                                         .Distinct()
                                         .ToList();

            if (classCount > uniqueValues.Count)
            {
                if (uniqueValues.Count > 1)
                {
                    classCount = uniqueValues.Count;
                }
                else
                {
                    throw new System.ArgumentException(string.Format(DashboardSharedStrings.GADGET_MAP_NOT_ENOUGH_VALUES_TO_GENERATE_N_CLASSES, 2));
                }
            }

            List <double> rangeStarts = new List <double>();

            double totalRange = thematicItem.Max - thematicItem.Min;
            double portion    = totalRange / classCount;

            rangeStarts.Add(thematicItem.Min);
            double startRangeValue = thematicItem.Min;

            IEnumerable <double> valueEnumerator =
                from aValue in valueList
                orderby aValue
                select aValue;

            double increment = (double)valueList.Count / (double)classCount;

            for (double i = increment - 1.0; Math.Round(i, 4) < valueList.Count - 1; i += increment)
            {
                double value0 = valueEnumerator.ElementAt(Convert.ToInt32(Math.Floor(i)));
                double value1 = valueEnumerator.ElementAt(Convert.ToInt32(Math.Ceiling(i)));
                double value  = (value1 + value0) / 2.0;

                if (value < thematicItem.Min)
                {
                    value = thematicItem.Min;
                }

                rangeStarts.Add(value);
            }

            return(rangeStarts);
        }
Exemple #2
0
        private List <double> RemoveOutOfRangeValues(ThematicItem thematicItem)
        {
            List <double> RangeList = new List <double>();

            foreach (var Item in thematicItem.RangeStarts)
            {
                if (Item >= thematicItem.Min && Item <= thematicItem.Max)
                {
                    RangeList.Add(Item);
                }
            }

            return(RangeList);
        }
Exemple #3
0
        public void ResetRangeValues(string shapeKey, string dataKey, string valueField, int classCount)
        {
            _classCount = classCount;
            _shapeKey   = shapeKey;
            _dataKey    = dataKey;

            DataTable     loadedData    = GetLoadedData(_dashboardHelper, _dataKey, ref valueField);
            GraphicsLayer graphicsLayer = graphicsLayer = GetGraphicsLayer();

            if (graphicsLayer == null)
            {
                return;
            }

            bool layerContainsGraphics = graphicsLayer.Graphics == null || graphicsLayer.Graphics.Count == 0 ? false : true;

            if (layerContainsGraphics)
            {
                _thematicItem = GetThematicItem(_classCount, loadedData, graphicsLayer);
                PopulateRangeValues();
            }
        }
Exemple #4
0
        public string PopulateRangeValues(DashboardHelper dashboardHelper, string shapeKey, string dataKey, string valueField, List <SolidColorBrush> colors, int classCount, string legendText, bool?showPolyLabels = false)
        {
            _classCount      = classCount;
            _dashboardHelper = dashboardHelper;
            _shapeKey        = shapeKey;
            _dataKey         = dataKey;
            _valueField      = valueField;
            LegendText       = legendText;
            _colors          = colors;
            _showPolyLabels  = (bool)showPolyLabels;

            try
            {
                DataTable     loadedData    = GetLoadedData(dashboardHelper, dataKey, ref valueField);
                GraphicsLayer graphicsLayer = GetGraphicsLayer();

                bool layerContainsGraphics = graphicsLayer.Graphics.Count == 0 ? false : true;

                if (layerContainsGraphics)
                {
                    _thematicItem = GetThematicItem(_classCount, loadedData, graphicsLayer);

                    return(PopulateRangeValues());
                }
                else
                {
                    return(string.Empty);
                }
            }
            catch (ArgumentException exception)
            {
                return(exception.Message);
            }
            catch
            {
                return(string.Format(DashboardSharedStrings.GADGET_MAP_NOT_ENOUGH_VALUES_TO_GENERATE_N_CLASSES, classCount));
            }
        }
        private void SetRangeValues()
        {
            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(_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.Graphics.Clear();

                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();

                    // 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 = Orientation.Horizontal;
                    classStackPanel.Children.Add(swatchRect);
                    classStackPanel.Children.Add(classTextBlock);

                    legendList.Items.Add(classStackPanel);
                }

                TextBlock  minTextBlock  = new TextBlock();
                StackPanel minStackPanel = new StackPanel();
                minStackPanel.Orientation = 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();
                StackPanel maxStackPanel = new StackPanel();
                maxStackPanel.Orientation = Orientation.Horizontal;
                maxTextBlock.Text         = String.Format("Max: {0} ({1})", thematicItem.Max, thematicItem.MaxName);
                maxStackPanel.Children.Add(maxTextBlock);
                legendList.Items.Add(maxStackPanel);

                LegendStackPanel.Children.Add(legendList);
            }
        }
        public void SetShapeRangeValues(DashboardHelper dashboardHelper, string shapeKey, string dataKey, string valueField, List<SolidColorBrush> colors, int classCount)
        {
            try
            {
                this.classCount = classCount;
                this.dashboardHelper = dashboardHelper;
                this.shapeKey = shapeKey;
                this.dataKey = dataKey;
                this.valueField = valueField;
                this.colors = colors;

                List<string> columnNames = new List<string>();
                if (dashboardHelper.IsUsingEpiProject)
                {
                    columnNames.Add("UniqueKey");
                }
                columnNames.Add(valueField);
                columnNames.Add(dataKey);

                DataTable loadedData;

                if (valueField.Equals("{Record Count}"))
                {
                    GadgetParameters gadgetOptions = new GadgetParameters();
                    gadgetOptions.MainVariableName = dataKey;

                    Dictionary<string, string> inputVariableList = new Dictionary<string, string>();
                    inputVariableList.Add("freqvar", dataKey);
                    inputVariableList.Add("allvalues", "false");
                    inputVariableList.Add("showconflimits", "false");
                    inputVariableList.Add("showcumulativepercent", "false");
                    inputVariableList.Add("includemissing", "false");
                    inputVariableList.Add("maxrows", "500");

                    gadgetOptions.InputVariableList = inputVariableList;
                    loadedData = dashboardHelper.GenerateFrequencyTable(gadgetOptions).First().Key;
                    foreach (DataRow dr in loadedData.Rows)
                    {
                        dr[0] = dr[0].ToString().Trim();
                    }
                    valueField = "freq";
                }
                else
                {
                    loadedData = dashboardHelper.GenerateTable(columnNames);
                }

                GraphicsLayer graphicsLayer = myMap.Layers[layerId.ToString()] as GraphicsLayer;
                ThematicItem thematicItem = new ThematicItem() { Name = dataKey, Description = dataKey, CalcField = "" };
                List<double> valueList = new List<double>();
                for (int i = 0; i < graphicsLayer.Graphics.Count; i++)
                {
                    Graphic graphicFeature = graphicsLayer.Graphics[i];
                    //string filterExpression = dataKey + " = '" + graphicFeature.Attributes[shapeKey].ToString().Trim() + "'";
                    string filterExpression = "";
                    if (dataKey.Contains(" ") || dataKey.Contains("$") || dataKey.Contains("#"))
                        filterExpression += "[";
                    filterExpression += dataKey;
                    if (dataKey.Contains(" ") || dataKey.Contains("$") || dataKey.Contains("#"))
                        filterExpression += "]";
                    filterExpression += " = '" + graphicFeature.Attributes[shapeKey].ToString().Replace("'", "''").Trim() + "'";

                    double graphicValue = Double.PositiveInfinity;
                    try
                    {
                        graphicValue = Convert.ToDouble(loadedData.Select(filterExpression)[0][valueField]);
                    }
                    catch (Exception ex)
                    {
                        graphicValue = Double.PositiveInfinity;
                    }

                    string graphicName = graphicFeature.Attributes[shapeKey].ToString();

                    if (i == 0)
                    {
                        thematicItem.Min = Double.PositiveInfinity;
                        thematicItem.Max = Double.NegativeInfinity;
                        thematicItem.MinName = string.Empty;
                        thematicItem.MaxName = string.Empty;
                    }
                    else
                    {
                        if (graphicValue < thematicItem.Min) { thematicItem.Min = graphicValue; thematicItem.MinName = graphicName; }
                        if (graphicValue > thematicItem.Max && graphicValue != Double.PositiveInfinity) { thematicItem.Max = graphicValue; thematicItem.MaxName = graphicName; }
                    }

                    if (graphicValue < Double.PositiveInfinity)
                    {
                        valueList.Add(graphicValue);
                    }
                }
                thematicItem.RangeStarts = new List<double>();

                double totalRange = thematicItem.Max - thematicItem.Min;
                double portion = totalRange / classCount;

                thematicItem.RangeStarts.Add(thematicItem.Min);
                double startRangeValue = thematicItem.Min;
                IEnumerable<double> valueEnumerator =
                from aValue in valueList
                orderby aValue
                select aValue;

                int increment = Convert.ToInt32(Math.Round((double)valueList.Count / (double)classCount));
                for (int i = increment; i < valueList.Count; i += increment)
                {
                    double value = valueEnumerator.ElementAt(i);
                    if (value < thematicItem.Min)
                        value = thematicItem.Min;
                    thematicItem.RangeStarts.Add(value);
                }

                if (graphicsLayer.Graphics != null && graphicsLayer.Graphics.Count > 0)
                {

                    for (int i = 0; i < graphicsLayer.Graphics.Count; i++)
                    {
                        Graphic graphicFeature = graphicsLayer.Graphics[i];

                        //string filterExpression = dataKey + " = '" + graphicFeature.Attributes[shapeKey].ToString().Trim() + "'";
                        string filterExpression = "";
                        if (dataKey.Contains(" ") || dataKey.Contains("$") || dataKey.Contains("#"))
                            filterExpression += "[";
                        filterExpression += dataKey;
                        if (dataKey.Contains(" ") || dataKey.Contains("$") || dataKey.Contains("#"))
                            filterExpression += "]";
                        filterExpression += " = '" + graphicFeature.Attributes[shapeKey].ToString().Replace("'", "''").Trim() + "'";

                        double graphicValue = Double.PositiveInfinity;
                        try
                        {
                            graphicValue = Convert.ToDouble(loadedData.Select(filterExpression)[0][valueField]);
                        }
                        catch (Exception)
                        {
                            graphicValue = Double.PositiveInfinity;
                        }

                        int brushIndex = GetRangeIndex(graphicValue, thematicItem.RangeStarts);

                        SimpleFillSymbol symbol = new SimpleFillSymbol()
                        {
                            Fill = graphicValue == Double.PositiveInfinity ? new SolidColorBrush(Colors.Transparent) : colors[brushIndex],
                            BorderBrush = new SolidColorBrush(Colors.Black),
                            BorderThickness = 1
                        };

                        graphicFeature.Symbol = symbol;

                        TextBlock t = new TextBlock();
                        t.Background = Brushes.White;
                        if (graphicValue == Double.PositiveInfinity)
                        {
                            t.Text = graphicFeature.Attributes[shapeKey].ToString().Trim() + " : No Data";
                        }
                        else
                        {
                            t.Text = graphicFeature.Attributes[shapeKey].ToString().Trim() + " : " + graphicValue.ToString();
                        }
                        t.FontSize = 14;
                        Border border = new Border();
                        border.BorderThickness = new Thickness(1);
                        Panel panel = new StackPanel();
                        panel.Children.Add(t);
                        border.Child = panel;

                        graphicFeature.MapTip = border;
                    }

                }

                if (LegendStackPanel == null)
                {
                    LegendStackPanel = new StackPanel();
                }
                LegendStackPanel.Children.Clear();

                System.Windows.Controls.ListBox legendList = new System.Windows.Controls.ListBox();
                legendList.Margin = new Thickness(5);
                legendList.Background = Brushes.White;// new LinearGradientBrush(Color.FromArgb(0xFF, 0xFF, 0xFF, 0xFF), Color.FromArgb(0x7F, 0xFF, 0xFF, 0xFF), 45);
                legendList.BorderBrush = Brushes.Black;
                legendList.BorderThickness = new Thickness(3);
                //LegendTitle.Text = thematicItem.Description;

                for (int c = 0; c < classCount; c++)
                {
                    try
                    {
                        Rectangle swatchRect = new Rectangle()
                        {
                            Width = 20,
                            Height = 20,
                            Stroke = new SolidColorBrush(Colors.Black),
                            Fill = colors[c]
                        };

                        TextBlock classTextBlock = new TextBlock();

                        if (c == 0)
                        {
                            if (thematicItem.RangeStarts[1] == thematicItem.Min)
                                classTextBlock.Text = String.Format("  Exactly {0}", Math.Round(thematicItem.RangeStarts[1], 2));
                            else
                                classTextBlock.Text = String.Format("  Less than {0}", Math.Round(thematicItem.RangeStarts[1], 2));
                        }
                        else if (c == classCount - 1)
                            classTextBlock.Text = String.Format("  {0} and above", Math.Round(thematicItem.RangeStarts[c], 2));
                        else if (thematicItem.RangeStarts.Count <= c + 1)
                        {
                            classTextBlock.Text = String.Format("  {0} and above", Math.Round(thematicItem.RangeStarts[c], 2));
                        }
                        // Middle classifications
                        else
                        {
                            if (thematicItem.RangeStarts[c] == thematicItem.RangeStarts[c + 1])
                                classTextBlock.Text = String.Format("  Exactly {0}", Math.Round(thematicItem.RangeStarts[c], 2));
                            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);
                        if (thematicItem.RangeStarts.Count <= c + 1)
                        {
                            break;
                        }
                    }
                    catch (Exception ex)
                    {
                    }
                }

                TextBlock minTextBlock = new TextBlock();
                StackPanel minStackPanel = new StackPanel();
                minStackPanel.Orientation = System.Windows.Controls.Orientation.Horizontal;
                minTextBlock.Text = thematicItem.MinName != null ? String.Format("Min: {0} ({1})", thematicItem.Min, thematicItem.MinName.Trim()) : String.Format("Min: {0} ({1})", thematicItem.Min, string.Empty);
                minStackPanel.Children.Add(minTextBlock);
                legendList.Items.Add(minStackPanel);

                TextBlock maxTextBlock = new TextBlock();
                StackPanel maxStackPanel = new StackPanel();
                maxStackPanel.Orientation = System.Windows.Controls.Orientation.Horizontal;
                maxTextBlock.Text = thematicItem.MaxName != null ? String.Format("Max: {0} ({1})", thematicItem.Max, thematicItem.MaxName.Trim()) : String.Format("Max: {0} ({1})", thematicItem.Max, string.Empty);
                maxStackPanel.Children.Add(maxTextBlock);
                legendList.Items.Add(maxStackPanel);

                LegendStackPanel.Children.Add(legendList);

            }
            catch (Exception ex)
            {
            }
        }
Exemple #7
0
        private void SetRangeValues()
        {
            if (this.Layer == null)
            {
                return;
            }

            // if necessary, update ColorList based on current number of classes.
            int _classCount = (int)ClassCountCombo.SelectedItem;

            if (_lastGeneratedClassCount != _classCount)
            {
                CreateColorList();
            }

            // Field on which to generate a classification scheme.
            ThematicItem thematicItem = ThematicItemList[FieldCombo.SelectedIndex];

            // 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]);

                if (i == 0)
                {
                    thematicItem.Min = graphicValue;
                    thematicItem.Max = graphicValue;
                }
                else
                {
                    if (graphicValue < thematicItem.Min)
                    {
                        thematicItem.Min = graphicValue;
                    }
                    if (graphicValue > thematicItem.Max)
                    {
                        thematicItem.Max = graphicValue;
                    }
                }

                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 (ClassTypeCombo.SelectedIndex == 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(_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[ColorBlendCombo.SelectedIndex];

            if (_featureSet != null && _featureSet.Features.Count > 0)
            {
                ClassBreaksRenderer renderer = new ClassBreaksRenderer()
                {
                    // Attribute = thematicItem.Name
                };
                for (int i = 1; i < thematicItem.RangeStarts.Count; i++)
                {
                    renderer.Classes.Add(new ClassBreakInfo()
                    {
                        MinimumValue = thematicItem.RangeStarts[i - 1],
                        MaximumValue = thematicItem.RangeStarts[i],
                        Symbol       = new SimpleFillSymbol()
                        {
                            Fill            = brushList[i],
                            BorderThickness = 0
                        }
                    });
                }
                renderer.Classes[0].MinimumValue = double.MinValue;
                renderer.Classes[renderer.Classes.Count - 1].MaximumValue = double.MaxValue;
                Layer.Renderer = renderer;
                Layer.Refresh();
            }
        }
Exemple #8
0
        public void SetShapeRangeValues(DashboardHelper dashboardHelper, string shapeKey, string dataKey, string valueField, List <SolidColorBrush> brushList, int classCount, string missingText, string legendText, bool?showPolyLabels = false)
        {
            try
            {
                _classCount      = classCount;
                _dashboardHelper = dashboardHelper;
                _shapeKey        = shapeKey;
                _dataKey         = dataKey;
                _valueField      = valueField;
                _missingText     = missingText;
                _legendText      = legendText;
                _showPolyLabels  = (bool)showPolyLabels;

                if (brushList != null)
                {
                    _colors = brushList;
                }

                DataTable loadedData = GetLoadedData(dashboardHelper, dataKey, ref valueField);

                if (_valueField == "{Record Count}")
                {
                    _valueField = "freq";
                }

                GraphicsLayer graphicsLayer = GetGraphicsLayer() as GraphicsLayer;

                _thematicItem = GetThematicItem(classCount, loadedData, graphicsLayer);

                if (this.UseQuantiles == false && RangeStarts_FromControls != null && RangeStarts_FromControls.Count > 0)
                {
                    _thematicItem.RangeStarts = RangeStarts_FromControls;
                    PopulateRangeValues();
                }
                else
                {
                    PopulateRangeValues();
                }

                GraphicCollection textGraphics = new GraphicCollection();

                if (graphicsLayer.Graphics != null && graphicsLayer.Graphics.Count > 0)
                {
                    for (int i = graphicsLayer.Graphics.Count - 1; i > -1; i--)
                    {
                        if (graphicsLayer.Graphics[i].Symbol is TextSymbol)
                        {
                            graphicsLayer.Graphics.RemoveAt(i);
                        }
                    }
                }

                bool usePoleOfInaccessibility = true;

                if (graphicsLayer.Graphics != null && graphicsLayer.Graphics.Count > 0)
                {
                    for (int i = 0; i < graphicsLayer.Graphics.Count; i++)
                    {
                        Graphic graphicFeature = graphicsLayer.Graphics[i];

                        if (graphicFeature.Symbol is TextSymbol)
                        {
                            continue;
                        }
                        if (graphicFeature.Attributes.Count == 0)
                        {
                            continue;
                        }

                        string filterExpression = "";

                        if (dataKey.Contains(" ") || dataKey.Contains("$") || dataKey.Contains("#"))
                        {
                            filterExpression += "[";
                        }

                        filterExpression += dataKey;

                        if (dataKey.Contains(" ") || dataKey.Contains("$") || dataKey.Contains("#"))
                        {
                            filterExpression += "]";
                        }

                        filterExpression += " = '" + GetShapeValue(graphicFeature, shapeKey) + "'";

                        double graphicValue = Double.PositiveInfinity;
                        try
                        {
                            DataRow[] postFilterExpression = loadedData.Select(filterExpression);

                            if (postFilterExpression.Length > 0)
                            {
                                graphicValue = Convert.ToDouble(postFilterExpression[0][_valueField]);
                            }
                        }
                        catch (Exception)
                        {
                            graphicValue = Double.PositiveInfinity;
                        }

                        int brushIndex = GetRangeIndex(graphicValue, _thematicItem.RangeStarts);

                        if (brushList != null)
                        {
                            Color color = ((SolidColorBrush)brushList[brushIndex]).Color;
                            Brush fill  = new SolidColorBrush(Color.FromArgb(Opacity, color.R, color.G, color.B));

                            if (graphicValue == Double.PositiveInfinity)
                            {
                                color = ((SolidColorBrush)brushList[brushList.Count - 1]).Color;
                                fill  = new SolidColorBrush(Color.FromArgb(Opacity, color.R, color.G, color.B));
                            }

                            SimpleFillSymbol symbol = new SimpleFillSymbol();

                            symbol.Fill            = fill;
                            symbol.BorderBrush     = new SolidColorBrush(Colors.Black);
                            symbol.BorderThickness = 1;

                            graphicFeature.Symbol = symbol;
                        }

                        if (ShowPolyLabels)
                        {
                            TextSymbol textSymbol = new TextSymbol();
                            textSymbol.Foreground = new SolidColorBrush(Colors.Black);
                            textSymbol.FontSize   = 11;

                            if (graphicFeature.Attributes[shapeKey] != null)
                            {
                                string sShapekey     = graphicFeature.Attributes[shapeKey].ToString().Trim();
                                int    indexShapeKey = sShapekey.LastIndexOfAny(new char[] { ' ', '-' });
                                int    iShapeKeyLen  = sShapekey.Length;

                                //  Break up label if possible and put on new line.  Adjust offsets as needed.
                                if (indexShapeKey != -1) //& iShapeKeyLen > 10)
                                {
                                    textSymbol.Text    = sShapekey.Substring(0, indexShapeKey) + "\r\n    " + sShapekey.Substring(indexShapeKey + 1, (iShapeKeyLen - (indexShapeKey + 1)));
                                    textSymbol.OffsetX = iShapeKeyLen / 0.8;
                                    textSymbol.OffsetY = 6;
                                }
                                else
                                {
                                    textSymbol.Text    = sShapekey;
                                    textSymbol.OffsetX = iShapeKeyLen / 0.4;
                                    textSymbol.OffsetY = 3;
                                }
                                //Console.WriteLine(textSymbol.Text);
                                //textSymbol.OffsetX = textSymbol.Text.Length / 0.4;
                                textSymbol.OffsetY = textSymbol.FontSize / 2.0;

                                Envelope extentEnvelope = graphicFeature.Geometry.Extent;
                                MapPoint pole           = extentEnvelope.GetCenter();

                                ObservableCollection <ESRI.ArcGIS.Client.Geometry.PointCollection> rings = new ObservableCollection <ESRI.ArcGIS.Client.Geometry.PointCollection>();

                                //if (graphicFeature.Attributes[shapeKey].ToString().Trim() == "Richmond Hill") usePoleOfInaccessibility = true;

                                if (usePoleOfInaccessibility)
                                {
                                    if (graphicFeature.Geometry is ESRI.ArcGIS.Client.Geometry.Polygon)
                                    {
                                        rings = ((ESRI.ArcGIS.Client.Geometry.Polygon)graphicFeature.Geometry).Rings;
                                        Tuple <double, double> coords;

                                        bool showDebugCells = true;

                                        if (showDebugCells)
                                        {
                                            double precision = 0.1;
                                            double denom     = 64.0;

                                            precision = extentEnvelope.Width / denom < precision? extentEnvelope.Width / denom : precision;
                                            precision = extentEnvelope.Height / denom < precision ? extentEnvelope.Height / denom : precision;

                                            coords = PolyLabel.PoleOfInaccessibility(rings, precision, graphicsLayer: graphicsLayer);

                                            //Envelope extent = graphicFeature.Geometry.Extent;
                                            //Cell extentCell = new Cell(extent.XMin, extent.YMin, extent.Width / 2, rings);
                                            //PolyLabel.AddDebugGraphic(extentCell, graphicsLayer);
                                        }
                                        else
                                        {
                                            coords = PolyLabel.PoleOfInaccessibility(rings);
                                        }

                                        pole.X = coords.Item1;
                                        pole.Y = coords.Item2;
                                    }

                                    //usePoleOfInaccessibility = false;
                                }

                                Graphic textGraphic = new Graphic();

                                textGraphic.Symbol   = textSymbol;
                                textGraphic.Geometry = pole;

                                textGraphics.Add(textGraphic);
                            }
                        }

                        TextBlock t = new TextBlock();
                        t.Background = Brushes.White;

                        if (graphicValue == Double.PositiveInfinity)
                        {
                            t.Text = GetShapeValue(graphicFeature, shapeKey) + " " + DashboardSharedStrings.DASHBOARD_MAP_NO_DATA;
                        }
                        else
                        {
                            t.Text = GetShapeValue(graphicFeature, shapeKey) + " : " + graphicValue.ToString();
                        }

                        t.FontSize = 14;

                        Border border = new Border();
                        border.BorderThickness = new Thickness(1);
                        Panel panel = new StackPanel();
                        panel.Children.Add(t);
                        border.Child = panel;

                        graphicFeature.MapTip = border;

                        if (graphicFeature.Attributes.Keys.Contains("EpiInfoValCol"))
                        {
                            graphicFeature.Attributes["EpiInfoValCol"] = graphicValue;
                        }
                        else
                        {
                            graphicFeature.Attributes.Add("EpiInfoValCol", graphicValue);
                        }
                    }

                    graphicsLayer.Graphics.AddRange(textGraphics);

                    if (graphicsLayer is FeatureLayer)
                    {
                        ClassBreaksRenderer renderer = new ClassBreaksRenderer();
                        renderer.Field = "EpiInfoValCol";

                        Color color = ((SolidColorBrush)brushList[brushList.Count - 1]).Color;
                        Brush fill  = new SolidColorBrush(Color.FromArgb(Opacity, color.R, color.G, color.B));

                        renderer.DefaultSymbol = new SimpleFillSymbol()
                        {
                            Fill            = fill,
                            BorderBrush     = new SolidColorBrush(Colors.Black),
                            BorderThickness = 1
                        };

                        for (int i = 0; i < _thematicItem.RangeStarts.Count; i++)
                        {
                            ClassBreakInfo classBreakInfo = new ClassBreakInfo();
                            classBreakInfo.MinimumValue = double.Parse(RangeValues[i, 0]);
                            classBreakInfo.MaximumValue = double.Parse(RangeValues[i, 1]);

                            color = ((SolidColorBrush)brushList[i]).Color;
                            fill  = new SolidColorBrush(Color.FromArgb(Opacity, color.R, color.G, color.B));

                            classBreakInfo.Symbol = new SimpleFillSymbol()
                            {
                                Fill            = fill,
                                BorderBrush     = new SolidColorBrush(Colors.Black),
                                BorderThickness = 1
                            };

                            renderer.Classes.Add(classBreakInfo);
                        }

                        graphicsLayer.Renderer = renderer;
                    }
                }

                SetLegendSection(brushList, classCount, missingText, _thematicItem);
            }
            catch
            {
            }
        }
Exemple #9
0
        public ThematicItem GetThematicItem(int classCount, DataTable loadedData, GraphicsLayer graphicsLayer)
        {
            ThematicItem thematicItem = new ThematicItem()
            {
                Name        = _dataKey,
                Description = _dataKey,
                CalcField   = ""
            };

            List <double> valueList       = new List <double>();
            List <string> usedShapeValues = new List <string>();

            thematicItem.RowCount = loadedData.Rows.Count;

            string filterExpression = "";

            for (int i = 0; i < graphicsLayer.Graphics.Count; i++)
            {
                Graphic graphicFeature = graphicsLayer.Graphics[i];

                if (graphicFeature.Symbol is TextSymbol)
                {
                    continue;
                }

                string shapeValue = string.Empty;

                shapeValue = GetShapeValue(graphicFeature, shapeValue);

                if (usedShapeValues.Contains(shapeValue))
                {
                    continue;
                }

                usedShapeValues.Add(shapeValue);

                filterExpression = "";

                if (_dataKey.Contains(" ") || _dataKey.Contains("$") || _dataKey.Contains("#"))
                {
                    filterExpression += "[";
                }

                filterExpression += _dataKey;

                if (_dataKey.Contains(" ") || _dataKey.Contains("$") || _dataKey.Contains("#"))
                {
                    filterExpression += "]";
                }

                filterExpression += " = '" + shapeValue + "'";

                double graphicValue = Double.PositiveInfinity;

                try
                {
                    loadedData.CaseSensitive = false;

                    DataRow[] rows = loadedData.Select(filterExpression);

                    if (rows.Length > 0)
                    {
                        object found = rows[0][_valueField];
                        string valueField;

                        if (found is string)
                        {
                            valueField   = (string)found;
                            graphicValue = Convert.ToDouble(valueField);
                        }
                        else if (found is double)
                        {
                            graphicValue = (Double)found;
                        }
                        else
                        {
                            graphicValue = Convert.ToDouble(found);
                        }
                    }
                }
                catch { }

                string graphicName = shapeValue;

                if (i == 0)
                {
                    thematicItem.Min     = Double.PositiveInfinity;
                    thematicItem.Max     = Double.NegativeInfinity;
                    thematicItem.MinName = string.Empty;
                    thematicItem.MaxName = string.Empty;
                }
                else
                {
                    if (graphicValue < thematicItem.Min)
                    {
                        thematicItem.Min     = graphicValue;
                        thematicItem.MinName = graphicName;
                    }

                    if (graphicValue > thematicItem.Max && graphicValue != Double.PositiveInfinity)
                    {
                        thematicItem.Max     = graphicValue;
                        thematicItem.MaxName = graphicName;
                    }
                }

                if (graphicValue < Double.PositiveInfinity)
                {
                    valueList.Add(graphicValue);
                }
            }

            if (valueList.Count == 0)
            {
                //MessageBox.Show(string.Format(DashboardSharedStrings.GADGET_MAP_NOT_ENOUGH_VALUES_TO_GENERATE_N_CLASSES, classCount));
                throw new System.ArgumentException(string.Format(DashboardSharedStrings.GADGET_MAP_NOT_ENOUGH_VALUES_TO_GENERATE_N_CLASSES, classCount)
                                                   + Environment.NewLine + Environment.NewLine
                                                   + "[" + filterExpression + "]?");
            }

            valueList.Sort();

            if (this.UseQuantiles)
            {
                thematicItem.RangeStarts = CalculateRangeStarts(classCount, thematicItem, valueList);
                // dpb thematicItem.ClassDescriptions = CalculateQuantiles(classCount, thematicItem, valueList);
            }
            else
            {
                thematicItem.RangeStarts = ClassRangesDictionary.RangeStarts;
            }

            return(thematicItem);
        }
Exemple #10
0
        public void SetLegendSection(List <SolidColorBrush> colors, int classCount, string missingText, ThematicItem thematicItem)
        {
            if (colors == null)
            {
                return;
            }

            if (LegendStackPanel == null)
            {
                LegendStackPanel = new StackPanel();
            }

            LegendStackPanel.Children.Clear();

            System.Windows.Controls.ListBox legendList = new System.Windows.Controls.ListBox();
            legendList.Padding         = new Thickness(0, 10, 0, 0);
            legendList.Background      = Brushes.White;
            legendList.BorderBrush     = Brushes.Black;
            legendList.BorderThickness = new Thickness(0);

            Rectangle missingSwatchRect = new Rectangle()
            {
                Width             = 20,
                Height            = 20,
                Stroke            = new SolidColorBrush(Colors.Black),
                Fill              = colors[colors.Count - 1],
                Margin            = new Thickness(0, 0, 5, 0),
                VerticalAlignment = VerticalAlignment.Top
            };

            TextBlock titleTextBlock = new TextBlock();

            titleTextBlock.Text         = LegendText;
            titleTextBlock.FontWeight   = FontWeights.Bold;
            titleTextBlock.MaxWidth     = 256;
            titleTextBlock.TextWrapping = TextWrapping.Wrap;
            StackPanel titleTextStackPanel = new StackPanel();

            titleTextStackPanel.Orientation = System.Windows.Controls.Orientation.Horizontal;
            titleTextStackPanel.Children.Add(titleTextBlock);
            titleTextStackPanel.Margin = new Thickness(10, 0, 10, 10);
            legendList.Items.Add(titleTextStackPanel);

            TextBlock missingClassTextBlock = new TextBlock();

            missingClassTextBlock.Text         = String.Format("  " + missingText);
            missingClassTextBlock.MaxWidth     = 256;
            missingClassTextBlock.TextWrapping = TextWrapping.Wrap;
            StackPanel missingClassStackPanel = new StackPanel();

            missingClassStackPanel.Orientation = System.Windows.Controls.Orientation.Horizontal;
            missingClassStackPanel.Children.Add(missingSwatchRect);
            missingClassStackPanel.Children.Add(missingClassTextBlock);
            missingClassStackPanel.Margin = new Thickness(10, 0, 10, 5);
            legendList.Items.Add(missingClassStackPanel);

            SetLegendText(colors, classCount, thematicItem.RangeStarts, legendList);

            TextBlock  minTextBlock  = new TextBlock();
            StackPanel minStackPanel = new StackPanel();

            minStackPanel.Orientation = System.Windows.Controls.Orientation.Horizontal;
            minTextBlock.Text         = thematicItem.MinName != null ? DashboardSharedStrings.DASHBOARD_MAP_MIN + String.Format(" {0} ({1})", thematicItem.Min, thematicItem.MinName.Trim()) : DashboardSharedStrings.DASHBOARD_MAP_MIN + String.Format(" {0} ({1})", thematicItem.Min, string.Empty);
            minTextBlock.MaxWidth     = 256;
            minTextBlock.TextWrapping = TextWrapping.Wrap;
            minStackPanel.Children.Add(minTextBlock);
            minStackPanel.Margin = new Thickness(10, 5, 10, 5);
            legendList.Items.Add(minStackPanel);

            TextBlock  maxTextBlock  = new TextBlock();
            StackPanel maxStackPanel = new StackPanel();

            maxStackPanel.Orientation = System.Windows.Controls.Orientation.Horizontal;
            maxTextBlock.Text         = thematicItem.MaxName != null ? DashboardSharedStrings.DASHBOARD_MAP_MAX + String.Format(" {0} ({1})", thematicItem.Max, thematicItem.MaxName.Trim()) : DashboardSharedStrings.DASHBOARD_MAP_MAX + String.Format(" {0} ({1})", thematicItem.Max, string.Empty);
            maxTextBlock.MaxWidth     = 256;
            maxTextBlock.TextWrapping = TextWrapping.Wrap;
            maxStackPanel.Children.Add(maxTextBlock);
            maxStackPanel.Margin = new Thickness(10, 0, 10, 10);
            legendList.Items.Add(maxStackPanel);

            LegendStackPanel.Children.Add(legendList);
        }
Exemple #11
0
        public Dictionary <double, ClassDescription> CalculateQuantiles(int classCount, ThematicItem thematicItem, List <double> valueList)
        {
            List <double> uniqueValues = valueList
                                         .Select(e => e)
                                         .Distinct()
                                         .ToList();

            if (classCount > uniqueValues.Count)
            {
                if (uniqueValues.Count > 2)
                {
                    classCount = uniqueValues.Count;
                }
                else
                {
                    throw new System.ArgumentException(string.Format(DashboardSharedStrings.GADGET_MAP_NOT_ENOUGH_VALUES_TO_GENERATE_N_CLASSES, classCount));
                }
            }

            List <double> rangeStarts = new List <double>();

            double totalRange = thematicItem.Max - thematicItem.Min;
            double portion    = totalRange / classCount;

            Dictionary <double, ClassDescription> classDetail = new Dictionary <double, ClassDescription>();

            IEnumerable <double> valueEnumerator =
                from aValue in valueList
                orderby aValue
                select aValue;

            double increment = (double)valueList.Count / (double)classCount;

            List <double> inClass = new List <double>();

            int ordinalStart    = 0;
            int ordinalEnd      = 0;
            int ordinalUnderCut = 0;
            int ordinalOverCut  = 0;

            double valueUnderCut = valueEnumerator.ElementAt(ordinalUnderCut);
            double valueOverCut  = valueEnumerator.ElementAt(ordinalOverCut);
            double cutpoint      = (valueUnderCut + valueOverCut) / 2.0;

            for (int classOrdinal = 1; classOrdinal <= classCount; classOrdinal++)
            {
                if (classOrdinal == 1)
                {
                    ordinalStart = 0;
                    ordinalEnd   = Convert.ToInt32(Convert.ToInt32(Math.Floor(increment * classOrdinal))) - 1;

                    cutpoint = Math.Round(valueEnumerator.ElementAt(ordinalStart), 4);
                }
                else
                {
                    ordinalStart    = Convert.ToInt32(Convert.ToInt32(Math.Ceiling(increment * (classOrdinal - 1)))) - 1;
                    ordinalEnd      = Convert.ToInt32(Convert.ToInt32(Math.Floor(increment * classOrdinal))) - 1;
                    ordinalUnderCut = Convert.ToInt32(Convert.ToInt32(Math.Floor(increment * (classOrdinal - 1)))) - 1;
                    ordinalOverCut  = Convert.ToInt32(Convert.ToInt32(Math.Ceiling(increment * (classOrdinal - 1)))) - 1;

                    valueUnderCut = valueEnumerator.ElementAt(ordinalUnderCut);
                    valueOverCut  = valueEnumerator.ElementAt(ordinalOverCut);

                    cutpoint = Math.Round((valueUnderCut + valueOverCut) / 2.0, 4);
                }

                ClassDescription description = new ClassDescription();
                description.Start      = valueEnumerator.ElementAt(ordinalStart);
                description.End        = valueEnumerator.ElementAt(ordinalEnd);
                description.ValueCount = 1 + ordinalEnd - ordinalStart;

                classDetail.Add(cutpoint, description);
                rangeStarts.Add(cutpoint);
            }

            return(classDetail);
        }