Silverlight图表 - 饼图在图表上显示相关值

时间:2011-04-23 14:03:39

标签: silverlight charts pie-chart

我想在饼图本身上显示相关值(不在图例区域中)。 我正在使用Silverlight 4 + Silverlight 4 Toolkit(2010年4月)。

这应该是仙女的共同要求,但我还没有设法找到解决方案。 我该怎么做?

  <toolkit:Chart Name="samplePieChart" Title="Sample" Width="600">
                    <toolkit:Chart.Series>
                        <toolkit:PieSeries Name="samplePieSeries" 
                                           ItemsSource="{Binding Questions}" 
                                           IndependentValueBinding="{Binding Name}" 
                                           DependentValueBinding="{Binding Count}"
                                           IsSelectionEnabled="True"
                                           />
                    </toolkit:Chart.Series>
                </toolkit:Chart>

1 个答案:

答案 0 :(得分:9)

我记得我有一个link,其中包含与图表相关的资源集合。

在该页面上,您可以找到可以帮助您向饼图添加标签的链接: LabeledPieChart

此解决方案使用工具包Chart类中的派生类。虽然我说可以在不创建新类的情况下创建类似的行为,但我认为它比使用现有控件更容易。

无论如何,我可以发布显示依赖值的饼图系列的自定义样式。 您需要的只是某种转换器,它将切片的路径几何转换为Left的{​​{1}}或Top属性,以及数据点的自定义样式。 / p>

转换器:

Canvas

public class GeometryToNumberConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { var g = value as Geometry; var type = parameter as string; if (type == "Left") return (g.Bounds.Left + g.Bounds.Right) / 2.0; else if (type == "Top") return (g.Bounds.Top + g.Bounds.Bottom) / 2.0; else return null; } public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } } 类的模板中有几行xaml:

PieDataPoint

以下是<Canvas Background="Transparent"> <TextBlock Text="{TemplateBinding FormattedDependentValue}" Canvas.Left="{Binding Geometry, RelativeSource={RelativeSource TemplatedParent}, Converter={StaticResource GeometryToNumberConverter}, ConverterParameter=Left}" Canvas.Top="{Binding Geometry, RelativeSource={RelativeSource TemplatedParent}, Converter={StaticResource GeometryToNumberConverter}, ConverterParameter=Top}"/> </Canvas> 样式的完整代码:

PieDataPoint

您可以应用此样式:

<UserControl.Resources>
    <local:GeometryToNumberConverter x:Name="GeometryToNumberConverter" />

    <Style x:Key="LabelDataPointStyle" TargetType="chart:PieDataPoint">
        <Setter Property="Background" Value="Orange"/>
        <Setter Property="BorderBrush" Value="White"/>
        <Setter Property="BorderThickness" Value="1"/>
        <Setter Property="IsTabStop" Value="False"/>
        <Setter Property="RatioStringFormat" Value="{}{0:p2}"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="chart:PieDataPoint">
                    <Grid
                    x:Name="Root"
                    Opacity="0">
                        <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>
                        <Path
                        x:Name="Slice"
                        Data="{TemplateBinding Geometry}"
                        Fill="{TemplateBinding Background}"
                        Stroke="{TemplateBinding BorderBrush}"
                        StrokeMiterLimit="1">
                            <ToolTipService.ToolTip>
                                <StackPanel>
                                    <ContentControl Content="{TemplateBinding FormattedDependentValue}"/>
                                    <ContentControl Content="{TemplateBinding FormattedRatio}"/>
                                </StackPanel>
                            </ToolTipService.ToolTip>
                        </Path>
                        <Path
                        x:Name="SelectionHighlight"
                        Data="{TemplateBinding GeometrySelection}"
                        Fill="Red"
                        StrokeMiterLimit="1"
                        IsHitTestVisible="False"
                        Opacity="0"/>
                        <Path
                        x:Name="MouseOverHighlight"
                        Data="{TemplateBinding GeometryHighlight}"
                        Fill="White"
                        StrokeMiterLimit="1"
                        IsHitTestVisible="False"
                        Opacity="0"/>
                        <Canvas IsHitTestVisible="False">
                            <TextBlock Text="{TemplateBinding FormattedDependentValue}" IsHitTestVisible="False"
                                       Canvas.Left="{Binding Geometry, RelativeSource={RelativeSource TemplatedParent}, Converter={StaticResource GeometryToNumberConverter}, ConverterParameter=Left}"
                                       Canvas.Top="{Binding Geometry, RelativeSource={RelativeSource TemplatedParent}, Converter={StaticResource GeometryToNumberConverter}, ConverterParameter=Top}"/>
                        </Canvas>

                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</UserControl.Resources>

图表将显示为单色橙色图表。

如果你不喜欢它,here is我已经展示了如何更改图表的<chart:PieSeries DataPointStyle="{StaticResource LabelDataPointStyle}" ... 属性的链接。

这是具有3种颜色的调色板示例,您可以通过类比添加其他颜色:

Palette

不要忘记从系列定义中删除<datavis:ResourceDictionaryCollection x:Key="DefaultPalette"> <!-- Blue --> <ResourceDictionary> <RadialGradientBrush x:Key="Background" GradientOrigin="-0.1,-0.1" Center="0.075,0.015" RadiusX="1.05" RadiusY="0.9"> <GradientStop Color="#FFB9D6F7" /> <GradientStop Color="#FF284B70" Offset="1" /> </RadialGradientBrush> <Style x:Key="DataPointStyle" TargetType="chart:PieDataPoint" BasedOn="{StaticResource LabelDataPointStyle}"> <Setter Property="Background" Value="{StaticResource Background}" /> </Style> </ResourceDictionary> <!-- Red --> <ResourceDictionary> <RadialGradientBrush x:Key="Background" GradientOrigin="-0.1,-0.1" Center="0.075,0.015" RadiusX="1.05" RadiusY="0.9"> <GradientStop Color="#FFFBB7B5" /> <GradientStop Color="#FF702828" Offset="1" /> </RadialGradientBrush> <Style x:Key="DataPointStyle" TargetType="chart:PieDataPoint" BasedOn="{StaticResource LabelDataPointStyle}"> <Setter Property="Background" Value="{StaticResource Background}" /> </Style> </ResourceDictionary> <!-- Light Green --> <ResourceDictionary> <RadialGradientBrush x:Key="Background" GradientOrigin="-0.1,-0.1" Center="0.075,0.015" RadiusX="1.05" RadiusY="0.9"> <GradientStop Color="#FFB8C0AC" /> <GradientStop Color="#FF5F7143" Offset="1" /> </RadialGradientBrush> <Style x:Key="DataPointStyle" TargetType="chart:PieDataPoint" BasedOn="{StaticResource LabelDataPointStyle}"> <Setter Property="Background" Value="{StaticResource Background}" /> </Style> </ResourceDictionary> </datavis:ResourceDictionaryCollection> 属性:DataPointStyle =&GT; <chart:PieSeries DataPointStyle="{StaticResource LabelDataPointStyle}" ...