Hide Legend Items

2 posts, 0 answers
  1. SUMO
    SUMO avatar
    1 posts
    Member since:
    Nov 2008

    Posted 24 Aug 2011 Link to this post

    Hi,

    I have a situation where I need to hide series on a bar chart.

    I can hide the series by doing this...
    //hide unused series
    barChart.DefaultView.ChartArea.DataSeries[1].Definition.Visibility = SeriesVisibility.Collapsed;

    however, the series stays in the legend. So I need to remove it there too.

    But this doesn't work... how do I hide the series from the legend as well?
    barChart.DefaultView.ChartLegend.Items[1].Visibility = Visibility.Collapsed;

    Here's my XAML where I specify 2 series...
    <telerikControls:RadChart x:Name="barChart" Grid.Column="1" Grid.Row="3" Height="475" Width="720" HorizontalAlignment="Left" Background="Transparent" BorderThickness="0" AxisForeground="White" AxisElementBrush="White" >
                <telerikControls:RadChart.PaletteBrushes>
                    <SolidColorBrush   Color="#009ad4" />
                    <SolidColorBrush   Color="#bfe9f5" />
                </telerikControls:RadChart.PaletteBrushes>
                <telerikControls:RadChart.DefaultView>
                    <telerikCharting:ChartDefaultView ChartLegendPosition="Top">
                        <telerikCharting:ChartDefaultView.ChartArea>
                            <telerikCharting:ChartArea LegendName="ChartLegend1"></telerikCharting:ChartArea>
                        </telerikCharting:ChartDefaultView.ChartArea>
                        <telerikCharting:ChartDefaultView.ChartLegend>
                            <telerikCharting:ChartLegend BorderThickness="0" Header="" x:Name="ChartLegend1" Background="Transparent">                           
                            </telerikCharting:ChartLegend>
                        </telerikCharting:ChartDefaultView.ChartLegend>
                    </telerikCharting:ChartDefaultView>
                </telerikControls:RadChart.DefaultView>
     
                <!--SERIES MAPPING -->
                <telerikControls:RadChart.SeriesMappings>
                    <telerikCharting:SeriesMapping LegendLabel="2011" >
                        <telerikCharting:SeriesMapping.SeriesDefinition>
                            <telerikCharting:BarSeriesDefinition ItemStyle="{StaticResource BarStyle}"></telerikCharting:BarSeriesDefinition>
                        </telerikCharting:SeriesMapping.SeriesDefinition>
                        <telerikCharting:SeriesMapping.ItemMappings>
                            <telerikCharting:ItemMapping FieldName="Title" DataPointMember="XCategory"></telerikCharting:ItemMapping>
                            <telerikCharting:ItemMapping FieldName="Dec" DataPointMember="YValue"></telerikCharting:ItemMapping>
                        </telerikCharting:SeriesMapping.ItemMappings>
                    </telerikCharting:SeriesMapping>
                    <telerikCharting:SeriesMapping LegendLabel="2010">
                        <telerikCharting:SeriesMapping.SeriesDefinition>
                            <telerikCharting:BarSeriesDefinition ItemStyle="{StaticResource BarStyle}"></telerikCharting:BarSeriesDefinition>
                        </telerikCharting:SeriesMapping.SeriesDefinition>
                        <telerikCharting:SeriesMapping.ItemMappings>
                            <telerikCharting:ItemMapping FieldName="Title" DataPointMember="XCategory"></telerikCharting:ItemMapping>
                            <telerikCharting:ItemMapping FieldName="Dec" DataPointMember="YValue"></telerikCharting:ItemMapping>
                        </telerikCharting:SeriesMapping.ItemMappings>
                    </telerikCharting:SeriesMapping>
                </telerikControls:RadChart.SeriesMappings>
            </telerikControls:RadChart>

    And my onload where I hide the series...

    private void UserControl_Loaded(object sender, RoutedEventArgs e)
            {
                //perform extra styling on load
                barChart.DefaultView.ChartTitle.Visibility = Visibility.Collapsed;
                barChart.DefaultView.ChartArea.AxisY.StripLinesVisibility = Visibility.Hidden;
                barChart.DefaultView.ChartArea.AxisY.MajorGridLinesVisibility = Visibility.Visible;
                barChart.DefaultView.ChartArea.AxisY.AxisStyles.ItemLabelStyle = this.Resources["AxisYLabelStyle"] as Style;
                
                //attach click event
                barChart.DefaultView.ChartArea.ItemClick +=new EventHandler<ChartItemClickEventArgs>(ChartArea_ItemClick);
     
                //Set itemsource
                barChart.ItemsSource = ItemsSource.Items;
     
                //hide unused series
                barChart.DefaultView.ChartArea.DataSeries[1].Definition.Visibility = SeriesVisibility.Collapsed;
       
    //doesn't hide legend item...
    barChart.DefaultView.ChartLegend.Items[1].Visibility = Visibility.Collapsed;
            }


    Thanks!

    Ken
  2. Evgenia
    Admin
    Evgenia avatar
    1406 posts

    Posted 29 Aug 2011 Link to this post

    Hi Sumo,

    The expected behavior is that the LegendLabel of the Series should be visible no matter if the Series visibility is collapsed or hidden. However there is a workaround you could use in your case.

    If you set the series' LegendDisplayMode to None and then set the series' Visibility to Collapsed, this will hide both the series and the legend label.

    For example:
    mySeriesMapping.SeriesDefinition.LegendDisplayMode = LegendDisplayMode.None;
    mySeriesMapping.SeriesDefinition.Visibility = SeriesVisibility.Collapsed;

    Note that the LegendDisplayMode should be defined first. Also this will work only for Visibility set to Collapsed.

    Best wishes,
    Evgenia
    the Telerik team

    Thank you for being the most amazing .NET community! Your unfailing support is what helps us charge forward! We'd appreciate your vote for Telerik in this year's DevProConnections Awards. We are competing in mind-blowing 20 categories and every vote counts! VOTE for Telerik NOW >>

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