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?
Hi,
I ran into the same problem. Your samples ist noch working correct. It only display all line series,if the item count in every group is the same. If there is a diffrence the series is not displayed. Is there a solution? We are using the latest 2012.2 WPF Version .
Thanks in advance
Ansgar
I believe everything is good for now. Thank you.
Hello,
I am just checking if you require any further assistance on the matter.
Sincerely,
Krasimir
Developer Support Engineer
Infragistics
www.infragistics.com/support
Thank you for your post. I have been looking into the issue that you have described and it seems that the XamDataChart is not drawing the data, because the ItemsSource of the CategoryXAxis is not being changed when the new data is being loaded. I can suggest setting the ItemsSource of the CategoryXAxis to null after clearing the Series collection and changing the if statement in the foreach loop in order to check whether the current group has more items than the items in the ItemsSource of the axis:
chart.Series.Clear(); //Clearing the ItemsSource of the Axis xAxis.ItemsSource = null; var goupedPriceHistory = (from i in data group i by i.LocationCode into location select new { LocationConde = location.Key, Data = location.ToList<PriceData>() }).ToList(); foreach (var item in goupedPriceHistory) { LineSeries series = new LineSeries(); series.Title = item.LocationConde; series.XAxis = xAxis; series.YAxis = yAxis; series.MarkerType = MarkerType.None; series.ItemsSource = item.Data; series.ValueMemberPath = "Price"; //Checking whether the current group is containing more items the items in the Axis if (xAxis.ItemsSource == null || xAxis.ItemsCount < item.Data.Count) xAxis.ItemsSource = item.Data; chart.Series.Add(series); }
I have attached a modified sample application that demonstrates the approach that I have described.
Please let me know if you need any further assistance on the matter.
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); }