天天躁日日躁狠狠躁AV麻豆-天天躁人人躁人人躁狂躁-天天澡夜夜澡人人澡-天天影视香色欲综合网-国产成人女人在线视频观看-国产成人女人视频在线观看

WPF 動態模擬CPU 使用率曲線圖

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

     新建項目將DynamicDataDisplay.dll 加載到References 中,打開MainWindow.xaml 添加命名空間xmlns:d3="http://research.microsoft.com/DynamicDataDisplay/1.0"。通過<d3:ChartPlotter> 創建一個圖表框架,在其中添加兩條整型坐標軸,X軸:<d3:HorizontalIntegerAxis>,Y軸:<d3:VerticalIntegerAxis>。<d3:Header> 用來設置圖表名稱,<d3:VerticalAxisTitle> 用來設置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使用率,并將這些數據生成坐標點(Point)繪制在圖表中。 以下是MainWindow.xaml.cs 部分的代碼內容。

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> 動態存儲圖表坐標點,PerformanceCounter 獲取CPU使用率數值,DispatcherTimer 計時器在規定間隔進行取數操作,整型i 作為CPU使用率坐標點的X軸數值。

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

     AnimatedPlot 事件用于構造坐標點,通過設置cpuPerformance 相關參數,并使用NextValue() 方法獲取當前CPU使用率數據作為Y值,整型i 作為X值。將X、Y值構造為坐標點(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 中的坐標點繪制到圖表中,曲線顏色定義為綠色,粗細設置為2,曲線名稱為"Percentage"。設置計時器間隔為1秒,連續執行AnimatedPlot 事件實時繪制新坐標點。

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

鼠標右鍵可將圖表拷貝到其他文檔:

CopyPlot

動態演示

鼠標左鍵拖動圖表瀏覽任意位置曲線數據,鼠標中鍵可以縮放顯示曲線圖。

Capture

源代碼下載

WpfPerformance.zip

NET技術WPF 動態模擬CPU 使用率曲線圖,轉載需保留來源!

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

主站蜘蛛池模板: 亚洲一区在线观看无码欧美 | 亚洲色综合中文字幕在线 | 三级黄色在线 | 高h肉辣文黄蓉 | 在线色av| 囯产精品一品二区三区 | 超碰在线视频 免费 | 国产欧美无码亚洲 | 国产亚洲精品久久孕妇呦呦你懂 | 一受n攻高h全肉np | 嫩草影院精品视频在线观看 | 色久久一个亚洲综合网 | 麻豆婷婷狠狠色18禁久久 | www.色小姐| 狠狠干.in | 免费观看99热只有精品 | 神马电影我不卡4k手机在线观看 | 性啪啪chinese东北女人 | 在线观看亚洲专区5555 | 一本之道高清在线观看一区 | 欧美xxxxx九色视频免费观看 | 免费可以看污动画软件 | 国产在线高清亚洲精品一区 | 国产亚洲精品久久久久久入口 | 扒开老师大腿猛进AAA片 | 精品国产福利一区二区在线 | 被肉日常np高h | 麻豆精品2021最新 | 99国产精品偷窥熟女精品视频 | 99视频精品免视3 | 亚洲无线观看国产 | 一区两区三不卡 | 国产亚洲精品AV片在线观看播放 | 亚洲天堂一区二区三区 | 欧美亚洲视频在线二区 | 超碰97av 在线人人操 | 粉嫩小护士 | 麻豆传煤网站网址入口在线下载 | 日韩欧美群交P内射捆绑 | 祺鑫WRITEAS流出来了 | 国产精品VIDEOS麻豆TUBE |