日常记录: 创建自定义样式的 echarts 仪表盘

Echarts 仪表盘

需要自定义样式和指针,实现了渐变的表盘和数据展示,代码记录一下~
注: 指针这里不知道怎么画,所以把图片转成base64直接使用了,大佬们如果有啥更好的方法欢迎指导~

import * as echarts from 'echarts';

var chartDom = document.getElementById('main');
var myChart = echarts.init(chartDom, 'dark');
var option;

const baseIcon = 'image://data:image/png;base64,...'; // 太长了 这里省略了

option = {
  tooltip: {
    formatter: '{a} <br/>{b} : {c}%'
  },
  series: [
    {
      name: 'Pressure',
      type: 'gauge',
      detail: {
        formatter: '{value}%',
        color: 'rgba(	238,130,238, 1)',
        fontWeight: 700,
        fontSize: 100,
        fontFamily: 'Arial',
        offsetCenter: ['0', '-24%']
      },
      data: [
        {
          value: 50,
          name: 'SCORE'
        }
      ],
      endAngle: 0,
      colorBy: 'data',
      center: ['50%', '95%'],
      startAngle: 180,
      min: 0,
      splitNumber: 10,
      axisLine: {
        show: true,
        roundCap: true,
        lineStyle: {
          color: [
            [
              1,
              new echarts.graphic.LinearGradient(0, 0, 1, 0, [
                {
                  offset: 0,
                  color: 'rgba(123,104,238,0.85)'
                },
                {
                  offset: 0.5,
                  color: 'rgba(221,160,221,0.85)'
                },
                {
                  offset: 1,
                  color: 'rgba(255,182,193,0.85)'
                }
              ])
            ]
          ],
          width: 16
        }
      },
      splitLine: {
        length: '6%',
        distance: 7,
        lineStyle: {
          color: '#ffffff',
          width: 1
        }
      },
      // 刻度样式
      axisTick: {
        show: true,
        splitNumber: 5,
        lineStyle: {
          color: '#ffffff'
        }
      },
      // 刻度标签
      axisLabel: {
        distance: 25,
        formatter: function (value) {
          if (value % 20 === 0) return value + '%';
        },
        color: '#ffffff',
        fontFamily: 'Arial',
        fontSize: 20
      },
      // 指针
      pointer: {
        icon: baseIcon,
        length: 25,
        width: 16,
        offsetCenter: [0, '-94%']
      },
      title: {
        offsetCenter: ['0', '0%'],
        color: 'rgba(255, 255, 255, 1)',
        fontWeight: 400
      }
    }
  ]
};

option && myChart.setOption(option);


效果展示

最终效果如图
在这里插入图片描述
echarts 文档很详细 基本就照着文档属性一个一个配的没啥大问题~

参考文档: https://echarts.apache.org/examples/zh/index.html#chart-type-gauge

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值