EChart.js 笔记二

交互组件

  Echart.js 中交互组件比较多。例如: legend(图例)、title(标题组件)、visualMap(视觉映射组件)、dataZoom(数据缩放组件)、timeline(时间线组件)。

数据缩放组件 - dataZoom

  支持 grid(直角坐标系)、 polar(极坐标系)。

  

  先支持单一横轴:

option = {
    xAxis: {
        type: 'value'
    },
    yAxis: {
        type: 'value'
    },
    dataZoom: [
        {   // 这个dataZoom组件,默认控制x轴。
            type: 'slider', // 这个 dataZoom 组件是 slider(滑动) 型 dataZoom 组件
            start: 10,      // 左边在 10% 的位置。
            end: 60         // 右边在 60% 的位置。
        }
    ],
    series: [
        {
            type: 'scatter', // 这是个『散点图』
            itemStyle: {
                opacity: 0.8
            },
            symbolSize: function (val) {  // 控制散点大小
                return val[2] * 40;
            },
            data: [["14.616","7.241","0.896"],["3.958","5.701","0.955"],["2.768","8.971","0.669"],["9.051","9.710","0.171"],["14.046","4.182","0.536"],["12.295","1.429","0.962"],["4.417","8.167","0.113"],["0.492","4.771","0.785"],["7.632","2.605","0.645"],["14.242","5.042","0.368"]]
        }
    ]
}

  这样就可以完成横向拖动观察数据的功能;

  优化:在表格中用滚轮完成缩放,只需要更新 dataZoom 中的设置即可:

option = {
    ...,
    dataZoom: [
        {   // 这个dataZoom组件,默认控制x轴。
            type: 'slider', // 这个 dataZoom 组件是 slider 型 dataZoom 组件
            start: 10,      // 左边在 10% 的位置。
            end: 60         // 右边在 60% 的位置。
        },
        {   // 这个dataZoom组件,也控制x轴。
            type: 'inside', // 这个 dataZoom 组件是 inside(内部鼠标滚轮) 型 dataZoom 组件
            start: 10,      // 左边在 10% 的位置。
            end: 60         // 右边在 60% 的位置。
        }
    ],
    ...
}

   如果想要添加对 Y 轴的支持,更新 dataZoom 中 yAxisIndex 即可:

option = {
    ...,
    dataZoom: [
        {
            type: 'slider',
            xAxisIndex: 0,
            start: 10,
            end: 60
        },
        {
            type: 'inside',
            xAxisIndex: 0,
            start: 10,
            end: 60
        },
        {
            type: 'slider',
            yAxisIndex: 0,
            start: 30,
            end: 80
        },
        {
            type: 'inside',
            yAxisIndex: 0,
            start: 30,
            end: 80
        }
    ],
    ...
}
原文地址:https://www.cnblogs.com/cc-freiheit/p/9110560.html