PHP + Mysql + jQuery for area data statistics on map of China

Source: Internet
Author: User
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

  1. Type = "text/javascript" src = "jquery. js" >
  2. Type = "text/javascript" src = "raphael. js" >
  3. 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

  1. Id = "map" >

PHP

We 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

  1. $ Host = "localhost"; // host
  2. $ Db_user = "root"; // database username
  3. $ Db_pass = ""; // Password
  4. $ Db_name = "demo"; // Database Name
  5. $ Link = mysql_connect ($ host, $ db_user, $ db_pass); // connect to the database
  6. Mysql_select_db ($ db_name, $ link );
  7. Mysql_query ("SET names UTF8 ");
  8. $ SQL = "select active from mapdata order by id asc"; // query
  9. $ Query = mysql_query ($ SQL );
  10. While ($ row = mysql_fetch_array ($ query )){
  11. $ Arr [] = $ row ['active'];
  12. }
  13. Echo json_encode ($ arr); // JSON format
  14. 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.

JQuery

First, we use jquery's get () method to obtain json data.

Js Code

  1. $ (Function (){
  2. $. Get ("json. php", function (json ){
  3. ...
  4. });
  5. });

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

  1. $ (Function (){
  2. $. Get ("json. php", function (json) {// get data
  3. Var data = string2Array (json); // convert the Array
  4. Var flag;
  5. Var arr = new Array (); // defines the new Array, corresponding to the level
  6. For (var I = 0; I
  7. Var d = data [I];
  8. If (d <100 ){
  9. Flag = 0;
  10. } Else if (d> = 100 & d <500 ){
  11. Flag = 1;
  12. } Else if (d> = 500 & d <2000 ){
  13. Flag = 2;
  14. } Else if (d> = 2000 & d <5000 ){
  15. Flag = 3;
  16. } Else if (d> = 5000 & d <10000 ){
  17. Flag = 4;
  18. } Else {
  19. Flag = 5;
  20. }
  21. Arr. push (flag );
  22. }
  23. // Define the color
  24. Var colors = ["# d7eef8", "#97d6f5", "#3 fbeef", "#00a2e9", "# 0084be", "#005c86"];
  25. // Call the Map Drawing Method
  26. Varr = Raphael ("map", 600,500 );
  27. PaintMap (R );
  28. Var textAttr = {
  29. "Fill": "#000 ",
  30. "Font-size": "12px ",
  31. "Cursor": "pointer"
  32. };
  33. Var I = 0;
  34. For (var state in china ){
  35. China [state] ['path']. color = Raphael. getColor (0.9 );
  36. (Function (st, state ){
  37. // Obtain the central coordinates of the current image
  38. Var xx = st. getBBox (). x + (st. getBBox (). width/2 );
  39. Var yy = st. getBBox (). y + (st. getBBox (). height/2 );
  40. // Modify the text offset coordinates of some maps
  41. Switch (china [state] ['name']) {
  42. Case "Jiangsu ":
  43. Xx + = 5;
  44. Yy-= 10;
  45. Break;
  46. Case "Hebei ":
  47. Xx-= 10;
  48. Yy + = 20;
  49. Break;
  50. Case "Tianjin ":
  51. Xx + = 10;
  52. Yy + = 10;
  53. Break;
  54. Case "Shanghai ":
  55. Xx + = 10;
  56. Break;
  57. Case "Guangdong ":
  58. Yy-= 10;
  59. Break;
  60. Case "Macao ":
  61. Yy + = 10;
  62. Break;
  63. Case "Hong Kong ":
  64. Xx + = 20;
  65. Yy + = 5;
  66. Break;
  67. Case "Gansu ":
  68. Xx-= 40;
  69. Yy-= 30;
  70. Break;
  71. Case "Shaanxi ":
  72. Xx + = 5;
  73. Yy + = 10;
  74. Break;
  75. Case "Inner Mongolia ":
  76. Xx-= 15;
  77. Yy + = 65;
  78. Break;
  79. Default:
  80. }
  81. // Write text
  82. China [state] ['text'] = R. text (xx, yy, china [state] ['name']). attr (textAttr );
  83. Var fillcolor = colors [arr [I]; // obtain the corresponding color.
  84. St. attr ({fill: fillcolor}); // fill in the background color
  85. St [0]. onmouseover = function (){
  86. St. animate ({fill: "# fdd", stroke: "# eee" },500 );
  87. China [state] ['text']. toFront ();
  88. R. safari ();
  89. };
  90. St [0]. onmouseout = function (){
  91. St. animate ({fill: fillcolor, stroke: "# eee" },500 );
  92. China [state] ['text']. toFront ();
  93. R. safari ();
  94. };
  95. }) (China [state] ['path'], state );
  96. I ++;
  97. }
  98. });
  99. });

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

  1. Function string2Array (string ){
  2. Eval ("var result =" + decodeURI (string ));
  3. Return result;
  4. }

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.