[echarts] 柱形图 设置滚动条

Echarts具有自适应的功能,因此当x轴的数据非常多的时候,会自动把柱形的宽度挤的非常细,这样的交互肯定是过不去的。

因此就需要有这样一个属性: dataZoom

想要读详细文档的同学,可以看官方文档:

只是想要简单版的同学,看我的这个简易版本即可: 这就是一个最简单的柱形图,带横向滚动条版本。

option = {
          
   
    color: [#3398DB],
    tooltip : {
          
   
        trigger: axis,
        axisPointer : {
          
               // 坐标轴指示器,坐标轴触发有效
            type : shadow        // 默认为直线,可选为:line | shadow
        }
    },
    grid: {
          
   
        left: 3%,
        right: 4%,
        bottom: 3%,
        containLabel: true
    },
    xAxis : [
        {
          
   
            type : category,
            data : [Mon, Tue, Wed, Thu, Fri, Sat, Sun],
            axisTick: {
          
   
                alignWithLabel: true
            }
        }
    ],
    yAxis : [
        {
          
   
            type : value
        }
    ],
    series : [
        {
          
   
            name:直接访问,
            type:bar,
            barWidth: 60%,
            data:[10, 52, 200, 334, 390, 330, 220]
        }
    ],
    dataZoom: {
          
   
        show: true, // 为true 滚动条出现
        realtime: true,  
        type:slider, // 有type这个属性,滚动条在最下面,也可以不行,写y:36,这表示距离顶端36px,一般就是在图上面。
        height: 20, // 表示滚动条的高度,也就是粗细
        start: 20, // 表示默认展示20%~80%这一段。
        end: 80
        }
};

echarts通过dataZoom来控制默认显示固定条数数据:5条

xData = this.allData.map((v) => v.value);

// 默认只显示5个柱子
startValue = this.allData[0] ? this.allData[0].value: (this.allData[0] ? this.allData[0].value: 0);

endValue = this.allData[4] ? this.allData[4].value: (this.allData[this.allData.length - 1] ? this.allData[this.allData.length - 1].value: 0);


xAxis : [
        {
          
   
            type : category,
            data : xData ,
            axisTick: {
          
   
                alignWithLabel: true
            }
        }
    ],

dataZoom: [
          {
          
   
            type: slider,
            show: true,
            handleSize: 20, // 滑动条的 左右2个滑动条的大小
            height: 14, // 组件高度
            bottom: 1, // 右边的距离
            handleColor: #ddd, // h滑动图标的颜色
            startValue: startValue,
            endValue: endValue, 
            handleStyle: {
          
    // 两侧缩放手柄的样式配置。
              borderColor: #cacaca,
              borderWidth: 1,
              shadowBlur: 2,
              background: #ddd,
              shadowColor: #ddd,
            },
            backgroundColor: white, // 两边未选中的滑动条区域的颜色
          },
          // 下面这个属性是拖到里面
          {
          
   
            type: inside,
            show: true,
            start: 1,
            end: 100,
          },
        ],


经验分享 程序员 微信小程序 职场和发展