使用canvas绘制类似任务管理器使用率图表

1.效果展示

 2.代码实现

样式&结构代码

<style>
    .container {
        width: 600px;
        height: 200px;
        background-color: red;
        margin: 0 auto;
    }
</style>

<div class="container">
    <canvas id="cpuCanvas" style="width: 100%; height:100%;"></canvas>
</div>

工具类dataQueue

class dataQueue {
    constructor() {
        this.data = [];
    }
    //添加数据到列队中
    push(params) {
        this.data.push(params);
    }
    //获取列队中的数据
    get(index = 0) {
        return this.data[index];
    }

    getAll() {
        return this.data;
    }
    //获取最后一个元素
    getLast() {
        return this.data[this.data.length - 1];
    }
    //获取指定元素的位置
    getIndex(item) {
        return this.data.lastIndexOf(item);
    }
    //从列队中删除数据
    dequeue() {
        return this.data.shift();
    }

    //判断列队是否为空
    isEmpty() {
        return this.data.length === 0;
    }
    //列队长度
    size() {
        return this.data.length;
    }
    //清空列队
    clear() {
        this.data = [];
    }
    //打印列队
    toString() {
        return this.data.toString();
    }
}

逻辑处理代码 

let queue = new dataQueue();
let canvas = document.getElementById("cpuCanvas");
canvas.width = canvas.clientWidth * devicePixelRatio;
canvas.height = canvas.clientHeight * devicePixelRatio;
let ctx = canvas.getContext("2d");
function drawLineChart() {
    ctx.clearRect(0, 0, canvas.width, canvas.height);
    ctx.beginPath();
    ctx.fillStyle = 'blue'

    let value = queue.getAll();
    for (let i = 0; i < value.length; i++) {
        ctx.lineTo(ConvertIndexToX(value.length, value.length - 1 - i), PercentageToY(value[i]));
        if (value.length - 1 - i == 0) {
            ctx.lineTo(canvas.clientWidth * devicePixelRatio, canvas.clientHeight * devicePixelRatio);
            ctx.lineTo(ConvertIndexToX(value.length, i + 1), canvas.clientHeight * devicePixelRatio);
            ctx.fill();
        }
    }
    ctx.stroke();
}
// 将一个百分比转化为y
function PercentageToY(percentage) {
    const canvasHeight = canvas.clientHeight;
    const y = (canvasHeight - ((canvasHeight / 100) * percentage)) * devicePixelRatio;
    return y;
}
//将数据索引位置转化为X值
function ConvertIndexToX(accuracy, index) {
    const canvasWidth = canvas.clientWidth;
    let x = ((canvasWidth / accuracy) * index) * devicePixelRatio;
    //反转X轴
    x = canvasWidth * devicePixelRatio - x;
    return x;
}
//监听窗体大小改变事件
window.onresize = function () {
    canvas.width = canvas.clientWidth * devicePixelRatio;
    canvas.height = canvas.clientHeight * devicePixelRatio;
    drawLineChart();
}
function pushData(data, accuracy) {
    if (queue.size() == accuracy) {
        queue.dequeue();
    }
    queue.push(data);
    drawLineChart();
}
//用于模拟数据
function analogData(time = 2000) {
    let interval = setInterval(() => {
        pushData(Math.floor(Math.random() * 100), 60);
    }, 1000 / 10);
    setTimeout(() => {
        interval = clearInterval(interval);
    }, time);
}
  • 11
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

界心_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值