前台代码:
<Window
x:Class="WPFVisifireChartsApp.RealtimeLineGraph"
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:local="clr-namespace:WPFVisifireChartsApp"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:vc="clr-namespace:Visifire.Charts;assembly=WPFVisifire.Charts"
Title="RealtimeLineGraph"
Width="800"
Height="450"
mc:Ignorable="d">
<Grid>
<vc:Chart
AnimatedUpdate="true"
BorderBrush="Gray"
BorderThickness="0.5"
CornerRadius="7,7,0,0"
ShadowEnabled="True"
Theme="Theme1">
<vc:Chart.Titles>
<vc:Title FontSize="14" Text="实时曲线图" />
</vc:Chart.Titles>
<vc:Chart.Series>
<vc:DataSeries
x:Name="ds"
LabelEnabled="True"
RenderAs="QuickLine" />
</vc:Chart.Series>
</vc:Chart>
</Grid>
</Window>
后台代码:
using System;
using System.Windows;
using System.Windows.Threading;
namespace WPFVisifireChartsApp
{
public partial class RealtimeLineGraph : Window
{
public RealtimeLineGraph()
{
InitializeComponent();
ds.AutoFitToPlotArea = true;
Random random = new Random();
var index = 0;
for (int i = 0; i < 1000; i++)
{
var pt = new Visifire.Charts.DataPoint();
pt.XValue = index++;
pt.YValue = random.NextDouble();
ds.DataPoints.Add(pt);
}
DispatcherTimer timer = new DispatcherTimer();
timer.Interval = TimeSpan.FromMilliseconds(100);
timer.Tick += (s, e) =>
{
var pt = new Visifire.Charts.DataPoint();
pt.XValue = index++;
pt.YValue = random.NextDouble();
ds.DataPoints.Add(pt);
};
timer.Start();
this.Closing += (s, e) => timer.Stop();
}
}
}
运行一段时间以后,X轴会出现滚动条。滚动条本身可以拖动。
但是一屏内展示的曲线点实时有点少,不知道怎么样改变同一时间展示的点数。
因为同时展示的点比较少,倒是不卡,但不能说明这个性能就好。