如何在WPF Toolkit AreaSeries图表上显示数据
本文关键字:显示 数据 AreaSeries WPF Toolkit | 更新日期: 2023-09-27 17:59:26
我正在使用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));
}
}
}
您的代码有两个问题:
- 您没有将
List
绑定到AreaSeriesItemSource
- 你切换了独立和独立
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);
}