基于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>
相关推荐
沐言人生4 小时前
自媒体人的效率神器!监控、采集、下载、发布,这个免费开源工具全拿下!
前端·vue.js·后端
怕浪猫5 小时前
NVIDIA 全产品谱系与演进
javascript·面试·github
excel5 小时前
为什么 super 中能接收到 resolve 和 reject?
前端
Flynt7 小时前
AI 生成的界面,能放心点吗?我把 Google A2UI 的三层防线拆开测了一遍
前端·google·agent
前端混子8 小时前
我用 AI 做了个「禾月小历」,一套代码跑微信小程序和 H5
前端
IT_陈寒8 小时前
Java实战技巧总结:当你的finally块没有执行时会发生什么?
前端·人工智能·后端
小帅不太帅9 小时前
一年一次黑五,为了买软件真省钱,我做了个开源小站
前端·人工智能·github
JavaGuide10 小时前
刚刚,阿里版 Codex 上线了匿名模型 HungerG,无限 Token!
前端·后端
zhanlang021711 小时前
HTML 零基础入门到企业实战全套教程【第九章 9.3】移动端 HTML 基础适配入门
前端·html
用户693717500138411 小时前
AI 帮我写了大半年的代码,我却越来越不会写代码了
前端·后端·github