Maps for WPF | ComponentOne
In This Topic
    Legend
    In This Topic

    Legends in maps provide valuable information, which helps in interpreting what the map is displaying. Legends can be represented in various colors and shapes based on the data. You can add legends to describe the vector elements and markers on a map.

    Here, we will be using a list box to populate the legend items with Maps control.

    Image depicting chart and legend.

    The following code snippet depicts how to add legend items with Maps control.

    void InitLegend()
            {
                // create legend 
                legend.Items.Clear();
                ColorValues cvals = (ColorValues)countries.Converter;
                int cnt = cvals.Count;
                double sz = 20;
                for (int i = 0; i < cnt - 1; i++)
                {
                    ColorValue cv = cvals[i];
                    ListBoxItem lbi = new ListBoxItem()
                    {
                        Height = sz,
                        Margin = new Thickness(0),
                        Padding = new Thickness(0)
                    };
                    StackPanel sp = new StackPanel() { Orientation = Orientation.Horizontal };
                    LinearGradientBrush lgb = new LinearGradientBrush() { StartPoint = new Point(0, 0), EndPoint = new Point(0, 1) };
                    lgb.GradientStops.Add(new GradientStop() { Color = cv.Color, Offset = 0 });
                    lgb.GradientStops.Add(new GradientStop() { Color = cvals[i + 1].Color, Offset = 1 });
                    sp.Children.Add(new Rectangle()
                    {
                        Width = sz,
                        Height = sz,
                        Fill = lgb,
                        Stroke = new SolidColorBrush(Colors.LightGray),
                        StrokeThickness = 0.5,
                        RenderTransform = new TranslateTransform() { Y = 0.5 * sz }
                    });
                    sp.Children.Add(new TextBlock()
                    {
                        Height = sz,
                        Text = cv.Value.ToString(),
                        VerticalAlignment = VerticalAlignment.Center,
                    });
                    lbi.Content = sp;
                    legend.Items.Add(lbi);
                }
                legend.Items.Add(new ListBoxItem() { Height = sz });
            }