Delphi的TCanvas类可以实现各种复杂的图形输出功能,基于近期项目的需求,利用它实现了一个很炫的动态折线图(模拟了资源管理器中CPU使用率的折线图),可以直观地展现出数值的实时变化情况。

这段代码里边有几个核心的地方:

  • 首先是为了缓解刷新时画布闪烁,利用了双缓冲的原理;
  • 其次结合队列,保证了数据的顺序压入;
  • 还有就是一些简单的数组算法。

最终的效果如下:

单元代码如下:

unit Unit1;
 
interface
 
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls,Contnrs;
 
const
  {* 网格间隔 *}
  GridSpace = ;
  {* 移动步长(能够被间隔整除) *}
  MoveStep = ;
  {* Y轴最大值(最大刻度) *}
  MaxY = ;
 
type
  TForm1 = class(TForm)
    Timer1: TTimer;
    Button1: TButton;
    Image1: TImage;
    procedure DrawPL(Shower:TImage);
    procedure Timer1Timer(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;
 
var
  Form1: TForm1;
  {* 网格竖线X坐标数组 *}
  GridXPArr: array of Integer;
  {* 点坐标数组 *}
  PointLst: array of TPoint;
  {* 数值队列 *}
  YPQueue: TQueue;
  {* 数值指针 *}
  PYValue: PInteger;
  {* 网格偏移量 *}
  X: Word;
 
implementation
 
{$R *.dfm}
 
procedure TForm1.Button1Click(Sender: TObject);
var
  YValue:Integer;
begin
  Randomize;
  YValue := Random();
  //新Y坐标点按顺序压入队列
  New(PYValue);
  PYValue^ := YValue;
  YPQueue.Push(PYValue);
end;
 
procedure TForm1.DrawPL(Shower:TImage);
var
  Bit: TBitmap;
  i: Integer;
  PW,PH: Integer;
  YValue:Integer;
begin
  //偏移量计算
  Inc(X);
  if X = GridSpace div MoveStep then
  X := ;
  //初始化画布(双缓冲)
  Bit := TBitmap.Create;
  try
    PW := Shower.Width;
    PH := Shower.Height;
    Bit.Width := PW;
    Bit.Height := PH;
    //初始化网格竖线X坐标数组长度为宽/间隔+1
    SetLength(GridXPArr,PW div GridSpace + );
    with Bit.Canvas do
    begin
      Brush.Color := clBlack;
      Brush.Style := bsSolid;
      Rectangle(,,PW,PH);
      Pen.Color := $;
      //画网格,根据偏移量实现动态效果
      for i := to PW div GridSpace + do
      begin
        GridXPArr[i] := GridSpace * i - X * MoveStep;
        MoveTo(GridXPArr[i],);
        LineTo(GridXPArr[i],PH);
      end;
      for i := to PH div GridSpace do
      begin
        MoveTo(,GridSpace * i);
        LineTo(PW,GridSpace * i);
      end;
      //画折线
      Pen.Color := clLime;
      YValue := ;
      //如果队列中有新的Y坐标点,则输出
      if YPQueue.Count > then
      begin
        PYValue := YPQueue.Pop;
        YValue := PYValue^;
        Dispose(PYValue);
      end;
      //画笔移动到起点位置
      MoveTo(,PH);
      //每执行一次函数,Y坐标向前移动一位,并连线各个点
      for i := to Length(PointLst) - do
      begin
        PointLst[i].Y := PointLst[i + ].Y;
        LineTo(PointLst[i+].X,PointLst[i+].Y);
      end;
      //按比例更新最后一位坐标点
      PointLst[Length(PointLst)-].X := PW;
      PointLst[Length(PointLst)-].Y := PH - (YValue  * PH div MaxY);
      //打印信息(可根据需要调整显示位置和内容)
      Brush.Style:=bsClear;
      Font.Color:=clYellow;
      TextOut(,,'数值:'+inttostr(YValue));
    end;
    Shower.Canvas.Draw(,,Bit);
  finally
    Bit.Free;
  end;
end;
 
procedure TForm1.FormCreate(Sender: TObject);
var
  i:Integer;
begin
  YPQueue := TQueue.Create;
  //初始化坐标点个数为宽/步长+1
  SetLength(PointLst,Image1.Width div MoveStep + );
  //初始化坐标点为X轴基线位置
  for i := to Length(PointLst) - do
  begin
    PointLst[i].X := i*MoveStep;
    PointLst[i].Y := Image1.Height;
  end;
end;
 
procedure TForm1.FormDestroy(Sender: TObject);
begin
  YPQueue.Free;
end;
 
procedure TForm1.FormShow(Sender: TObject);
begin
  DrawPL(Image1);
end;
 
procedure TForm1.Timer1Timer(Sender: TObject);
begin
  DrawPL(Image1);
end;
 
end.

模拟Windows任务管理器CPU使用率的动态折线图-农夫山泉的更多相关文章

  1. achartengine画出动态折线图

    achartengine画出动态折线图的效果最近有个项目需要用到实时曲线图,我也上网搜索了一下,最后还是选择使用achartengine这个现成的东西,毕竟自己再canvas一下实在是太麻烦,而且项目 ...

  2. 原生JS实现动态折线图

    原生JS实现动态折线图 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> & ...

  3. zabbix监控windows系统CPU使用率

    参考网站:https://blog.csdn.net/reblue520/article/details/76287113 Zabbix 自带的模块没有 CPU 使用率(百分比)这个监控项,我们可以通 ...

  4. 获取Windows操作系统的CPU使用率以及内存使用率

    此功能参考了ProcessHacker项目的代码. 声明定义 typedef struct _UINT64_DELTA { ULONG64 Value; ULONG64 Delta; } UINT64 ...

  5. zabbix 监控windows端cpu使用率百分比

    参考网站:http://www.fyluo.com/?post=108 zabbix自带的模版没有CPU使用率(百分比)这个监控项,那么我们可以通过添加计数器的方式实现CPU百分比的监控. 在zabb ...

  6. 开发成功-cpu-mem监控动态折线图--dom esayui js java

    jsp ------------------------------------------------------------------------------------------- ---- ...

  7. WPF动态折线图

    此项目源码下载地址:https://github.com/lizhiqiang0204/WpfDynamicChart 效果图如下: 此项目把折线图制作成了一个控件,在主界面设置好参数直接调用即可,下 ...

  8. Echarts 动态折线图

    <script src="http://echarts.baidu.com/build/dist/echarts-all.js"></script>< ...

  9. 【转】STM32: 一种计算CPU使用率的方法及其实现原理

    1  前言出于性能方面的考虑,有的时候,我们希望知道CPU的使用率为多少,进而判断此CPU的负载情况和对于当前运行环境是否足够“胜任”.本文将介绍一种计算CPU占有率的方法以及其实现原理. 2  移植 ...

随机推荐

  1. php正則表達式中的非贪婪模式匹配的使用

    php正則表達式中的非贪婪模式匹配的使用 通常我们会这么写: $str = "http://www.baidu/.com? url=www.sina.com/"; preg_mat ...

  2. HUAWEI HiAI亮相Droidcon柏林2018开发者峰会 开启HiAI海外生态

    柏林时间6月25日到27日,华为HiAI亮相Droidcon柏林2018开发者峰会,有1200多位海外开发者参加了此次峰会,来自HUAWEI HiAI领域的多名专家携手Prisma和金山WPS,以“E ...

  3. LeetCode_Minimum Depth of Binary Tree

    一.题目 Minimum Depth of Binary Tree My Submissions Given a binary tree, find its minimum depth. The mi ...

  4. cocos2dx游戏 地图

    #include "HelloWorld.h" USING_NS_CC; CCScene* MyHelloWorld::scene() { // 'scene' is an aut ...

  5. 【SoapUI、Postman、WebServiceStudio、Jmeter】接口测试工具结合测试webservice接口(发送XML格式参数)

    目录: SoapUI测试webservice接口,发送XML格式参数 Postman测试webservice接口,发送XML格式参数 WebServiceStudio.exe测试webservice接 ...

  6. RabbitMQ集群安装配置+HAproxy+Keepalived高可用

    RabbitMQ集群安装配置+HAproxy+Keepalived高可用 转自:https://www.linuxidc.com/Linux/2016-10/136492.htm rabbitmq 集 ...

  7. 线程池 Future 带返回结果

    package com.aibi.cmdc.bigscreen.action; import java.util.ArrayList; import java.util.HashMap; import ...

  8. linux使用rsync+inotify-tools+ssh实现文件实时同步

    假设某服务器架构中有两台web服务器(IP为192.168.1.252和192.168.1.254),一台代码更新发布服务器(IP为192.168.1.251),需要同步的目录是/data/www/, ...

  9. 批处理--md5校检

    @echo off rem 获取文件xx.zip的MD5 for /f "delims=" %%i in ('md5.exe xx.zip') do (set md5_var=%% ...

  10. MySQL的基本操作--第一弹

    前言:在听许嵩,忆当年,意气风发 ———————————————————————————————————————————————— 好了,今天和大家同步讲解mysql的知识了.都是最基本的知识. 一. ...