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
260
Set Legend Palette on Pie chart
posted

I have a Pie XamWebChart and I want to set the legend palette to a fixed set of colors. The chart is bound to an observable collection that is periodically updated via a WCF service.

Setting the Start- and EndPaletteBrushes just gives me values that are too close together. For example,  I want to set slice one to be red, slice 2 to be blue, etc. This is easily done when the chart is not bound, but that doesn't help me.

I'm just looking for some example Xaml. I would rather not do it in code.

Thanks,

Bill

  • 30692
    Suggested Answer
    Offline posted

    Bill,

    Unfortunately the binding options for the fills of the datapoints are constrained at the moment, so this will require some code behind. I've put together a sample of how you might achieve some of this behavior.

    Markup:

     

    <UserControl x:Class="SilverlightApplication23.MainPage"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

        mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="480"

        xmlns:igChart="clr-namespace:Infragistics.Silverlight.Chart;assembly=Infragistics.Silverlight.DataVisualization.Chart.v9.2">

      <Grid x:Name="LayoutRoot">

            <igChart:XamWebChart x:Name="theChart">

                <igChart:XamWebChart.Series>

                    <igChart:Series ChartType="Pie"

                                    DataSource="{Binding}"

                                    DataMapping="Label=Label; Value=Value" />

                </igChart:XamWebChart.Series>

            </igChart:XamWebChart>

      </Grid>

    </UserControl>

     

    Code Behind:

     

    public partial class MainPage : UserControl

        {

            public MainPage()

            {

                InitializeComponent();

     

                DataContext = DataItemsViewModel.GetTestData();

                theChart.Series[0].DataPoints.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(DataPoints_CollectionChanged);

            }

     

            void DataPoints_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)

            {

                for (int i = 0; i < theChart.Series[0].DataPoints.Count; i++)

                {

                    theChart.Series[0].DataPoints[i].Fill =

                        (DataContext as DataItemsViewModel)[i].Fill;

                }

            }

        }

     

        public class DataItemsViewModel : ObservableCollection<DataItemViewModel>

        {

            public static DataItemsViewModel GetTestData()

            {

                DataItemsViewModel ret = new DataItemsViewModel();

                ret.Add(new DataItemViewModel() { Label = "test1", Value = 4 });

                ret.Add(new DataItemViewModel() { Label = "test2", Value = 8 });

                ret.Add(new DataItemViewModel() { Label = "test3", Value = 2 });

                ret.Add(new DataItemViewModel() { Label = "test4", Value = 1 });

     

                return ret;

            }

        }

     

        public class DataItemViewModel

        {

            private static int _currentColorIndex = 0;

            private static object _colorIndexLock = new Object();

            private int _colorIndex = 0;

            private Color[] _colors = { Colors.Black, Colors.Blue, Colors.Brown, Colors.Cyan, Colors.DarkGray,

                                         Colors.Gray, Colors.Green, Colors.LightGray, Colors.Magenta, Colors.Orange,

                                         Colors.Purple, Colors.Red, Colors.White, Colors.Yellow};

     

            public DataItemViewModel()

            {

                lock (_colorIndexLock)

                {

                    _colorIndex = _currentColorIndex % (_colors.Length - 1);

                    _currentColorIndex++;

                }

            }

     

            public SolidColorBrush Fill

            {

                get

                {

                    return new SolidColorBrush(_colors[_colorIndex]);

                }

            }

     

            public string Label { get; set; }

            public double Value { get; set; }

        }

    -Graham