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} ...
Yes, Graham
I tried the same, as
<XamWebChart Series="{Binding ChartSeriesCollection}
where ChartSeriesCollection is a SeriesCollection object in the viewmodel.
And I got XAM_PARSE_EXCEPTOIN at the above XAML Syntax.
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.
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.