Table of Contents
PHP+Mysql+jQuery中国地图区域数据统计实例讲解,jquery实例讲解
Home php教程 php手册 PHP+Mysql+jQuery中国地图区域数据统计实例讲解,jquery实例讲解

PHP+Mysql+jQuery中国地图区域数据统计实例讲解,jquery实例讲解

Jun 13, 2016 am 08:53 AM
jquery mysql mysql database php Statistics

PHP+Mysql+jQuery中国地图区域数据统计实例讲解,jquery实例讲解

今天我要给大家介绍在实际应用中,如何把数据载入到地图中。本文结合实例,使用PHP+Mysql+jQuery实现中国地图各省份数据统计效果。


本例以统计某产品在各省份的活跃用户数为背景,数据来源于mysql数据库,根据各省份的活跃用户数,分成不同等级,并以不同的背景色显示各省份的活跃程度,符合实际应用需求。

HTML

首先在head部分载入raphael.js库文件和chinamapPath.js路径信息文件。

<script type="text/javascript" src="jquery.js"></script> 
<script type="text/javascript" src="raphael.js"></script> 
<script type="text/javascript" src="chinamapPath.js"></script>
Copy after login

然后在body中需要放置地图的位置放置div#map。

<div id="map"></div>
Copy after login

PHP

我们准备一张mysql表名为mapdata,这张表存储的是产品在各个省份的活跃用户数据。我们使用PHP读取mysql表中的数据,并将读取的数据以json格式输出,并将PHP文件命名为json.php。

$host="localhost";//主机 
$db_user="root";//数据库用户名 
$db_pass="";//密码 
$db_name="demo";//数据库名称 
 
$link=mysql_connect($host,$db_user,$db_pass);//连接数据库 
mysql_select_db($db_name,$link); 
mysql_query("SET names UTF8"); 
 
$sql = "select active from mapdata order by id asc";//查询 
$query = mysql_query($sql); 
 
while($row=mysql_fetch_array($query)){ 
  $arr[] = $row['active']; 
} 
echo json_encode($arr);//JSON格式 
mysql_close($link);//关闭连接
Copy after login

值得注意的是,我们要把mapdata表中各省份的排序与chinamapPath.js文件中的各省份顺序一致,这样才能保证读取的数据能和地图中的省份对应上。

jQuery

首先我们使用jquery的get()方法获取json数据。

$(function(){ 
  $.get("json.php",function(json){ 
    ... 
  }); 
});
Copy after login

获取到json数据后,我们先要将json数据转换为数组,然后我们遍历整个数组,根据json数据中各省份活跃用户数的多少,我们作一个等级区分,这里我将等级分为0-5六个等级,活跃用户数越大背景颜色越深,这样在地图上显示就会一目了然的看出不同省份的数据等级程度。

请看整理好的代码:

$(function(){ 
 $.get("json.php",function(json){//获取数据 
 var data = string2Array(json);//转换数组 
  
 var flag; 
 var arr = new Array();//定义新数组,对应等级 
 for(var i=0;i<data.length;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); 
 } 
 //定义颜色 
 var colors = ["#d7eef8","#97d6f5","#3fbeef","#00a2e9","#0084be","#005c86"]; 
  
 //调用绘制地图方法 
 var R = 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) { 
    
   //获取当前图形的中心坐标 
   var xx = st.getBBox().x + (st.getBBox().width / 2); 
   var yy = st.getBBox().y + (st.getBBox().height / 2); 
    
   //修改部分地图文字偏移坐标 
   switch (china[state]['name']) { 
    case "江苏": 
     xx += 5; 
     yy -= 10; 
     break; 
    case "河北": 
     xx -= 10; 
     yy += 20; 
     break; 
    case "天津": 
     xx += 10; 
     yy += 10; 
     break; 
    case "上海": 
     xx += 10; 
     break; 
    case "广东": 
     yy -= 10; 
     break; 
    case "澳门": 
     yy += 10; 
     break; 
    case "香港": 
     xx += 20; 
     yy += 5; 
     break; 
    case "甘肃": 
     xx -= 40; 
     yy -= 30; 
     break; 
    case "陕西": 
     xx += 5; 
     yy += 10; 
     break; 
    case "内蒙古": 
     xx -= 15; 
     yy += 65; 
     break; 
    default: 
   } 
   //写入文字 
   china[state]['text'] = R.text(xx, yy, china[state]['name']).attr(textAttr); 
    
   var fillcolor = colors[arr[i]];//获取对应的颜色 
    
   st.attr({fill:fillcolor});//填充背景色 
    
   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++; 
 } 
 }); 
});
Copy after login

上述代码中,使用var fillcolor = colors[arr[i]];获取对应等级的颜色值,然后通过st.attr({fill:fillcolor});将颜色填充到对应的省份区块中。此外string2Array()函数是将字符串转换为数组。

function string2Array(string) { 
  eval("var result = " + decodeURI(string)); 
  return result; 
}
Copy after login

通过以上步骤,我们就可以看到一个不同省份不同背景色的中国地图,根据不同颜色可以区分省份之间的活跃用户数差异程度,达到预期目标,小伙伴们希望这篇文章对大家的学习有所帮助。

Statement of this Website
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

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 Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

MySQL: An Introduction to the World's Most Popular Database MySQL: An Introduction to the World's Most Popular Database Apr 12, 2025 am 12:18 AM

MySQL is an open source relational database management system, mainly used to store and retrieve data quickly and reliably. Its working principle includes client requests, query resolution, execution of queries and return results. Examples of usage include creating tables, inserting and querying data, and advanced features such as JOIN operations. Common errors involve SQL syntax, data types, and permissions, and optimization suggestions include the use of indexes, optimized queries, and partitioning of tables.

PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP's Current Status: A Look at Web Development Trends PHP's Current Status: A Look at Web Development Trends Apr 13, 2025 am 12:20 AM

PHP remains important in modern web development, especially in content management and e-commerce platforms. 1) PHP has a rich ecosystem and strong framework support, such as Laravel and Symfony. 2) Performance optimization can be achieved through OPcache and Nginx. 3) PHP8.0 introduces JIT compiler to improve performance. 4) Cloud-native applications are deployed through Docker and Kubernetes to improve flexibility and scalability.

Why Use MySQL? Benefits and Advantages Why Use MySQL? Benefits and Advantages Apr 12, 2025 am 12:17 AM

MySQL is chosen for its performance, reliability, ease of use, and community support. 1.MySQL provides efficient data storage and retrieval functions, supporting multiple data types and advanced query operations. 2. Adopt client-server architecture and multiple storage engines to support transaction and query optimization. 3. Easy to use, supports a variety of operating systems and programming languages. 4. Have strong community support and provide rich resources and solutions.

PHP: The Foundation of Many Websites PHP: The Foundation of Many Websites Apr 13, 2025 am 12:07 AM

The reasons why PHP is the preferred technology stack for many websites include its ease of use, strong community support, and widespread use. 1) Easy to learn and use, suitable for beginners. 2) Have a huge developer community and rich resources. 3) Widely used in WordPress, Drupal and other platforms. 4) Integrate tightly with web servers to simplify development deployment.

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

The Importance of MySQL: Data Storage and Management The Importance of MySQL: Data Storage and Management Apr 12, 2025 am 12:18 AM

MySQL is an open source relational database management system suitable for data storage, management, query and security. 1. It supports a variety of operating systems and is widely used in Web applications and other fields. 2. Through the client-server architecture and different storage engines, MySQL processes data efficiently. 3. Basic usage includes creating databases and tables, inserting, querying and updating data. 4. Advanced usage involves complex queries and stored procedures. 5. Common errors can be debugged through the EXPLAIN statement. 6. Performance optimization includes the rational use of indexes and optimized query statements.

MySQL's Place: Databases and Programming MySQL's Place: Databases and Programming Apr 13, 2025 am 12:18 AM

MySQL's position in databases and programming is very important. It is an open source relational database management system that is widely used in various application scenarios. 1) MySQL provides efficient data storage, organization and retrieval functions, supporting Web, mobile and enterprise-level systems. 2) It uses a client-server architecture, supports multiple storage engines and index optimization. 3) Basic usages include creating tables and inserting data, and advanced usages involve multi-table JOINs and complex queries. 4) Frequently asked questions such as SQL syntax errors and performance issues can be debugged through the EXPLAIN command and slow query log. 5) Performance optimization methods include rational use of indexes, optimized query and use of caches. Best practices include using transactions and PreparedStatemen

See all articles