WPF Toolkit을 사용한 차트 그리기 [선형차트]

Posted in Programming by

1. WPF Toolkit 다운로드 및 설치

http://www.c-sharpcorner.com/Downloads/Details.aspx?DownloadId=16

(windows7 기준 : C:\Program Files (x86)\WPF Toolkit 자동 설치)

 

2. 서비스 참조 추가

[설치 디렉토리]/[버전(v3.5.40619.1)]/System.Windows.Controls.DataVisualization.Toolkit.dll

 

3. Window1.xaml 네임 스페이스 추가

<Window x:Class="testChart.Window1"
    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="Window1" Height="300" Width="500">

 

4. Window1.xaml 차트 코드 작성

 <DVC:Chart Name="myChart" Width="300" Height="500">
      <DVC:Chart.Series>
        <DVC:LineSeries IndependentValueBinding="{Binding Path=Key}" DependentValueBinding="{Binding Path=Value}">
        </DVC:LineSeries>
      </DVC:Chart.Series>
    </DVC:Chart>

 

5. Window1.xaml.cs 데이터 코드 작성

using System.Windows.Controls.DataVisualization.Charting;

public partial class Window1 : Window
{
  public Window1()
  {
    InitializeComponent();
			
    ((LineSeries)MyChart.Series[0]).ItemsSource = new KeyValuePair<int, int>[]
    {
      new KeyValuePair<int,int>(0, 100),
      new KeyValuePair<int,int>(1, 130),
      new KeyValuePair<int,int>(2, 150),
    };
  }
}

 

6. 실행결과

 

※ Reference

http://www.c-sharpcorner.com/uploadfile/mahesh/charting-in-wpf/