Chart: Not support binding a data collection to generate series

Apr 2, 2010 at 8:02 AM

I'm using the Chart control and have such a user scenario: Just like the ItemsControl, it accepts a data collection as ItemsSource and generate UI items.

However, Chart control only support add series by Chart.Series. So to achieve this scenario, I have to use some workaround (like get the Chart instance in C# code, and generate series based on data and add them into Chart.Series), which is ugly and painful.

So is there any plan to support this user scenarion?

Apr 3, 2010 at 7:16 AM

I have had this problem in the past (when trying to create the series in a separate thread, and then add them) and have solved it (not perfectly) but it works by creating an attachedproperty which the chart then binds to. See ( ).

Also, this issue has been noted in the issue tracker ( ), but so far no response from Microsoft. (and it also contains links noting how to create the attachedproperties)



Apr 3, 2010 at 8:10 AM

Thanks Ahbrens for your suggestion, and it does work.

However, the approach is still non-XAML friendly, I have to prepare the Series Collection at code side, which means I have create all the series in code.

What I want is the way similar to ItemsControl, which is given a data collection and something like Style/Template. So follow the ItemsControl's pattern, I create a new class derived from Chart, add the following DP:

ItemsSource, SeriesTemplate, SeriesTemplateSelector.

The SeriesTemplate is of a customized type SeriesTemplate, which can create Series Instance based on the template. I try to derive SeriesTemplate from FrameworkTemplate (like ItemsPanelTemplate). However, the FrameworkTemplate cannot be derived since it has some internal abstract methods. So as a workaround, I just use DataTemplate.

So at last, the usage code looks like:

<chart:Chart ItemsSource="{Binding Items}" Grid.Column="2" Style="{StaticResource ChartStyle}" Panel.ZIndex="-1">
                                                <chart:LineSeries ItemsSource="{Binding Items}" IndependentValuePath="TimePoint" DependentValuePath="Value">
                                                        <chart:LinearAxis Orientation="Y" Maximum="200" Minimum="20" Width="0"/>



Apr 3, 2010 at 9:58 AM

Sorry about that misunderstanding.

I am glad you found a solution.