【问题标题】:Silverlight 3 Toolkit Charting : How to display the value on the bar?Silverlight 3 Toolkit Charting:如何在柱上显示值?
【发布时间】:2010-02-12 19:47:38
【问题描述】:

Silverlight 3 工具包图表非常棒。我正在使用 BarSeries,silverlight 显示与值绑定成比例的条形长度。但是有什么方法可以在条形图上或在条形图旁边获得实际值?这是我的 XAML

<chartingToolkit:Chart
                        Grid.Column="1"
                        x:Name="chartEnvironmentStatusGlobal"
                        Title="Environment Status">
                        <chartingToolkit:BarSeries
                            x:Name="chartEnvironmentStatus"
                            Title="Pass"
                            HorizontalAlignment="Stretch"
                            VerticalAlignment="Stretch"
                            Background="Green"
                            IndependentValueBinding="{Binding Path=Instance}"
                            DependentValueBinding="{Binding Path=Count}"
                            AnimationSequence="LastToFirst">
                            </chartingToolkit:BarSeries>
                        <chartingToolkit:BarSeries
                            x:Name="chartEnvironmentStatus1"
                            Title="Fail"
                            HorizontalAlignment="Stretch"
                            VerticalAlignment="Stretch"
                            Background="Red"
                            IndependentValueBinding="{Binding Path=Instance}"
                            DependentValueBinding="{Binding Path=Count}"
                            AnimationSequence="LastToFirst">
                            </chartingToolkit:BarSeries>
                    </chartingToolkit:Chart>

提前谢谢你。

【问题讨论】:

    标签: silverlight silverlight-toolkit


    【解决方案1】:

    您需要为 BarDataPoint 创建一个新模板。我不会在这里发布整个模板,因为 a) 它非常大,并且 b) 我不确定我在版权方面的立场。

    如果你有 blend 你应该可以很容易地获得现有的模板,你应该能够使用该工具创建副本。或者,您可以从以下源代码中获取它:-

    #someSourceCodeRootFolder\Controls.DataVisualization.Toolkit\Charting\DataPoint\BarDataPoint.xaml
    

    在资源字典中创建这个:-

    <Style x:Key="BarDataPointWithContent" TargetType="charting:BarDataPoint">
      <Setter Property="Template">
        <Setter.Value>
          <Border ... copy from original template... >
             <!-- Wads of original VisualStateManager stuff here -->
             <Grid Background="{TemplateBinding Background}">
               <!-- Original set of Rectangles and Border here -->
               <TextBlock Text="{TemplateBinding FormattedDependentValue}"
                 HorizontalAlignment="Center" />
             </Grid>
             <ToolTipService.ToolTip>
                 <!-- Do something different here perhaps -->
             </ToolTipService.ToolTip>
          </Border>
        </Setter.Value>
      </Setter>
    </Style>
    

    基本上我所做的是添加最后的TextBlock 并将其绑定到工具提示在其ContentControl 中使用的相同FormattedDependentValue 属性。您可以为 TextBlock 添加更多样式以获得您想要的外观,您可能还希望对工具提示内容做一些不同的事情。

    因此,在这种风格下,您可以在图表本身中执行此操作:-

    <chartingToolkit:BarSeries.DataPointStyle>
      <Style TargetType="BarDataPoint" BasedOn="{StaticResouce BarDataPointWithContent}" >
        <Setter Property="Background" Value="Red" />
      </Style>
    </chartingToolkit:BarSeries.DataPointStyle>
    

    潜伏MSofties的注意事项

    您能否将模板添加到文档中的某个位置,以便我们不需要源代码、Blend 或 Reflector 来提取它们?

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-07-14
    • 2011-10-07
    • 1970-01-01
    • 1970-01-01
    • 2010-12-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多