中文字幕日韩一区二区_国产一区二区av_国产毛片av_久久久久国产一区_色婷婷电影_国产一区二区精品

WPF 動態(tài)模擬CPU 使用率曲線圖

     在工作中經(jīng)常會遇到需要將一組數(shù)據(jù)繪制成曲線圖的情況,最簡單的方法是將數(shù)據(jù)導(dǎo)入Excel,然后使用繪圖功能手動生成曲線圖。但是如果基礎(chǔ)數(shù)據(jù)頻繁更改,則手動創(chuàng)建圖形可能會變得枯燥乏味。本篇將利用DynamicDataDisplay  在WPF 中動態(tài)模擬CPU 使用率圖表,實現(xiàn)動態(tài)生成曲線圖。

     新建項目將DynamicDataDisplay.dll 加載到References 中,打開MainWindow.xaml 添加命名空間xmlns:d3="http://research.microsoft.com/DynamicDataDisplay/1.0"。通過<d3:ChartPlotter> 創(chuàng)建一個圖表框架,在其中添加兩條整型坐標(biāo)軸,X軸:<d3:HorizontalIntegerAxis>,Y軸:<d3:VerticalIntegerAxis>。<d3:Header> 用來設(shè)置圖表名稱,<d3:VerticalAxisTitle> 用來設(shè)置Y軸名稱。

<Window x:Class="WpfPerformance.MainWindow"        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"        xmlns:d3="http://research.microsoft.com/DynamicDataDisplay/1.0"        Title="CPU Performance" Loaded="Window_Loaded" Height="350" Width="525">    <Grid>        <Grid.RowDefinitions>            <RowDefinition Height="Auto"/>            <RowDefinition Height="*"/>        </Grid.RowDefinitions>        <StackPanel Orientation="Horizontal">            <TextBlock Text="CPU Usage" Margin="20,10,0,0"                       FontSize="15" 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.VerticalAxis>                <d3:VerticalIntegerAxis />            </d3:ChartPlotter.VerticalAxis>            <d3:ChartPlotter.HorizontalAxis>                <d3:HorizontalIntegerAxis />            </d3:ChartPlotter.HorizontalAxis>            <d3:Header Content="CPU Performance History"/>            <d3:VerticalAxisTitle Content="Percentage"/>        </d3:ChartPlotter>    </Grid></Window>

XAML

  接下來工作需要通過C#每秒獲取一次CPU使用率,并將這些數(shù)據(jù)生成坐標(biāo)點(Point)繪制在圖表中。 以下是MainWindow.xaml.cs 部分的代碼內(nèi)容。

using System;using System.Diagnostics;using System.Windows;using System.Windows.Media;using System.Windows.Threading;using Microsoft.Research.DynamicDataDisplay;using Microsoft.Research.DynamicDataDisplay.DataSources;namespace WpfPerformance{    public partial class MainWindow : Window    {        private ObservableDataSource<Point> dataSource = new ObservableDataSource<Point>();        private PerformanceCounter cpuPerformance = new PerformanceCounter();        private DispatcherTimer timer = new DispatcherTimer();        private int i = 0;        public MainWindow()        {            InitializeComponent();        }        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);            i++;        }        private void Window_Loaded(object sender, RoutedEventArgs e)        {            plotter.AddLineGraph(dataSource, Colors.Green, 2, "Percentage");            timer.Interval = TimeSpan.FromSeconds(1);            timer.Tick += new EventHandler(AnimatedPlot);            timer.IsEnabled = true;            plotter.Viewport.FitToView();        }    }}

     通過ObservableDataSource<Point> 動態(tài)存儲圖表坐標(biāo)點,PerformanceCounter 獲取CPU使用率數(shù)值,DispatcherTimer 計時器在規(guī)定間隔進(jìn)行取數(shù)操作,整型i 作為CPU使用率坐標(biāo)點的X軸數(shù)值。

private ObservableDataSource<Point> dataSource = new ObservableDataSource<Point>();private PerformanceCounter cpuPerformance = new PerformanceCounter();private DispatcherTimer timer = new DispatcherTimer();private int i = 0;

     AnimatedPlot 事件用于構(gòu)造坐標(biāo)點,通過設(shè)置cpuPerformance 相關(guān)參數(shù),并使用NextValue() 方法獲取當(dāng)前CPU使用率數(shù)據(jù)作為Y值,整型i 作為X值。將X、Y值構(gòu)造為坐標(biāo)點(Point),并通過異步方式存儲在dataSource 中。

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);    i++;}

     最后通過Window_Loaded 將事件加載到<Window> 中,AddLineGraph 方法將dataSource 中的坐標(biāo)點繪制到圖表中,曲線顏色定義為綠色,粗細(xì)設(shè)置為2,曲線名稱為"Percentage"。設(shè)置計時器間隔為1秒,連續(xù)執(zhí)行AnimatedPlot 事件實時繪制新坐標(biāo)點。

private void Window_Loaded(object sender, RoutedEventArgs e){    plotter.AddLineGraph(dataSource, Colors.Green, 2, "Percentage");    timer.Interval = TimeSpan.FromSeconds(1);    timer.Tick += new EventHandler(AnimatedPlot);    timer.IsEnabled = true;    plotter.Viewport.FitToView();}

CPU

鼠標(biāo)右鍵可將圖表拷貝到其他文檔:

CopyPlot

動態(tài)演示

鼠標(biāo)左鍵拖動圖表瀏覽任意位置曲線數(shù)據(jù),鼠標(biāo)中鍵可以縮放顯示曲線圖。

Capture

源代碼下載

WpfPerformance.zip

NET技術(shù)WPF 動態(tài)模擬CPU 使用率曲線圖,轉(zhuǎn)載需保留來源!

鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請第一時間聯(lián)系我們修改或刪除,多謝。

主站蜘蛛池模板: 成人做爰69片免费观看 | 亚洲日本中文 | 欧美三级视频在线观看 | 91久久婷婷 | 午夜视频一区 | 国产专区在线 | 国产精品成人一区二区三区 | 欧美极品在线 | 欧美另类视频 | 亚洲一区二区三区高清 | 欧美综合在线视频 | 欧美一级做a爰片免费视频 国产美女特级嫩嫩嫩bbb片 | 久草久草久草 | 伦理二区 | 亚洲国产精品久久久久 | 亚洲视频1区 | 亚洲高清在线观看 | 久久精品久久综合 | 欧美成人精品一区二区三区 | 国产精品久久久久影院色老大 | 中文字幕日韩一区 | 盗摄精品av一区二区三区 | 亚洲国产精品成人综合久久久 | 日本黄色免费大片 | 欧美精品在欧美一区二区 | 欧美一区二区三区免费电影 | 久久久久久国产精品 | 欧美最猛黑人 | 国产一区二区美女 | 黄色电影在线免费观看 | 亚洲激情一区二区三区 | 久久久精品一区二区三区 | av免费看片 | 国产精品久久久久久久久免费桃花 | 日韩综合在线 | 欧美视频在线播放 | 国产乱码精品1区2区3区 | 黄色网络在线观看 | 91av久久久 | 精品中文字幕在线 | 国产亚洲一区二区三区 |