I'm still new to WPF, Data Binding, Infragistics, and reallly GUI Windows Development... Sometimes I find working with the XamDataChart a little confusing for displaying complex objects, but I think it falls down to my understanding of how the XAML Binding Works with the charting package.
I have
public class PriceDataHistory : ObservableCollection<Price>
Price is a custom object that looks like:
public class Price { public string Country { get; set; } public string LocationCode { get; set; } public DateTime Date { get; set; } public double Price { get; set; } }
I have many data points in the Observable collection and I would like to display them grouped by LocationCode.
So, for example If I have three different locations I would like to display 3 series on the chart.
public class PriceDataHistory : ObservableCollection<Price> { public PriceDataHistory() { for (int i = 0; i < _priceDates.Count; i++) { this.Add(new Price { LocationCode = _location[0], Date = DateTime.Parse(_priceDates[i]), Price = float.Parse(_price0[i]) }); this.Add(new Price { LocationCode = _location[1], Date = DateTime.Parse(_priceDates[i]), Price = float.Parse(_price1[i]) }); this.Add(new Price { LocationCode = _location[2], Date = DateTime.Parse(_priceDates[i]), Price = float.Parse(_price2[i]) }); } } ///... not necessary for example } In the example data they all have the same number of points and they all have the same Date. In the future I will not know how many different LocationCodes there will be, because I'm using test data right now so it is static. Is what I want even possible? How should I go about binding to my object in XAML?
public class PriceDataHistory : ObservableCollection<Price> { public PriceDataHistory() { for (int i = 0; i < _priceDates.Count; i++) { this.Add(new Price { LocationCode = _location[0], Date = DateTime.Parse(_priceDates[i]), Price = float.Parse(_price0[i]) }); this.Add(new Price { LocationCode = _location[1], Date = DateTime.Parse(_priceDates[i]), Price = float.Parse(_price1[i]) }); this.Add(new Price { LocationCode = _location[2], Date = DateTime.Parse(_priceDates[i]), Price = float.Parse(_price2[i]) }); } }
///... not necessary for example
}
In the example data they all have the same number of points
and they all have the same Date.
In the future I will not know how many different
LocationCodes there will be, because I'm using test
data right now so it is static.
Is what I want even possible? How should I go about
binding to my object in XAML?
Hello,
Thank you for your post. I have been reading through it and if I understand correctly you have a ObservableColleciton of Price objects and you wish to group your collection by the LocationCode property and create a series for each group. If my assumption is correct, I can suggest creating a method to which you can pass the PriceDataHistory and the XamDataChart. In this method using a linq query you can group the PriceDataHistory by the LocationCode property into a new collection of objects that will contains the LocationCode and a collection of the data for the given location. By looking though this collection you can create a new series for each LocationCode and add it to the XamDataChart. I have created a sample application for you, which demonstrates the approach that I have described.
Please let me know if you need any further assistance on the matter.
Sincerely,
Krasimir
Developer Support Engineer
Infragistics
www.infragistics.com/support
I'm still following the manual way of getting the data in the chart. If I need to update the data for the entire chart. If the number of days that I want to display changes I find that the chart does not refresh and show the data. Here is what I'm doing in code. The first time this runs each location has 365 days worth of data, and the second time it has 20 days worth of data (for example) going to the same chart. The second time the chart is loaded it shows no data.
chart.Series.Clear(); var priceGroupedByLocationCode = (from i in data group i by i.LocationCode into location select new { LocationCode = location.Key, Data = location.ToList<Price>() }).ToList(); foreach (var item in priceGroupedByLocationCode) { LineSeries series = new LineSeries(); series.Title = item.LocationCode; series.XAxis = x; series.YAxis = y; series.ItemsSource = item.Data; series.ValueMemberPath = "Price"; series.MarkerType = MarkerType.None; if (x.ItemsSource == null || x.ItemsCount < 1) x.ItemsSource = item.Data; chart.Series.Add(series); }
This looks like what I was going for, but I was hoping to have a solution where the binding was done in XAML.
I can complete my tasks using this programmed method, is it possible to create a converter that would do this?