quanyawei
2024-03-28 2c6d91d758241e1f97fe2790d92b754ea2cc860c
src/components/Echarts/LineChartSpe.vue
@@ -1,17 +1,20 @@
<template>
  <!-- <div> -->
  <!-- <div>1111111111111111111111111111111111111111</div> -->
  <div :class="className" :style="{ height: height, width: width }" />
  <div
    :class="className"
    :style="{ height: height, width: width }"
  />
  <!-- </div> -->
</template>
<script>
import echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import resize from './mixins/resize'
import * as echarts from 'echarts' // echarts theme
// import resize from './mixins/resize'
require('echarts/theme/macarons')
export default {
  mixins: [resize],
  // mixins: [resize],
  props: {
    className: {
      type: String,
@@ -38,25 +41,25 @@
    //   required: true
    // }
  },
  data() {
  data () {
    return {
      chart: null,
    }
  },
  watch: {
    chartData: {
      handler(val) {
      handler (val) {
        this.setOptions(val)
      },
      deep: true,
      deep: true
    },
  },
  mounted() {
  mounted () {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
  beforeDestroy () {
    if (!this.chart) {
      return
    }
@@ -64,12 +67,21 @@
    this.chart = null
  },
  methods: {
    initChart() {
    initChart () {
      this.chart = echarts.init(this.$el, 'macarons')
      this.setOptions(this.chartData)
      this.chart.clear()
      // this.setOptions(this.chartData)
    },
    setOptions(val) {
      function fontSize(res) {
    setOptions (val) {
      console.log('val,val', val)
      // val.series.forEach(element => {
      //   element.data = element.data.map(item => item === 0 ? 0.1 : item)
      // })
      if (this.chart) {
        this.chart.clear()
      }
      let that = this
      function fontSize (res) {
        let clientWidth =
          window.innerWidth ||
          document.documentElement.clientWidth ||
@@ -78,107 +90,85 @@
        let fontSize = 100 * (clientWidth / 1920)
        return res * fontSize
      }
      /**
   * 增加监听,mouseover事件(鼠标放在上面)
   * 节点事件触发 是默认开启的,我们再开启了线事件触发后,
   * 目前鼠标放在线上或者放在节点上都能触发下面的监听,
   * 事件触发后,修改series内对应系列的参数配置。
   * 注:params内包含系列名称等信息
   */
      this.chart.on('mouseover', function (params) {
        that.chart.setOption({
          series: [
            {
              name: params.seriesName,
              label: {
                show: true,
                fontSize: 16,
                position: [10, -15],
                formatter: '{c}'
              }
            }
          ]
        })
      })
      // 增加监听,mouseout事件(鼠标离开)
      this.chart.on('mouseout', function (params) {
        that.chart.setOption({
          series: [
            {
              name: params.seriesName,
              lineStyle: {
                width: 2
              },
              label: {
                show: false
              },
              endLabel: {
                show: false
              }
            }
          ]
        })
      })
      this.chart.setOption(
        {
          xAxis: {
            data: val.xAxis,
            boundaryGap: true,
            boundaryGap: false,
            axisTick: {
              show: true,
            },
          },
          grid: {
            left: 100,
            right: 10,
            bottom: 20,
            top: 30,
            height: fontSize(4),
            width: fontSize(15),
            left: '5%',
            right: '5%',
            top: '5%',
            containLabel: true,
            height: fontSize(4.3),
          },
          // toolbox: {
          //   feature: {
          //     dataZoom: {
          //       yAxisIndex: 'none',
          //     },
          //     restore: {},
          //     saveAsImage: {},
          //   },
          // },
          // tooltip: {
          //   trigger: 'axis',
          //   position: function(pt) {
          //     return [pt[0], '10%']
          //   }
          // },
          tooltip: {
            trigger: 'axis',
            position: function (pt) {
              return [pt[0], '10%']
            },
            // formatter(params) {
            //   console.log(params,'ssss');
            //   let data=[]
            //   for (let i = 0; i < params.length; i++) {
            //     data+=params[i].seriesName + '&nbsp:' +'&nbsp&nbsp'+params[i].value+ '<br/>'
            //   }
            // return data
            // },
            backgroundColor: 'rgba(50,50,50,0.5)',
            textStyle: {
              color: 'rgb(255, 255, 255);',
              fontSize: fontSize(0.117), // 字体大小
              lineHeight: 0
            }
          },
          yAxis: [
            {
              type: 'value',
              position: 'left',
              axisLabel: {
                show: true,
              },
            },
            {
              type: 'value',
              position: 'right',
              axisLabel: {
                show: true,
              },
            },
            {
              type: 'value',
              position: 'right',
              offset: 50,
              axisLabel: {
                show: true,
              },
              min: 'dataMin', //取最小值为最小刻度
              max: 'dataMax', //取最大值为最大刻度
              min: function (value) {
                //取最小值向下取整为最小刻度
                return Math.floor(value.min)
              },
              max: function (value) {
                //取最大值向上取整为最大刻度
                return Math.ceil(value.max)
              },
              scale: true, //自适应
              minInterval: 0.1, //分割刻度
            },
            {
              type: 'value',
              position: 'left',
              axisLabel: {
                show: true,
              },
              offset: 50,
            },
          ],
          yAxis: val.yAxis,
          legend: {
            icon: 'circle',
            itemHeight: 15,
            // textStyle: {
            //   fontSize: 18
            // },
            data: val.title,
            tooltip: {
              show: true,
            },
            textStyle: {
              fontSize: fontSize(0.15),
            },
            widht: 'auto',
            height: 'auto',
            top: fontSize(5.5),
          },
          dataZoom: [
            {
@@ -193,34 +183,11 @@
              height: fontSize(0.4),
            },
          ],
          series: val.series,
          // [{
          //   name: JSON.parse(JSON.stringify(val.title)),
          //   itemStyle: {
          //     normal: {
          //       color: '#7AC5CD',
          //       lineStyle: {
          //         color: '#7AC5CD',
          //         width: 2
          //       }
          //     }
          //   },
          //   smooth: true,
          //   type: 'line',
          //   data: val.series,
          //   animationDuration: 2800,
          //   animationEasing: 'cubicInOut',
          //   areaStyle: {},
          //   label: {
          //     normal: {
          //       show: true,
          //       position: 'top'
          //     }
          //   }
          // }]
          series: val.series
        },
        true
      )
      // window.onresize = this.chart.resize
    },
  },
}