生活随笔
收集整理的這篇文章主要介紹了
DynamicDataDisplay 实时曲线图的使用和沿轴移动的效果
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
原文地址
?由于項目需要,最近在找關于繪制實時曲線圖的文章,但看了很多自己實現(xiàn)的話太慢,所以使用了第三方控件來實現(xiàn)(由于是項目中使用所以我比較傾向與開源的項目,如果出問題的話可以很好的找到根源)。這里記錄是讓我以后可以回顧,也可以讓志同道合的程序猿減少搜索時間。
? ? ? ? 下面我們就介紹一下DynamicDataDisplay中實時曲線圖的功能(其他沒有時間去研究),由于在網上能找到類似的配置文章。
首先我們要去官網下載dll(或者直接NuGet,這里沒有用過就不介紹了),DynamicDataDisplay找到需要的DynamicDataDisplay.dll并在項目中引用
然后在項目中配置命名空間?xmlns:d3="http://research.microsoft.com/DynamicDataDisplay/1.0",再在內容中添加<d3:ChartPlotter>基本配置問題就已經解決。
? ? ?CPU的使用率的實時顯示問題,CPU所使用的api可以自己去查找,我這里就不說明了。
? ? ?下面我們來直接貼代碼
MainWindow.xaml文件內容如下:
[html]?view plaincopy
<Window?xmlns:dxga="http://schemas.devexpress.com/winfx/2008/xaml/gauges"??????????xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"??????????xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"??????????x:Class="LinechartApplication.MainWindow"??????????Title="MainWindow"?Height="400"?Width="650"??????????xmlns:d3="http://research.microsoft.com/DynamicDataDisplay/1.0"?Loaded="Window_Loaded">??????<Grid>??????????<Grid.RowDefinitions>??????????????<RowDefinition?Height="Auto"/>??????????????<RowDefinition?Height="*"/>??????????</Grid.RowDefinitions>??????????<StackPanel?Orientation="Horizontal">??????????????<Button?Content="滾屏or不滾屏"??Click="Button_Click"/>??????????</StackPanel>??????????<d3:ChartPlotter?x:Name="plotter"?Margin="10,10,20,10"?Grid.Row="1">??????????????<d3:ChartPlotter.VerticalAxis>??????????????????<d3:VerticalIntegerAxis?/>??????????????</d3:ChartPlotter.VerticalAxis>????????????????<d3:ChartPlotter.HorizontalAxis>??????????????????<d3:HorizontalIntegerAxis?/>??????????????</d3:ChartPlotter.HorizontalAxis>????????????????<d3:Header?Content="CPU?實時性能"/>??????????????<d3:VerticalAxisTitle?Content="百分比"/>??????????</d3:ChartPlotter>??????</Grid>??</Window>??
[csharp]?view plaincopy
using?System;??using?System.Globalization;??using?System.IO;??using?System.Reflection;??using?System.Threading;??using?System.Windows;??using?System.Diagnostics;??using?System.Windows.Threading;??using?System.Windows.Media;??using?Microsoft.Research.DynamicDataDisplay;??using?Microsoft.Research.DynamicDataDisplay.DataSources;??using?System.Collections;????namespace?LinechartApplication??{??????///?<summary>??????///?MainWindow.xaml?的交互邏輯??????///?</summary>??????public?partial?class?MainWindow?:?Window??????{????????????private?ObservableDataSource<Point>?dataSource?=?new?ObservableDataSource<Point>();??????????private?PerformanceCounter?performanceCounter?=?new?PerformanceCounter();??????????private?DispatcherTimer?dispatcherTimer?=?new?DispatcherTimer();??????????private?int?currentSecond?=?0;????????????bool?buttonbool?=?false;//標志是否滾屏??????????public?MainWindow()??????????{??????????????InitializeComponent();????????????}????????????private?void?Window_Loaded(object?sender,?RoutedEventArgs?e)??????????{??????????????plotter.AddLineGraph(dataSource,?Colors.Red,?2,?"百分比");??????????????plotter.LegendVisible?=?true;??????????????dispatcherTimer.Interval?=?TimeSpan.FromSeconds(1);??????????????dispatcherTimer.Tick?+=?timer_Tick;??????????????dispatcherTimer.IsEnabled?=?true;??????????????plotter.Viewport.FitToView();??????????}????????????int?xaxis?=?0;??????????int?yaxis?=?0;??????????int?group?=?20;//默認組距????????????Queue?q?=?new?Queue();??????????private?void?timer_Tick(object?sender,?EventArgs?e)??????????{??????????????performanceCounter.CategoryName?=?"Processor";??????????????performanceCounter.CounterName?=?"%?Processor?Time";??????????????performanceCounter.InstanceName?=?"_Total";??????????????double?x?=?currentSecond;??????????????double?y?=?performanceCounter.NextValue();??????????????Point?point?=?new?Point(x,?y);??????????????dataSource.AppendAsync(base.Dispatcher,?point);??????????????if?(wendu)??????????????{??????????????????if?(q.Count?<?group)??????????????????{??????????????????????q.Enqueue((int)y);//入隊??????????????????????yaxis??=?0;??????????????????????foreach?(int?c?in?q)??????????????????????????if?(c?>?yaxis)??????????????????????????????yaxis?=?c;??????????????????}??????????????????else?{??????????????????????q.Dequeue();//出隊??????????????????????q.Enqueue((int)y);//入隊??????????????????????yaxis?=?0;??????????????????????foreach?(int?c?in?q)??????????????????????????if?(c?>?yaxis)??????????????????????????????yaxis?=?c;??????????????????}????????????????????if?(currentSecond?-?group?>?0)??????????????????????xaxis?=?currentSecond?-?group;??????????????????else??????????????????????xaxis?=?0;????????????????????Debug.Write("{0}\n",?yaxis.ToString());??????????????????plotter.Viewport.Visible?=?new?System.Windows.Rect(xaxis,?0,?group,?yaxis);//主要注意這里一行??????????????}??????????????currentSecond++;??????????}????????????private?void?Button_Click(object?sender,?RoutedEventArgs?e)??????????{??????????????if?(wendu)??????????????{??????????????????wendu?=?false;??????????????}??????????????else?{??????????????????wendu?=?true;??????????????}??????????}??????}??}??
DynamicDataDisplay實現(xiàn)實時顯示曲線的功能有很多但沒有看到x軸移動的功能所以下面就是主要內容(如何沿著x軸移動)在使用DynamicDataDisplay時候我發(fā)現(xiàn)會出現(xiàn)圖像縮放的情況(就是x軸一直被縮放,所有數(shù)據(jù)都會被壓縮在一塊,很不方便)。使用plotter.Viewport.Visible = new System.Windows.Rect(xaxis, 0, group, yaxis);這行代碼就可以解決按照x軸移動的功能。這個就像一個窗口,我們定義窗口位置和大小就能想看到我們所需要的數(shù)據(jù)。第一和第二個參數(shù)是原點坐標,第三個參數(shù)是X軸長度,第四個參數(shù)是Y長度。定義好了之后所有在這個區(qū)間的內容都會被顯示,多余的參數(shù)不顯示。隨著窗口的移動也就是X軸移動的功能。代碼中,使用了隊列,隊列的長度就等于顯示窗口的長度也就是X軸的長度。隊列的作用是取該隊列中最大的值來確定窗口的高度。
? 不滾屏時圖像(我們可以看到X軸都擠到一塊)
? 滾屏時圖像(我們可以看到X起始點和結束點)
? ? ??資源下載http://download.csdn.net/detail/u013187531/9842653
?
轉載于:https://www.cnblogs.com/nimorl/p/9156788.html
總結
以上是生活随笔為你收集整理的DynamicDataDisplay 实时曲线图的使用和沿轴移动的效果的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。