Ok so I am just practicing what is probably some very basic stuff with charts in wpf. I am fairly new to linq and wpf, so I apologize in advance if this is a silly question. I’ve got my LINQ to entity query returning all columns, I want to select two of the columns (Month (string), Points(int)) throw them into a dictionary, and then binding that to my chart..
Here is my XAML::
<Window x:Class="Chart.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:DV="clr-namespace:System.Windows.Controls.DataVisualization;assembly=System.Windows.Controls.DataVisualization.Toolkit"
xmlns:DVC="clr-namespace:System.Windows.Controls.DataVisualization.Charting;assembly=System.Windows.Controls.DataVisualization.Toolkit"
Title="MainWindow" Height="350" Width="525">
<Grid>
<DVC:Chart Name="testchart" Title="Statistics" Width="500" Height="300" LegendTitle="Stats">
<DVC:Chart.Series>
<DVC:BarSeries Title="Points"
IndependentValueBinding=""
DependentValueBinding="">
</DVC:BarSeries>
</DVC:Chart.Series>
</DVC:Chart>
</Grid>
And here is the code behind:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Controls.DataVisualization.Charting;
namespace Chart
{
public partial class MainWindow : Window
{
Dictionary<string, int> _data = new Dictionary<string, int>();
public MainWindow()
{
InitializeComponent();
using (CE ctx = new CE())
{
var query = (from r in ctx.ChartOnes
select r).ToDictionary(k => k.Month);
}
}
}
}
Thanks Dessus for your timely response! I will play around with your suggestions today. I did get some help from a co-worker yesterday and he was able to show me how to get it to work. Here is the edited code snippets:
<DVC:Chart Name="testchart" Title="Statistics" Width="500" Height="300" LegendTitle="Stats">
<DVC:Chart.Series>
<DVC:ColumnSeries Title="Points"
IndependentValueBinding="{Binding Path=Key}"
DependentValueBinding="{Binding Path=Value.Points}">
</DVC:ColumnSeries>
</DVC:Chart.Series>
</DVC:Chart>
and the code-behind:
private void LoadBarChartData()
{
CE ctx = new CE();
var query = (from r in ctx.ChartOnes
select new { Points = r.Points, Month = r.Month }).ToDictionary(k => k.Month);
((ColumnSeries)testchart.Series[0]).ItemsSource = query;
}
You should look up a model view view-model (MVVM) example of how to do databinding in WPF for chart controls (see: http://geekswithblogs.net/BillSontag/archive/2009/10/31/wpf-2-way-data-binding.aspx). The way you are doing your query in code behind will make it annoying (but can work) to get a reference to your control, but MVVM will make life easier once you get it working and get used to it. Your Linq query appears like it should output to a datatable as opposed to a dictionary.
Side Note: Any updates to the underlying LINQ collections will not trigger updates in your chart control. For that you would need something like: http://bindablelinq.codeplex.com/