Echarts开发移动端组件的踩坑总结(一)

柱状图篇

1、实现x轴时间均匀分布:

xAxis有 interval 属性,按照指定的间隔数字进行分布

export const xAxis = function (data, xAxisConfig) {
  	let interval = Math.ceil(data.length / 7)
  	// 自定义x轴的数字显示
	let axisLabel = {
     interval: function (index, value) {
       return (index) % 4 === 0
     }
   }
	// 指定间隔数字
	// let axisLabel = {
    // 	   interval: 3
    // }

  return {
    type: 'category',
    data,
    axisTick: false,
    axisLabel,
    axisLine: {
      show: true,
      lineStyle: {
        color: '',
      },
    },
  }
}

在这里插入图片描述

2、x轴超出的可左右滑动 (dataZoom)

思路是固定双指缩放,固定屏幕x轴展示的数据个数

// 左右滚动,禁止缩放
export const getDataZoom = function () {
  return [
    {
      id: 'dataZoomX',
      type: 'inside',
      rangeMode: ['value'],
      zoomLock: true,
      startValue: '0%',
      endValue: 5,
      minValueSpan: 5,
      maxValueSpan: 5,
    },
  ]
}

在这里插入图片描述

3、对比双轴图

(1)Y轴左右各一条,但是分割线需要对齐重合

export const yAxis = function (data, yConfig = [], type = 'single-column') {

  const lineArr = data.filter(item => item.type === 'line')
  return yConfig.map((item, index) => {
    const arr = index === 1 ? lineArr.length ? lineArr[0].data : [] :
     data.reduce((pre, next) => {
      if (next.type == 'bar') {
        return pre.concat(next.data)
      }
      return pre
    }, [])
    
    return {
      name: item.name,
      nameLocation: 'end',
      nameTextStyle: {
        color: '#8F939A',
        fontStyle: 'PingFangSC-Regular, PingFang SC',
        fontWeight: 400,
        fontSize: 12,
        lineHeight: 33,
        padding: index === 0 ? type === 'single-column' ? [0, 0, -10, -19] : 
        [0, 0, -10, -10] : [0, -17, -10, 0] // 上右下左与原位置距离
      },
      type: 'value',
      splitNumber: 4,
      interval: Math.ceil(Math.max.apply(null, arr) / 4),
      min: 0,
      max: Math.ceil(Math.max.apply(null, arr) / 4) * 4,
      offset: 0,
    }
  })
}

(2)自定义legend的样式,因为折线的样式不同

export const legend = function (data) {
  const color = ['#1042DF', '#03C1E2']
  let i = 0;
  return data.map((item) => {
    const { name, type = 'bar' } = item
    return {
      id: name,
      right: data.length > 2 ? `${65 - i * 25}%` : 
      data.length > 1 ? `${50 - i * 25}%` : 'center',
      bottom: 30,
      itemWidth: 7,
      icon: 'roundRect',
      itemHeight: type === 'line' ? 2 : 7,
      // 超出的文字用...
      formatter(name) {
        return name.length > 5 ? name.slice(0, 5) + '...' : name
      },
      data: [{
        name,
        textStyle: {
          color: '#666666',
        },
        itemStyle: {
          color: type === 'line' ? '#D9B800' : color[i++],
        },
      }]
    }
  })

}

在这里插入图片描述

(3)legend的点击事件可以取消或者显示对应的图形,现在需要对其进行拦截,当数据类型超过三个才可以隐藏
在这里插入图片描述

   const chartDom: any = document.getElementById(props.id)
   const myChart: any = echarts.init(chartDom, undefined, {
     width,
     height,
     renderer: 'svg'
   })
   echartsConfig && myChart.setOption(echartsConfig)
   // 禁止取消图例
    myChart.on('legendselectchanged', function (params: any) {
      let len = myChart.getOption().series.length
      if (len < 4) {
      
      // 这是legend是上面数组的情况下:
        const legend: any = myChart.getOption().legend;
        legend.forEach((item: any) => {
          item.selected[params.name] = true
        })
        myChart.setOption({
          legend
        })

        // legend 是对象 的情况下:
         myChart.setOption({
            legend: { selected: { [params.name]: true } }
          })
      }
  })
4、横向柱状图

(1)控制整个echart图的左右边距

export const grid = function () {
  return {
    left: 36,
    right: 60,
    top: 20,
    bottom: 0,
    containLabel: true
  }
}

(2)数据超出部分上下滚动

export const getDataZoom = function () {
    return [{
      "type": "inside",
      "showDetail": false,
      "show": false,
      zoomLock: true,
      "yAxisIndex": [0],
      "startValue": 0,
      "endValue": 7,
      "handleSize": 8,
    }]
 }

在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

那就可爱多一点点

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值