import { MUTICHARTCOLORS } from "@/common/constant.js";

const getPieChart = (data) => {
  let option = {
    series: [
      {
        type: 'pie',
        radius: [80, 110],
        height: '100%',
        left: 'center',
        width: '100%',
        itemStyle: {
          borderColor: '#fff',
          borderWidth: 1
        },
        label: {
          alignTo: 'edge',
          // formatter: '{name|{b}}\n{time|{c} 个 {d}%}',
          formatter: function (params) {
            console.log(params, 'params')
            // params.c 是当前数据项的 value 值
            const time = params.data.value / 1000000; // 将值除以 1000000
            return `{name|${params.data.name}}\n{time|${time.toFixed(2)}}亿美元`; // 使用 toFixed(2) 保留两位小数
          },
          minMargin: 15,
          edgeDistance: 10,
          lineHeight: 15,
          rich: {
            name: {
              fontSize: 16,
              color: 'rgba(59, 65, 75, 1)',
              fontWeight: 700,
              lineHeight: 20
            },
            time: {
              fontSize: 14,
              color: 'rgba(59, 65, 75, 1)',
              lineHeight: 20
            }
          }
        },
        labelLine: {
          length: 15,
          length2: 0,
          maxSurfaceAngle: 80
        },
        labelLayout: function (params) {
          const isLeft = params.labelRect.x < 556 / 2;
          const points = params.labelLinePoints;
          // Update the end point.
          points[2][0] = isLeft
            ? params.labelRect.x
            : params.labelRect.x + params.labelRect.width;
          return {
            labelLinePoints: points
          };
        },
        data: (Array.isArray(data) ? data : []).map((d, index) => ({
          ...d,
          itemStyle: { ...(d?.itemStyle || {}), color: d?.color || MUTICHARTCOLORS[index % MUTICHARTCOLORS.length] }
        }))
      }]
  }
  return option
}

export default getPieChart;