Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
1052
XamPieChart
posted

Hi There,

I have been looking into using the XamPieChart, to replace the current XamWebChart we use, and have a couple of quick questions.

Firstly is there anyway to hide the labels on the chart but show then on a legend. I have the following setup:

        <ig:ItemLegend x:Name="Legend"
                       VerticalAlignment="Bottom"
                       HorizontalAlignment="Right"  
                       Margin="5">
        </ig:ItemLegend>
        <ig:XamPieChart Name="pieChart" 
ItemsSource="{Binding PieSeries}"
ValueMemberPath
="Value"
LabelMemberPath
="Label"
LabelsPosition
="None"                 Legend="{Binding ElementName=Legend}"/>

When I have the LabelsPosition set to BestFit the legend shows the correct labels, however when I set it to none the legend shows the text 'Label'. Is there anyway to remedy this?

Also is there anyway to set a pallet which used interpolation to generate colours? If I set the brushed property to have two brushes and use RGB Interpolation it just alternates the colours. What I was hoping for was a StartColour/EndColour type thing where it generates the colours between.

Thanks in advance

Parents
No Data
Reply
  • 85
    Suggested Answer
    posted

    This is perhaps a little late for the OP but for anyone else trying to solve these problems I hope it helps...

     

    Hiding label text on the pie chart

    To hide label text on the pie chart without affecting the legend you can simply set the foreground colour of the pie chart to "Transparent" and set the label position to any value other than "None".

    An example style is shown here:

     

        <Style TargetType="ig:XamPieChart"
               x:Key="PieChartStyle">
     
            <Setter Property="LabelsPosition"
                    Value="Center" />
     
            <!-- This is to prevent the label values from appearing on the chart slices-->
            <Setter Property="Foreground"
                    Value="Transparent" />         
     
        </Style>   

     

    Interpolating chart colours

    The WPF DV XamChart control has StartPaletteBrush and EndPaletteBrush properties for this purpose. In the Silverlight control it is necessary to roll our own. The best method I have found is to create a ValueConverter that has two public properties - one for the start colour, and one for the end colour. It should accept an integer parameter which represents the number of slices on the chart. It then returns a BrushCollection object containing the colours which are calculated using linear RGB interpolation. Set the Brushes property of the XamPieChart to a binding that uses this converter and give the number of slices as the converter parameter. This works for me because I know how many items my chart is displaying. The classes involved are shown below:

     

        public class ColorsToBrushCollectionConverter : IValueConverter
        {
            public Color StartColour { getset; }
            public Color EndColour { getset; }
     
            public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
            {
                if (value.GetType() != typeof(int))
                    return DependencyProperty.UnsetValue;
     
                int steps = (int) value;
     
                BrushCollection brushes = new BrushCollection();
     
                ColorInterpolator.InterpolateColours(StartColour, EndColour, steps).ToList().ForEach(c => brushes.Add(new SolidColorBrush(c)));
                
                return brushes;
            }
     
            public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
            {
                throw new NotImplementedException();
            }
        }   
    
    
    
    
    
    
        public static class ColorInterpolator
        {
            public static Color[] InterpolateColours(Color startColour, Color endColour, int steps)
            {
                Color[] colours = new Color[steps];
                double s = (double) steps;
     
                double aDelta = (endColour.A - startColour.A) / s;
                double rDelta = (endColour.R - startColour.R) / s;
                double gDelta = (endColour.G - startColour.G) / s;
                double bDelta = (endColour.B - startColour.B) / s;
     
                for (int i = 0; i < steps; i++)
                {
                    Color c = Color.FromArgb(
                        (byte) (startColour.A + (i * aDelta)),                    
                        (byte) (startColour.R + (i * rDelta)),
                        (byte) (startColour.G + (i * gDelta)),
                        (byte) (startColour.B + (i * bDelta)));
     
                    colours[i] = c;
                }
     
                return colours;
            }
     
        }
Children
No Data