Chart Legend is missing

3 posts, 1 answers
  1. Liberte
    Liberte avatar
    6 posts
    Member since:
    Jul 2008

    Posted 20 Jul 2009 Link to this post

    Hello guys, i have that lines in my code-behind:
    1 public partial class MainWindow : Window 
    2     { 
    3         public MainWindow() 
    4         { 
    5             this.InitializeComponent(); 
    6             MainWindowViewModel model = new MainWindowViewModel(); 
    7             RadChart1.DefaultView.ChartArea = model.MainChartArea; 
    8             RadChart1.DefaultView.ChartLegend.UseAutoGeneratedItems = true
    9         }         
    10     } 
    11     public class MainWindowViewModel 
    12     { 
    13         private ChartArea _area; 
    14         public ChartArea MainChartArea 
    15         { 
    16             get  
    17             { 
    18                 if (_area == null
    19                     _area = CreateChartArea(); 
    20                 return _area; 
    21             } 
    22         }         
    23         private ChartArea CreateChartArea() 
    24         { 
    25             ChartArea area = new ChartArea(); 
    26  
    27             DataSeries ser1 = new DataSeries(); 
    28             ser1.Add(new DataPoint(23)); 
    29             ser1.Add(new DataPoint(45)); 
    30             ser1.Add(new DataPoint(33)); 
    31             ser1.Add(new DataPoint(12)); 
    32             ser1.Add(new DataPoint(50)); 
    33             ser1.LegendLabel = "Series 1 Label"
    34             area.DataSeries.Add(ser1); 
    35  
    36             DataSeries ser2 = new DataSeries(); 
    37             ser2.Add(new DataPoint(45)); 
    38             ser2.Add(new DataPoint(50)); 
    39             ser2.Add(new DataPoint(60)); 
    40             ser2.Add(new DataPoint(30)); 
    41             ser2.Add(new DataPoint(25)); 
    42             ser2.LegendLabel = "Series 2 Label"
    43             area.DataSeries.Add(ser2); 
    44  
    45             return area; 
    46         } 
    47     } 
    and that is the XAML part:
    1 <Grid x:Name="LayoutRoot"
    2         <control:RadChart x:Name="RadChart1"
    3             <control:RadChart.DefaultView> 
    4                 <chart:ChartDefaultView> 
    5                      
    6                     <chart:ChartDefaultView.ChartLegend> 
    7                         <chart:ChartLegend x:Name="CustomLegend" Header="Legend Header" UseAutoGeneratedItems="True"
    8                         </chart:ChartLegend> 
    9                     </chart:ChartDefaultView.ChartLegend> 
    10                     <chart:ChartDefaultView.ChartTitle> 
    11                         <chart:ChartTitle> 
    12                             <TextBlock Text="Declarative RadChart"/> 
    13                         </chart:ChartTitle> 
    14                     </chart:ChartDefaultView.ChartTitle> 
    15                 </chart:ChartDefaultView> 
    16             </control:RadChart.DefaultView> 
    17         </control:RadChart> 
    18  
    19     </Grid> 
    but now... i don't have the Legend generated. So what's missing? How to get back the Legend if i use something like MVVM?


  2. Answer
    Giuseppe
    Admin
    Giuseppe avatar
    2363 posts

    Posted 23 Jul 2009 Link to this post

    Hi Liberte,

    You need to explicitly associate the ChartArea and the ChartLegend in this scenario as you are swapping the default ChartArea instance:

    public Window1() 
        this.InitializeComponent(); 
        MainWindowViewModel model = new MainWindowViewModel(); 
        model.MainChartArea.Legend = RadChart1.DefaultView.ChartLegend; 
        RadChart1.DefaultView.ChartArea = model.MainChartArea; 
        RadChart1.DefaultView.ChartLegend.UseAutoGeneratedItems = true



    Regards,
    Manuel
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
  3. Liberte
    Liberte avatar
    6 posts
    Member since:
    Jul 2008

    Posted 24 Jul 2009 Link to this post

    Oh, yes .. that makes sense. Thanks :}
Back to Top