【问题标题】:Change Silverlight Chart Legend Item Layout更改 Silverlight 图表图例项目布局
【发布时间】:2012-08-03 11:30:58
【问题描述】:

我正在定制 Silverlight 工具包图表的布局。我有两个要求:

1) 将图例区域移动到图表底部(已解决)。

2) 更改图例中元素的布局,使其彼此相邻显示, IE。 {legend 1},{legend 2},{legend 3},而不是默认的列格式。


1) 使用 ControlTemplate 很容易解决(见下文)。

2) 如何更改图例项的布局?可以通过进一步自定义Chart的ControlTemplate来实现,还是Legend需要自己的ControlTemplate?

图表本身定义为:

<chartingToolkit:Chart Name="chartCompareMain" 
                       Template="{StaticResource ChartLayoutLegendBottom}">
        <chartingToolkit:Chart.Axes>
             <chartingToolkit:DateTimeAxis Orientation="X" 
                        AxisLabelStyle="{StaticResource ChartDateFormat}">
             </chartingToolkit:DateTimeAxis>
             <chartingToolkit:LinearAxis  Orientation="Y"/>
        </chartingToolkit:Chart.Axes>
</chartingToolkit:Chart>    

移动图例项的ControlTemplate(基于默认模板)是:

<ControlTemplate x:Key="ChartLayoutLegendBottom" TargetType="chartingToolkit:Chart">
        <Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Padding="{TemplateBinding Padding}">
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="*" />
                    <RowDefinition Height="Auto" />
                </Grid.RowDefinitions>
                <dataviz:Title Grid.Row="0" Content="{TemplateBinding Title}" Style="{TemplateBinding TitleStyle}" />
                <Grid Grid.Row="1" Margin="0,15,0,15">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*" />
                    </Grid.ColumnDefinitions>
                    <chartingprimitives:EdgePanel x:Name="ChartArea" Style="{TemplateBinding ChartAreaStyle}" Grid.Column="0" >
                        <Grid Canvas.ZIndex="-1" Style="{TemplateBinding PlotAreaStyle}" />
                        <Border Canvas.ZIndex="10" BorderBrush="#FF919191" BorderThickness="1" />
                    </chartingprimitives:EdgePanel>
                </Grid>
                <dataviz:Legend x:Name="Legend" Header="{TemplateBinding LegendTitle}" Style="{TemplateBinding LegendStyle}" Grid.Row="2"/>
            </Grid>
        </Border>
    </ControlTemplate>

【问题讨论】:

    标签: silverlight charts toolkit legend


    【解决方案1】:

    为了完整起见,这里是LegendStyle="{StaticResource BottomLegendLayout}(找出样式的非常有用的工具是Silverlight Default Style Browser)

            <Style x:Name="BottomLegendLayout" TargetType="dataviz:Legend">
            <Setter Property="BorderBrush" Value="Black" />
            <Setter Property="BorderThickness" Value="1" />
            <Setter Property="IsTabStop" Value="False" />
            <Setter Property="ItemContainerStyle">
                <Setter.Value>
                    <Style TargetType="chartingToolkit:LegendItem" >
                        <Setter Property="IsTabStop" Value="False" />
                        <Setter Property="Template">
                            <Setter.Value>
                                <ControlTemplate TargetType="chartingToolkit:LegendItem">
                                    <Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}">
                                        <StackPanel Orientation="Horizontal" VerticalAlignment="Center" Margin="2,2,5,2"> 
                                            <Rectangle Width="8" Height="8" Fill="{Binding Background}" Stroke="{Binding BorderBrush}" StrokeThickness="1" Margin="0,0,3,0" VerticalAlignment="Center" />
                                            <dataviz:Title Content="{TemplateBinding Content}"  VerticalAlignment="Center"/>
                                        </StackPanel>
                                    </Border>
                                </ControlTemplate>
                            </Setter.Value>
                        </Setter>
                    </Style>
                </Setter.Value>
            </Setter>
            <Setter Property="TitleStyle">
                <Setter.Value>
                    <Style TargetType="dataviz:Title">
                        <Setter Property="Margin" Value="0,5,0,10" />
                        <Setter Property="FontWeight" Value="Bold" />
                        <Setter Property="HorizontalAlignment" Value="Center" />
                    </Style>
                </Setter.Value>
            </Setter>
            <Setter Property="ItemsPanel"> <!-- change layout container for legend items to be horizonal -->
                <Setter.Value>
                    <ItemsPanelTemplate>
                        <StackPanel Orientation="Horizontal" HorizontalAlignment="Center"/>
                    </ItemsPanelTemplate>
                </Setter.Value>
            </Setter>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="dataviz:Legend">
                        <!--  This is the border around the legend area.
                        <Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Padding="2">
                        -->
                            <Grid>
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="Auto"/>
                                    <RowDefinition />
                                </Grid.RowDefinitions>
                                <!-- Uncomment the next line to show a grid title. -->
                                <!--<dataviz:Title Grid.Row="0" x:Name="HeaderContent" Content="{TemplateBinding Header}" ContentTemplate="{TemplateBinding HeaderTemplate}" Style="{TemplateBinding TitleStyle}"/>-->
                                <ScrollViewer Grid.Row="0" VerticalScrollBarVisibility="Auto" BorderThickness="0" Padding="0" IsTabStop="False">  
                                    <ItemsPresenter x:Name="Items" /> 
                                </ScrollViewer>
                            </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>       
    

    【讨论】:

      【解决方案2】:

      将以下内容添加到图表中即可(来自here):

      <chartingToolkit:Chart.LegendStyle>
          <Style TargetType="dataviz:Legend">
              <Setter Property="ItemsPanel">
                  <Setter.Value>
                      <ItemsPanelTemplate>
                          <StackPanel Orientation="Horizontal"/>
                      </ItemsPanelTemplate>
                  </Setter.Value>
              </Setter>
          </Style>
      </chartingToolkit:Chart.LegendStyle>
      

      【讨论】:

      • 你怎么知道有一个 StackPanel?我在哪里可以找到某种参考图表是如何在里面制作的?
      • 上面的问题在 7 年前就已经回答过了……老实说,我不记得了。您可以查看silverlight.codeplex.com/SourceControl/latest 的源代码。它可能会有所帮助。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-25
      • 2011-04-18
      • 2018-03-16
      • 1970-01-01
      相关资源
      最近更新 更多