echarts中dataZoom的使用(与坐标轴重叠,与无论后台数据有多少,页面展示的数据量正合适)

<template>
  <div ref="chart" id="Chart" style="width:100%;height:400px;"></div>
</template>

<script>
export default {
  data() {
    return {}
  },
  props: ['title', 'classList', 'data','dataZoom'],
  methods: {
   chart() {
      // 基于准备好的dom,初始化echarts实例
      let myChart = this.$echarts.init(document.getElementById('Chart'))
      // 绘制图表
      myChart.setOption({
        color: ['#3398DB','red'],
        title: {
          x: 'left',
          text: this.title
        },
        legend: {
          left: 'right',
          top: 'top'
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'shadow'
          }
        },
        grid: {
          left: '3%',
          right: '4%',
          bottom: '12%',
          containLabel: true
        },
        xAxis: [
          {
            type: 'category',
            data: this.classList,
            axisTick: {
              alignWithLabel: true
            },
            axisLabel: {
              //设置x轴的字
              show: true,
              interval: 0, //使x轴横坐标全部显示
              rotate: 50, //倾斜度 -90 至 90 默认为0
              formatter: function(value, index) {
                if (value.length > 8) {
                  return value.substr(0, 5) + '...'
                } else {
                  return value
                }
              }
            }
          }
        ],
        yAxis: [
          {
            type: 'value',
            name: '人数',
            boundaryGap: [0, '100%']
          }
        ],
        series: [
          {
            name: this.data,
            type: 'line',
            barWidth: '60%',
            data: this.data
          },
        ],
        dataZoom:this.dataZoom?this.dataZoom:[]
       
      })
      window.onresize = function() {
        myChart.resize()
      }
    }
  },
  created() {},
  mounted() {
    this.chart()
  }
}
</script>
<style scoped>
</style>

1.以上代码是引入echarts,横坐标与dataZoom是从父组件传递过来的,dataZoom使用父组件传递是为了确保数据量少的图表不需要拉伸展示数据

const dataZoom = [
  {
    id: 'dataZoomX',
    type: 'slider',
    xAxisIndex: [0],
    filterMode: 'filter',
    start: 0,
    end: 50,
  }
]

  以上是dadaZoom的值,我们需要动态设置end的值,以确保不管数据有多少,打开后不会挤成一团,

 this.dataZoom[0].end=10/this.source.length*100

以上代码,显示当数据是10条数据的时候,展示正合适。

可根据调取数据,确定几条数据合适

2.dataZoom与坐标轴重合,

grid: {
          left: '3%',
          right: '4%',
          bottom: '12%',
          containLabel: true
        },

  在以上字段中。设置bottom的值,即可放置在妥善的位置。

posted @ 2019-12-30 18:06  冰晨之露  阅读(3074)  评论(0编辑  收藏  举报