Chinaunix首页 | 论坛 | 博客
  • 博客访问: 398294
  • 博文数量: 25
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 125
  • 用 户 组: 普通用户
  • 注册时间: 2018-07-07 22:36
文章分类
文章存档

2020年(9)

2019年(14)

2018年(2)

我的朋友

分类: PHP

2019-12-20 09:27:45

一款PHP+jQuery实现的中国地图热点数据统计展示实例,当鼠标滑动到地图指定省份区域,在弹出的提示框中显示对应省份的数据信息。

首先在页面中加一个div#tip,用来展示地图信息的提示框和#map用来生成地图。
  1. <div id="map"></div>
  2. <div id="tip"></div>

接着我们引入jQuery库、raphael.js及chinamapPath.js(中国地图数据)
  1. <script type="text/javascript" src="jquery.js"></script>
  2. <script type="text/javascript" src="raphael.js"></script>
  3. <script type="text/javascript" src="chinamapPath.js"></script>

通过调用raphael绘制出中国地图,然后载入统计数据,由于地图区块小,我们不在地图载入的时候就将数据显示在地图区块上了,我们通过鼠标交互实现将数据信息更好的展示给用户。当鼠标滑向省份区块时,通过e.clientX和e.clientY定位鼠标坐标,然后通过jquery的css()方法定位提示框div#tip,并且将对应省份的的名称和活跃用户数加到提示框里并展现出来,代码如下:
  1. $(function() {
  2.     $.get("json.php",
  3.     function(json) {
  4.         var data = string2Array(json);
  5.         var flag;
  6.         var arr = new Array();
  7.         for (var i = 0; i < data.length; i++) {
  8.             var d = data[i];
  9.             if (d < 100) {
  10.                 flag = 0;
  11.             } else if (d >= 100 && d < 500) {
  12.                 flag = 1;
  13.             } else if (d >= 500 && d < 2000) {
  14.                 flag = 2;
  15.             } else if (d >= 2000 && d < 5000) {
  16.                 flag = 3;
  17.             } else if (d >= 5000 && d < 10000) {
  18.                 flag = 4;
  19.             } else {
  20.                 flag = 5;
  21.             }
  22.             arr.push(flag);
  23.         }
  24.         var colors = ["#d7eef8", "#97d6f5", "#3fbeef", "#00a2e9", "#0084be", "#005c86"];
  25.         var R = Raphael("map", 600, 500);
  26.  
  27.         //调用绘制地图方法
  28.         paintMap(R);
  29.  
  30.         var i = 0;
  31.         for (var state in china) {
  32.             china[state]['path'].color = Raphael.getColor(0.9); (function(st, state) {
  33.                 var prodata = data[i];
  34.                 var fillcolor = colors[arr[i]];
  35.                 st.attr({
  36.                     fill: fillcolor
  37.                 }); //填充背景色
  38.                 xOffset = 70;
  39.                 yOffset = 180;
  40.                 st.hover(function(e) {
  41.                     st.animate({
  42.                         fill: "#fdd",
  43.                         stroke: "#eee"
  44.                     },
  45.                     500);
  46.                     R.safari();
  47.                     $("#tip").css({
  48.                         "top": (e.clientY - xOffset) + "px",
  49.                         "left": (e.clientX - yOffset) + "px"
  50.                     }).fadeIn("fast").html("

    " + china[state]['name'] + "

    活跃用户数:" + prodata + "

    "
    );
  51.                 },
  52.                 function() {
  53.                     st.animate({
  54.                         fill: fillcolor,
  55.                         stroke: "#eee"
  56.                     },
  57.                     500);
  58.                     R.safari();
  59.                     $("#tip").hide();
  60.                 });
  61.  
  62.                 st.mousemove(function(e) {
  63.                     $("#tip").css({
  64.                         "top": (e.clientY - xOffset) + "px",
  65.                         "left": (e.clientX - yOffset) + "px"
  66.                     });
  67.                     R.safari();
  68.                 });
  69.  
  70.             })(china[state]['path'], state);
  71.             i++;
  72.         }
  73.     });
  74. });
  75.  
  76. function string2Array(string) {
  77.     eval("var result = " + decodeURI(string));
  78.     return result;
  79. }

本文转自: 转载请注明出处!
阅读(1177) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~