vue3+ts项目中使用水球图

下载水球图

  1. npm install echarts

  2. npm install echarts-liquidfill

在对应组件中引入也可在main.ts中引入水球图

 import * as echarts from 'echarts'
  import 'echarts-liquidfill'

 使用echarts必须给echarts图表设置宽高

<template>
    <div class="water-wave">
      <div ref="liquidEchart" style="width: 200px; height: 200px"></div>
    </div>
  </template>

此处绑定ref 是为了给图表数据 宽高是为了显示水球图

效果图

底部发光图是切图定位显示

 代码如下

<template>
    <div class="water-wave">
      <div ref="liquidEchart" style="width: 200px; height: 200px">

    </div>

    </div>
  </template>

  <script lang="ts">
  import { defineComponent, onMounted, ref } from 'vue'
  import * as echarts from 'echarts'
  import 'echarts-liquidfill'

  export default defineComponent({
    name: 'WaterWave',
    setup() {
      // 组件逻辑
      const liquidEchart = ref<HTMLElement>()
      onMounted(() => {
        initLiquidEchart()
      })
      const initLiquidEchart = () => {
        let myChart = echarts.init(liquidEchart.value!)
        let value = 0.6
        // 把配置和数据放这里
        myChart.setOption({
          title: {
            // 标题
            text: '',
            textStyle: {
              // 标题的样式
              color: '#888', // 字体颜色
              fontFamily: 'Microsoft YaHei', // 字体
              fontSize: 24,
              fontWeight: '400',
              align: 'center', // 文字的水平方式
              baseline: 'middle',
              position: 'inside',
              verticalAlign: 'middle', // 文字的垂直方式
            },
            left: 'center', // 定位
            top: '20%',
          },
           series: [{
    type: 'liquidFill',
    radius: '80%', //水球大小
    center: ['50%', '50%'],
    waveAnimation: true,
    color: [
      {
        type: 'linear',
        x: 0,
        y: 0,
        x2: 0,
        y2: 1,
        colorStops: [
          {
            offset: 0,
            color: '#138FE2',
          },
          {
            offset: 1,
            color: '#126ABC',
          },
        ],
        globalCoord: false,
      },
    ],
    data: [0.3, 0.3], // data个数代表波浪数
    amplitude: 10, //振幅
    backgroundStyle: {
      borderWidth: 2, //边框大小
      borderColor:'rgba(17, 94, 176, 0.8)',//边框颜色
      color: 'rgba(17, 94, 176, 0.4)',
    },
    label: {
      normal: {
        textStyle: {
          fontSize: 24,
          fontWeight: 'bold',
          color: '#fff',
        },
      },
    },
    outline: {
      borderDistance: 0,
      itemStyle: {
        borderWidth: 4,
        borderColor: 'transparent',
      },
    },
  }],
        })
      }

      return { liquidEchart }
    },
  })
  </script>

  <style lang="scss" scoped  />

借鉴代码

效果图

<template>
  <div class="water-wave">
    <div ref="liquidEchart" style="width: 200px; height: 200px"></div>
  </div>
</template>

<script lang="ts">
import { defineComponent, onMounted, ref } from 'vue'
import * as echarts from 'echarts'
import 'echarts-liquidfill'

export default defineComponent({
  name: 'WaterWave',
  setup() {
    // 组件逻辑
    const liquidEchart = ref<HTMLElement>()
    onMounted(() => {
      initLiquidEchart()
    })
    const initLiquidEchart = () => {
      let myChart = echarts.init(liquidEchart.value!)
      let value = 0.6
      // 把配置和数据放这里
      myChart.setOption({
        title: {
          // 标题
          text: '',
          textStyle: {
            // 标题的样式
            color: '#888', // 字体颜色
            fontFamily: 'Microsoft YaHei', // 字体
            fontSize: 24,
            fontWeight: '400',
            align: 'center', // 文字的水平方式
            baseline: 'middle',
            position: 'inside',
            verticalAlign: 'middle', // 文字的垂直方式
          },
          left: 'center', // 定位
          top: '20%',
        },
        series: [
          {
            type: 'liquidFill',
            radius: '78%', // 水球大小
            waveAnimation: true,
            center: ['50%', '50%'],
            color: ['#FFEFD6', '#FEAF2E'],
            data: [value, value], // data个数代表波浪数
            amplitude: 10, //振幅
            // 图形样式
            itemStyle: {
              opacity: 1, // 波浪的透明度
              shadowBlur: 0, // 波浪的阴影范围
            },
            backgroundStyle: {
              borderWidth: 1, // 边框的宽度
              borderColor: '#FEAF2E', // 边框颜色
              color: '#fff',
            },
            label: {
              // 数据展示样式
              show: true,
              textStyle: {
                color: '#000',
                insideColor: '#fff',
                fontSize: 30,
                fontWeight: 600,
              },
              formatter: (params: any) => {
                return `${(params.value * 100).toFixed(1)}/200` // 文字显示
              },
            },
            outline: {
              show: false,
            },
          },
        ],
      })
    }

    return { liquidEchart }
  },
})
</script>

<style lang="scss" scoped src="./index.scss" />

vue3项目中封装为组件

新建组件liquidFill.vue 

<template></template>

<script lang="ts" setup>
// 1. 引入
import { nextTick, watch } from 'vue';
import echarts from '@/assets/ts/echarts';
import 'echarts-liquidfill';
const props = defineProps({
    // 父容器ID
    pid: {
        type: String,
        required: true,
    },
    title: {
        type: String,
        required: true,
    },
    value: {
        type: Number,
        required: true,
    },
    // 水球图背景颜色
    bgColor: {
        type: String,
        default: 'rgba(32, 165, 255, 0.3)',
    },
    // 波纹的颜色
    color: {
        type: String,
        default: '#20a5ff',
    },
    // 单位
    unit: {
        type: String,
        default: '',
    },
});

nextTick(() => {
    // 2. 容器
    const container = document.querySelector('#' + props.pid) as HTMLElement;
    if (container) initChart(container);
});
// 3. 初始化 echarts 实例, 将配置添加给 echarts 实例
let myChart: echarts.ECharts | null = null;
const initChart = (container?: HTMLElement) => {
    if (!myChart) myChart = echarts.init(container as HTMLElement);
    myChart.setOption(option);
};

watch(
    () => props.value,
    newVal => {
        option.series[0].data[0] = newVal;
        initChart();
    },
);
// 4.配置项
const option = {
    title: {
        text: props.title,
        x: '40%',
        y: '88%',
        textStyle: {
            fontSize: 14,
            fontWeight: '100',
            color: '#fff',
            lineHeight: 16,
            textAlign: 'center',
        },
    },

    series: [
        {
            type: 'liquidFill',
            radius: '70%',
            center: ['50%', '40%'],
            color: [
                {
                    type: 'linear',
                    x: 0,
                    y: 0,
                    x2: 0,
                    y2: 1,
                    colorStops: [
                        {
                            offset: 0,
                            // color: "#446bf5",
                            color: props.color,
                        },
                        {
                            offset: 1,
                            color: '#2ca3e2',
                        },
                    ],
                    globalCoord: false,
                },
            ],
            data: [props.value, 0.5], // data个数代表波浪数
            backgroundStyle: {
                borderWidth: 1,
                color: props.bgColor,
            },
            label: {
                // show:false,
                fontSize: 50,
                color: '#fff',
                formatter: '{c}' + props.unit,
            },
            outline: {
                show: false,
                borderDistance: 0,
                itemStyle: {
                    borderWidth: 2,
                    borderColor: '#112165',
                },
            },
        },
    ],
};
</script>

使用

  <div id="load-chart" v-if="curSiteInfo.loadPer || curSiteInfo.loadPer === 0">
                    <LiquidFill pid="load-chart" title="负荷率" :value="curSiteInfo.loadPer" unit="%"></LiquidFill>
                </div>
                <div id="power-factor" v-if="curSiteInfo.Pf || curSiteInfo.Pf === 0">
                    <LiquidFill pid="power-factor" title="功率因数" :value="curSiteInfo.Pf" color="rgba(37, 227, 255)"
                        bgColor="rgba(37, 227, 255,0.3)"></LiquidFill>

                </div>
 #load-chart,
        #power-factor {
            width: 50%;
            height: 83%;
            margin-top: 8%;
            background: url('@/assets/img/Profile/DomesticService/bottom.png') no-repeat 80% 93%;
            background-size: 95% 20%;
        }

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Vue 3 + TypeScript项目使用,您可以按照以下步骤进行操作: 1. 安装Vue CLI:如果您还没有安装Vue CLI,可以使用以下命令进行安装: ``` npm install -g @vue/cli ``` 2. 创建项目使用Vue CLI创建一个新的Vue 3项目,可以运行以下命令: ``` vue create my-project ``` 3. 选择配置:在创建项目的过程,您将被提示选择一些配置选项。请确保选择TypeScript作为您的项目的语言。 4. 安装依赖:项目创建完成后,进入项目目录并安装依赖项。在命令行运行以下命令: ``` cd my-project npm install ``` 5. 创建Vue组件:现在,您可以开始在Vue 3 + TypeScript项目创建组件。打开 src 目录,并创建一个新的 .vue 文件,例如 `HelloWorld.vue`。 ```vue <template> <div> <h1>{{ greeting }}</h1> </div> </template> <script lang="ts"> import { defineComponent } from 'vue'; export default defineComponent({ data() { return { greeting: 'Hello, World!' }; } }); </script> ``` 6. 使用组件:在您的应用程序使用该组件,在 `App.vue` 导入并注册 `HelloWorld.vue` 组件。 ```vue <template> <div id="app"> <HelloWorld /> </div> </template> <script lang="ts"> import { defineComponent } from 'vue'; import HelloWorld from './components/HelloWorld.vue'; export default defineComponent({ name: 'App', components: { HelloWorld } }); </script> ``` 现在,您可以运行您的Vue 3 + TypeScript项目使用该组件了。运行以下命令启动开发服务器: ``` npm run serve ``` 这只是一个简单的示例,您可以根据自己的需求在项目使用更多的Vue组件和功能。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值