This is a migrated thread and some comments may be shown as answers.

I can't get the legend to show up on RadCartesianChart

2 Answers 461 Views
Chart
This is a migrated thread and some comments may be shown as answers.
Bruno
Top achievements
Rank 1
Bruno asked on 13 Jul 2015, 09:42 AM

I have a project where we have an unknown number of series to display and I'd like to make a legend. From the online documentation, it looks like telerik supports all of this, so I must be doing something wrong : the legend just doesn't show up. Could anyone point me to the problem ? Thank you very much.

Out of curiosity, I would also like to know if there's a way to bind to the series properties once they're displayed - e.g. if I had to design a legend by hand, how would I bind to the colours of the series if I don't know them before-hand (because they're dependent on the chosen palette) ?

I've reproduced this in a smaller project, here's my code :

01.using System.Collections.Generic;
02.using System.Windows;
03. 
04.namespace telerikTestSandbox
05.{
06.    public class Serie
07.    {
08.        public Serie(string title)
09.        {
10.            Title = title;
11.            Data = new List<Point>() { new Point(1, 5), new Point(2, 15), new Point(3, 10) };
12.        }
13. 
14.        public List<Point> Data { get; set; }
15. 
16.        public string Title { get; set; }
17.    }
18.}

01.using System.Collections.Generic;
02.using System.Windows;
03. 
04.namespace telerikTestSandbox
05.{
06.    /// <summary>
07.    /// Interaction logic for MainWindow.xaml
08.    /// </summary>
09.    public partial class MainWindow : Window
10.    {
11.        public MainWindow()
12.        {
13.            DataContext = this;
14. 
15.            Serie serie2 = new Serie("Test2");
16.            serie2.Data = new List<Point>() { new Point(1, 25), new Point(2, 30), new Point(3,20)};
17.            ParticipationData.Add(serie2);
18.            Serie serie3 = new Serie("Test3");
19.            serie3.Data = new List<Point>() { new Point(1, 15), new Point(2, 20), new Point(3, 25) };
20.            ParticipationData.Add(serie3);
21.        }
22. 
23.        public List<Serie> ParticipationData
24.        {
25.            get { return (List<Serie>)GetValue(ParticipationDataProperty); }
26.            set { SetValue(ParticipationDataProperty, value); }
27.        }
28. 
29.        // Using a DependencyProperty as the backing store for ParticipationData.  This enables animation, styling, binding, etc...
30.        public static readonly DependencyProperty ParticipationDataProperty =
31.            DependencyProperty.Register("ParticipationData", typeof(List<Serie>), typeof(MainWindow), new PropertyMetadata(new List<Serie>() {new Serie("Test1")}));
32.    }
33.}

01.<Window x:Class="telerikTestSandbox.MainWindow"
04.        Title="MainWindow" Height="350" Width="525"
05.        xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"
06.        xmlns:sys="clr-namespace:System;assembly=mscorlib"
07.        xmlns:local="clr-namespace:telerikTestSandbox"
08.        >
09.    <Window.Resources>
10.        <Style x:Key="PerformanceVisualisationStyle" TargetType="telerik:ScatterLineSeries" BasedOn="{StaticResource ScatterLineSeriesStyle}">
11.            <Setter Property="PointSize" Value="3,3" />
12.            <Setter Property="RenderMode" Value="Full" />
13.            <Setter Property="Grid.IsEnabled" Value="True" />
14.            <Setter Property="Grid.Visibility" Value="Visible" />
15.            <Setter Property="Grid.ShowGridLines" Value="True"/>
16.            <Setter Property="LegendSettings">
17.                <Setter.Value>
18.                    <telerik:SeriesLegendSettings Title="{Binding Title}"/>
19.                </Setter.Value>
20.            </Setter>
21.        </Style>
22.    </Window.Resources>
23.     
24.    <Grid>
25.        <Grid.RowDefinitions>
26.            <RowDefinition />
27.            <RowDefinition />
28.        </Grid.RowDefinitions>
29.        <telerik:RadCartesianChart Grid.Row="0" x:Name="Chart" Palette="Lilac" Height="250" Width="500">
30.            <telerik:RadCartesianChart.HorizontalAxis>
31.                <telerik:LinearAxis Title="Position" />
32.            </telerik:RadCartesianChart.HorizontalAxis>
33.            <telerik:RadCartesianChart.VerticalAxis>
34.                <telerik:LinearAxis Title="Value" />
35.            </telerik:RadCartesianChart.VerticalAxis>
36.            <telerik:RadCartesianChart.SeriesProvider>
37.                <telerik:ChartSeriesProvider Source="{Binding ParticipationData}">
38.                    <telerik:ChartSeriesProvider.SeriesDescriptors>
39.                        <telerik:ScatterSeriesDescriptor ItemsSourcePath="Data" XValuePath="X" YValuePath="Y"
40.                                                         Style="{StaticResource PerformanceVisualisationStyle}">
41.                        </telerik:ScatterSeriesDescriptor>
42.                    </telerik:ChartSeriesProvider.SeriesDescriptors>
43.                </telerik:ChartSeriesProvider>
44.            </telerik:RadCartesianChart.SeriesProvider>
45. 
46.            <telerik:RadCartesianChart.Grid>
47.                <telerik:CartesianChartGrid MajorXLinesRenderMode="All" MajorLinesVisibility="XY" />
48.            </telerik:RadCartesianChart.Grid>
49.        </telerik:RadCartesianChart>
50. 
51.        <telerik:RadLegend Grid.Row="1" Background="White"
52.                       BorderBrush="Black"
53.                       BorderThickness="1"
54.                       Items="{Binding LegendItems, ElementName=Chart}"
55.                       HorizontalAlignment="Right"
56.                       VerticalAlignment="Top" />
57.    </Grid>
58.</Window>

2 Answers, 1 is accepted

Sort by
0
Martin Ivanov
Telerik team
answered on 15 Jul 2015, 08:53 AM
Hello Bruno,

Thank you for the code snippets. Based on them I created a small example, but I was not able to reproduce the reported behavior. The legend is displayed correctly. Can you please take a look at the attached project and let me know if I am missing something? Also, can you please tell me which version of our controls you are using?

About, creating the legend manually, you can populate its Items with objects of type LegendItem. If the series color is set with a palette as in your case, you can get it using the palette's GetEntry() method. For example:
var paletteEntry = chart.Palette.GetEntry(series, chart.Series.IndexOf(series));

I hope this information is useful.

Regards,
Martin
Telerik
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 Feedback Portal and vote to affect the priority of the items
0
Bruno
Top achievements
Rank 1
answered on 15 Jul 2015, 12:16 PM

Thanks a lot for your reply.

I tried copying code from your sample and still had the same problem. Then I found a way to make it work. Apparently I had to explicitly set a style to the RadLegend control. I can't say I understand it completely but from what I found on other posts on this forum it may have to do with our project using the NoXaml binaries of telerik. Anyway, I found a style for the legend and now it works fine

 

Sorry for the inconvenience, and again, thanks for the support !

Tags
Chart
Asked by
Bruno
Top achievements
Rank 1
Answers by
Martin Ivanov
Telerik team
Bruno
Top achievements
Rank 1
Share this question
or