Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

ECharts Event Handling

ECharts Event Handling

In ECharts, we can listen to the user's operations to call the corresponding function.

ECharts uses the on method to listen to user behavior, such as monitoring the user's click behavior.

Events in ECharts are divided into two types:

User mouse operation clicks, such as 'click', 'dblclick', 'mousedown', 'mousemove', 'mouseup', 'mouseover', 'mouseout', 'globalout', 'contextmenu' events.

The other is a behavior event triggered after the user uses an interactive component, such as the 'legendselectchanged' event triggered when toggling a legend, the 'datazoom' event triggered when zooming a data area, and so on.



myChart.on('click', function (params) {

    // Name of the data printed to the console after the user clicks

    console.log(params);

});



myChart.on('legendselectchanged', function (params) {

    console.log(params);

});



chart.on('click', 'series.line', function (params) {

    console.log(params);

});



chart.on('mouseover', {seriesIndex: 1, name: 'xx'}, function (params) {

    console.log(params);

});


Mouse Events

The mouse event types supported by ECharts include 'click', 'dblclick', 'mousedown', 'mousemove', 'mouseup', 'mouseover', 'mouseout', 'globalout', and 'contextmenu' events.

The following example pops up a dialog when clicking a bar chart:

Example

// Initialize the ECharts instance based on the prepared DOM

var

myChart

=

echarts.

init

(

document.

getElementById

(

'main'

)

)

;

// Specify the chart's configuration and data

var

option

=

{

    xAxis

:

{

        data

:

[

"Shirt"

,

"Sweater"

,

"Chiffon Shirt"

,

"Pants"

,

"High Heels"

,

"Socks"

]

   

}

,

    yAxis

:

{

}

,

    series

:

[

{

        name

:

'Sales'

,

        type

:

'bar'

,

        data

:

[

5

,

20

,

36

,

10

,

10

,

20

]

   

}

]

}

;

// Display the chart using the just-specified configuration and data.

myChart.

setOption

(

option

)

;

// Handle the click event and pop up the data name

myChart.

on

(

'click'

,

function

(

params

)

{

    alert

(

params.

name

)

;

}

)

;

Try it »

All mouse events contain the parameter params, which is an object containing the data information of the clicked graphic; its format is as follows:

{

    // Name of the component belonging to the currently clicked graphic element,

    // Its value can be 'series', 'markLine', 'markPoint', 'timeLine', etc.

    componentType: string,

    // Series type. Possible values: 'line', 'bar', 'pie', etc. Meaningful when componentType is 'series'.

    seriesType: string,

    // The index of the series in the passed option.series. Only meaningful when componentType is 'series'.

    seriesIndex: number,

    // Series name. Meaningful when componentType is 'series'.

    seriesName: string,

    // Data name, category name

    name: string,

    // Index of the data in the passed data array

    dataIndex: number,

    // The original data item passed in

    data: Object,

    // Sankey, graph, and other charts contain two types of data, nodeData and edgeData,

    // The value of dataType will be 'node' or 'edge', indicating whether the current click is on a node or an edge.

    // In most other charts, there is only one data, so dataType is meaningless.

    dataType: string,

    // Input data value

    value: number|Array

    // Color for data graphics. Only meaningful when componentType is 'series'.

    color: string

}

How to distinguish where the mouse clicked:

myChart.on('click', function (params) {

    if (params.componentType === 'markPoint') {

        // Clicked on markPoint

        if (params.seriesIndex === 5) {

            // Clicked on the markPoint of the series with index 5.

        }

    }

    else if (params.componentType === 'series') {

        if (params.seriesType === 'graph') {

            if (params.dataType === 'edge') {

                // Clicked on the graph's edge.

            }

            else {

                // Clicked on the graph's node.

            }

        }

    }

});

Use query to trigger the callback only for the graphic elements of the specified component:

chart.on(eventName, query, handler);

query can be a string or an object.

If it is a string, it represents the component type. The format can be 'mainType' or 'mainType.subType'. For example:



chart.on('click', 'series', function () {...});

chart.on('click', 'series.line', function () {...});

chart.on('click', 'dataZoom', function () {...});

chart.on('click', 'xAxis.category', function () {...});

If it is an object, it can contain one or more of the following properties, each of which is optional:

{

    Index: number // 组件 index

    Name: string // 组件 name

    Id: string // 组件 id

    dataIndex: number // 数据项 index

    name: string // 数据项 name

    dataType: string // 数据项 type,如关系图中的 'node', 'edge'

    element: string // 自定义系列中的 el 的 name

}

For example:

chart.setOption({

    // ...

    series: [{

        name: 'uuu'

        // ...

    }]

});

chart.on('mouseover', {seriesName: 'uuu'}, function () {

    // This method is called when a graphic element in the series named 'uuu' is 'mouseover'ed.

});

For example:



chart.setOption({

    // ...

    series: [{

        // ...

    }, {

        // ...

        data: [

            {name: 'xx', value: 121},

            {name: 'yy', value: 33}

        ]

    }]

});

chart.on('mouseover', {seriesIndex: 1, name: 'xx'}, function () {

    // This method is called when an element with name 'xx' in series index 1 is 'mouseover'ed.

});

For example:



chart.setOption({

    // ...

    series: [{

        type: 'graph',

        nodes: [{name: 'a', value: 10}, {name: 'b', value: 20}],

        edges: [{source: 0, target: 1}]

    }]

});

chart.on('click', {dataType: 'node'}, function () {

    // This method is called when a node in the relationship graph is clicked.

});

chart.on('click', {dataType: 'edge'}, function () {

    // This method is called when an edge in the relationship graph is clicked.

});

For example:

chart.setOption({

    // ...

    series: {

        // ...

        type: 'custom',

        renderItem: function (params, api) {

            return {

                type: 'group',

                children: [{

                    type: 'circle',

                    name: 'my_el',

                    // ...

                }, {

                    // ...

                }]

            }

        },

        data: [[12, 33]]

    }

})

chart.on('mouseup', {element: 'my_el'}, function () {

    // This method is called when the element named 'my_el' is 'mouseuped'.

});

In the callback function, you can obtain the data name and series name from this object, then index into your own data store to get other information, update the chart, show a tooltip, etc., as in the example code below:

Example

myChart.

on

(

'click'

,

function

(

parmas

)

{

    $.

get

(

'detail?q='

params.

name

,

function

(

detail

)

{

        myChart.

setOption

(

{

            series

:

[

{

                name

:

'pie'

,

               

// Show the data distribution within a single bar via a pie chart

                data

:

[

detail.

data

]

           

}

]

       

}

)

;

   

}

)

;

}

)

;


Behavior Events of Component Interaction

In ECharts, basically all component interaction behaviors trigger corresponding events; the commonly used events and their parameters are listed in the events documentation.

The following is an example of listening to a legend toggle:

Example

// The legend toggle behavior only triggers the legendselectchanged event

myChart.

on

(

'legendselectchanged'

,

function

(

params

)

{

   

// Get the selected state of the clicked legend

   

var

isSelected

=

params.

selected

[

params.

name

]

;

   

// Print in the console

    console.

log

(

(

isSelected

?

'Selected'

:

'Deselected'

)

'Legend'

params.

name

)

;

   

// Print the states of all legends

    console.

log

(

params.

selected

)

;

}

)

;

Try it »


Trigger Component Behavior in ECharts via Code

Above we only described the user's interactive operations, but sometimes we also need to call methods in the program to trigger chart behavior, such as displaying a tooltip.

ECharts triggers chart behavior through dispatchAction({ type: '' }), uniformly managing all actions, and you can also record the user's behavior path as needed.

The above example is used to rotate the tooltip in a pie chart:

Example

setInterval

(

function

(

)

{

   

var

dataLen

=

option.

series

[

0

]

.

data

.

length

;

   

// Cancel the previously highlighted graphic

    myChart.

dispatchAction

(

{

        type

:

'downplay'

,

        seriesIndex

:

0

,

        dataIndex

:

app.

currentIndex

   

}

)

;

    app.

currentIndex

=

(

app.

currentIndex

1

)

%

dataLen

;

   

// Highlight the current graphic

    myChart.

dispatchAction

(

{

        type

:

'highlight'

,

        seriesIndex

:

0

,

        dataIndex

:

app.

currentIndex

   

}

)

;

   

// Show tooltip

    myChart.

dispatchAction

(

{

        type

:

'showTip'

,

        seriesIndex

:

0

,

        dataIndex

:

app.

currentIndex

   

}

)

;

}

,

1000

)

;

Try it »