WPF图表工具包图例位置

时间:2017-08-25 10:23:42

标签: c# wpf charts legend

如何在WPF Charting Toolkit中更改图例的位置?

我正在使用Line Series,图例显示在右侧,缩小了图表区域的宽度。

我想在图表的右上角找到图例。

怎么做?

1 个答案:

答案 0 :(得分:1)

您需要稍微修改ControlTemplate

<chartingToolkit:Chart Title="...">
    <chartingToolkit:Chart.Template>
        <ControlTemplate TargetType="{x:Type chartingToolkit:Chart}"
                                 xmlns:visualizationToolkit="clr-namespace:System.Windows.Controls.DataVisualization;assembly=System.Windows.Controls.DataVisualization.Toolkit" 
                                 xmlns:Primitives="clr-namespace:System.Windows.Controls.DataVisualization.Charting.Primitives;assembly=System.Windows.Controls.DataVisualization.Toolkit" >
            <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Padding="{TemplateBinding Padding}">
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="*"/>
                    </Grid.RowDefinitions>
                    <visualizationToolkit:Title Content="{TemplateBinding Title}" Style="{TemplateBinding TitleStyle}"/>
                    <Grid Grid.Row="1">
                        <Primitives:EdgePanel x:Name="ChartArea" Style="{TemplateBinding ChartAreaStyle}">
                            <Grid Style="{TemplateBinding PlotAreaStyle}" Panel.ZIndex="-1"/>
                            <Border BorderBrush="#FF919191" BorderThickness="1" Panel.ZIndex="10"/>
                        </Primitives:EdgePanel>
                        <visualizationToolkit:Legend x:Name="Legend"
                                                             Style="{TemplateBinding LegendStyle}" 
                                                             Title="{TemplateBinding LegendTitle}"
                                                             HorizontalAlignment="Right"
                                                             VerticalAlignment="Top"
                                                             Margin="0 2 2 0"/>
                    </Grid>
                </Grid>
            </Border>
        </ControlTemplate>
    </chartingToolkit:Chart.Template>
</chartingToolkit:Chart>