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
      }
    }
  ]
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ECharts 的 `graphic` 可以用来添加图形元素,包括文本、图片、矩形、圆形、线段等等。它的使用方法如下: 1. 在 `option` 添加 `graphic` 属性,值为一个数组,数组每个元素代表一个图形元素。 2. 每个图形元素都需要设置一个唯一的 `id` 属性,以便在后面的操作引用。 3. 根据需要设置图形元素的 `type` 属性,比如设置为 `text` 表示文本,设置为 `image` 表示图片,设置为 `rect` 表示矩形,设置为 `circle` 表示圆形,设置为 `line` 表示线段等等。 4. 根据需要设置图形元素的 `style` 属性,比如设置文本内容、字体大小、颜色、位置等等。 5. 根据需要设置图形元素的 `shape` 属性,比如设置矩形的宽度、高度、圆角半径等等。 6. 根据需要设置图形元素的 `position` 属性,表示图形元素的位置,可以设置为绝对位置或相对位置。 7. 根据需要设置图形元素的 `z` 属性,用来控制图形元素的层级关系,值越大表示越靠前。 下面是一个使用 `graphic` 添加文本和图片的示例: ``` option = { graphic: [ { id: 'text1', type: 'text', style: { text: 'Hello ECharts', font: 'bold 16px Arial', fill: '#333', x: 50, y: 50 } }, { id: 'image1', type: 'image', style: { image: 'https://www.echartsjs.com/zh/asset/img/logo.png', width: 100, height: 100, x: 100, y: 100 } } ], series: [ { data: [1, 2, 3, 4, 5], type: 'bar' } ] }; ``` 在上面的示例,我们添加了一个文本和一个图片。文本的 `id` 设置为 `text1`,类型为 `text`,内容为 `Hello ECharts`,字体为 `bold 16px Arial`,颜色为 `#333`,位置为 `(50, 50)`。图片的 `id` 设置为 `image1`,类型为 `image`,图片地址为 `https://www.echartsjs.com/zh/asset/img/logo.png`,宽度为 `100`,高度为 `100`,位置为 `(100, 100)`。在 `series` 我们添加了一个柱状图的数据系列。 除了文本和图片,`graphic` 还支持添加其他类型的图形元素,具体可以参考 ECharts 官网的文档。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值