Legend is always visible in the DataTemplate mode

2 posts, 0 answers
  1. Alex
    Alex avatar
    13 posts
    Member since:
    Jun 2011

    Posted 02 Aug 2011 Link to this post

    Please try the next code and fill the difference when the Chart used as DataTemplate and as control.

    WPF 4.0 (4.0.30319)
    Telerik.Windows.Controls.Charting.dll 2011.2.725.40


    Codebehind:
    public partial class XAxisTest : Window
        {
            public XAxisTest()
            {
                InitializeComponent();
                InitializeComponent();
                Context = new Context();
                DataContext = this;
            }
            public Context Context { get; set; }
        }
        public class Context
        {
            public Context()
            {
                Charts = new ObservableCollection<ChartDataHolder> { new ChartDataHolder(1), new ChartDataHolder(100) };
            }
            public ChartDataHolder FirstData
            {
                get
                {
                    return Charts[0];
                }
            }
            public ObservableCollection<ChartDataHolder> Charts { get; private set; }
        }
        public class ChartDataHolder
        {
            public ChartDataHolder(double delta)
            {
                ChartData = new ObservableCollection<ChartDataItem>();
                for (int i = 0; i < 20; i++)
                {
                    ChartData.Add(new ChartDataItem() { ValueX = DateTime.Now.Second + i, ValueY = DateTime.Now.Second * delta + i });
                }
            }
            public ObservableCollection<ChartDataItem> ChartData { get; private set; }
        }
        public class ChartDataItem
        {
            public int ValueX { get; set; }
            public double ValueY { get; set; }
        }

    View

    <Window>
    <Window.Resources>
            <DataTemplate x:Key="measTemplate" x:Shared="false">
                <GroupBox DockPanel.Dock="Bottom" Height="200" Header="I have a legend, but why?">
                    <telerik:RadChart ItemsSource="{Binding ChartData, Mode=OneWay}"  >
                        <telerik:RadChart.DefaultView>
                            <telerik:ChartDefaultView>
                                <telerik:ChartDefaultView.ChartLegend>
                                    <telerik:ChartLegend Visibility="Collapsed" UseAutoGeneratedItems="True" Header=" " x:Name="chartLegend">
                                    </telerik:ChartLegend>
                                </telerik:ChartDefaultView.ChartLegend>
                                <telerik:ChartDefaultView.ChartArea>
                                    <telerik:ChartArea LegendName="chartLegend" EnableAnimations="True">
                                        <telerik:ChartArea.AxisX>
                                            <telerik:AxisX />
                                        </telerik:ChartArea.AxisX>
                                        <telerik:ChartArea.AxisY>
                                            <telerik:AxisY DefaultLabelFormat="F2"/>
                                        </telerik:ChartArea.AxisY>
                                    </telerik:ChartArea>
                                </telerik:ChartDefaultView.ChartArea>
                            </telerik:ChartDefaultView>
                        </telerik:RadChart.DefaultView>
                        <telerik:RadChart.SeriesMappings>
                            <telerik:SeriesMapping LegendLabel="C">
                                <telerik:SeriesMapping.SeriesDefinition>
                                    <telerik:LineSeriesDefinition ShowItemLabels="False"/>
                                </telerik:SeriesMapping.SeriesDefinition>
                                <telerik:ItemMapping FieldName="ValueY" DataPointMember="YValue"/>
                                <telerik:ItemMapping FieldName="ValueX" DataPointMember="XValue"/>
                            </telerik:SeriesMapping>
                        </telerik:RadChart.SeriesMappings>
                    </telerik:RadChart>
                </GroupBox>
            </DataTemplate>
                </Window.Resources>
            <DockPanel>


            <GroupBox DockPanel.Dock="Top" Height="200" Header="I have no a legend">
                <telerik:RadChart ItemsSource="{Binding Context.FirstData.ChartData, Mode=OneWay}" Margin="6" >
                <telerik:RadChart.DefaultView>
                    <telerik:ChartDefaultView>
                        <telerik:ChartDefaultView.ChartLegend>
                            <telerik:ChartLegend Visibility="Collapsed" UseAutoGeneratedItems="True" Header=" " x:Name="chartLegend1">
                            </telerik:ChartLegend>
                        </telerik:ChartDefaultView.ChartLegend>
                        <telerik:ChartDefaultView.ChartArea>
                            <telerik:ChartArea LegendName="chartLegend1" EnableAnimations="True">
                                <telerik:ChartArea.AxisX>
                                    <telerik:AxisX />
                                </telerik:ChartArea.AxisX>
                                <telerik:ChartArea.AxisY>
                                    <telerik:AxisY DefaultLabelFormat="F2"/>
                                </telerik:ChartArea.AxisY>
                            </telerik:ChartArea>
                        </telerik:ChartDefaultView.ChartArea>
                    </telerik:ChartDefaultView>
                </telerik:RadChart.DefaultView>
                <telerik:RadChart.SeriesMappings>
                    <telerik:SeriesMapping LegendLabel="C">
                        <telerik:SeriesMapping.SeriesDefinition>
                            <telerik:LineSeriesDefinition ShowItemLabels="False"/>
                        </telerik:SeriesMapping.SeriesDefinition>
                        <telerik:ItemMapping FieldName="ValueY" DataPointMember="YValue"/>
                        <telerik:ItemMapping FieldName="ValueX" DataPointMember="XValue"/>
                    </telerik:SeriesMapping>
                </telerik:RadChart.SeriesMappings>
            </telerik:RadChart>
            </GroupBox>
            <ItemsControl  ItemsSource="{Binding Context.Charts}" ItemTemplate="{StaticResource measTemplate}">
                <ItemsControl.ItemsPanel>
                    <ItemsPanelTemplate>
                        <UniformGrid Columns="1" />
                    </ItemsPanelTemplate>
                </ItemsControl.ItemsPanel>
            </ItemsControl>
        </DockPanel>
    </Window>
  2. Yavor
    Admin
    Yavor avatar
    401 posts

    Posted 05 Aug 2011 Link to this post

    Hi Alex,

    It seems that the ChartLegend Visibility property is modified by the chart internal mechanisms even if custom layout is used. I have forwarded your report to our developers for further review. You can try meanwhile to listen for Loaded event and manually hide the legend from the code behind like this:

    private void RadChart_Loaded(object sender, RoutedEventArgs e)
    {
        RadChart chart = sender as RadChart;
        ChartLegend legend = chart.ChildrenOfType<ChartLegend>().Single();
        legend.Visibility = Visibility.Collapsed;
    }

    You can also remove the legend from the template completely if you don't need it. Your simplified layout can look like this:
    Copy Code
    <DataTemplate>
        <telerik:RadChart ItemsSource="{Binding}" UseDefaultLayout="False">
            <telerik:ChartArea Name="chartArea" />
     
            <telerik:RadChart.SeriesMappings>
                <telerik:SeriesMapping LegendLabel="SUCCESS"ChartAreaName="chartArea">
                    <telerik:SeriesMapping.ItemMappings>
                        <telerik:ItemMapping DataPointMember="YValue" />
                    </telerik:SeriesMapping.ItemMappings>
                </telerik:SeriesMapping>
            </telerik:RadChart.SeriesMappings>
        </telerik:RadChart>
    </DataTemplate>

    Hope this helps! Greetings,
    Yavor Ivanov
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get now >>

  3. UI for WPF is Visual Studio 2017 Ready
Back to Top