Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I bind my LINQ query results (put into a dictionary) to a Chart in WPF?
    primarykey
    data
    text
    <p>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..</p> <p>Here is my XAML::</p> <pre><code>&lt;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"&gt; &lt;Grid&gt; &lt;DVC:Chart Name="testchart" Title="Statistics" Width="500" Height="300" LegendTitle="Stats"&gt; &lt;DVC:Chart.Series&gt; &lt;DVC:BarSeries Title="Points" IndependentValueBinding="" DependentValueBinding=""&gt; &lt;/DVC:BarSeries&gt; &lt;/DVC:Chart.Series&gt; &lt;/DVC:Chart&gt; &lt;/Grid&gt; </code></pre> <p></p> <p>And here is the code behind:</p> <pre><code>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&lt;string, int&gt; _data = new Dictionary&lt;string, int&gt;(); public MainWindow() { InitializeComponent(); using (CE ctx = new CE()) { var query = (from r in ctx.ChartOnes select r).ToDictionary(k =&gt; k.Month); } } } } </code></pre> <p>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:</p> <pre><code> &lt;DVC:Chart Name="testchart" Title="Statistics" Width="500" Height="300" LegendTitle="Stats"&gt; &lt;DVC:Chart.Series&gt; &lt;DVC:ColumnSeries Title="Points" IndependentValueBinding="{Binding Path=Key}" DependentValueBinding="{Binding Path=Value.Points}"&gt; &lt;/DVC:ColumnSeries&gt; &lt;/DVC:Chart.Series&gt; &lt;/DVC:Chart&gt; </code></pre> <p>and the code-behind:</p> <pre><code> private void LoadBarChartData() { CE ctx = new CE(); var query = (from r in ctx.ChartOnes select new { Points = r.Points, Month = r.Month }).ToDictionary(k =&gt; k.Month); ((ColumnSeries)testchart.Series[0]).ItemsSource = query; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload