WPF dynamic simulation CPU usage Curve

Source: Internet
Author: User

During work, you often encounter the need to draw a group of data into a graph. The simplest way is to import the data into Excel and then use the drawing function to manually generate a graph. However, if the basic data is frequently changed, manual creation of images may become boring. This article uses DynamicDataDisplay to dynamically simulate the CPU usage chart in WPF to generate a graph dynamically.

The new project loads DynamicDataDisplay. dll to References and opens MainWindow. xaml to add the namespace xmlns: d3 = "http://research.microsoft.com/DynamicDataDisplay/1.0 ". Use <d3: ChartPlotter> to create a chart framework and add two integer coordinate axes, X axis: <d3: HorizontalIntegerAxis> and Y axis: <d3: VerticalIntegerAxis>. <D3: Header> is used to set the chart name. <d3: VerticalAxisTitle> is used to set the Y axis name.

<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 PerfZ running? Http://www.bkjia.com/kf/ware/vc/ "target =" _ blank "class =" keylink "> vcm1hbmNl" 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"
FontSize = "15" FontWeight = "Bold"/>
<TextBlock x: Name = "cpuUsageText" Margin = "10, 10, 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>


Next, you need to obtain the CPU usage once per second through C #, and generate the coordinates (points) of the data to be drawn in the chart. The following is the code of 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. CES;

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 }%", 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 ();
}
}
} Use ObservableDataSource <Point> to dynamically store the coordinate points of the chart and PerformanceCounter to obtain the CPU usage value. The DispatcherTimer timer performs the Count operation at the specified interval. The integer I is used as the X-axis value of the CPU usage coordinate Point.

Private ObservableDataSource <Point> dataSource = new ObservableDataSource <Point> ();
Private PerformanceCounter cpuPerformance = new PerformanceCounter ();
Private DispatcherTimer timer = new DispatcherTimer ();
Private int I = 0; The AnimatedPlot event is used to construct coordinate points. You can set cpuPerformance parameters and use NextValue () to obtain the current CPU usage data as the Y value, and integer I as the X value. Construct X and Y values as coordinate points and store them in dataSource asynchronously.

Private void AnimatedPlot (object sender, EventArgs e)
{
CpuPer

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.