Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
swdev:dotnet:wpf:charting [2010/11/15 16:23]
smayr
swdev:dotnet:wpf:charting [2010/11/16 15:55] (current)
smayr
Line 1: Line 1:
 == Charting == == Charting ==
 +
 +== Using MS Chart ==
 +  * Create a WPF (.NET 4.0) project.
 +  * Add Assembly references:
 +    * ''WindowsFormsIntegrations''
 +    * ''System.Windows.Forms''
 +    * ''System.Windows.Controls.DataVisualization.Toolkit''
 +  * Place a ''WindowsFormsHost'' control inside a ''Grid'' (in XAML).
 +  * Place a ''Chart'' control inside.
 +
 +Create a bar chart:
 +<code xml>
 +<Window x:Class="MSChart.MainWindow"
 +        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 +        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 +        xmlns:winforms="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
 +        xmlns:charting="clr-namespace:System.Windows.Forms.DataVisualization.Charting;assembly=System.Windows.Forms.DataVisualization"
 +        Title="MainWindow" Height="350" Width="525" >
 +    <Grid>
 +        <Grid>
 +            <WindowsFormsHost Width="300" Height="200" Background="WhiteSmoke">
 +                <charting:Chart x:Name="chart1" />
 +            </WindowsFormsHost>
 +        </Grid>
 +    </Grid>
 +</Window>
 +</code>
 +
 +Add code behind to populate the chart:
 +<code csharp>
 +
 +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.Forms.DataVisualization.Charting;
 +
 +namespace MSChart
 +{
 +    /// <summary>
 +    /// Interaction logic for MainWindow.xaml
 +    /// </summary>
 +    public partial class MainWindow : Window
 +    {
 +        public MainWindow()
 +        {
 +            InitializeComponent();
 +
 +            // Create Chart Area
 +            ChartArea chartArea1 = new ChartArea();
 +
 +            // Add Chart Area to the Chart
 +            chart1.ChartAreas.Add(chartArea1);
 +
 +            // Create a data series
 +            Series series1 = new Series();
 +            Series series2 = new Series();
 +
 +            // Add data points to the first series
 +            series1.Points.Add(34);
 +            series1.Points.Add(24);
 +            series1.Points.Add(32);
 +            series1.Points.Add(28);
 +            series1.Points.Add(44);
 +
 +            // Add data points to the second series
 +            series2.Points.Add(14);
 +            series2.Points.Add(44);
 +            series2.Points.Add(24);
 +            series2.Points.Add(32);
 +            series2.Points.Add(28);
 +
 +            // Add series to the chart
 +            chart1.Series.Add(series1);
 +            chart1.Series.Add(series2);
 +        }
 +    }
 +}
 +</code>
 +== Using WPFToolkit ==
  
 Required Required
Line 10: Line 99:
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
-        xmlns:toolkit="http://schemas.microsoft.com/wpf/2008/toolkit" +    xmlns:toolkit="http://schemas.microsoft.com/wpf/2008/toolkit" 
-        xmlns:charting="clr-namespace:System.Windows.Controls.DataVisualization.Charting;assembly=System.Windows.Controls.DataVisualization.Toolkit"+    xmlns:charting="clr-namespace:System.Windows.Controls.DataVisualization.Charting;assembly=System.Windows.Controls.DataVisualization.Toolkit"
     Title="Window1" Height="300" Width="300">     Title="Window1" Height="300" Width="300">
     <Grid>     <Grid>
Line 26: Line 115:
 </code> </code>
  
-C#:+Populate Pie Chart in C#:
 <code csharp> <code csharp>
 chart.DataContext = new KeyValuePair<string, int>[] { chart.DataContext = new KeyValuePair<string, int>[] {
Line 37: Line 126:
  
 Source: [[http://blogs.msdn.com/b/wpfsdk/archive/2009/06/25/new-chart-controls-for-wpf.aspx|Chart Controls for WPF ship in the Toolkit]]          Source: [[http://blogs.msdn.com/b/wpfsdk/archive/2009/06/25/new-chart-controls-for-wpf.aspx|Chart Controls for WPF ship in the Toolkit]]         
 +
 +== References ==
 +  * [[http://weblogs.asp.net/scottgu/archive/2010/02/07/built-in-charting-controls-vs-2010-and-net-4-series.aspx|Built-in Charting Controls (VS2010 and .NET 4 Series)]]
 +  * [[http://www.smallworkarounds.net/2009/07/microsoft-charting-controls-how-to.html|MS Charting Controls HOW TO]]
 +  * [[http://code.msdn.microsoft.com/mschart|MS Chart]]
 +  * [[http://code.msdn.microsoft.com/mschart/Release/ProjectReleases.aspx?ReleaseId=1591|Download MS Chart Windows Forms sample projects]]
 +  * [[http://www.microsoft.com/downloads/en/details.aspx?FamilyId=EE8F6F35-B087-4324-9DBA-6DD5E844FD9F&displaylang=en|Download MS Chart CHM Documentation]]