Charting: How do I remove a legend from graph?

Jul 27, 2010 at 12:22 PM
Edited Jul 27, 2010 at 12:54 PM

How do I remove a legend from graph?  For example a LineSeries chart.  An example of the XAML that does this would be nice.

PS.  In fact can I extend this to ask how to remove both (a) legend and (b) title from a graph.  That is such that the remaining area to render in my WPF application is just the inner box that represents the graph itself - i.e. so the extra spacing around this for the title and legend will drop away therefore.

Jul 27, 2010 at 6:41 PM
Edited Jul 27, 2010 at 6:41 PM

To remove the Legend, I copied the chart style template from WPFToolkit\DataVisualization\Themes\generic.xaml (look for charting:chart) and removed the reference to legend.

I left the title in, but you can just remove the datavis:Title line below.

 

<Grid.Resources>
    <!-- chart style modified from WPFToolkit\DataVisualization\Themes\generic.xaml -->
    <Style TargetType="charting:Chart">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="charting:Chart">
                    <Grid>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto" />
                            <RowDefinition Height="*" />
                        </Grid.RowDefinitions>

                        <datavis:Title Content="{TemplateBinding Title}" Style="{TemplateBinding TitleStyle}" />

                        <!-- Use a nested Grid to avoid possible clipping behavior resulting from ColumnSpan+Width=Auto -->
                        <Grid Grid.Row="1" Margin="5,15,0,15">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="*" />
                                <ColumnDefinition Width="Auto" />
                            </Grid.ColumnDefinitions>

                            <chartingprimitives:EdgePanel x:Name="ChartArea" Style="{TemplateBinding ChartAreaStyle}">
                                <Grid Canvas.ZIndex="-1" Style="{TemplateBinding PlotAreaStyle}" />
                                <Border Canvas.ZIndex="10" BorderBrush="#FF919191" BorderThickness="1" />
                            </chartingprimitives:EdgePanel>
                        </Grid>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</Grid.Resources>

 

I have these references defined for my UserControl:

    xmlns:datavis="clr-namespace:System.Windows.Controls.DataVisualization;assembly=System.Windows.Controls.DataVisualization.Toolkit"
    xmlns:chartingprimitives="clr-namespace:System.Windows.Controls.DataVisualization.Charting.Primitives;assembly=System.Windows.Controls.DataVisualization.Toolkit"
    xmlns:charting="clr-namespace:System.Windows.Controls.DataVisualization.Charting;assembly=System.Windows.Controls.DataVisualization.Toolkit"

<Grid.Resources>
                                        <!-- chart style modified from WPFToolkit\DataVisualization\Themes\generic.xaml -->
                                        <Style TargetType="charting:Chart">
                                            <Setter Property="Template">
                                                <Setter.Value>
                                                    <ControlTemplate TargetType="charting:Chart">
                                                        <Grid>
                                                            <Grid.RowDefinitions>
                                                                <RowDefinition Height="Auto" />
                                                                <RowDefinition Height="*" />
                                                            </Grid.RowDefinitions>

                                                            <datavis:Title Content="{TemplateBinding Title}" Style="{TemplateBinding TitleStyle}" />

                                                            <!-- Use a nested Grid to avoid possible clipping behavior resulting from ColumnSpan+Width=Auto -->
                                                            <Grid Grid.Row="1" Margin="5,15,0,15">
                                                                <Grid.ColumnDefinitions>
                                                                    <ColumnDefinition Width="*" />
                                                                    <ColumnDefinition Width="Auto" />
                                                                </Grid.ColumnDefinitions>

                                                                <chartingprimitives:EdgePanel x:Name="ChartArea" Style="{TemplateBinding ChartAreaStyle}">
                                                                    <Grid Canvas.ZIndex="-1" Style="{TemplateBinding PlotAreaStyle}" />
                                                                    <Border Canvas.ZIndex="10" BorderBrush="#FF919191" BorderThickness="1" />
                                                                </chartingprimitives:EdgePanel>
                                                            </Grid>
                                                        </Grid>
                                                    </ControlTemplate>
                                                </Setter.Value>
                                            </Setter>
                                        </Style>
                                    </Grid.Resources><Grid.Resources>
                                        <!-- chart style modified from WPFToolkit\DataVisualization\Themes\generic.xaml -->
                                        <Style TargetType="charting:Chart">
                                            <Setter Property="Template">
                                                <Setter.Value>
                                                    <ControlTemplate TargetType="charting:Chart">
                                                        <Grid>
                                                            <Grid.RowDefinitions>
                                                                <RowDefinition Height="Auto" />
                                                                <RowDefinition Height="*" />
                                                            </Grid.RowDefinitions>

                                                            <datavis:Title Content="{TemplateBinding Title}" Style="{TemplateBinding TitleStyle}" />

                                                            <!-- Use a nested Grid to avoid possible clipping behavior resulting from ColumnSpan+Width=Auto -->
                                                            <Grid Grid.Row="1" Margin="5,15,0,15">
                                                                <Grid.ColumnDefinitions>
                                                                    <ColumnDefinition Width="*" />
                                                                    <ColumnDefinition Width="Auto" />
                                                                </Grid.ColumnDefinitions>

                                                                <chartingprimitives:EdgePanel x:Name="ChartArea" Style="{TemplateBinding ChartAreaStyle}">
                                                                    <Grid Canvas.ZIndex="-1" Style="{TemplateBinding PlotAreaStyle}" />
                                                                    <Border Canvas.ZIndex="10" BorderBrush="#FF919191" BorderThickness="1" />
                                                                </chartingprimitives:EdgePanel>
                                                            </Grid>
                                                        </Grid>
                                                    </ControlTemplate>
                                                </Setter.Value>
                                            </Setter>
                                        </Style>
                                    </Grid.Resources>
Jul 28, 2010 at 6:59 AM

thanks lkvarda,

Do you know how I could better get an overview on how the templates approach here works, and the various Graph elements in XAML? 

I'm guessing the answer may be in two parts: (a) re usage of Grid.Resources this might be a XAML/WPF thing that I have to read up on, and (b) the specific Graph XAML - which one would need to be across.  Re the last point here is there any doco/tutorial you know of that I could look at? 

 

Jul 28, 2010 at 2:05 PM
You can set Width and Height property to Zero. Which will disable the legend style in graph.