vue+echarts实现雷达图及刻度标注

发布于:2024-10-13 ⋅ 阅读:(14) ⋅ 点赞:(0)


前言

最近项目有做数据可视化 大屏 不免再次使用些echarts应用 记录下其中echarts雷达图的实现


代码实现

先上代码

<template>
  <div class="container">
    <div ref="chart" style="width: 500px; height: 500px; margin-top: 20px"></div>
  </div>
</template>

<script>
export default {
  name: 'testDemo',
  data() {
    return {};
  },
  methods: {
    initChart() {
      this.chart = this.$echarts.init(this.$refs.chart);
      const option = {
        color: ['#FFC481', '#8EC6F8', '#56D8CD'],
        title: {
          x: 'center',
          y: 'center',
          textStyle: {
            fontSize: 12,
            rich: {
              a: {
                color: '#0066FF',
                fontSize: 18,
                align: 'center',
              },
              b: {
                color: '#00BBFF',
                fontSize: 12,
                height: 16,
                align: 'centerc',
              },
              c: {
                color: 'orange',
                fontSize: 12,
                height: 16,
                align: 'center',
              },
            },
          },
        },
        // 配置图例
        legend: {
        },
        radar: {
          indicator: [
            {name: '资本背景', max: 100, min: 0, index: 0, axisLabel: {show: true}}, //显示刻度
            {name: '知识产权', max: 100, min: 0, index: 1},
            {name: '成长性', max: 100, min: 0, index: 2},
            {name: '风险状况', max: 100, min: 0, index: 3},
            {name: '经营质量', max: 100, min: 0, index: 4},
            {name: '企业规模', max: 100, min: 0, index: 5},
          ],
          radius: 80, //大小
          startAngle: 120, // 雷达图的旋转偏移量
          splitNumber: 5, // 分层5层,当前最大1000,可理解为每层200
          triggerEvent: true,
          name: {
            formatter: (value, indicator) => {
              // 获取对应的数值
              const valueAtIndex = this.radarData[indicator.index];
              return `{a|${value}}: {b|${valueAtIndex}}`; // 显示名称和对应的数值
            },
            rich: {
              a: {
                color: '#333',
                fontSize: 12,
              },
              b: {
                color: '#333',
                fontSize: 12,
              },
            },
          },
        },
        textStyle: {
          color: '#333333',
        },
        series: [
          {
            type: 'radar',
            areaStyle: {
              normal: {
                //添加阴影效果的配置部分
                color: new this.$echarts.graphic.LinearGradient(0, 0, 0, 1, [
                  {offset: 0, color: 'rgba(255, 196, 129, 0.8)'},
                  {offset: 1, color: 'rgba(255, 196, 129, 0.3)'},
                ]),
                opacity: 0.8,
              },
            },
            data: [
              {
                value: [34,23,34,87,68,67],
              },
            ],
          },
        ],
      };
      this.chart.setOption(option);
    }
  },
  mounted() {
    // 等dom渲染后再初始化图表
    this.initChart()
  }
};
</script>

<style scoped>
</style>

实现效果

在这里插入图片描述


总结

initChart方法中,使用this.$echarts.init来初始化一个ECharts实例,并设置图表的配置项option。这些配置项定义了图表的标题、提示框、雷达图的指标、名称的富文本样式、全局文本样式以及系列列表等。最后,通过this.chart.setOption(option)将配置项应用到图表实例上