vue echarts 水球图 多个水球图并存配置

echarts 水球图

下面介绍为vue-cli开发环境下
npm下载

npm install echarts --save
npm install echarts-liquidfill --save

按需加载,在所需的组件内引入

import echarts from "echarts";
import "echarts-liquidfill"; //在这里引入

组件代码如下(此处为8个水球图并存的页面)

<template>
  <el-row>
    <el-col>
      <div
        :class="className"
        :style="{ height: height, width: width }"
        ref="water"
      />
    </el-col>
  </el-row>
</template>

<script>
import echarts from "echarts";
import "echarts-liquidfill"; //在这里引入
export default {
  props: {
    className: {
      type: String,
      default: "chart",
    },
    width: {
      type: String,
      default: "100%",
    },
    height: {
      type: String,
      default: "300px",
    },
    waterData: {
      type: Array,
    },
    waterDataColor: {
      type: Array,
    },
  },
  data() {
    return {
      chart: null,
      data: [],
    };
  },
  beforeDestroy() {
    if (!this.chart) {
      return;
    }
    this.chart.dispose();
    this.chart = null;
  },
  // 写watch监听父页面向本组件传递过来的值,改动之后
  // 重新调用this.initWater()渲染视图数据
  watch: {
    waterData(curVal, oldVal) {
      if (curVal) {
        this.waterData = curVal;
        this.waterData.forEach((item) => {
          return this.data.push(item.inRate);
        });
        this.$nextTick(() => {
          this.initWater();
        });
      }
    },
  },
  created() {},
  methods: {
    initWater() {
      this.chart = echarts.init(this.$refs.water);
      this.chart.setOption({
        title: [
          {
            text: "1号仓库",
            x: "20%",
            y: 120,
            textAlign: "center",
            textStyle: {
              fontSize: "13",
              fontWeight: "100",
              color: "#b6b5b5",
              textAlign: "center",
            },
          },
          {
            text: "2号仓库",
            x: "40%",
            y: 120,
            textAlign: "center",
            textStyle: {
              fontSize: "13",
              fontWeight: "100",
              color: "#b6b5b5",
              textAlign: "center",
            },
          },
          {
            text: "3号仓库",
            x: "60%",
            y: 120,
            textAlign: "center",
            textStyle: {
              fontSize: "13",
              fontWeight: "100",
              color: "#b6b5b5",
              textAlign: "center",
            },
          },
          {
            text: "4号仓库",
            x: "80%",
            y: 120,
            textAlign: "center",
            textStyle: {
              fontSize: "13",
              fontWeight: "100",
              color: "#b6b5b5",
              textAlign: "center",
            },
          },
          {
            text: "5号仓库",
            x: "20%",
            y: 270,
            textAlign: "center",
            textStyle: {
              fontSize: "13",
              fontWeight: "100",
              color: "#b6b5b5",
              textAlign: "center",
            },
          },
          {
            text: "6号仓库",
            x: "40%",
            y: 270,
            textAlign: "center",
            textStyle: {
              fontSize: "13",
              fontWeight: "100",
              color: "#b6b5b5",
              textAlign: "center",
            },
          },
          {
            text: "7号仓库",
            x: "60%",
            y: 270,
            textAlign: "center",
            textStyle: {
              fontSize: "13",
              fontWeight: "100",
              color: "#b6b5b5",
              textAlign: "center",
            },
          },
          {
            text: "8号仓库",
            x: "80%",
            y: 270,
            textAlign: "center",
            textStyle: {
              fontSize: "13",
              fontWeight: "100",
              color: "#b6b5b5",
              textAlign: "center",
            },
          },
        ],
        // 鼠标滑入提示
        tooltip: {
          show: true,
        },
        series: [
          {
            type: "liquidFill",
            radius: "37%",
            z: 6,
            center: ["20%", "20%"], //此处为图形X轴相距数据
            color: this.waterDataColor,
            data: [this.data[0], { value: this.data[0], direction: "right" }], //第一个参数控制容器内满溢程度(最大为1)
            backgroundStyle: { borderWidth: 1 },
            label: { fontSize: 18 }, //图内百分比值文本大小
            name: "1号仓库", //鼠标滑入提示文本
            outline: {
              show: true,
              itemStyle: { borderWidth: 1, borderColor: this.waterDataColor[1] },
              borderDistance: 0,
            },
            waveAnimation: true, // 禁止左右波动
          },
          {
            type: "liquidFill",
            radius: "37%",
            z: 6,
            center: ["40%", "20%"],
            color: this.waterDataColor,
            data: [this.data[1], { value: this.data[1], direction: "left" }],
            backgroundStyle: { borderWidth: 1 },
            label: { fontSize: 18 },
            name: "2号仓库",
            outline: {
              show: true,
              itemStyle: { borderWidth: 1, borderColor: this.waterDataColor[1] },
              borderDistance: 0,
            },
          },
          {
            type: "liquidFill",
            radius: "37%",
            z: 6,
            center: ["60%", "20%"],
            color: this.waterDataColor,
            data: [this.data[2], { value: this.data[2], direction: "right" }],
            backgroundStyle: { borderWidth: 1 },
            label: { fontSize: 18 },
            name: "3号仓库",
            outline: {
              show: true,
              itemStyle: { borderWidth: 1, borderColor: this.waterDataColor[1] },
              borderDistance: 0,
            },
          },
          {
            type: "liquidFill",
            radius: "37%",
            z: 6,
            center: ["80%", "20%"],
            color: this.waterDataColor,
            data: [this.data[3], { value: this.data[3], direction: "left" }],
            backgroundStyle: { borderWidth: 1 },
            label: { fontSize: 18 },
            name: "4号仓库",
            outline: {
              show: true,
              itemStyle: { borderWidth: 1, borderColor: this.waterDataColor[1] },
              borderDistance: 0,
            },
          },
          {
            type: "liquidFill",
            radius: "37%",
            z: 6,
            center: ["20%", "70%"],
            color: this.waterDataColor,
            data: [this.data[4], { value: this.data[4], direction: "right" }],
            backgroundStyle: { borderWidth: 1 },
            label: { fontSize: 18 },
            name: "5号仓库",
            outline: {
              show: true,
              itemStyle: { borderWidth: 1, borderColor: this.waterDataColor[1] },
              borderDistance: 0,
            },
          },
          {
            type: "liquidFill",
            radius: "37%",
            z: 6,
            center: ["40%", "70%"],
            color: this.waterDataColor,
            data: [this.data[5], { value: this.data[5], direction: "left" }],
            backgroundStyle: { borderWidth: 1 },
            label: { fontSize: 18 },
            name: "6号仓库",
            outline: {
              show: true,
              itemStyle: { borderWidth: 1, borderColor: this.waterDataColor[1] },
              borderDistance: 0,
            },
          },
          {
            type: "liquidFill",
            radius: "37%",
            z: 6,
            center: ["60%", "70%"],
            color: this.waterDataColor,
            data: [this.data[6], { value: this.data[6], direction: "left" }],
            backgroundStyle: { borderWidth: 1 },
            label: { fontSize: 18 },
            name: "7号仓库",
            outline: {
              show: true,
              itemStyle: { borderWidth: 1, borderColor: this.waterDataColor[1] },
              borderDistance: 0,
            },
          },
          {
            type: "liquidFill",
            radius: "37%",
            z: 6,
            center: ["80%", "70%"],
            color: this.waterDataColor,
            data: [this.data[7], { value: this.data[7], direction: "right" }],
            backgroundStyle: { borderWidth: 1 },
            label: { fontSize: 18 },
            name: "8号仓库",
            outline: {
              show: true,
              itemStyle: { borderWidth: 1, borderColor: this.waterDataColor[1] },
              borderDistance: 0,
            },
          },
        ],
      });
    },
  },
};
</script>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
要在 Vue 中使用 Echarts 生成多个水球,你需要先安装 Echarts: ``` npm install echarts --save ``` 然后在 Vue 组件中引入 Echarts: ``` import echarts from 'echarts' ``` 接下来,你需要创建一个包含多个水球的容器,在 Vue 的 `mounted` 钩子函数中使用 Echarts 渲染水球: ``` <template> <div class="container"> <div class="chart" ref="chart1"></div> <div class="chart" ref="chart2"></div> <!-- 其他水球的容器 --> </div> </template> <script> import echarts from 'echarts' export default { mounted() { // 使用 Echarts 渲染水球 this.renderChart(this.$refs.chart1, 50) // 第一个水球,进度为 50% this.renderChart(this.$refs.chart2, 80) // 第二个水球,进度为 80% // 渲染其他水球 }, methods: { renderChart(container, percent) { const chart = echarts.init(container) chart.setOption({ series: [ { type: 'liquidFill', data: [percent / 100], color: ['#00a6ff', '#71c9ff', '#c7e1ff'], // 其他配置项 } ] }) } } } </script> ``` 在这个例子中,我们创建了多个水球的容器,并在 `mounted` 钩子函数中使用 `renderChart` 方法来渲染每个水球。在 `renderChart` 方法中,我们使用 Echarts 提供的 `liquidFill` 类型来绘制水球,通过传递不同的进度值来生成多个水球。 当然,你还可以根据自己的需求来调整代码,比如使用 Vue 组件来封装水球,或者将数据传递给组件来动态生成水球等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

李泽举

如对你有帮助,那我就没白写

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

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

打赏作者

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

抵扣说明:

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

余额充值