如何在VUE框架下渲染出来一个水球图

Vue 框架下,使用 ECharts 渲染水球图通常需要借助 echarts-liquidfill 插件,该插件专门用于创建水球图效果。


1. 安装依赖

首先,确保你已经安装了 ECharts 和 echarts-liquidfill

npm install echarts echarts-liquidfill

2. 在 Vue 组件中使用

在 Vue 组件中,可以这样使用 ECharts 渲染水球图:

<template>
  <d2-container>
  <div ref="chart" class="chart-container"></div>
</d2-container>
</template>

<script>
import * as echarts from 'echarts';
import 'echarts-liquidfill';

export default {
  name: 'LiquidFillChart',
  mounted() {
    this.initChart();
  },
  methods: {
    initChart() {
      const chartDom = this.$refs.chart;
      const myChart = echarts.init(chartDom);

      const option = {
        series: [{
          type: 'liquidFill',
          data: [0.6, 0.5, 0.4, 0.3], // 水球图的数据,表示填充的比例
          radius: '80%', // 水球图的半径
          outline: {
            show: false // 是否显示外边框
          },
          label: {
            show: true,
            formatter: (param) => {
              return `${(param.value * 100).toFixed(0)}%`; // 显示百分比
            },
            fontSize: 28,
            color: '#fff',
            fontWeight: 'bold',
            textShadow: '2px 2px 4px rgba(0, 0, 0, 0.5)' // 添加文字阴影
          },
          backgroundStyle: {
            color: '#E6F7FF', // 背景颜色
            borderWidth: 10,
            borderColor: '#1890FF',
            shadowBlur: 10,
            shadowColor: 'rgba(0, 0, 0, 0.3)', // 背景阴影
            shadowOffsetX: 5,
            shadowOffsetY: 5
          },
          itemStyle: {
            color: {
              type: 'linear',
              x: 0,
              y: 0,
              x2: 0,
              y2: 1,
              colorStops: [{
                offset: 0, color: '#1890FF' // 渐变起始颜色
              }, {
                offset: 1, color: '#0050B3' // 渐变结束颜色
              }]
            },
            shadowBlur: 10,
            shadowColor: 'rgba(0, 0, 0, 0.5)', // 水球阴影
            shadowOffsetX: 5,
            shadowOffsetY: 5
          },
          emphasis: {
            itemStyle: {
              opacity: 0.8
            }
          }
        }]
      };

      myChart.setOption(option);
    }
  }
};
</script>

<style scoped>
.chart-container {
  width: 98%;
  height: 400px;
  background: #f0f0f0; /* 背景颜色 */
  border-radius: 15px; /* 圆角 */
  box-shadow: 5px 5px 15px rgba(0, 0, 0, 0.2); /* 容器阴影 */
  padding: 20px; /* 内边距 */
}
</style>

3. 说明

  • type: 'liquidFill':指定渲染水球图。
  • data: [0.6]:表示水球的填充程度,支持多个值创建波浪效果,如 [0.6, 0.55, 0.5]
  • radius: '80%':控制水球大小。
  • outline: { show: false }:关闭外边框。
  • backgroundStyle.color:设置水球背景色。
  • label:显示水球内的数值,并调整字体颜色和大小。

4. 动态数据

如果你想动态修改水球图的值,例如从后端获取数据,可以在 data 里添加 value 变量,然后在 watch 里监听它的变化:

<template>
  <div ref="chart" style="width: 300px; height: 300px;"></div>
</template>

<script>
import * as echarts from 'echarts';
import 'echarts-liquidfill';

export default {
  data() {
    return {
      value: 0.6 // 初始值
    };
  },
  mounted() {
    this.initChart();
  },
  watch: {
    value(newVal) {
      this.updateChart(newVal);
    }
  },
  methods: {
    initChart() {
      this.myChart = echarts.init(this.$refs.chart);
      this.setChartOption();
    },
    setChartOption() {
      const option = {
        series: [
          {
            type: 'liquidFill',
            data: [this.value],
            radius: '80%',
            outline: { show: false },
            label: { fontSize: 20, color: '#000' }
          }
        ]
      };
      this.myChart.setOption(option);
    },
    updateChart(newValue) {
      this.myChart.setOption({
        series: [{ data: [newValue] }]
      });
    }
  }
};
</script>

然后你可以在 methods 里动态修改 this.value,比如从后端接口获取数据。


5. 总结

  • echarts-liquidfill 用于创建水球图,数据范围为 0-1。
  • 可以自定义水球大小、颜色、边框等样式。
  • 通过 watch 监听数据变化,实现动态更新水球图。

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值