0

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

   <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); (即键跟随值)。知道怎么做吗?目前,工具提示仅显示依赖值。

4

1 回答 1

1

I think this question has the answer you are looking for WPF toolkit charting : Customize datapoint label

Basically you want to add more entries to your ToolTipService.ToolTip declaration

<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>
于 2013-05-07T18:38:42.900 回答