echarts中使用graphic在画布上写文字

本文详细介绍了如何在Echarts折线图中使用graphic配置实现每个区间显示不同文案,包括了文本类型的配置、位置和样式设置。

一个项目需求:折线图中每个区间展示一个文案
在这里插入图片描述
使用echarts中的graphic配置选项

graphic: [
  {
    type:'text',
    right: '15%',
    top: '10%',
    z: 999,
    style:{
      text: '极度拥挤',
      fill: '#E72938',
      fontWeight: 400,
      fontSize: 10
    }
  },
  {
    type:'text',
    right: '15%',
    top: '25%',
    z: 999,
    style:{
      text: '拥挤',
      fill: '#FF891A',
      fontWeight: 400,
      fontSize: 10
    }
  },
  {
    type:'text',
    right: '15%',
    top: '41%',
    z: 999,
    style:{
      text: '正常',
      fill: '#523CF2',
      fontWeight: 400,
      fontSize: 10
    }
  },
  {
    type:'text',
    right: '15%',
    top: '55%',
    z: 999,
    style:{
      text: '畅通',
      fill: '#50A5FE',
      fontWeight: 400,
      fontSize: 10
    }
  },
  {
    type:'text',
    right: '15%',
    top: '71%',
    z: 999,
    style:{
      text: '极度畅通',
      fill: '#1CD6F3',
      fontWeight: 400,
      fontSize: 10
    }
  }
],

完整的options选项

{
  graphic: [
    {
      type:'text',
      right: '15%',
      top: '10%',
      z: 999,
      style:{
        text: '极度拥挤',
        fill: '#E72938',
        fontWeight: 400,
        fontSize: 10
      }
    },
    {
      type:'text',
      right: '15%',
      top: '25%',
      z: 999,
      style:{
        text: '拥挤',
        fill: '#FF891A',
        fontWeight: 400,
        fontSize: 10
      }
    },
    {
      type:'text',
      right: '15%',
      top: '41%',
      z: 999,
      style:{
        text: '正常',
        fill: '#523CF2',
        fontWeight: 400,
        fontSize: 10
      }
    },
    {
      type:'text',
      right: '15%',
      top: '55%',
      z: 999,
      style:{
        text: '畅通',
        fill: '#50A5FE',
        fontWeight: 400,
        fontSize: 10
      }
    },
    {
      type:'text',
      right: '15%',
      top: '71%',
      z: 999,
      style:{
        text: '极度畅通',
        fill: '#1CD6F3',
        fontWeight: 400,
        fontSize: 10
      }
    }
  ],
  grid: {
    x: 0,
    y: 10,
    x2: 0,
    y2: 30,
    containLabel: true
  },
  xAxis: {
    boundaryGap: false,
    type: 'category',
    data: dateList,
    axisTick: {
      // 是否显示x轴刻度
      show: false
    },
    axisLine: {
      // 是否显示x轴线
      show: false
    },
    axisLabel: {
      color: '#999999',
      interval: 0,
      formatter: (value, index) => {
        const length = dateList.length
        if (index === 0) {
          return '               ' + dayjs(value).format('YYYY-MM')
        }
        if (index === length - 1) {
          return dayjs(value).format('YYYY-MM') + '               '
        }
        if (length > 3 && parseInt(length / 2) === index) {
          return dayjs(value).format('YYYY-MM')
        }
        return ''
      }
    }
  },
  yAxis: [
    {
      type: 'value',
      min: 100,
      max: 0,
      splitNumber: 10, //设置坐标轴的分割段数
      interval: (100 - 0) / 5, // 标轴分割间隔
      axisLabel: {
        color: '#999999'
      }
    },
    {
      type: 'value',
      min: minData2,
      max: maxData2,
      splitNumber: 10,
      interval: (maxData2 - minData2) / 5,
      axisLabel: {
        color: '#999999',
        formatter: function (value) {
          return value.toFixed(0)
        }
      }
    }
  ],
  series: [
    {
      name: '走势线1',
      type: 'line',
      color: ['#E72938'],
      symbol: 'none',
      smooth: true,
      yAxisIndex: 0, //在单个图表实例中存在多个y轴的时候有用
      data: crowdList,
      lineStyle: {
        width: 1
      },
      markArea: { //标记区域
        data: [
          [{
            yAxis: '100',//y轴坐标控制
            itemStyle: {
              color: {
                type: 'linear',
                x: 0,
                y: 0,
                x2: 1,
                y2: 0,
                colorStops: [{
                    offset: 0, color: '#FEFAF9' // 0% 处的颜色
                }, {
                    offset: 1, color: '#FEF5F3' // 100% 处的颜色
                }],
                global: false // 缺省为 false
              }
            },
            emphasis: {
              disabled: true
            }
          }, {
            yAxis: '80'
          }],
          [{
            yAxis: '80',//y轴坐标控制
            itemStyle: {
              color: {
                type: 'linear',
                x: 0,
                y: 0,
                x2: 1,
                y2: 0,
                colorStops: [{
                    offset: 0, color: '#FFFEFB' // 0% 处的颜色
                }, {
                    offset: 1, color: '#FEFAF3' // 100% 处的颜色
                }],
                global: false // 缺省为 false
              }
            },
            emphasis: {
              disabled: true
            }
          }, {
            yAxis: '60'
          }],
          [{
            yAxis: '60',//y轴坐标控制
            itemStyle: {
              color: {
                type: 'linear',
                x: 0,
                y: 0,
                x2: 1,
                y2: 0,
                colorStops: [{
                    offset: 0, color: '#FAFAFF' // 0% 处的颜色
                }, {
                    offset: 1, color: '#F1F0FF' // 100% 处的颜色
                }],
                global: false // 缺省为 false
              }
            },
            emphasis: {
              disabled: true
            }
          }, {
            yAxis: '40'
          }],
          [{
            yAxis: '40',//y轴坐标控制
            itemStyle: {
              color: {
                type: 'linear',
                x: 0,
                y: 0,
                x2: 1,
                y2: 0,
                colorStops: [{
                    offset: 0, color: '#F4FAFF' // 0% 处的颜色
                }, {
                    offset: 1, color: '#E5F3FF' // 100% 处的颜色
                }],
                global: false // 缺省为 false
              }
            },
            emphasis: {
              disabled: true
            }
          }, {
            yAxis: '20'
          }],
          [{
            yAxis: '20',
            itemStyle: {
              color: {
                type: 'linear',
                x: 0,
                y: 0,
                x2: 1,
                y2: 0,
                colorStops: [{
                    offset: 0, color: '#F6FDFF' // 0% 处的颜色
                }, {
                    offset: 1, color: '#EDFAFE' // 100% 处的颜色
                }],
                global: false // 缺省为 false
              }
            },
            emphasis: {
              disabled: true
            },
          }, {
            yAxis: '0'
          }]
        ]
      },
    },
    {
      name: '走势线2',
      type: 'line',
      color: ['#2C7AF4'],
      symbol: 'none',
      smooth: true,
      yAxisIndex: 1,
      data: valueList,
      lineStyle: {
        width: 1
      }
    }
  ]
}
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值