How can I bind the XamWebChart to a SeriesCollection. I'm getting data from a service, in Xml format, containing the values and styles for few series' with several datapoints.
I created SeriesCollection in my ViewModel. Now how should I bind my view to make it use the SeriesCollection to draw graphs.
You will want to check this post to get you started:
https://es.infragistics.com/community/forums/f/ultimate-ui-for-wpf/38169/series-binding-in-xaml-custom-attached-property
Unfortunately there seems to be a problem with the forums this morning, so this post isn't currently accessible.
-Graham
Its back up. So you can check that out if you want. But the exampl linked is really more for if you want to create the series based on a template in Xaml. If you are already creating the series collection in your View Model have you tried just binding it to the chart?
I.E if the property holding the SeriesCollection on your DataContext is called Series. Have you tried
<XamWebChart Series="{Binding Series} ...
The chart doesn't currently tolerate being populated with a null Series collection, so, if using a binding, you have to make sure that what the chart is being bound to can always provide a non-null series collection. For example, if i define my view model as a static resource so that it is accessible at the time of the creation of the page, then it behaves correctly. You could also create a proxy object to bind to that either provided an empty series collection or the actual series collection when accessible. Here is a sample:
The xaml:
<UserControl x:Class="SilverlightApplication60.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" xmlns:local="clr-namespace:SilverlightApplication60"> <Grid x:Name="LayoutRoot"> <Grid.Resources> <local:PageViewModel x:Name="testVM" /> </Grid.Resources> <igChart:XamWebChart DataContext="{StaticResource testVM}" Series="{Binding DefinedSeries}"> </igChart:XamWebChart> </Grid> </UserControl>
And the code behind:
public partial class MainPage : UserControl { public MainPage() { InitializeComponent(); } } public class PageViewModel : INotifyPropertyChanged { public PageViewModel() { _definedSeries = new SeriesCollection(); CreateTestData(); } private void CreateTestData() { Series lineSeries = new Series(); lineSeries.ChartType = ChartType.Line; lineSeries.DataMapping = "Label = Label; Value = Value"; lineSeries.DataSource = new TestChartData(); Series columnSeries = new Series(); columnSeries.ChartType = ChartType.Column; columnSeries.DataMapping = "Label = Label; Value = Value"; columnSeries.DataSource = new TestChartData(); _definedSeries.Add(lineSeries); _definedSeries.Add(columnSeries); } private SeriesCollection _definedSeries; public SeriesCollection DefinedSeries { get { return _definedSeries; } set { _definedSeries = value; if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs("DefinedSeries")); } } } public event PropertyChangedEventHandler PropertyChanged; } public class TestChartData : ObservableCollection<TestChartDataItem> { public TestChartData() { Add(new TestChartDataItem { Label = "1", Value = 1 }); Add(new TestChartDataItem { Label = "2", Value = 2 }); Add(new TestChartDataItem { Label = "3", Value = 3 }); } } public class TestChartDataItem { public string Label { get; set; } public double Value { get; set; } }
I've also created a bug (30475) indicating that the chart should tolerate being bound to view model properties that are initially null or unreachable.You can check the status of the bug at any time here or by contacting Developer Support.-Graham
Hi Graham,
Your reply helped. But when I try to update the datacontext of the control later sometime, its not getting updated. Its always showing the initial data.
I have all the property change notifications in place.
In the code, when I get new data I updated the data context as,
ChartControl.DataContext = newVM;
Help me.
Are you sure you are changing the data context at the right level? In the example I posted, if I change the chart's DataContext the series are updated. Are you changing the data context above the element which you originally set it on? Do you have a sample you can provide that exhibits the behavior you are seeing?
The bug is in review, you can check its status at any point by contacting Developer Support and providing them the bug number 30475.
Does this problem exists in both DataVisualization and Silverlight XamWebCharts?
Your problem is that when you are setting the actual DataContext programmatically you are setting it at a higher level than you are setting it inside the user control. The DataContext set on the chart will take precedence over the UserControl's DataContext. You have to replace the DataContext at the same level or lower than it was originally set.
One way to resolve your problem would be to give the chart in your user control a name
e.g. x:Name="theChart"
and then instead of this line:
ucChart.DataContext = vmChart;
write
(ucChart.FindName("theChart") as FrameworkElement).DataContext = vmChart;
let me know how it goes.
Please help me with rendering part.
I have used the code as is, it's not rendering anything on the screen.
I am referring to the post made by:
[Infragistics] Graham Murray on 07-26-2010 5:31 PM
Is there a slightly easier way to achieve databinding rather than use what Graham has suggested?
Hello,
Thank you for your post. I have been looking into it, but since in the thread there are a coouple of samples and links to other threads with samples, could pelase send me the one that you have issues with.
Looking forward for your reply.
Hello Graham,
I have been trying to use your example with the latest version of Infragistics Wpf release. However in the function GetAxes, you have code such as this:
xAxis = (series as AnchoredCategorySeries).XAxis;But there is no property called XAxis defined anymore. It seems that they have removed this property. How do I modify the code to get this example to work with the new version of infragistics 2012 version.
Hi,
Where are you running into the overhead? Do you have a profile? You may be able to tweak that method a bit to avoid the issue. There should be a bit of overhead when the custom type is generated, but as long as most of the dynamic types look the same it should be hitting the dictionary for each subsequent type that needs to be created.
Also, xamGrid probably supports binding against dictionaries, I would assume, is there a reason you aren't using that approach?
I'm not too familar with the xamGrid's backlog. I'd recommend makign a feature request, or possibly emailing productmanager@infragistics.com to inquire further.