Skip to main content
Home Forums Silverlight Programming Silverlight Controls and Silverlight Toolkit Line Chart with Linq Data and 2 Series
1 replies. Latest Post by snowbladin on December 16, 2008.
(0)
snowbladin
Member
0 points
2 Posts
12-16-2008 11:20 AM |
I have a silverlight app that uses a line chart, grabs data from the server via LINQ, and I am attempting to create 2 different series based on the single set of data. It shows both series after they are added to the chart in the legend, but both of them seem to end up using the data from the second series. And that's in spite of the fact that when I run the debugger in VS and inspect the ItemsSource property on each series, each has its own data.
I would just hard-code the series, but the number of needed series will vary based on the data that comes back.
Here's the XAML.
<charting:Chart x:Name="errorChart" Grid.Row="2" Grid.Column="1" PlotAreaStyle="{StaticResource PlotAreaStyle1}" ChartAreaStyle="{StaticResource ChartAreaStyle1}"> </charting:Chart>
Here's the data.
Count ApplicationVersion ApplicationName ExceptionSendDate------- ---------------------- -------------------- -----------------------2 1.1.1.2 app2 2008-12-12 14:30:30.0004 1.1.1.1 app1 2008-12-12 14:30:00.0001 1.1.1.1 app2 2008-12-12 14:30:00.000
Here's the code that creates the series and adds them to the chart.
void cli_GetChartDataCompleted(object sender, SilverlightApplication.ServiceReference1.GetChartDataCompletedEventArgs e) { errorChart.Series.Clear(); errorChart.Axes.Clear(); LinearAxis ax = new LinearAxis(); ax.Title = "Quantity"; ax.Orientation = AxisOrientation.Vertical; ax.Minimum = 0; ax.ShowGridLines = true; errorChart.Axes.Add(ax); var types = (from apps in e.Result select apps.ApplicationName).Distinct().ToList(); foreach (var appname in types) { LineSeries ls = new LineSeries(); ls.Title = appname; ls.IndependentValueBinding = new Binding("ExceptionSendDate"); ls.DependentValueBinding = new Binding("Count"); var bindErr = (from err in e.Result where err.ApplicationName == appname select err); ls.ItemsSource = bindErr; errorChart.Series.Add(ls); }
12-16-2008 2:12 PM |
I figured it out. Had to read up on LINQ on Rick Srahl's Site. It helped me create this code to replace my function.
Apparently, because of the nature of LINQ queries, you can't bind to them in the loop like I was doing, without first dumbing them down into a List object.
void cli_GetChartDataCompleted(object sender, SilverlightApplication.ServiceReference1.GetChartDataCompletedEventArgs e) { errorChart.Series.Clear(); errorChart.Axes.Clear(); LinearAxis ax = new LinearAxis(); ax.Title = "Quantity"; ax.Orientation = AxisOrientation.Vertical; ax.Minimum = 0; ax.ShowGridLines = true; errorChart.Axes.Add(ax); var types = (from apps in e.Result select apps.ApplicationName).Distinct().ToList(); foreach (var appname in types) { LineSeries ls = new LineSeries(); ls.Title = appname; ls.IndependentValueBinding = new Binding("ExceptionSendDate"); ls.DependentValueBinding = new Binding("Count"); var list = (from err2 in e.Result where err2.ApplicationName == appname select err2).ToList(); var bindErr = list; ls.ItemsSource = bindErr; errorChart.Series.Add(ls); } }
It always helps to talk about the problem! =)
LivingRoad Web Development