ECharts map is mainly used to visualize geographical area data and display data distribution information in different areas. The ECharts official website provides map data downloads such as China maps and world maps, and the maps can be called through js introduction or asynchronous loading of json files.
Effect demonstration Source code download
This article will explain how to use PHP+jQuery+MySQL to asynchronously load ECharts map data with examples. We take the map of China as an example to show the GDP data of each province in my country last year (2015). By asynchronously requesting PHP, the data in MySQL is read and then displayed on the map. Therefore, in addition to front-end knowledge, this article also requires you to understand PHP and MySQL related knowledge.
HTML
First place div#myChart on the page where the map needs to be loaded.
1 2 |
|
Then load Echarts and China map js files. Since asynchronous ajax loading data is used in the example of this article, the jQuery library file needs to be loaded.
1 2 3 |
|
Javascript
In the next js part, first set the Echarts option content. Please see the following code and comments.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
|
Then we use jQuery’s Ajax() to request the data asynchronously.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
|
Obviously, we see that a post request is sent to mapdata.php through jQuery's $.ajax(), requiring the return of data in json format. When the request is successful and a response is received, the map data is re-rendered.
PHP
The task of mapdata.php is to read the data in the mysql data table and then return it to the front end. The first thing is to connect to the database. This part of the code is in connect.php. Please download the source code to view it. Then there is the sql query, reading the data in the table echarts_map, and finally returning it in json format.
1 2 3 4 5 6 7 8 9 10 11 12 |
|
MySQL
Finally, the mysql data table structure information is provided. For data information, you can download the source code and import the sql into your mysql. Please pay attention to modifying the database configuration information of connect.php during the demonstration.
1 2 3 4 5 6 |
|