2016-07-31 36 views
0

我正在使用WPF工具包在AreaSeries圖表上顯示數據,但沒有出現。不知道我做錯了什麼。請幫忙/建議。如何顯示WPF Toolkit AreaSeries圖表上的數據?

下面是XAML代碼:

<Window xmlns:chart="clr-namespace:System.Windows.Controls.DataVisualization.Charting;assembly=System.Windows.Controls.DataVisualization.Toolkit" 

x:Class="GRAPHPrototype.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    xmlns:local="clr-namespace:GRAPHPrototype" 
    mc:Ignorable="d" 
    Title="MainWindow" Height="350" Width="525"> 
<Grid>  
    <chart:Chart Name="chart1">    
    </chart:Chart> 
</Grid> 

和C#代碼:

public partial class MainWindow : Window 
{ 
    static List<Ready4LOS> Ready4LOS = new List<Data.Ready4LOS>(); 
    public MainWindow() 
    { 
     InitializeComponent(); 

     InitChart(); 
     SampleData(); 
     chart1.DataContext = Ready4LOS; 
    } 

    private void SampleData() 
    { 

     Ready4LOS.Add(new Data.Ready4LOS() { Case = 5, Time = DateTime.Now.AddMinutes(-75) }); 
     Ready4LOS.Add(new Data.Ready4LOS() { Case = 15, Time = DateTime.Now.AddMinutes(-65) }); 
     Ready4LOS.Add(new Data.Ready4LOS() { Case = 5, Time = DateTime.Now.AddMinutes(-55) }); 
     Ready4LOS.Add(new Data.Ready4LOS() { Case = 8, Time = DateTime.Now.AddMinutes(-45) }); 
     Ready4LOS.Add(new Data.Ready4LOS() { Case = 22, Time = DateTime.Now.AddMinutes(-35) }); 
     Ready4LOS.Add(new Data.Ready4LOS() { Case = 35, Time = DateTime.Now.AddMinutes(-25) }); 
     Ready4LOS.Add(new Data.Ready4LOS() { Case = 4, Time = DateTime.Now.AddMinutes(-05) }); 
    } 

    private void InitChart() 
    { 
     System.Windows.Data.Binding indi = new System.Windows.Data.Binding("Case"); 
     System.Windows.Data.Binding dep = new System.Windows.Data.Binding("Time"); 
     AreaSeries ares = new AreaSeries(); 
     ares.IndependentValueBinding = indi; 
     ares.DependentValueBinding = dep; 

     DateTimeAxis dta = new DateTimeAxis(); 
     dta.Interval = 10; 
     dta.IntervalType = DateTimeIntervalType.Minutes; 
     dta.Title = "Time"; 
     dta.Orientation = AxisOrientation.X; 
     dta.Minimum = DateTime.Now.AddMinutes(-80); 
     dta.Maximum = DateTime.Now; 

     LinearAxis yaxis = new LinearAxis(); 
     yaxis.Minimum = 0; 
     yaxis.Interval = 2; 
     yaxis.Title = "Case"; 
     yaxis.Orientation = AxisOrientation.Y; 
     chart1.Axes.Add(yaxis); 
     chart1.Axes.Add(dta); 
     chart1.Series.Add(ares); 
    } 
} 

我有這種格式

class Ready4LOS : INotifyPropertyChanged 
{ 
    int _case; 
    DateTime _time; 

    public int Case 
    { 
     get 
     { 
      return _case; 
     } 
     set 
     { 
      _case = value; 
      NotifyPropertyChanged("Case"); 
     } 
    } 

    public DateTime Time 
    { 
     get 
     { 
      return _time; 
     } 
     set 
     { 
      _time = value; 
      NotifyPropertyChanged("Time"); 
     } 
    } 

    public event PropertyChangedEventHandler PropertyChanged; 
    private void NotifyPropertyChanged(String info) 
    { 
     if (PropertyChanged != null) 
     { 
      PropertyChanged(this, new PropertyChangedEventArgs(info)); 
     } 
    } 
} 
+0

感激,如果某一個格式化的問題,如果不這樣做妥善由我:( – rvsingh42

+0

感謝Funk重新格式化。 – rvsingh42

回答

1

有輸入數據你的代碼有兩個問題:

  1. 你沒有綁定您的ListItemSource
  2. 您切換INDI和DEP檢測AreaSeries Binding

嘗試

private void InitChart() 
{ 
    System.Windows.Data.Binding indi = new System.Windows.Data.Binding("Case"); 
    System.Windows.Data.Binding dep = new System.Windows.Data.Binding("Time"); 
    AreaSeries ares = new AreaSeries(); 
    ares.ItemsSource = Ready4LOS; 
    ares.IndependentValueBinding = dep; 
    ares.DependentValueBinding = indi; 

    DateTimeAxis dta = new DateTimeAxis(); 
    dta.Interval = 10; 
    dta.IntervalType = DateTimeIntervalType.Minutes; 
    dta.Title = "Time"; 
    dta.Orientation = AxisOrientation.X; 
    dta.Minimum = DateTime.Now.AddMinutes(-80); 
    dta.Maximum = DateTime.Now; 

    LinearAxis yaxis = new LinearAxis(); 
    yaxis.Minimum = 0; 
    yaxis.Interval = 2; 
    yaxis.Title = "Case"; 
    yaxis.Orientation = AxisOrientation.Y; 

    chart1.Axes.Add(dta); 
    chart1.Axes.Add(yaxis); 
    chart1.Series.Add(ares); 
} 
+0

非常感謝..我會盡力讓你知道如果這工作。 – rvsingh42

+0

它工作:),但我不明白提到的第二點。 – rvsingh42

+0

@ rvsingh42不客氣。在你原來的代碼中,你有'ares.IndependentValueBinding = indi;' 'ares.DependentValueBinding = dep;'給出錯誤。將其更改爲'ares.IndependentValueBinding = dep;' 'ares.DependentValueBinding = indi;'。 – Funk

相關問題