search
HomeWeb Front-endJS TutorialHow to implement chart linkage in ECharts

How to implement chart linkage in ECharts

How to implement chart linkage in ECharts requires specific code examples

When we need to display multiple related data, it is necessary to display the data in the form of a chart An intuitive and effective way. In practical applications, we often encounter situations where multiple charts of different types need to be displayed in conjunction. As a powerful data visualization library, ECharts provides chart linkage functions, which can help us quickly realize this requirement.

The method of realizing chart linkage in ECharts is through event triggering and data interaction. By monitoring the events of a certain chart, when the event is triggered, the corresponding data can be obtained and corresponding operations can be performed, thereby achieving the linkage effect of the chart. The following will explain how to implement chart linkage in ECharts through specific code examples.

First, we need to prepare two different types of charts, a bar chart and a line chart. For convenience, we use the sample data officially provided by ECharts for demonstration. The following is the HTML code for the bar chart and line chart:

<div id="bar" style="width: 600px;height:400px;"></div>
<div id="line" style="width: 600px;height:400px;"></div>

Then, introduce the ECharts library in JavaScript and write the corresponding code to create the chart and monitor the chart events. The following is the complete JavaScript code:

// 图表数据
var barData = [
    {name: '周一', value: 120},
    {name: '周二', value: 200},
    {name: '周三', value: 150},
    {name: '周四', value: 80},
    {name: '周五', value: 70},
    {name: '周六', value: 110},
    {name: '周日', value: 130}
];

var lineData = [
    {name: '周一', value: 190},
    {name: '周二', value: 230},
    {name: '周三', value: 170},
    {name: '周四', value: 120},
    {name: '周五', value: 90},
    {name: '周六', value: 150},
    {name: '周日', value: 160}
];

// 创建柱状图
var barChart = echarts.init(document.getElementById('bar'));
var barOption = {
    xAxis: {
        type: 'category',
        data: barData.map(item => item.name)
    },
    yAxis: {
        type: 'value'
    },
    series: [{
        type: 'bar',
        data: barData.map(item => item.value)
    }]
};
barChart.setOption(barOption);

// 创建折线图
var lineChart = echarts.init(document.getElementById('line'));
var lineOption = {
    xAxis: {
        type: 'category',
        data: lineData.map(item => item.name)
    },
    yAxis: {
        type: 'value'
    },
    series: [{
        type: 'line',
        data: lineData.map(item => item.value)
    }]
};
lineChart.setOption(lineOption);

// 监听柱状图点击事件
barChart.on('click', function(params) {
    // 获取点击的数据
    var data = barData[params.dataIndex];
    
    // 根据点击的数据更新折线图数据
    lineOption.series[0].data = [data.value, data.value, data.value, data.value, data.value, data.value, data.value];
    lineChart.setOption(lineOption);
});

In the above code, instances of the bar chart and line chart are first created and their initial data is set. Then, set the data to the chart by calling the setOption method. Then, by listening to the click event of the bar chart, obtain the click data in the event callback function, then update the line chart data based on the click data, and set the updated data to the line chart through the setOption method . In this way, the linkage effect of the bar chart and the line chart is achieved.

It should be noted that the above is just a simple example, and actual applications may involve more complex data interaction and chart linkage requirements. But the overall implementation idea and operation method are the same: by listening to the events of the chart, obtain the data and perform corresponding operations.

Through the above sample code, we can see that it is not complicated to implement chart linkage in ECharts. With the rich functions and flexible operations provided by ECharts, we can easily achieve interactive effects between multiple charts, providing more possibilities for data analysis and display.

The above is the detailed content of How to implement chart linkage in ECharts. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Python vs. JavaScript: The Learning Curve and Ease of UsePython vs. JavaScript: The Learning Curve and Ease of UseApr 16, 2025 am 12:12 AM

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

Python vs. JavaScript: Community, Libraries, and ResourcesPython vs. JavaScript: Community, Libraries, and ResourcesApr 15, 2025 am 12:16 AM

Python and JavaScript have their own advantages and disadvantages in terms of community, libraries and resources. 1) The Python community is friendly and suitable for beginners, but the front-end development resources are not as rich as JavaScript. 2) Python is powerful in data science and machine learning libraries, while JavaScript is better in front-end development libraries and frameworks. 3) Both have rich learning resources, but Python is suitable for starting with official documents, while JavaScript is better with MDNWebDocs. The choice should be based on project needs and personal interests.

From C/C   to JavaScript: How It All WorksFrom C/C to JavaScript: How It All WorksApr 14, 2025 am 12:05 AM

The shift from C/C to JavaScript requires adapting to dynamic typing, garbage collection and asynchronous programming. 1) C/C is a statically typed language that requires manual memory management, while JavaScript is dynamically typed and garbage collection is automatically processed. 2) C/C needs to be compiled into machine code, while JavaScript is an interpreted language. 3) JavaScript introduces concepts such as closures, prototype chains and Promise, which enhances flexibility and asynchronous programming capabilities.

JavaScript Engines: Comparing ImplementationsJavaScript Engines: Comparing ImplementationsApr 13, 2025 am 12:05 AM

Different JavaScript engines have different effects when parsing and executing JavaScript code, because the implementation principles and optimization strategies of each engine differ. 1. Lexical analysis: convert source code into lexical unit. 2. Grammar analysis: Generate an abstract syntax tree. 3. Optimization and compilation: Generate machine code through the JIT compiler. 4. Execute: Run the machine code. V8 engine optimizes through instant compilation and hidden class, SpiderMonkey uses a type inference system, resulting in different performance performance on the same code.

Beyond the Browser: JavaScript in the Real WorldBeyond the Browser: JavaScript in the Real WorldApr 12, 2025 am 12:06 AM

JavaScript's applications in the real world include server-side programming, mobile application development and Internet of Things control: 1. Server-side programming is realized through Node.js, suitable for high concurrent request processing. 2. Mobile application development is carried out through ReactNative and supports cross-platform deployment. 3. Used for IoT device control through Johnny-Five library, suitable for hardware interaction.

Building a Multi-Tenant SaaS Application with Next.js (Backend Integration)Building a Multi-Tenant SaaS Application with Next.js (Backend Integration)Apr 11, 2025 am 08:23 AM

I built a functional multi-tenant SaaS application (an EdTech app) with your everyday tech tool and you can do the same. First, what’s a multi-tenant SaaS application? Multi-tenant SaaS applications let you serve multiple customers from a sing

How to Build a Multi-Tenant SaaS Application with Next.js (Frontend Integration)How to Build a Multi-Tenant SaaS Application with Next.js (Frontend Integration)Apr 11, 2025 am 08:22 AM

This article demonstrates frontend integration with a backend secured by Permit, building a functional EdTech SaaS application using Next.js. The frontend fetches user permissions to control UI visibility and ensures API requests adhere to role-base

JavaScript: Exploring the Versatility of a Web LanguageJavaScript: Exploring the Versatility of a Web LanguageApr 11, 2025 am 12:01 AM

JavaScript is the core language of modern web development and is widely used for its diversity and flexibility. 1) Front-end development: build dynamic web pages and single-page applications through DOM operations and modern frameworks (such as React, Vue.js, Angular). 2) Server-side development: Node.js uses a non-blocking I/O model to handle high concurrency and real-time applications. 3) Mobile and desktop application development: cross-platform development is realized through ReactNative and Electron to improve development efficiency.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.