Newer
Older
CloudBrainNew / src / components / echart / EchartLine.vue
StephanieGitHub on 4 Feb 2021 6 KB first commit
<template>
  <div
    :id="id"
    :style="`width:${curWidth}px;height:${curHeight}px`"
  >
  </div>
</template>
<script>
/**
 * @author chenyanqing
 * @date 2019/10/19
 * @Description bar(产业经济-营商环境)
 * @props 数据格式:{
 *    id: 'traffic_govern_line_id',
      width: 0,
      height: 0,
      legend:['近一周日均车位占用率','近一周日均车位停车次数'], // 可不传
      xAxisData:['周一','周二','周三','周四','周五','周六','周日'],
      yAxisData: [
        {name:'近一周日均车位占用率',data:[120, 252, 101, 134, 290, 230, 110],color:'58,55,194'},
        {name:'近一周日均车位停车次数',data:[120, 222, 191, 234, 250, 200, 310],color:'109,202,23'},
      ]
    }
 *
 */

import { countSize } from '@/utils/utils'
export default {
  props: ['id', 'width', 'height', 'unit', 'xAxisData', 'yAxisData', 'legend'],
  data () {
    return {
      curWidth: this.width,
      curHeight: this.height,
      option: {
        grid: {
          left: 10,
          right: 30,
          bottom: 5,
          top: 20,
          containLabel: true
        },
        legend: {
          orient: 'horizontal',
          // data: [],
          align: 'left',
          right: '5%',
          itemWidth: 8,
          itemHeight: 8,
          icon: 'rect',
          textStyle: {
            width: '300',
            color: '#fff',
            fontSize: 10
          }
        },
        tooltip: {
          trigger: 'axis',
          textStyle: {
            fontSize: '13'
          },
          axisPointer: {
            type: 'cross',
            label: {
              fontSize: '13'
            }
          }
        },
        xAxis: [
          {
            type: 'category',
            boundaryGap: false,
            data: [
              // "2009年",
            ],
            axisLine: {
              lineStyle: {
                color: '#101d5d' // 轴线的颜色
              }
            },
            axisLabel: {
              color: '#fff', // X轴名称颜色
              fontSize: 8
            }
          }
        ],
        yAxis: [
          {
            name: '单位(家)',
            // stack: '总量', //stack相同数据会相加
            type: 'value',
            color: '#fff',
            nameTextStyle: {
              color: '#fff',
              fontSize: 9,
              verticalAlign: 'middle'
            },
            splitLine: {
              lineStyle: {
                color: ['#101641'],
                type: 'dashed'
              }
            },
            axisLine: {
              lineStyle: {
                color: '#101d5d'
              }
            },
            axisLabel: {
              color: '#fff',
              fontSize: 8
            }
          }
        ],

        series: [
          // {
          //   name: "总数量",
          //   type: "line",
          //   symbol: "none", //去掉折线节点
          //   smooth: true, //曲线变平滑
          //   itemStyle: {
          //     color: "#0192fd"
          //   },
          //   lineStyle: {
          //     width: countSize(0.03)
          //   },
          //   areaStyle: {
          //     color: new this.$echarts.graphic.LinearGradient(
          //       0,0,0,1, //变化度 //三种由深及浅的颜色
          //       [
          //         {
          //           offset: 0.1,
          //           color: "rgba(8,21,99,1)"
          //         },
          //         {
          //           offset: 1,
          //           color: "rgba(0,0,0,.1)"
          //         }
          //       ]
          //     )
          //   },
          //   data: []
          // }
        ]
      }
    }
  },
  watch: {
    width (newVal, oldVal) {
      this.curWidth = newVal
      this.refreshEchart()
    },
    height (newVal, oldVal) {
      this.curHeight = newVal
      this.refreshEchart()
    },
    xAxisData (newVal, oldVal) {
      this.option.xAxis[0].data = newVal
      this.refreshEchart()
    },
    legend (newVal, oldVal) {
      this.option.legend.data = newVal
      this.refreshEchart()
    },
    unit (newVal, oldVal) {
      this.option.yAxis[0].name = newVal
      this.refreshEchart()
    },
    yAxisData: {
      handler (newVal, oldValue) {
        let newSeries = []
        for (let item of newVal) {
          let series = {
            name: item.name,
            type: 'line',
            symbol: 'none', // 去掉折线节点
            smooth: true, // 曲线变平滑
            itemStyle: {
              color: `rgb(${item.color})`
            },
            lineStyle: {
              width: countSize(0.03)
            },
            areaStyle: {
              color: new this.$echarts.graphic.LinearGradient(
                0, 0, 0, 1, // 变化度 //三种由深及浅的颜色
                [
                  {
                    offset: 0.1,
                    color: `rgba(${item.color},.6)`
                  },
                  {
                    offset: 1,
                    color: 'rgba(0,0,0,.0)'
                  }
                ]
              )
            },
            data: item.data
          }
          newSeries.push(series)
        }
        this.$set(this.option, 'series', newSeries)
        this.refreshEchart()
      },
      deep: true
    }
  },
  mounted () {
    this.unit && (this.option.yAxis[0].name = this.unit)
    if (this.xAxisData.length) {
      this.option.legend.data = this.legend
      this.option.xAxis[0].data = this.xAxisData
      let newSeries = []
      for (let item of this.yAxisData) {
        let series = {
          name: item.name,
          type: 'line',
          symbol: 'none', // 去掉折线节点
          smooth: true, // 曲线变平滑
          itemStyle: {
            color: `rgb(${item.color})`
          },
          lineStyle: {
            width: countSize(0.03)
          },
          areaStyle: {
            color: new this.$echarts.graphic.LinearGradient(
              0, 0, 0, 1, // 变化度 //三种由深及浅的颜色
              [
                {
                  offset: 0.1,
                  color: `rgba(${item.color},.6)`
                },
                {
                  offset: 1,
                  color: 'rgba(0,0,0,.0)'
                }
              ]
            )
          },
          data: item.data
        }
        newSeries.push(series)
      }
      this.$set(this.option, 'series', newSeries)
      this.initEchart()
    }
  },
  methods: {
    refreshEchart () {
      if (this.curWidth && this.curHeight && this.xAxisData.length) {
        this.initEchart()
      }
    },
    initEchart () {
      const _div = document.getElementById(this.id)
      setTimeout(() => {
        let myChart = this.$echarts.init(_div)
        myChart.setOption(this.option, true)
        window.onresize = myChart.resize
      }, 500)
    }
  }
}
</script>

<style scoped>
</style>