『Echarts』🌏 Vue + Echarts 可视化

1,223 阅读4分钟
   只是笔记,更多细节建议参考官方文档。

引入

  • 文档地址:https://github.com/ecomfe/vue-echarts/blob/5.x/README.zh_CN.md
  • npm install echarts vue-echarts

柱状图 📊 bar

image.png

option = {
    grid: { // 定位
        top: '30%',
        left: '15%',
        right: '15%',
    },
    title: { // 标题
        text: 'Main Title',
        subtext: 'Sub Title',
        left: '10%',
        top: '10%',
        textStyle: { // 文本样式
            fontSize: 30,
            color: 'rgba(68, 189, 225, 1)',
            fontWeight: 'bolder',
            fontFamily: 'sans-serif',
            fontStyle: 'normal',
        },
        subtextStyle: { // 副标题文本样式
            fontSize: 20,
            color: 'rgba(150, 249, 243, 1)',
            fontStyle: 'italic',
        },
        show: true,
    },
    tooltip: { // 提示框组件
        trigger: 'axis',
        axisPointer: {
            type: 'shadow',
        },
    },
    legend: [ // 图例排序
        {
            top: '10%',
            right: '10%',
            data: ['补贴金'],
        },
        {
            top: '14%',
            right: '10%',
            itemWidth: 16,
            data: ['100岁以上', '90~99岁', '80~89岁'],
            orient: 'horizontal', // 方向默认值:horizontal(vertical)
            padding: [1, 1, 1, 1],
            align: 'left',
            lineStyle: {
            },
            textStyle: {
                color: 'rgba(68, 189, 225, 1)',
            },
        },
    ],
    xAxis: { // x 轴属性
        type: 'category',
        name:'', // 名称
        data: ['和平社区', '三瓦窑社区', '永安社区', '益州社区', '交子公园社区', '月牙湖社区'],
        axisLabel: { // 坐标轴刻度标签的相关设置
            rotate: 45,
        },
        axisLine: {
            show: true,
            lineStyle: {
                color: '##E9EAEB',
            },
        },
    },
    yAxis: [
        { // 第一个y轴
            type: 'value',
            name: '人',
            axisLine: {
                show: true,
                lineStyle: {
                    color: '#E9EAEB',
                },
            },
            axisTick: {
                show: true,
            },
            splitLine: { // 是否显示坐标轴在 grid 区域中的分隔线
                show: true, 
                lineStyle: {
                    type: 'dashed', // 线的类型'solid','dotted','dashed'
                },
            },
            axisLabel: { // 坐标轴刻度标签的相关设置。
                color: '#828282',
                fontSize: 12,
            },
        },
        { // 第二个y轴
            type: 'value',
            name: '万',
            axisLine: {
                show: true,
                lineStyle: {
                    color: '#E9EAEB',
                },
            },
            axisTick: {
                show: true,
            },
            splitLine: {
                show: false,
            },
            axisLabel: {
                color: '#828282',
                fontSize: 12,
            },
        },
    ],
    color:["#485AE9","#EE5B56","#F4AC56"],
    series: [
        {
            name: '补贴金',
            type: 'line',
            yAxisIndex: 1,
            color: '#EF8052',
            data: [1210, 1523, 1923, 1123, 1244, 5145],
            label: { // 图形上的文本标签,可用于说明图形的一些数据信息,比如值,名称等
                show: true,
                rich: {}, // 在 rich 里面,可以自定义富文本样式
            },
        },
        {
            name: '100岁以上',
            type: 'bar',
            stack: '年龄', // 数据堆叠,同个类目轴上系列配置相同的stack值可以堆叠放置。
            barWidth: '50%',
            data: [3210, 3132, 1301, 3314, 1910, 3310],
        },
        {
            name: '90~99岁',
            type: 'bar',
            stack: '年龄',
            barWidth: '50%',
            data: [1120, 1132, 101, 1314, 910, 230],
        },
        {
            name: '80~89岁',
            type: 'bar',
            stack: '年龄',
            barWidth: '50%',
            data: [2210, 1821, 191, 234, 2190, 330],
        },
    ],
};

折线图 📈

image.png

xAxis: {
          type: 'category',
          data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'],
          axisLabel: { // 坐标轴刻度标签的相关设置
            rotate: 45,
            color: '#fff',
          },
          axisLine: { // 坐标轴轴线样式
            show: true,
            lineStyle: {
              color: '#507058',
            },
          },
          axisTick: {
            show: false,
          },
        },
        yAxis: {
          type: 'value',
          axisLine: {
            show: false,
          },

          axisLabel: {
            show: false,
          },
          splitLine: {
            show: true,
            lineStyle: {
              color: '#507058',
              type: 'dashed',
            },
          },
        },
        series: [
          {
            symbol: 'emptyCircle',
            showSymbol: true,
            symbolSize: 5,
            markPoint: { // 最大值 + 样式
              data: [
                {
                  symbol: 'emptyCircle',
                  symbolSize: 8,
                  type: 'max',
                  name: '最大值',
                  label: {
                    show: true,
                    color: '#FBA8DA',
                    fontWeight: 'bolder',
                    position: 'top',
                    distance: 10,
                  },
                  itemStyle: {
                    color: '#FBA8DA',
                    borderColor: '#FBA8DA',
                    borderType: 'solid',
                    borderWidth: 5,
                  },
                },
                {
                  type: 'min',
                  name: '最小值',
                  symbol: 'emptyCircle',
                  symbolSize: 8,
                  label: {
                    show: true,
                    color: '#FAE494',
                    fontWeight: 'bolder',
                    position: 'top',
                    distance: 10,
                  },
                  itemStyle: {
                    color: '#FAE494',
                    borderColor: '#FAE494',
                    borderType: 'solid',
                    borderWidth: 5,
                  },
                },
              ],
              label: { textStyle: { color: '#fff' } },
              itemStyle: { color: 'transparent' },
            },
            color: '#A8FBC0',
            data: [150, 230, 224, 218, 135, 147, 260],
            type: 'line',
          },
        ],

饼图 🫓 pie

image.png

  • 选中模式的配置:selectedMode
  • 饼图图形上的文本标签:label
  • 标签的边距:label.edgeDistance
  • 标签的对齐方式:label.alignTo
  • 标签的视觉引导线配置:labelLine
  • 标签的统一布局配置:labelLayout
  • 图形样式:itemStyle
  • 高亮状态的扇区和标签样式:emphasis
  • 动画效果:animation
options: {
         tooltip: {
           trigger: 'item',
         },
         color: ['#A8FBC0', '#FAE494', '#C4C4C4'],
         series: [
           {
             label: { // 饼图图形上的文本标签,可用于说明图形的一些数据信息,比如值,名称等
               show: true,
               position: 'outside',
               formatter: '{b}:{d}%',
             },
             name: '访问来源',
             type: 'pie',
             radius: '50%',
             data: [
               { value: 580, name: '公牛' },
               { value: 484, name: '母牛' },
               { value: 800, name: '牛犊' },
             ],
             labelLine: {
               length2: 40,
             },
             emphasis: {
               itemStyle: {
                 shadowBlur: 10,
                 shadowOffsetX: 0,
                 shadowColor: 'rgba(0, 0, 0, 0.5)',
               },
             },
           },
         ],
       },
// 抵达边界
labelLayout: (params) => {
     const isLeft = params.labelRect.x < myChart.getWidth() / 2
     const points = params.labelLinePoints
     points[2][0] = isLeft
     ?params.labelRect.x
     :params.labelRect.x + params.labelRect.width
     
     return {
       labelLinePoints: points
     }
   }

半圆环

image.png

    title: {
        // 标题
        text: 'Main Title',
        subtext: 'Sub Title',
        left: 'center',
        top: '44%',
        textStyle: {
            // 文本样式
            fontSize: 30,
            color: 'rgba(68, 189, 225, 1)',
            fontWeight: 'bolder',
            fontFamily: 'sans-serif',
            fontStyle: 'normal',
        },
        subtextStyle: {
            // 副标题文本样式
            fontSize: 20,
            color: 'rgba(150, 249, 243, 1)',
            fontStyle: 'italic',
        },
        show: true,
    },
    polar: {},
    angleAxis: {
        axisLine: {
            show: false,
        },
        axisLabel: {
            show: false,
        },
        splitLine: {
            show: false,
        },
        axisTick: {
            show: false,
        },
        min: 0,
        max: 200,
        boundaryGap: ['0', '100'],
        startAngle: 180,
    },
    radiusAxis: {
        type: 'category',
        axisLine: {
            show: false,
        },
        axisTick: {
            show: false,
        },
        axisLabel: {
            show: false,
        },
    },
    series: [

        {
            type: 'bar',
            stack: '总数',
            name: '城管服务',
            data: [null, null, null, 50],
            coordinateSystem: 'polar',
            value: 819,
            color: '#F45B80',
        },
        {
            type: 'bar',
            stack: '总数',
            name: '工会服务',
            data: [null, null, null, 10],
            coordinateSystem: 'polar',
            value: 482,
            color: '#FFA8A7',
        },
        {
            type: 'bar',
            stack: '总数',
            name: '食品经营许可',
            data: [null, null, null, 10],
            coordinateSystem: 'polar',
            value: 365,
            color: '#1B3478',
        },
        {
            type: 'bar',
            stack: '总数',
            name: '就业服务',
            data: [null, null, null, 10],
            coordinateSystem: 'polar',
            value: 250,
            color: '#3677FF',
        },
        {
            type: 'bar',
            stack: '总数',
            name: 'EMS邮寄',
            data: [null, null, null, 10],
            coordinateSystem: 'polar',
            value: 25,
            color: '#FEB446',
        },
        {
            type: 'bar',
            stack: '总数',
            name: '团组织服务',
            data: [null, null, null, 10],
            coordinateSystem: 'polar',
            value: 16,
            color: '#64C0DE',
        },
    ],
    tooltip: {
        show: true,
        trigger: 'item',
        borderColor: '#fff',
    },
    grid: {
        left: 0,
        top: 0,
        bottom: 0,
        right: 0,
    },