基于echarts的水球的样式。

javascript 复制代码
<template>
    <div class="water-balloon">
        <div ref="barChart" style="height: 210px; width: calc(50vw - 22px)"></div>
    </div>
</template>
<script>
import * as echarts from 'echarts';

import 'echarts-liquidfill';
import ChartResizeMixin from '../mixins/chartResize';
export default {
    mixins: [ChartResizeMixin],
    name: 'WaterBalloon',
    props: { data: null },
    watch: {
        data(newVal) {
            // 数据更新时的处理
            this.initBarChart();
        }
    },
    data() {
        return {
            chart: "",
            targetX: 0,
            targetY: 0,
            targetz: 0,
        };
    },
    mounted() {
        this.initBarChart();
        // ...existing code...
        //window.addEventListener('resize', this.resizeHandler);
    },
    methods: {
        getColorByValue(value) {
            // if (typeof value !== 'number') {
            //     throw new Error('输入值必须是数字');
            // }

            if (value < 33) {
                return '#89CE87'; // 33以下:黄色
            } else if (value >= 33 && value <= 66) {
                return '#5170DE';   // 33-66:蓝色
            } else {
                return '#D24343';    // 66以上:红色
            }
        },
        calculateTarget(position) {
            const containerWidth = this.$refs.barChart.offsetWidth;
            switch (position) {
                case 'X':
                    return containerWidth / 6 - 26; // 计算 targetX
                case 'Y':
                    return containerWidth / 2 - 26; // 计算 targetY
                case 'Z':
                    return (containerWidth / 6) * 5 - 26; // 计算 targetZ
                default:
                    return 0;
            }
        },
        generateTitleConfig() {
            const positions = ['X', 'Y', 'Z'];
            return positions.map((position) => ({
                text: '利用率',
                x: this.calculateTarget(position),
                y: '60%',
                textStyle: {
                    fontSize: 14,
                    fontWeight: '100',
                    color: '#ffffff',
                    lineHeight: 16,
                    textAlign: 'center',
                },
            }));
        },
        handleResize() {

            if (this.chart) {
                this.chart.resize();
            }


            this.chart.setOption({
                title: this.generateTitleConfig(),
            });


        },
        chartslabel() {
            return {
                normal: {
                    textStyle: {
                        fontSize: 20,         // 字体大小
                        color: '#000',        // 字体颜色
                        fontWeight: 'bold',   // 字体粗细
                        fontFamily: 'Arial',  // 字体族
                        fontStyle: 'italic',  // 字体样式(italic/normal)
                        textBorderColor: '#BBB', // 文字描边颜色
                        textBorderWidth: 2    // 文字描边宽度
                    },
                    formatter: function (params) {
                        return (Math.floor(params.value * 10000) / 100) + "%"; //(params.value * 100).toFixed(2) *1 +"%";
                    },
                    position: 'inside',  // 位置:inside(内部)/outside(外部)/top(顶部)等

                },
            }
        },
        initBarChart() {

            const usageKeys = ['cpuUsage', 'memoryUsage', 'jobSlotUsage'];

            // 遍历 keys,统一处理数据初始化
            usageKeys.forEach((key) => {
                if (this.data[key] == null || this.data[key] === '-') {
                    this.data[key] = 0;
                }
            });
            //  this.data.cpuUsage = 50;
            this.chart = echarts.init(this.$refs.barChart);
            let value1 = (this.data.cpuUsage / 100);
            let value2 = (this.data.memoryUsage / 100);
            let value3 = (this.data.jobSlotUsage / 100);

            let colorStops1 = this.getColorByValue(this.data.cpuUsage);
            let colorStops2 = this.getColorByValue(this.data.memoryUsage);
            let colorStops3 = this.getColorByValue(this.data.jobSlotUsage);

            let that = this;

            let option = {

                title: this.generateTitleConfig(),

                series: [
                    {
                        type: 'liquidFill',
                        radius: '80%',
                        center: ['16.66666%', '50%'],
                        itemStyle: {
                            normal: {
                                shadowBlur: 15,
                                shadowColor: 'rgba(255, 255, 255, 0.5)'
                            }
                        },
                        color: [
                            {
                                type: 'linear',
                                x: 0,
                                y: 0,
                                x2: 0,
                                y2: 1,
                                colorStops: [
                                    {
                                        offset: 0,
                                        color: colorStops1,
                                    },
                                    {
                                        offset: 1,
                                        color: '#ADB3CD',
                                    },
                                ],
                                globalCoord: false,
                            },
                        ],
                        data: [value1, value1, value1], // data个数代表波浪数
                        backgroundStyle: {
                            color: {
                                type: 'radial', // 径向渐变
                                x: 0.5, // 圆心坐标(中心)
                                y: 0.5,
                                r: 1,
                                colorStops: [
                                    {
                                        offset: 1,
                                        color: 'rgba(75,84,97,0.5)',
                                    },
                                    {
                                        offset: 0,
                                        color: 'rgba(0, 0, 0,1)',
                                    },
                                ],
                                globalCoord: false,
                            },
                        },
                        // label: {
                        //     normal: {
                        //         textStyle: {
                        //             fontSize: 20,
                        //             color: '#fff',
                        //         },
                        //     },
                        // },
                        label: that.chartslabel(),
                        outline: {
                            // show: false
                            borderDistance: 0,
                            itemStyle: {
                                borderWidth: 2,
                                borderColor: {
                                    type: 'linear', // 线性渐变
                                    x: 0, y: 0, x2: 0, y2: 1, // 从左到右
                                    colorStops: [
                                        { offset: 0, color: 'rgba(0, 0, 0, 0.4)' }, // 起始颜色
                                        { offset: 1, color: 'rgba(255, 255, 255, 0.6)' }  // 结束颜色
                                    ],
                                    global: false // 非全局坐标系
                                },
                            },
                        },
                    },


                    { //第二个球的填充
                        type: 'liquidFill',
                        radius: '80%',
                        center: ['50%', '50%'],
                        itemStyle: {
                            normal: {
                                shadowBlur: 15,
                                shadowColor: 'rgba(255, 255, 255, 0.5)'
                            }
                        },
                        color: [
                            {
                                type: 'linear',
                                x: 0,
                                y: 0,
                                x2: 0,
                                y2: 1,
                                colorStops: [
                                    {
                                        offset: 0,
                                        color: colorStops2,
                                    },
                                    {
                                        offset: 1,
                                        color: '#AFB2CB',
                                    },
                                ],
                                globalCoord: false,
                            },
                        ],
                        data: [value2, value2], // data个数代表波浪数
                        backgroundStyle: {
                            color: {
                                type: 'radial', // 径向渐变
                                x: 0.5, // 圆心坐标(中心)
                                y: 0.5,
                                r: 1,
                                colorStops: [
                                    {
                                        offset: 1,
                                        color: 'rgba(75,84,97,0.5)',
                                    },
                                    {
                                        offset: 0,
                                        color: 'rgba(0, 0, 0,1)',
                                    },
                                ],
                                globalCoord: false,
                            },
                        },
                        // label: {
                        //     normal: {
                        //         textStyle: {
                        //             fontSize: 20,
                        //             color: '#fff',
                        //         },
                        //     },
                        // },
                        label: that.chartslabel(),
                        outline: {
                            // show: false
                            borderDistance: 0,
                            itemStyle: {
                                borderWidth: 2,
                                borderColor: {
                                    type: 'linear', // 线性渐变
                                    x: 0, y: 0, x2: 0, y2: 1, // 从左到右
                                    colorStops: [
                                        { offset: 0, color: 'rgba(0, 0, 0, 0.4)' }, // 起始颜色
                                        { offset: 1, color: 'rgba(255, 255, 255, 0.6)' }  // 结束颜色
                                    ],
                                    global: false // 非全局坐标系
                                },
                            },
                        },
                    },
                    { //第三个球的填充
                        type: 'liquidFill',
                        radius: '80%',
                        center: ['83.33333%', '50%'],
                        itemStyle: {
                            normal: {
                                shadowBlur: 15,
                                shadowColor: 'rgba(255, 255, 255, 0.5)'
                            }
                        },
                        color: [
                            {
                                type: 'linear',
                                x: 0,
                                y: 0,
                                x2: 0,
                                y2: 1,
                                colorStops: [
                                    {
                                        offset: 0,
                                        color: colorStops3,
                                    },
                                    {
                                        offset: 1,
                                        color: '#CDB1B3',
                                    },
                                ],
                                globalCoord: false,
                            },
                        ],
                        data: [value3, value3], // data个数代表波浪数
                        backgroundStyle: {
                            color: {
                                type: 'radial', // 径向渐变
                                x: 0.5, // 圆心坐标(中心)
                                y: 0.5,
                                r: 1,
                                colorStops: [
                                    {
                                        offset: 1,
                                        color: 'rgba(75,84,97,0.5)',
                                    },
                                    {
                                        offset: 0,
                                        color: 'rgba(0, 0, 0,1)',
                                    },
                                ],
                                globalCoord: false,
                            },
                        },
                        label: that.chartslabel(),
                        outline: {
                            // show: false
                            borderDistance: 0,
                            itemStyle: {
                                borderWidth: 2,
                                borderColor: {
                                    type: 'linear', // 线性渐变
                                    x: 0, y: 0, x2: 0, y2: 1, // 从左到右
                                    colorStops: [
                                        { offset: 0, color: 'rgba(0, 0, 0, 0.4)' }, // 起始颜色
                                        { offset: 1, color: 'rgba(255, 255, 255, 0.6)' }  // 结束颜色
                                    ],
                                    global: false // 非全局坐标系
                                },
                            },
                        },
                    },
                ],
            };

            this.chart.setOption(option);
        },
        // ...existing code...
    },
};
</script>

<style scoped></style>
相关推荐
学网安的肆伍5 小时前
【046-WEB攻防篇】注入工具&SQLMAP&Tamper编写&指纹修改&高权限操作&目录架构
前端·架构
森林的尽头是阳光5 小时前
tree回显问题
javascript·vue.js·elementui
weixin_BYSJ19876 小时前
「课设设计」springboot校园超市助购系统26449 (附源码)
java·javascript·spring boot·python·django·flask·php
Highcharts.js6 小时前
如何下载使用Highcharts Grid 开发web可编辑表格
前端·人工智能·grid 表格·web 表格·在线编辑表格·highcharts表格安装·表格开发工具
用户62960593247057 小时前
从“浏览器根本区分不了”到真正实现:刷新不退出,关闭标签页自动退出,前端判断浏览器关闭和刷新
前端
qetfw7 小时前
MWU:Vue 3 + FastAPI 的 MaaFramework 跨平台 WebUI 源码
前端·vue.js·python·fastapi·开源项目·效率工具
mfxcyh7 小时前
Vue3+Ant Design Vue 实现手动异步文件上传(含大小校验、弹窗上传)
前端·javascript·vue.js
wordbaby7 小时前
Web端热敏标签打印完整解决方案(QZ Tray + 译维A42)
前端
swipe7 小时前
03|Axios 请求进了后端之后:Controller、Request、Response 是怎么接住它的?
前端·后端·全栈
meilindehuzi_a7 小时前
Vue3进阶基础:指令修饰符、v-model表单绑定、动态样式、计算属性与侦听器实战
前端·javascript·vue.js