<!--
 * @Author: chris
 * @Date: 2025-10-22 11:42:40
 * @LastEditors: chris
 * @LastEditTime: 2025-10-22 14:59:49
-->
<script setup name="line-chart">
import LEchart from '@/uni_modules/lime-echart_1.0.5/components/l-echart/l-echart.vue'

const props = defineProps({
  echarts: {
    type: Object,
    required: true,
  },
  chartData: {
    type: Object,
    default: () => ({
      times: [],
      values: [],
      title: '',
      unit: '',
      color: '#4dabf7',
    }),
  },
})

const lineChartRef = ref(null)
let lineChart = null

onMounted(async () => {
  nextTick(async () => {
    await initChart()
  })
})

onUnmounted(() => {
  if (lineChart)
    lineChart.dispose()
})

async function initChart() {
  if (!lineChartRef.value || !props.echarts)
    return
  lineChart = await lineChartRef.value.init(props.echarts)
  refreshChart()
}

function refreshChart() {
  if (!lineChart) {
    nextTick(async () => {
      await initChart()
    })
    return
  }
  lineChart.showLoading()
  lineChart.setOption(createOptions())
  lineChart.hideLoading()
}

function createOptions() {
  const gradient = new props.echarts.graphic.LinearGradient(0, 0, 0, 1, [
    { offset: 0, color: `${props.chartData.color}80` }, // 80是透明度
    { offset: 1, color: `${props.chartData.color}20` }, // 20是透明度
  ])

  const option = {
    title: {
      text: props.chartData.title,
      top: '2%',
      textStyle: {
        color: '#333',
        fontSize: 16,
        fontWeight: 'bold',
      },
      left: 'center',
    },
    tooltip: {
      trigger: 'axis',
      backgroundColor: 'rgba(0, 0, 0, 0.7)',
      borderColor: '#333',
      textStyle: {
        color: '#fff',
      },
      formatter: (params) => {
        const data = params[0]
        return `${data.axisValue}： ${data.value} ${props.chartData.unit}`
      },
      axisPointer: {
        type: 'cross',
        label: {
          backgroundColor: '#6a7985',
        },
      },
    },
    grid: {
      left: '3%',
      right: '3%',
      bottom: '15%', // 增加底部空间以适应旋转的标签
      top: '20%',
      containLabel: true,
    },
    xAxis: {
      type: 'category',
      boundaryGap: false,
      data: props.chartData.times,
      axisLine: {
        lineStyle: {
          color: '#464646',
        },
      },
      axisLabel: {
        color: '#666',
        rotate: 45,
        fontSize: 10,
      },
    },
    yAxis: [
      {
        type: 'value',
        name: props.chartData.unit,
        nameTextStyle: {
          color: '#666',
        },
        axisLine: {
          lineStyle: {
            color: '#464646',
          },
        },
        axisLabel: {
          color: '#666',
        },
        splitLine: {
          lineStyle: {
            color: '#f0f0f0',
            type: 'dashed',
          },
        },
      },
    ],
    series: [
      {
        name: props.chartData.title,
        type: 'line',
        data: props.chartData.values,
        smooth: true,
        symbol: 'circle',
        symbolSize: 6,
        itemStyle: {
          color: props.chartData.color,
        },
        lineStyle: {
          width: 2,
        },
        areaStyle: {
          color: gradient,
        },
        emphasis: {
          focus: 'series',
        },
      },
    ],
  }

  return option
}

// 监听数据变化，更新图表
watch(() => props.chartData, () => {
  refreshChart()
}, { deep: true })
</script>

<template>
  <view class="line-chart">
    <l-echart id="soilLineChart" ref="lineChartRef" type="2d" />
  </view>
</template>

<style lang="scss" scoped>
.line-chart {
  width: 100%;
  height: 300px;
}
</style>
