This is a migrated thread and some comments may be shown as answers.

Problem adding SeriesMappings via Converter

1 Answer 12 Views
Chart
This is a migrated thread and some comments may be shown as answers.
Brandon
Top achievements
Rank 1
Brandon asked on 22 Feb 2012, 10:23 PM
I'm trying to create a list of charts, widgets, that are being bound based off of data coming back from the database. Given that the DataPoints, ChartType, and also the number of DataSeries are dynamic in every way, I've come up with a solution to use a DataTemplate that has a RadChart object and on that chart I set the SeriesMappings property by running the object through a Converter. The converter then returns back a SeriesMappingCollection, which I thought should be good, but instead the graph plots two points, 8 & 5 which happen to be the Capacity and Count (respectively) of the bound object. We've tried to get this to work for most of the day with no luck. One last thing, we did test to ensure the mappings work in straight XAML and it does, so we were able to rule that out as well. If anyone has any thoughts on this, that would be much appreciated.

RadChart within the DataTemplate:
<telerik:RadChart Grid.Row="1" SeriesMappings="{Binding Path=., Converter={StaticResource WidgetObjToSeriesMappingConverter}}" ItemsSource="{Binding Path=DataSeriesRoot}"/>

public class WidgetObjToSeriesMappingConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value == null)
                return null;
 
            var seriesMappingCol = new SeriesMappingCollection();
            var barChartWidget = (BarChartWidget)value;
 
            //Loop through each series, and configure
            for (int i = 0; i < barChartWidget.DataSeriesRoot.Count; i++)
            {
                var seriesMapping = new SeriesMapping();
 
                //Check what type of chart it will be
                if (value.GetType() == typeof(BarChartWidget))
                    seriesMapping.SeriesDefinition = new BarSeriesDefinition();
 
                seriesMapping.CollectionIndex = i;
                seriesMapping.SeriesDefinition.ShowItemLabels = false;
                seriesMapping.ItemMappings.Add(new ItemMapping("XValue", DataPointMember.XCategory));
                seriesMapping.ItemMappings.Add(new ItemMapping("YValue", DataPointMember.YValue));
                //Add the series to the collection
                seriesMappingCol.Add(seriesMapping);
            }
            //Return SeriesMapping collection object
            return seriesMappingCol;
        }
 
        object IValueConverter.ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

1 Answer, 1 is accepted

Sort by
0
Ves
Telerik team
answered on 27 Feb 2012, 10:30 AM
Hi Brandon,

Indeed, it seems there is a problem in this scenario. Luckily, the workaround is simple -- wrapping the chart into a UserControl does the trick. I have attached a small example, which follows your description and works fine on my end.

Best regards,
Ves
the Telerik team
Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
Tags
Chart
Asked by
Brandon
Top achievements
Rank 1
Answers by
Ves
Telerik team
Share this question
or