echarts趋势折线图

在这里插入图片描述

<template>
  <div ref="chart" class="trendChart"></div>
</template>

<script>
import { getTrend, getAllTrend } from '@/request/publicSentiment'

export default {
  name: 'trendChart',
  data () {
    return {
      trenddata: []
    }
  },
  props: {
	// 父传给子的值,作为入参  
    areaid: String,
    comunityid: String,
    dateid: Number,
    id: null
  },
  watch: {
  	// 监听入参,当入参改变时,重新调接口渲染图表
    areaid (newvalue) {
      this.initchart ()
    },
    dateid (value) {
      this.initchart ()
    }
  },
  mounted () {
    this.initchart()
  },
  methods: {
    async initchart () {
      const chart = this.$echarts.init(this.$refs.chart)
      chart.clear()
      // 获取数据
      let res
      if(this.areaid === '0') {
        res = await getAllTrend(this.dateid)
      }else {
        res = await getTrend(this.comunityid,this.areaid,this.dateid)
      }
      if (res.code === 200) {
        const data = res.data
        this.trenddata = data
      }
      // 折线趋势图的配置项
      const option = {
      	// 提示框组件
        tooltip: {
          trigger: 'axis', // 触发类型,axis是坐标轴触发,主要在折线图、柱状图等会使用类目轴的图表中使用
          position: function (pt) { // 提示框的位置设置
            return [pt[1], '10%']
          }
        },
        // 图表的位置
        grid: {
          left: '13%',
          right: '10%',
          top: '10%',
          bottom: '30%'
        },
        // x轴的相关配置
        xAxis: {
          type: 'category',
          boundaryGap: false, // 坐标轴两侧是否留白
          // x轴坐标刻度值
          axisLabel: {
            fontSize: 9,
            color: 'rgba(0, 0, 0, 0.45)'
          },
          // x轴坐标轴线
          axisLine: {
            lineStyle: {
              color: '#f0f0f0'
            }
          },
          // x轴坐标刻度线
          axisTick: {
            show: false
          },
          data: this.trenddata[0]
        },
        // y轴的相关配置
        yAxis: {
          type: 'value',  // 数值轴
          boundaryGap: [0, '100%'],  // 设置min max后无效
          axisLabel: {
            fontSize: 9,
            color: 'rgba(0, 0, 0, 0.45)'
          },
          splitNumber: 4, // 坐标轴的分割段数+min就可以设置间隔
          min: 0,
          // max: 80
        },
        // 缩放控件
        dataZoom: [
          {
            type: 'inside', // 内置控件,就是可以滑动的那部分
            start: 0, // 数据窗口的起始百分比,即开始显示位置
            end: 100 // 数据窗口的结束百分比,即结束显示位置
          },
          // 缩放控件的背景板那块
          {
            start: 0,
            end: 100,
            height: 14,
            borderRadius: 10,
            backgroundColor: '#F7F8FD', // 缩放控件的背景色
            // 数据阴影的样式
            dataBackground: {
              // 阴影线条的样式 
              lineStyle: {
                color: '#F7F8FD'
              },
              // 阴影的填充样式
              areaStyle: {
                color: 'transparent'
              }
            },            
            fillerColor: '#F7F8FD', // 选中范围的填充颜色
            handleIcon: 'circle', // 手柄的样式,也可以设置为自定义图片
            handleSize: 15 // 手柄的大小
          }
        ],
        series:
          [
            {
              name: '今日上报',
              type: 'line',
              symbol: 'none',
              sampling: 'lttb',
              smooth: true,
              itemStyle: {
                color: 'rgba(255, 71, 71, .5)'
              },
              areaStyle: {
                color: new this.$echarts.graphic.LinearGradient(0, 0, 0, 1, [
                  {
                    offset: 0,
                    color: 'rgba(255, 71, 71, .5)'
                  },
                  {
                    offset: 1,
                    color: 'rgba(255, 221, 221, 1)'
                  }
                ])
              },
              label: {
                show:true,
              },
              data: this.trenddata[1]
            }
          ]
      }
      chart.setOption(option)
      setTimeout(function (){
        window.onresize = function () {
            chart.resize();
        }
      },200);
    }
  }
}
</script>

<style scoped>
.trendChart {
  width: 100%;
  height: 100%;
}
</style>

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值