为什么ChartPlotter没有AddLineGraph方法
手头有个项目需要通过动态曲线显示传感器的值。
网上说使用DynamicDataDisplay最方便,新建了一个WPF应用(.NET Framework)。
通过“管理NuGet程序包”方式获取的DynamicDataDisplayWpf,版本是0.4.0。
现在的情况是AddLineGraph方法不能用,导致不能动态曲线。请帮忙分析一下原因,折腾一整天了!
XAML代码如下:
<Window x:Class="DataDisplayTest.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:DataDisplayTest"
xmlns:dynamicdatadisplay="clr-namespace:Microsoft.Research.DynamicDataDisplay;assembly=DynamicDataDisplay"
xmlns:d3="http://research.microsoft.com/DynamicDataDisplay/1.0"
mc:Ignorable="d"
Title="DataDisplayTest" Height="450" Width="800">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<StackPanel Orientation="Horizontal">
<TextBlock Text="CPU Usage" FontSize="15" Margin="20,10,0,0" FontWeight="Bold"/>
<TextBlock x:Name="cpuUsageText" Margin="10,10,0,0" FontSize="15"/>
</StackPanel>
<d3:ChartPlotter x:Name="plotter" Margin="10,10,20,10" Grid.Row="1">
<d3:ChartPlotter.MainVerticalAxis>
<d3:VerticalIntegerAxis />
</d3:ChartPlotter.MainVerticalAxis>
<d3:ChartPlotter.MainHorizontalAxis>
<d3:HorizontalIntegerAxis/>
</d3:ChartPlotter.MainHorizontalAxis>
<d3:Header Content="传感器测试"/>
<d3:VerticalAxisTitle Content="浓度"/>
</d3:ChartPlotter>
</Grid>
</Window>
.CS代码如下:
using System;
using System.Collections.Generic;
using System.Diagnostics;
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.Threading;
using Microsoft.Research.DynamicDataDisplay;
using Microsoft.Research.DynamicDataDisplay.DataSources;
namespace DataDisplayTest
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
private ObservableDataSource<Point> dataSource = new ObservableDataSource<Point>(); //数据源
private PerformanceCounter cpuPerformance = new PerformanceCounter(); //关于计算机性能类
private DispatcherTimer timer = new DispatcherTimer(); //委托的定时器
int i = 0; //时间轴
public MainWindow()
{
InitializeComponent();
this.Loaded += new RoutedEventHandler(Window_Loaded);
}
//**** 时间,用于构造坐标点
private void AnimatedPlot(object sender,EventArgs e) {
cpuPerformance.CategoryName = "Processor";
cpuPerformance.CounterName = "% Processor Time";
cpuPerformance.InstanceName = "_Total";
double x = i;
double y = cpuPerformance.NextValue();
Point point = new Point(x,y); //点
dataSource.AppendAsync(base.Dispatcher,point); //将点异步委托到数据源
cpuUsageText.Text = String.Format("{0:0%}",y); //格式化显示CPU使用率
i++; //时间+1
}
//窗口加载完毕后触发
private void Window_Loaded(object sender, EventArgs e) {
//******* 这里AddLineGraph不能用
plotter.AddLineGraph(dataSource,Colors.Green,2, "Percentage"); //这里提示ChartPlotter未包含AddLineGraph
//********
timer.Interval = TimeSpan.FromSeconds(1);
timer.Tick += new EventHandler(AnimatedPlot);
timer.IsEnabled = true;
plotter.Viewport.FitToView();
}
}
}