echarts饼图labelLine线的长度自适应设置

  const option = {

  color: ['#94b6f3', '#5087ec', '#b9cff7'],

  legend: {

  show: false

  },

  grid: {

  top: '1px',

  right: '1px',

  bottom: '1px',

  left: '2px'

  },

  series: {

  type: 'pie',

  radius: ['45%', '60%'],

  center: ['50%', '50%'],

  itemStyle: {

  borderColor: '#fff',

  borderWidth: 1

  },

  label: {

  alignTo: 'edge',

  formatter: '{name|{b}}

  {time|{c}人}',

  minMargin: 5,

  edgeDistance: 10,

  lineHeight: 15,

  width: 58,

  padding: [0, 6, 0, 10], // 关键代码!关键代码!关键代码!

  rich: {

  name: {

  align: 'left'

  },

  time: {

  fontSize: 12,

  color: '#333',

  align: 'left'

  }

  }

  },

  labelLine: {

  length: 15,

  length2: 0,

  maxSurfaceAngle: 80

  },

  labelLayout: (params) => {

  const isLeft = params.labelRect.x < this.curEChartObj.getWidth() / 2

  const centerX = this.curEChartObj.getWidth() / 2

  let newX = 0

  if (isLeft) {

  newX = centerX - 50 - params.labelRect.width

  } else {

  newX = centerX + 50 + params.labelRect.width

  }

  const points = params.labelLinePoints

  points[2][0] = isLeft

  ? newX + 10

  : newX

  return {

  x: newX,

  labelLinePoints: points

  }

  },

  data: pieData

  }

  }