Vue+Echart实现利用率表盘效果【组件已封装,可直接使用】

简介: Vue+Echart实现利用率表盘效果【组件已封装,可直接使用】

效果演示

当利用超过70%(可以自行设置),表盘变红


组件

里面对应两个图片资源,panelBackground_red.png 和 panelBackground_green.png,请前往百度网盘进行下载。如果喜欢其他颜色,可以使用.psd来修改导出就行。


链接:https://pan.baidu.com/s/12wvEGlKrvjEBFki9YbE3hQ

提取码:fibf


b5eaea683a4c46ee83fbe686ac3f8d23.png


在使用组件之前,记得引入echart组件,可以参考echart组件引入

<template>
    <!-- <div class="titleDiv">占用率</div> -->
    <div id="customGaugeContainer" ref="panel" style="height: 100%;"></div>
</template>
<script>
import * as echarts from 'echarts'
var app = {};
var ROOT_PATH = 'https://echarts.apache.org/examples';
// var _panelImageURL = ROOT_PATH + '/data/asset/img/custom-gauge-panel.png';
var _panelImageURL = require("@/assets/panel/panelBackground_green.png");
var _animationDuration = 1000;
var _animationDurationUpdate = 1000;
var _animationEasingUpdate = 'quarticInOut';
//这个不改
var _valOnRadianMax = 100;
//这个不改
var _pointerInnerRadius = 60;
//外轮廓半径
var _outerRadius = 110;
//内轮廓半径
var _innerRadius = 100;
//里面白色远的半径
var _insidePanelRadius = 100;
var _fontSize = 50;
var _currentDataIndex = 0;
var _shadowColor = '#20A53A';
var _textColor = '#20A53A';
var _shadowBlur = 20;
export default {
    name: 'SmartSchedulingSystemCustomGauge',
    props: {
        rate: {
            type: Number
        },
        timeChange: {
            type: Number
        }
    },
    watch: {
        rate: {
            handler(newValue, oldValue) {
                // console.log("刷新表盘数据")
                // console.log("oldValue:" + oldValue)
                // console.log("newValue:" + newValue)
                // console.log("rate:" + this.rate)
                //刷新表盘数据
                this.setOptions();
            },
            deep: true
        },
    },
    data() {
        return {
            myChart: undefined,
            scheduledTask: undefined,
        };
    },
    mounted() {
        this.$nextTick(() => {
            this.initChart();
            window.addEventListener('resize', this.myChart.resize());
            // this.setScheduledTask();
        })
    },
    methods: {
        initChart() {
            let dom = document.getElementById('customGaugeContainer');
            let clientHeight = dom.clientHeight;
            // console.log("clientHeight:" + clientHeight)
            _outerRadius = (clientHeight / 2) * 0.9;
            _innerRadius = (clientHeight / 2) * 0.8;
            _insidePanelRadius = (clientHeight / 2) * 0.8;
            _fontSize = (clientHeight / 2) * 0.3;
            _shadowBlur = (clientHeight / 2) * 0.2;
            // this.myChart = echarts.init(dom, null, {
            //     renderer: 'canvas',
            //     useDirtyRect: false
            // });
            this.myChart = echarts.init(this.$refs.panel);
            this.setOptions();
        },
        setOptions() {
            if (this.rate > 80) {
                _shadowColor = '#F33333';
                _textColor = '#F33333';
                _panelImageURL = require("@/assets/panel/panelBackground_red.png");
            } else {
                _shadowColor = '#20A53A';
                _textColor = '#20A53A';
                _panelImageURL = require("@/assets/panel/panelBackground_green.png");
            }
            let option = {
                animationEasing: _animationEasingUpdate,
                animationDuration: _animationDuration,
                animationDurationUpdate: _animationDurationUpdate,
                animationEasingUpdate: _animationEasingUpdate,
                //数据展示
                dataset: {
                    //100可以看成是百分比
                    source: [[1, this.rate]]
                },
                tooltip: {},
                angleAxis: {
                    type: 'value',
                    startAngle: 0,
                    show: false,
                    min: 0,
                    max: _valOnRadianMax
                },
                radiusAxis: {
                    type: 'value',
                    show: false
                },
                polar: {},
                series: [
                    {
                        type: 'custom',
                        coordinateSystem: 'polar',
                        renderItem: renderItem
                    }
                ]
            };
            if (option && typeof option === 'object') {
                this.myChart.setOption(option);
            }
        },
        /**
              * 设置定时任务
              */
        setScheduledTask() {
            this.scheduledTask = setInterval(function () {
                console.log("rate:" + this.rate)
            }, 2000);
        }
    },
};
function renderItem(params, api) {
    var valOnRadian = api.value(1);
    var coords = api.coord([api.value(0), valOnRadian]);
    var polarEndRadian = coords[3];
    var imageStyle = {
        image: _panelImageURL,
        x: params.coordSys.cx - _outerRadius,
        y: params.coordSys.cy - _outerRadius,
        width: _outerRadius * 2,
        height: _outerRadius * 2
    };
    return {
        type: 'group',
        children: [
            {
                type: 'image',
                style: imageStyle,
                clipPath: {
                    type: 'sector',
                    shape: {
                        cx: params.coordSys.cx,
                        cy: params.coordSys.cy,
                        r: _outerRadius,
                        r0: _innerRadius,
                        startAngle: 0,
                        endAngle: -polarEndRadian,
                        transition: 'endAngle',
                        enterFrom: { endAngle: 0 }
                    }
                }
            },
            // {
            //     type: 'image',
            //     style: imageStyle,
            //     clipPath: {
            //         type: 'polygon',
            //         shape: {
            //             points: makePionterPoints(params, polarEndRadian)
            //         },
            //         extra: {
            //             polarEndRadian: polarEndRadian,
            //             transition: 'polarEndRadian',
            //             enterFrom: { polarEndRadian: 0 }
            //         },
            //         during: function (apiDuring) {
            //             apiDuring.setShape(
            //                 'points',
            //                 makePionterPoints(params, apiDuring.getExtra('polarEndRadian'))
            //             );
            //         }
            //     }
            // },
            //白色中心圆
            {
                type: 'circle',
                shape: {
                    cx: params.coordSys.cx,
                    cy: params.coordSys.cy,
                    r: _insidePanelRadius
                },
                style: {
                    fill: '#fff',
                    shadowBlur: _shadowBlur,
                    shadowOffsetX: 0,
                    shadowOffsetY: 0,
                    //轮廓阴影颜色
                    shadowColor: _shadowColor
                }
            },
            {
                type: 'text',
                extra: {
                    valOnRadian: valOnRadian,
                    transition: 'valOnRadian',
                    enterFrom: { valOnRadian: 0 }
                },
                style: {
                    text: makeText(valOnRadian),
                    fontSize: _fontSize,
                    fontWeight: 700,
                    x: params.coordSys.cx,
                    y: params.coordSys.cy,
                    //字体颜色
                    fill: _textColor,
                    align: 'center',
                    verticalAlign: 'middle',
                    enterFrom: { opacity: 0 }
                },
                during: function (apiDuring) {
                    apiDuring.setStyle(
                        'text',
                        makeText(apiDuring.getExtra('valOnRadian'))
                    );
                }
            }
        ]
    };
}
function convertToPolarPoint(renderItemParams, radius, radian) {
    return [
        Math.cos(radian) * radius + renderItemParams.coordSys.cx,
        -Math.sin(radian) * radius + renderItemParams.coordSys.cy
    ];
}
function makePionterPoints(renderItemParams, polarEndRadian) {
    return [
        convertToPolarPoint(renderItemParams, _outerRadius, polarEndRadian),
        convertToPolarPoint(
            renderItemParams,
            _outerRadius,
            polarEndRadian + Math.PI * 0.03
        ),
        convertToPolarPoint(renderItemParams, _pointerInnerRadius, polarEndRadian)
    ];
}
function makeText(valOnRadian) {
    // Validate additive animation calc.
    if (valOnRadian < -10) {
        alert('illegal during val: ' + valOnRadian);
    }
    return ((valOnRadian / _valOnRadianMax) * 100).toFixed(1) + '%';
}
</script>
<style lang="scss" scoped>
#customGaugeContainer {
    // height: calc(100% - 20px);
    height: 100%;
}
</style>


使用方式

在页面引入组件

使用组件

<customGauge style="height: 150px;" :rate="server.jvm.usage">
</customGauge>


height设置组件的高度

rate属性设置利用率

目录
相关文章
|
3天前
|
缓存 监控 JavaScript
探讨优化Vue应用性能和加载速度的策略
【5月更文挑战第17天】本文探讨了优化Vue应用性能和加载速度的策略:1) 精简代码和组件拆分以减少冗余;2) 使用计算属性和侦听器、懒加载、预加载和预获取优化路由;3) 数据懒加载和防抖节流处理高频事件;4) 图片压缩和选择合适格式,使用CDN加速资源加载;5) 利用浏览器缓存和组件缓存提高效率;6) 使用Vue Devtools和性能分析工具监控及调试。通过这些方法,可提升用户在复杂应用中的体验。
10 0
|
4天前
|
JavaScript 前端开发
vue(1),小白看完都会了
vue(1),小白看完都会了
|
3天前
|
JavaScript 开发工具 git
Vue 入门系列:.env 环境变量
Vue 入门系列:.env 环境变量
10 1
|
3天前
|
JavaScript
vue知识点
vue知识点
11 0
|
4天前
|
JavaScript 前端开发 定位技术
Vue使用地图以及实现轨迹回放 附完整代码
Vue使用地图以及实现轨迹回放 附完整代码
Vue使用地图以及实现轨迹回放 附完整代码
|
4天前
|
JavaScript 数据库
ant design vue日期组件怎么清空 取消默认当天日期
ant design vue日期组件怎么清空 取消默认当天日期
|
4天前
|
JavaScript C++
vue高亮显示组件--转载
vue高亮显示组件--转载
9 0
|
4天前
|
JavaScript 前端开发 数据安全/隐私保护
揭秘Vue中v-model的内部工作机制
揭秘Vue中v-model的内部工作机制
|
4天前
|
JavaScript
Vue中避免滥用this去读取data中数据
Vue中避免滥用this去读取data中数据
|
4天前
|
JavaScript
vue中使用pinia及持久化
vue中使用pinia及持久化
6 0