Use raphael. in the js map of China article, I will show you how to use raphael. js draws a map of China. Today I want to introduce how to load data into the map in practical applications. This article uses PHPMysqljQuery in combination with examples to achieve data statistics on the number of copies in each province on map in China. This example calculates the number of active users of a product in each province.
Use raphael. in the js map of China article, I will show you how to use raphael. js draws a map of China. Today I want to introduce how to load data into the map in practical applications. This article uses PHPMysqljQuery in combination with examples to achieve data statistics on the number of copies in each province on map in China. This example calculates the number of active users of a product in each province.
Use raphael. in the js map of China article, I will show you how to use raphael. js draws a map of China. Today I want to introduce how to load data into the map in practical applications. Combined with examples, this article uses PHP + Mysql + jQuery to achieve the data statistics of each province on map in China.
In this example, the number of active users of a product in each province is counted. The data comes from the mysql database and is divided into different levels based on the number of active users in each province, the activity of each province is displayed with different background colors, meeting the actual application requirements.
HTML
Like the previous article "drawing a map of China using raphael. js" on this site, first load the raphael. js library file and the chinamapPath. js path file in the head section.
Html code
- Type = "text/javascript" src = "jquery. js" >
- Type = "text/javascript" src = "raphael. js" >
- Type = "text/javascript" src = "chinamapPath. js" >
Then place p # map in the position where the map needs to be placed in the body.
Html code
- Id = "map" >
PHPWe have prepared a mysql table named mapdata, which stores active user data of products in various provinces. We use PHP to read data from the mysql table, output the data in json format, and name the php file json. PHP.
[Php]View plaincopy
- $ Host = "localhost"; // host
- $ Db_user = "root"; // database username
- $ Db_pass = ""; // Password
- $ Db_name = "demo"; // Database Name
-
- $ Link = mysql_connect ($ host, $ db_user, $ db_pass); // connect to the database
- Mysql_select_db ($ db_name, $ link );
- Mysql_query ("SET names UTF8 ");
-
- $ SQL = "select active from mapdata order by id asc"; // query
- $ Query = mysql_query ($ SQL );
-
- While ($ row = mysql_fetch_array ($ query )){
- $ Arr [] = $ row ['active'];
- }
- Echo json_encode ($ arr); // JSON format
- Mysql_close ($ link); // close the connection
It is worth noting that we need to sort the provinces in the mapdata table in the same order as the provinces in the chinamapPath. js file so that the data read can correspond to the provinces in the map.
JQueryFirst, we use jquery's get () method to obtain json data.
Js Code
- $ (Function (){
- $. Get ("json. php", function (json ){
- ...
- });
- });
After obtaining the json data, we first need to convert the json data into an array. Then we traverse the entire array and make a classification based on the number of active users in each province in the json data, here I will divide the level into six levels: 0-5. The larger the number of active users, the deeper the background color. This will display the data level of different provinces at a glance on the map. This is basically the same as the previous article "using raphael. js to draw a map of China" on this site. The difference is that you can fill the corresponding color in different provinces. Please refer to the compiled code:
Js Code
- $ (Function (){
- $. Get ("json. php", function (json) {// get data
- Var data = string2Array (json); // convert the Array
-
- Var flag;
- Var arr = new Array (); // defines the new Array, corresponding to the level
- For (var I = 0; I
- Var d = data [I];
- If (d <100 ){
- Flag = 0;
- } Else if (d> = 100 & d <500 ){
- Flag = 1;
- } Else if (d> = 500 & d <2000 ){
- Flag = 2;
- } Else if (d> = 2000 & d <5000 ){
- Flag = 3;
- } Else if (d> = 5000 & d <10000 ){
- Flag = 4;
- } Else {
- Flag = 5;
- }
- Arr. push (flag );
- }
- // Define the color
- Var colors = ["# d7eef8", "#97d6f5", "#3 fbeef", "#00a2e9", "# 0084be", "#005c86"];
-
- // Call the Map Drawing Method
- Varr = Raphael ("map", 600,500 );
- PaintMap (R );
-
- Var textAttr = {
- "Fill": "#000 ",
- "Font-size": "12px ",
- "Cursor": "pointer"
- };
-
- Var I = 0;
- For (var state in china ){
- China [state] ['path']. color = Raphael. getColor (0.9 );
- (Function (st, state ){
-
- // Obtain the central coordinates of the current image
- Var xx = st. getBBox (). x + (st. getBBox (). width/2 );
- Var yy = st. getBBox (). y + (st. getBBox (). height/2 );
-
- // Modify the text offset coordinates of some maps
- Switch (china [state] ['name']) {
- Case "Jiangsu ":
- Xx + = 5;
- Yy-= 10;
- Break;
- Case "Hebei ":
- Xx-= 10;
- Yy + = 20;
- Break;
- Case "Tianjin ":
- Xx + = 10;
- Yy + = 10;
- Break;
- Case "Shanghai ":
- Xx + = 10;
- Break;
- Case "Guangdong ":
- Yy-= 10;
- Break;
- Case "Macao ":
- Yy + = 10;
- Break;
- Case "Hong Kong ":
- Xx + = 20;
- Yy + = 5;
- Break;
- Case "Gansu ":
- Xx-= 40;
- Yy-= 30;
- Break;
- Case "Shaanxi ":
- Xx + = 5;
- Yy + = 10;
- Break;
- Case "Inner Mongolia ":
- Xx-= 15;
- Yy + = 65;
- Break;
- Default:
- }
- // Write text
- China [state] ['text'] = R. text (xx, yy, china [state] ['name']). attr (textAttr );
-
- Var fillcolor = colors [arr [I]; // obtain the corresponding color.
-
- St. attr ({fill: fillcolor}); // fill in the background color
-
- St [0]. onmouseover = function (){
- St. animate ({fill: "# fdd", stroke: "# eee" },500 );
- China [state] ['text']. toFront ();
- R. safari ();
- };
- St [0]. onmouseout = function (){
- St. animate ({fill: fillcolor, stroke: "# eee" },500 );
- China [state] ['text']. toFront ();
- R. safari ();
- };
-
- }) (China [state] ['path'], state );
- I ++;
- }
- });
- });
In the above Code, use var fillcolor = colors [arr [I]; To obtain the color value of the corresponding level, and then use st. attr ({fill: fillcolor}); fill the color in the corresponding province block. In addition, the string2Array () function converts a string to an array.
Js Code
- Function string2Array (string ){
- Eval ("var result =" + decodeURI (string ));
- Return result;
- }
In this way, we can see a map of China with different backgrounds in different provinces. Different colors can be used to differentiate the number of active users in different provinces to achieve the expected goal.