vue3之echarts仪表盘

vue3之echarts仪表盘

效果如下:
在这里插入图片描述
版本
"echarts": "^5.5.1"

核心代码:

<template>
  <div ref="chartRef" class="circle"></div>
</template>
<script lang="ts" setup>
import { ref, onMounted, onBeforeMount } from 'vue';
import * as echarts from 'echarts';

const chartRef = ref(null);
const percent = ref(23);
let chart: any = null;

const topChartOptions = (value: number) => {
  return {
    series: [
      {
        name: 'S',
        z: 3,
        type: 'gauge', // 仪表盘
        center: ['50%', '82%'],
        radius: '135%',
        detail: {
          show: false,
        },
        title: {
          show: false,
        },
        data: [
          {
            value,
            name: 'Percent',
          },
        ],
        startAngle: '178', // 仪表盘起始角度
        endAngle: '2', // 仪表盘结束角度
        min: 0,
        max: 100,
        axisLine: {
          show: true,
          lineStyle: {
            roundCap: true,
            width: 8,
            color: [
              [
                value / 100,
                new echarts.graphic.LinearGradient(0, 0, 1, 0, [
                  {
                    offset: 0,
                    color: 'rgba(11, 163, 250, 1)',
                  },
                  {
                    offset: 1,
                    color: 'rgba(227, 250, 255, 1)', // 100% 处的颜色
                  },
                ]),
              ],
              [1, 'rgba(4, 211, 251, 0)'],
            ],
          },
        },
        axisTick: {
          show: false,
        },
        axisLabel: {
          show: false,
        },
        splitLine: {
          show: false,
        },
        pointer: {
          length: '22%',
          width: 4,
          icon: 'rect',
          offsetCenter: [0, '-87%'],
          itemStyle: {
            color: 'RGBA(191, 255, 238, 1)',
          },
        },
      },
      {
        name: 'T',
        z: 2,
        type: 'gauge', // 仪表盘
        center: ['50%', '82%'],
        radius: '135%',
        detail: {
          show: false,
        },
        title: {
          show: false,
        },
        data: [
          {
            value: 100,
            name: 'Percent',
          },
        ],
        startAngle: '178', // 仪表盘起始角度
        endAngle: '2', // 仪表盘结束角度
        min: 0,
        max: 100,
        axisLine: {
          show: true,
          lineStyle: {
            roundCap: true,
            width: 10,
            color: [
              [
                1,
                new echarts.graphic.LinearGradient(0, 0, 1, 0, [
                  {
                    offset: 0,
                    color: 'transparent',
                  },
                  {
                    offset: value / 100,
                    color: 'rgba(255, 192, 1, 1)',
                  },
                  {
                    offset: 1,
                    color: 'rgba(255, 242, 204, 1)', // 100% 处的颜色
                  },
                ]),
              ],
              [1, 'rgba(255, 192, 1, 0)'],
            ],
          },
        },
        axisTick: {
          show: false,
        },
        axisLabel: {
          show: false,
        },
        splitLine: {
          show: false,
        },
        pointer: {
          show: false,
        },
      },
    ],
  };
};

const initChart = () => {
  if (!chart) chart = echarts.init(chartRef.value);
  chart.setOption(topChartOptions(percent.value));
};

onMounted(() => {
  initChart();
});

const destroyChart = () => {
  if (!chart) {
    return;
  }
  chart.dispose();
  chart = null;
};

onBeforeMount(() => {
  destroyChart();
});
</script>
<style lang="scss" scoped>
.circle {
  width: 314px;
  height: 191px;
}
</style>

### 如何在 Vue2 中使用 ECharts 创建仪表盘 #### 安装依赖库 为了能够在 Vue2 项目中集成并使用 ECharts,需要先安装 `echarts` 和 `vue-echarts` 库。 ```bash npm install echarts vue-echarts --save ``` #### 注册全局组件 为了让整个应用都可以访问到 ECharts 组件,在项目的入口文件(通常是 main.js 或者类似的初始化脚本)里注册: ```javascript import { createApp } from &#39;vue&#39;; import App from &#39;./App.vue&#39;; // 导入 ECharts 主模块以及对应的 Vue 组件 import * as echarts from &#39;echarts/core&#39;; import VChart, { THEME_KEY } from "vue-echarts"; import { CanvasRenderer } from &#39;echarts/renderers&#39;; const app = createApp(App); app.component(&#39;v-chart&#39;, VChart).use(CanvasRenderer); // 可选配置主题颜色方案 app.provide(THEME_KEY, &#39;light&#39;); // or dark app.mount(&#39;#app&#39;); ``` #### 编写仪表板视图逻辑 接下来定义具体的仪表盘展示页面。这里假设有一个名为 Dashboard 的单文件组件 (`.vue`) 文件来承载实际的内容。 ```html <template> <div class="dashboard"> <!-- 使用 v-chart 自定义标签渲染图表 --> <div style="width: 600px;height:400px;"> <v-chart :option="chartOption"/> </div> </div> </template> <script> export default { data() { return { chartOption: { title: { text: &#39;简单示例&#39;, subtext: &#39;&#39;, top: &#39;top&#39; }, tooltip: {}, xAxis: [{ type: &#39;category&#39;, data: ["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"] }], yAxis: [{ type: &#39;value&#39; }], series: [{ name: &#39;销量&#39;, type: &#39;bar&#39;, data: [5, 20, 36, 10, 10, 20] }] } }; } }; </script> <style scoped> .dashboard { display: flex; justify-content: center; align-items: center; height: 100vh; background-color: white; } </style> ``` 上述代码展示了如何通过设置 `chartOption` 属性来自定义图表样式[^1]。对于更复杂的场景比如轮船仪表盘效果,则可能还需要进一步调整选项参数以适应特定需求[^2];而对于像餐厅运营这样的多维度数据分析型仪表盘来说,除了基本的数据可视化外,还应该考虑加入更多交互性和动态更新机制以便更好地支持业务决策过程[^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值