informationlayer.colorizer

9 posts, 3 answers
  1. Tamas
    Tamas avatar
    33 posts
    Member since:
    Jun 2010

    Posted 10 Nov 2010 Link to this post

    Hi,

    Is it possible to use the new Colorizer functionality on data other than to data stored in the dbf-s of the shapefiles? For example to set a List or Collection as the data source?

    Thanks and best regards,
    Tamas
  2. Answer
    Andrey
    Admin
    Andrey avatar
    1681 posts

    Posted 12 Nov 2010 Link to this post

    Hello Tamas,

    The Colorizer requires extended data property but you can set it up from code for elements of the collection. Then you should call the Prepare method for your collection before you add items to the information layer.

    I have attached a sample solution to get you started.


    Kind regards,
    Andrey Murzov
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  3. DevCraft banner
  4. Tamas
    Tamas avatar
    33 posts
    Member since:
    Jun 2010

    Posted 14 Nov 2010 Link to this post

    Hi Andrey,

    Many thanks for your solution!

    Best regards,
    Tamas
  5. Tamas
    Tamas avatar
    33 posts
    Member since:
    Jun 2010

    Posted 14 Nov 2010 Link to this post

    Hi Andrey,

    I tried to add a MapLegend to the sample you've sent me, but I was unsuccessful. I tried to copy it from the Heatmap example, but the maplegend showed only values of 0. I've changed the extendedpropertyname to the one in your example but it did not seem to work.

    Do I miss something?

    Thanks and best regards,
    Tamas
  6. Answer
    Andrey
    Admin
    Andrey avatar
    1681 posts

    Posted 17 Nov 2010 Link to this post

    Hi Tamas,

    The Colorizer from the sample solution has 100 as the TickMarkCount property value. So, the colorizer has 100 ranges with different colors. It is much more than the map legend could display.
    I would recommend you to use lesser value for the TickMarkCount property.
    I have attached new version of the sample solution which uses the map legend.

    Kind regards,
    Andrey Murzov
    the Telerik team
    See What's New in RadControls for Silverlight in Q3 2010 on Tuesday, November 16, 2010 11:00 AM - 12:00 PM EST or 10:00 PM - 11:00 PM EST: Register here>>
  7. Tamas
    Tamas avatar
    33 posts
    Member since:
    Jun 2010

    Posted 17 Nov 2010 Link to this post

    Hi Andrey,

    Many thanks for your help, this is perfect for me.

    Best regards,
    Tamas
  8. Tamas
    Tamas avatar
    33 posts
    Member since:
    Jun 2010

    Posted 21 Nov 2010 Link to this post

    Hi Andrey,

    I have tried to merge your example with an example where the USA states are displayed, but I had no luck. I think I have successfully added some data as ExtendedData to the shapes read by the informationlayer, but the colors are the same and the maplegend shows only NaN.

    Here is my code:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Animation;
    using System.Windows.Shapes;
    using System.Collections.ObjectModel;
    using Telerik.Windows.Controls.Map;
     
    namespace ColorizerAPI
    {
        public partial class MainPage : UserControl
        {
            private bool initialized;
     
            private const string ShapeRelativeUriFormat = "DataSources/Geospatial/USA/{0}";
     
            private const string StateExtendedPropertyName = "STATE_NAME";
            private const string CountyExtendedPropertyName = "NAME";
            private readonly string ValueExtendedPropertyName = "VALUE";
     
            private Collection<FrameworkElement> stateCollection;
     
            public MainPage()
            {
                InitializeComponent();
     
                SetupReaders();
     
            }
     
            public void SetupReaders()
            {
                this.informationLayer.Reader = new MapShapeReader();
                this.informationLayer.Reader.ReadCompleted += this.informationLayerReaderReadCompleted;
                this.informationLayer.Reader.Source = new Uri(string.Format(ShapeRelativeUriFormat, "usa_states.shp"), UriKind.Relative);
    //            this.informationLayer.Reader.DataSource = new Uri(string.Format(ShapeRelativeUriFormat, "usa_states.dbf"), UriKind.Relative);
                this.informationLayer.Reader.ToolTipFormat = StateExtendedPropertyName;
                this.informationLayer.Reader.ToolTipStyle = this.LayoutRoot.Resources["CustomToolTipStyle"] as Style;
     
            }
     
            private void informationLayerReaderReadCompleted(object sender, ReadShapesCompletedEventArgs eventArgs)
            {
                stateCollection = new Collection<FrameworkElement>();
     
                int i = 0;
                foreach (MapShape shape in this.informationLayer.Items)
                {
                    i++;
                    SetExtendedProperty(shape, double.Parse(i.ToString()));
                    stateCollection.Add(shape);
                }
    //            this.informationLayer.Colorizer.Prepare(stateCollection);
     
                this.radMap.InitializeCompleted += new EventHandler(radMap_InitializeCompleted);
                this.informationLayer.Colorizer.ExtendedPropertyName = ValueExtendedPropertyName;
            }
     
            private void SetExtendedProperty(MapShape shape, double value)
            {
     
                if (!shape.ExtendedData.PropertySet.ContainsKey(ValueExtendedPropertyName))
                    shape.ExtendedData.PropertySet.RegisterProperty(ValueExtendedPropertyName, string.Empty, typeof(double), 0d);
                shape.ExtendedData.SetValue(ValueExtendedPropertyName, value);
            }
                 
            private void radMap_InitializeCompleted(object sender, EventArgs e)
            {
                if (!this.initialized)
                {
                    this.initialized = true;
     
                    // prepare colorizer
                    this.informationLayer.Colorizer.ExtendedPropertyName = this.ValueExtendedPropertyName;
                    this.informationLayer.Colorizer.Prepare(stateCollection);
     
                    // add items to the information layer
                    this.AddElementsToLayer(stateCollection);
                }
            }
     
            /// <summary>
            /// Adds elements of collection to the information layer.
            /// </summary>
            private void AddElementsToLayer(Collection<FrameworkElement> collection)
            {
                foreach (FrameworkElement element in collection)
                {
                    this.informationLayer.Items.Add(element);
                }
            }
     
            /// <summary>
            /// Sets value to extended data.
            /// </summary>
            private void SetExtendedProperty(ExtendedPropertySet propertySet, MapShape shape, double value)
            {
                // create ExtendedData
                shape.ExtendedData = new ExtendedData(propertySet);
                // setup the property value
                shape.ExtendedData.SetValue(this.ValueExtendedPropertyName, value);
            }
        }
    }

    And the XAML:

    <UserControl x:Class="ColorizerAPI.MainPage"
        mc:Ignorable="d"
        d:DesignHeight="300" d:DesignWidth="400">
        <Grid x:Name="LayoutRoot" Background="White">
            <telerik:RadMap Name="radMap"
                            Center="35,-96"
                            ZoomLevel="6">
                <telerik:RadMap.Provider>
                    <telerik:OpenStreetMapProvider/>
                </telerik:RadMap.Provider>
                <telerik:InformationLayer Name="informationLayer">
                    <telerik:InformationLayer.Colorizer>
                        <telerik:ColorMeasureScale Mode="Count"
                                                   TickMarkCount="10">
                            <telerik:ColorMeasureScale.ShapeFillCollection>
                                <telerik:MapShapeFill Stroke="Yellow"
                                                            StrokeThickness="1"
                                                            Fill="Green">
                                </telerik:MapShapeFill>
                                <telerik:MapShapeFill Stroke="Yellow"
                                                            StrokeThickness="1"
                                                            Fill="Yellow">
                                </telerik:MapShapeFill>
                                <telerik:MapShapeFill Stroke="Yellow"
                                                            StrokeThickness="1"
                                                            Fill="Orange">
                                </telerik:MapShapeFill>
                                <telerik:MapShapeFill Stroke="Yellow"
                                                            StrokeThickness="1"
                                                            Fill="Brown">
                                </telerik:MapShapeFill>
                            </telerik:ColorMeasureScale.ShapeFillCollection>
     
                            <telerik:ColorMeasureScale.HighlightFillCollection>
                                <telerik:MapShapeFill Stroke="Red"
                                                      StrokeThickness="3"
                                                      Fill="Green">
                                </telerik:MapShapeFill>
                                <telerik:MapShapeFill Stroke="Red"
                                                      StrokeThickness="3"
                                                      Fill="Yellow">
                                </telerik:MapShapeFill>
                                <telerik:MapShapeFill Stroke="Red"
                                                      StrokeThickness="3"
                                                      Fill="Orange">
                                </telerik:MapShapeFill>
                                <telerik:MapShapeFill Stroke="Red"
                                                      StrokeThickness="3"
                                                      Fill="Brown">
                                </telerik:MapShapeFill>
                            </telerik:ColorMeasureScale.HighlightFillCollection>
                        </telerik:ColorMeasureScale>
                    </telerik:InformationLayer.Colorizer>
                </telerik:InformationLayer>
                <telerik:MapLegend LabelLocation="TopLeft"
                               LabelLayout="Between"                          
                               Layer="{Binding ElementName=informationLayer}"
                               Orientation="Horizontal"
                               Format="{}{0:F2}"
                               MarkerSpacing="25"
                               MarkerSize="30,10"
                               MarkerRadiusX="5"
                               MarkerRadiusY="5"
                               Foreground="White"
                               FontWeight="Bold"
                               Background="#3f007f7f"
                               HorizontalAlignment="Left"
                               VerticalAlignment="Top"
                               Margin="15,15,0,0"
                               Name="legend">
                </telerik:MapLegend>
            </telerik:RadMap>
        </Grid>
    </UserControl>


    Do I do something wrong?

    Thanks and best regards,
    Tamas
  9. Answer
    Andrey
    Admin
    Andrey avatar
    1681 posts

    Posted 23 Nov 2010 Link to this post

    Hi Tamas,

    When the Colorizer is assigned in XAML code and you assign the MapShapeReader to information layer from C# code then the Colorizer seems to work  incorrectly. It prepares its parameters before the extended property is assigned to the shapes.
    I have changed the code to reset the colorizer after the shapes are processed.
    The changed code is below.
    using System;
    using System.Collections.Generic;
    using System.Windows;
    using System.Windows.Controls;
    using Telerik.Windows.Controls.Map;
      
    namespace ColorizerAPI
    {
        public partial class MainPage : UserControl
        {
            private bool initialized;
      
            private const string ShapeRelativeUriFormat = "DataSources/Geospatial/USA/{0}";
      
            private const string StateExtendedPropertyName = "STATE_NAME";
            private const string CountyExtendedPropertyName = "NAME";
            private readonly string ValueExtendedPropertyName = "VALUE";
      
            public MainPage()
            {
                InitializeComponent();
      
                this.radMap.InitializeCompleted += new EventHandler(radMap_InitializeCompleted);
            }
      
            private void radMap_InitializeCompleted(object sender, EventArgs e)
            {
                if (!this.initialized)
                {
                    this.initialized = true;
      
                    this.SetupReaders();
                }
            }
      
            public void SetupReaders()
            {
                this.informationLayer.Reader = new MapShapeReader();
                this.informationLayer.Reader.PreviewReadCompleted += new PreviewReadShapesCompletedEventHandler(Reader_PreviewReadCompleted);
                this.informationLayer.Reader.Source = new Uri(string.Format(ShapeRelativeUriFormat, "usa_states"), UriKind.Relative);
                this.informationLayer.Reader.ToolTipFormat = StateExtendedPropertyName;
            }
      
            private void Reader_PreviewReadCompleted(object sender, PreviewReadShapesCompletedEventArgs eventArgs)
            {
                int i = 0;
                foreach (MapShape shape in eventArgs.Items)
                {
                    i++;
                    SetExtendedProperty(shape, double.Parse(i.ToString()));
                }
      
                this.SetColorizer(eventArgs.Items);
            }
      
            private void SetColorizer(List<FrameworkElement> list)
            {
                var colorizer = this.informationLayer.Colorizer as ColorMeasureScale;
      
                colorizer.ExtendedPropertyName = ValueExtendedPropertyName;
                colorizer.SetColorByExtendedData(list, ValueExtendedPropertyName, true);
            }
      
            private void SetExtendedProperty(MapShape shape, double value)
            {
      
                if (!shape.ExtendedData.PropertySet.ContainsKey(ValueExtendedPropertyName))
                    shape.ExtendedData.PropertySet.RegisterProperty(ValueExtendedPropertyName, string.Empty, typeof(double), 0d);
      
                shape.ExtendedData.SetValue(ValueExtendedPropertyName, value);
            }
        }
    }

    Sincerely,
    Andrey Murzov
    the Telerik team
    Browse the videos here>> to help you get started with RadControls for Silverlight
  10. Tamas
    Tamas avatar
    33 posts
    Member since:
    Jun 2010

    Posted 23 Nov 2010 Link to this post

    Hi Andrey,

    Oops yes that makes a difference :)

    Many thanks for your help, have a nice day
    Tamas
Back to Top
DevCraft banner