0

For my PieSeries, I'd like to have the legend with buttons which have the Fill colour of the relevant slice, I would also like the buttons to have the percentage as shown in the tooltip using the FormattedRatio. Here's what I have so far:

PieSeries image with buttons and orange slices

Unfortunately, as can be seen all the slices have now gone orange, which forums have said is a standard problem but provide no obvious reason, also "FormattedRatio" is not available to the LegendItem. (I would like it to say Example B : 10.17%.)

Does anyone know how I can get colours back and how I can use the percentage in the legendItem?

Edit:

Have been able to change the background of the slices using this answer but the LegentItems don't change according to the new colour scheme.

enter image description here

The colours are correct - I'm using test data which is wrong.

Community
  • 1
  • 1
xhedgepigx
  • 340
  • 1
  • 2
  • 19
  • It seems that you changed the `DataPointStyle` property for some reason, that's why the chart became orange. In my example I changed only the legend style and I didn't have any issues with slice colors. – vortexwolf Sep 28 '12 at 20:58

1 Answers1

1

There is an easy way to change colors: redefine the Palette property. I already answered a similar question about colors, here is it: https://stackoverflow.com/a/5626435/427225

As to the legend items, it is not possible to access the FormattedRatio property, because the toolkit library contains lot of bugs, but you can display properties of bound items, and I will show you an example.

At first, you need to create the style for the LegendItem class. It is the same as the default one, except of 2 changes: your own ContentTemplate which I named testItemTemplate, and changed binding of the Content property to {Binding DataContext} instead of the previous one.

<UserControl.Resources>
    <Style x:Key="testLegendItemStyle" TargetType="chart:LegendItem">
        <Setter Property="ContentTemplate" Value="{StaticResource testItemTemplate}" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="chart:LegendItem">
                    <Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}">
                        <StackPanel Orientation="Horizontal">
                            <Rectangle Width="8" Height="8" Fill="{Binding Background}" Stroke="{Binding BorderBrush}" StrokeThickness="1" Margin="0,0,3,0"/>
                            <datavis:Title Content="{Binding DataContext}" ContentTemplate="{TemplateBinding ContentTemplate}"/>
                        </StackPanel>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</UserControl.Resources>

<chart:Chart>
    <chart:PieSeries ItemsSource="{Binding Items}" IndependentValuePath="Title" DependentValuePath="Value" LegendItemStyle="{StaticResource testLegendItemStyle}" />
</chart:Chart>

Next, you need to write the previously mentioned testItemTemplate according to your models, here is the example.

C# view models

var items = new[] {
    new ItemViewModel(){ Title = "Apples", Value = 35, CalculatedAndFormattedValue = "1%" },
    new ItemViewModel(){ Title = "Bananas", Value = 43, CalculatedAndFormattedValue = "4%"  },
    new ItemViewModel(){ Title = "Oranges", Value = 29, CalculatedAndFormattedValue = "3%"  },
    new ItemViewModel(){ Title = "Cherries", Value = 51, CalculatedAndFormattedValue = "2%"  },
    new ItemViewModel(){ Title = "Lemons", Value = 31, CalculatedAndFormattedValue = "5%"  },
};

Xaml DataTemplate

<DataTemplate x:Key="testItemTemplate">
    <TextBlock>
        <Run Text="{Binding Title}" />
        <Run Text=" - " />
        <Run Text="{Binding CalculatedAndFormattedValue}" />
    </TextBlock>
</DataTemplate>

pie chart with custom legend items

Needless to say, that you should calculate percents by yourself

Community
  • 1
  • 1
vortexwolf
  • 13,967
  • 2
  • 54
  • 72
  • A few questions - How can I change/remove the tooltip on the pie slices? If I do (DataPointStyle) then the colour info is lost again, what do you do to get around this? – xhedgepigx Oct 01 '12 at 13:38
  • @xhedgepigx The only way is to create a custom style of DataPoint. Look at the last code block of my answer: http://stackoverflow.com/a/5766953/427225. You need to do something similar: create a DataPoint style, then create a custom Palette, styles of which must have the BasedOn attribute, then apply this palette to the chart and remove the DataPointStyle property from the chart. – vortexwolf Oct 01 '12 at 17:23
  • @xhedgepigx The default Palette you can find at this path `C:\Program Files (x86)\Microsoft SDKs\Silverlight\v4.0\Toolkit\Apr10\Source\Source code.zip\Controls.DataVisualization.Toolkit\Themes\generic.xaml`, inside the style of the Chart control. In the answer which I linked I copied only 3 brushes, but as far as I see you need more of them. – vortexwolf Oct 01 '12 at 17:30
  • When I tried that method, it would colour 2 of my 4 slices in with apparently randomly chosen DefaultPalette options and have the other two as grey or black which are not options in the Palette. Any ideas? – xhedgepigx Oct 02 '12 at 11:01
  • @xhedgepigx Why do you use a randomly chosen palette? You need a palette with lot of styles and colors, 16 is preferrable. At first try to use a palette with default data point styles. If it is displayed fine, apply your custom data point style. – vortexwolf Oct 02 '12 at 11:39
  • My palette isn't random it's a set of 4 colours as my pie will only ever have 4 slices however the Chart, when loaded, appears to choose two of the colours from my Palette and then either grey or black as the other two colours. It is very odd. Thanks for your help. – xhedgepigx Oct 02 '12 at 15:19