WPF图表ScatterSeries中的自定义工具提示

时间:2013-05-07 16:06:24

标签: wpf xaml charts

我正在尝试使用WPF图表工具包设置自定义工具提示。使用here中的演示代码。自定义工具提示的代码如下所示(抱歉详细):

   <Grid>
        <Grid.Resources>
            <Style
            x:Key="MyScatterDataPointStyle"
            TargetType="chartingToolkit:ScatterDataPoint">
                <Setter Property="Background" Value="Green"/>
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="chartingToolkit:ScatterDataPoint">
                            <Border
                            BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="{TemplateBinding BorderThickness}"
                            Opacity="0"
                            x:Name="Root">
                                <VisualStateManager.VisualStateGroups>
                                    <VisualStateGroup x:Name="CommonStates">
                                        <VisualStateGroup.Transitions>
                                            <VisualTransition GeneratedDuration="0:0:0.1"/>
                                        </VisualStateGroup.Transitions>
                                        <VisualState x:Name="Normal"/>
                                        <VisualState x:Name="MouseOver">
                                            <Storyboard>
                                                <DoubleAnimation
                                                Storyboard.TargetName="MouseOverHighlight"
                                                Storyboard.TargetProperty="Opacity"
                                                To="0.6"
                                                Duration="0"/>
                                            </Storyboard>
                                        </VisualState>
                                    </VisualStateGroup>
                                    <VisualStateGroup x:Name="SelectionStates">
                                        <VisualStateGroup.Transitions>
                                            <VisualTransition GeneratedDuration="0:0:0.1"/>
                                        </VisualStateGroup.Transitions>
                                        <VisualState x:Name="Unselected"/>
                                        <VisualState x:Name="Selected">
                                            <Storyboard>
                                                <DoubleAnimation
                                                Storyboard.TargetName="SelectionHighlight"
                                                Storyboard.TargetProperty="Opacity"
                                                To="0.6"
                                                Duration="0"/>
                                            </Storyboard>
                                        </VisualState>
                                    </VisualStateGroup>
                                    <VisualStateGroup x:Name="RevealStates">
                                        <VisualStateGroup.Transitions>
                                            <VisualTransition GeneratedDuration="0:0:0.5"/>
                                        </VisualStateGroup.Transitions>
                                        <VisualState x:Name="Shown">
                                            <Storyboard>
                                                <DoubleAnimation
                                                Storyboard.TargetName="Root"
                                                Storyboard.TargetProperty="Opacity"
                                                To="1"
                                                Duration="0"/>
                                            </Storyboard>
                                        </VisualState>
                                        <VisualState x:Name="Hidden">
                                            <Storyboard>
                                                <DoubleAnimation
                                                Storyboard.TargetName="Root"
                                                Storyboard.TargetProperty="Opacity"
                                                To="0"
                                                Duration="0"/>
                                            </Storyboard>
                                        </VisualState>
                                    </VisualStateGroup>
                                </VisualStateManager.VisualStateGroups>
                                <Grid
                                Background="{TemplateBinding Background}">
                                    <Rectangle>
                                        <Rectangle.Fill>
                                            <LinearGradientBrush>
                                                <GradientStop
                                                Color="#77ffffff"
                                                Offset="0"/>
                                                <GradientStop
                                                Color="#00ffffff"
                                                Offset="1"/>
                                            </LinearGradientBrush>
                                        </Rectangle.Fill>
                                    </Rectangle>
                                    <Border
                                    BorderBrush="#ccffffff"
                                    BorderThickness="1">
                                        <Border
                                        BorderBrush="#77ffffff"
                                        BorderThickness="1"/>
                                    </Border>
                                    <Rectangle x:Name="SelectionHighlight" Fill="Red" Opacity="0"/>
                                    <Rectangle x:Name="MouseOverHighlight" Fill="White" Opacity="0"/>
                                </Grid>
                                <ToolTipService.ToolTip>
                                    <StackPanel>
                                        <ContentControl
                                        Content="Custom ToolTip"
                                        FontWeight="Bold"/>
                                        <ContentControl
                                        Content="{TemplateBinding FormattedDependentValue}"/>
                                    </StackPanel>
                                </ToolTipService.ToolTip>
                            </Border>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </Grid.Resources>

我绑定的“数据”是一个字典,因此DependentValuePath =“Value.X”和IndependentValuePath =“Value.Y”。

我的图表代码如下所示:

        <chartingToolkit:Chart Name="MyChart">
            <chartingToolkit:Chart.Series>
                <chartingToolkit:ScatterSeries
                    ItemsSource="{Binding Data}"
                    DependentValuePath="Value.X"
                    IndependentValuePath="Value.Y"
                    DataPointStyle="{StaticResource MyScatterDataPointStyle}">
                    <chartingToolkit:ScatterSeries.IndependentAxis>
                        <chartingToolkit:LinearAxis Name="XAxis" Title="Area Under ROC Curve" Orientation="X" Minimum="0" />
                    </chartingToolkit:ScatterSeries.IndependentAxis>
                    <chartingToolkit:ScatterSeries.DependentRangeAxis>
                        <chartingToolkit:LinearAxis Name="YAxis" Title="Calibration Error" Orientation="Y" Minimum="0" />
                    </chartingToolkit:ScatterSeries.DependentRangeAxis>
                </chartingToolkit:ScatterSeries>
            </chartingToolkit:Chart.Series>
        </chartingToolkit:Chart>

我希望工具提示的格式为String.Format(“{0}:{1},{2}”,Key,Value.X,Value.Y); (即钥匙跟着价值)。知道怎么做吗?目前工具提示只显示相关值。

1 个答案:

答案 0 :(得分:1)

我认为这个问题有你想要的答案 WPF toolkit charting : Customize datapoint label

基本上,您希望在ToolTipService.ToolTip声明中添加更多条目

<ToolTipService.ToolTip>
    <StackPanel>
        <ContentControl
          Content="Custom ToolTip"
          FontWeight="Bold"/>
        <ContentControl
          Content="{TemplateBinding Key}"/><!--I am not sure what the correct property will be Key is a guess-->
        <ContentControl
          Content="{TemplateBinding IndependentValue}"/>
        <ContentControl
          Content="{TemplateBinding DependentValue}"/>
    </StackPanel>
</ToolTipService.ToolTip>