Silverlight 图表 - 饼图显示图表上的依赖值

发布于 2024-11-02 17:45:59 字数 799 浏览 0 评论 0 原文

我想在饼图本身上显示相关值(不在图例区域中)。 我正在使用 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>

I would like to display the dependent value on the pie chart itself(not in the Legend area).
I'm using Silverlight 4 + Silverlight 4 Toolkit (April 2010).

This should be fairy common request, but I haven't managed to find a solution.
How should I do this?

  <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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

放我走吧 2024-11-09 17:45:59

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

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

此解决方案使用工具包 Chart 类。尽管我说过可以在不创建新类的情况下创建类似的行为,但我认为这并不比使用现有控件更容易。

无论如何,我可以发布显示依赖值的饼图系列的自定义样式。
您所需要的只是某种转换器,它将切片的路径几何形状转换为 CanvasLeftTop 属性,并且数据点的自定义样式。

转换器:

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();
    }
}

以及 PieDataPoint 类模板内的几行 xaml:

<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>

您可以这样应用此样式:

<chart:PieSeries DataPointStyle="{StaticResource LabelDataPointStyle}" ...

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

如果您不喜欢它,这里是链接我在其中展示了如何更改图表的 Palette 属性。

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

<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 属性:
=>
<图表:PieSeries ...

I have recalled that I have a link with the collection of resources related with charts.

And on that page you can find the link which can help you to add labels to the pie chart:
LabeledPieChart

This solution use the derived class from the toolkit Chart class. And although I said that it is possible to create a similar behavior without creating new classes, I don't think that it is easier than using the existing control.

Anyway I can post the custom style of the pie series which displays the dependent value.
All that you need is some kind of converter which converts the path geometry of the slice to the Left or the Top property of the Canvas, and the custom style of the data point.

Converter:

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();
    }
}

And a few lines of xaml inside the template of the PieDataPoint class:

<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>

Here is the full code of the PieDataPoint style:

<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>

You can apply this style so:

<chart:PieSeries DataPointStyle="{StaticResource LabelDataPointStyle}" ...

And the chart will be displayed as one-color orange chart.

If you don't like it, here is the link where I have shown how to change the Palette property of the chart.

This is the example of the Palette with 3 colors, other colors you can add by analogy:

<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>

And don't forget to remove the DataPointStyle property from the series definition: <chart:PieSeries DataPointStyle="{StaticResource LabelDataPointStyle}" ...
=>
<chart:PieSeries ...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文