Inserting series = wrong palette color

2 posts, 0 answers
  1. Brendan
    Brendan avatar
    34 posts
    Member since:
    Oct 2010

    Posted 14 Aug 2012 Link to this post

    I found a bug with the chart where inserting a series makes all colors incorrect, I have create as simple an example as I could to replicate the problem.

    The colors on the second chart are incorrect after swapping the series as it should be the same as Chart 3 - see attached image. The z-index is also incorrect.

    <StackPanel>
        <TextBlock Text="Chart 1 - Initial" />
        <telerik:RadCartesianChart x:Name="Chart1" Palette="Metro" Height="100">
            <telerik:RadCartesianChart.HorizontalAxis>
                <telerik:CategoricalAxis />
            </telerik:RadCartesianChart.HorizontalAxis>
     
            <telerik:RadCartesianChart.VerticalAxis>
                <telerik:LinearAxis />
            </telerik:RadCartesianChart.VerticalAxis>
        </telerik:RadCartesianChart>
     
        <TextBlock Text="Chart 2 - Should be swapped (same as Chart 3)" Margin="0,10,0,0" />
        <telerik:RadCartesianChart x:Name="Chart2" Palette="Metro" Height="100">
            <telerik:RadCartesianChart.HorizontalAxis>
                <telerik:CategoricalAxis />
            </telerik:RadCartesianChart.HorizontalAxis>
     
            <telerik:RadCartesianChart.VerticalAxis>
                <telerik:LinearAxis />
            </telerik:RadCartesianChart.VerticalAxis>
        </telerik:RadCartesianChart>
     
        <TextBlock Text="Chart 3 - Expected" Margin="0,10,0,0" />
        <telerik:RadCartesianChart x:Name="Chart3" Palette="Metro" Height="100">
            <telerik:RadCartesianChart.HorizontalAxis>
                <telerik:CategoricalAxis />
            </telerik:RadCartesianChart.HorizontalAxis>
     
            <telerik:RadCartesianChart.VerticalAxis>
                <telerik:LinearAxis />
            </telerik:RadCartesianChart.VerticalAxis>
        </telerik:RadCartesianChart>
    </StackPanel>

    public class Data
    {
        public int Item { get; set; }
        public int Value { get; set; }
    }
     
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
     
            // Add Line then Bar series to Chart 1
            InsertSeries(Chart1, true, 0);
            InsertSeries(Chart1, false, 1);
     
            // Add Line then Bar series to Chart 2
            InsertSeries(Chart2, true, 0);
            InsertSeries(Chart2, false, 1);
     
            // Remove Bar series, and re-insert to index 0 on Chart 2
            RemoveLastSeries(Chart2);
            InsertSeries(Chart2, false, 0);
     
            // Add Bar then Line series to Chart 3 (should be the same as Chart 2)
            InsertSeries(Chart3, false, 0);
            InsertSeries(Chart3, true, 1);
        }
     
        private void InsertSeries(RadCartesianChart chart, bool isLine, int index)
        {
            CategoricalSeries series = isLine ? (CategoricalSeries) new LineSeries() : new BarSeries();
            series.ItemsSource = GenerateData();
            series.CategoryBinding = new PropertyNameDataPointBinding("Item");
            series.ValueBinding = new PropertyNameDataPointBinding("Value");
     
            chart.Series.Insert(index, series);
        }
     
        private void RemoveLastSeries(RadCartesianChart chart)
        {
            chart.Series.RemoveAt(chart.Series.Count - 1);
        }
     
        private IEnumerable<Data> GenerateData()
        {
            for (int i = 0; i < 5; ++i)
            {
                yield return new Data()
                {
                    Item = i,
                    Value = i
                };
            }
        }
    }
  2. Evgenia
    Admin
    Evgenia avatar
    1407 posts

    Posted 17 Aug 2012 Link to this post

    Hello Brendan,

     Thanks for the code provided. However I don't think this is a bug, rather the expected behavior of the RadChartView. In RadChart2 you add Line and then Bar series and by default their colors are taken from the Metro pallette as Green and Blue. Then when you remove the Bar series, you still have Green line left. Even though you add the Bar series at the beginning of your series collection, they are assigned the next available color from the Metro palette - blue. And voilà - you repeated your Chart1 , not Chart 3 and this is the expected behaviour of the control.

    Greetings,
    Evgenia
    the Telerik team

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

  3. DevCraft banner
Back to Top