BIGEMPA Js API示例中心
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <!-- 以下CSS地址請(qǐng)?jiān)诎惭b軟件了替換成本地的地址 CSS地址請(qǐng)使用: http://localhost:9000/bigemap.js/v2.1.0/bigemap.css 軟件下載地址 http://www.bjxdny.cn/reader/download/detail201802017.html --> <link href='http://www.bjxdny.cn:9000/bigemap.js/v2.1.0/bigemap.css' rel='stylesheet' /> <!-- JS地址請(qǐng)使用: http://localhost:9000/bigemap.js/v2.1.0/bigemap.js --> <script src='http://www.bjxdny.cn:9000/bigemap.js/v2.1.0/bigemap.js'></script> <script src="http://www.bjxdny.cn/Public/common/js/jquery.min.js"></script> <script src="http://www.bjxdny.cn/Public/js/d3.min.js"></script> </head> <style> body { margin: 0; padding: 0; } #map { position: absolute; top: 0; bottom: 0; width: 100%; } #world { width: 100%; height: 100vh; background: url('/Public/images/d3xk.jpg') no-repeat; /* overflow: hidden; */ background-size: cover; } #svg { width: 1024px; height: 600px; margin: 0 auto; display: block; } #tooltip { opacity: 0; position: absolute; padding: 10px; background: #333333; border: 2px solid #e8e8e8; color: #33cc99; font-size: 14px; border-radius: 4px; } svg :hover { cursor: pointer } </style> <body> <div id="world"> <svg id="svg" #svg></svg> <div id="tooltip"></div> </div> </body> <script> // /定義繪制的svg的大小: var width = 1024; var height = 600; // /設(shè)置投影函數(shù): var projection = d3.geoMercator() .scale(700)// 投影的比例因子,可以按比例放大投影。 .center([105, 38])//將中心點(diǎn)設(shè)置為經(jīng)度105,緯度38,這里正好是中國(guó)地圖的中心點(diǎn)。 .translate([width / 2, height / 2]);// 將投影的中心設(shè)置為svg的中心。 var path = d3.geoPath(projection); //顏色比例尺 var colors = d3.scaleOrdinal(d3.schemeCategory10); $.get('/Public/d3json/sichuan.json', function (data) { // data = JSON.parse(data) console.log(data); var svg = d3.select('#svg') .attr('width', width) .attr('height', height); var g = svg.append('g') //.selectAll('path') 選中svg中所有匹配path的元素節(jié)點(diǎn) g.selectAll('path') //.data(data.features) 綁定當(dāng)前選擇器和數(shù)據(jù)。data的操作是“update”,表明選擇的dom元素已經(jīng)和數(shù)據(jù)進(jìn)行了綁定 .data(data.features) //.enter() 返回輸入(enter)選擇:當(dāng)前選擇中存在,但是當(dāng)前dom元素中還不存在的每個(gè)數(shù)據(jù)元素的占位符節(jié)點(diǎn)。 .enter() //.append('path') 在當(dāng)前選擇的每個(gè)元素最后追加具有指定名稱的新元素,返回包含追加元素的新選擇 .append('path') //.attr('d', path) 為所有選中元素設(shè)置名稱為”d”的屬性,值為path里面的每個(gè)值。即給svg添加的path元素設(shè)置d屬性,d屬性的值是需要繪制的路徑 .attr('d', path) //填充顏色 .attr('fill', 'white') .transition() .duration(1000) .delay((d, i) => { return i * 500 }) .attr('fill', function (d, i) { return colors(i); }) //邊款顏色 .attr('stroke', 'rgba(255, 255, 255, 1') //邊框?qū)挾? .attr('stroke-width', 2); var price = [] data.features.map((d, i) => { // console.log(d.properties); price.push([ { 'name': d.properties.name, 'log': d.properties.center[0], 'lat': d.properties.center[1] }, ]) }) console.log(price); //通過轉(zhuǎn)換的坐標(biāo)來給svg添加g元素進(jìn)行定點(diǎn): var location = g.selectAll('.location') .data(price) .enter() .append('g') .attr('class', 'location') .attr('transform', (d) => { console.log(d[0]); var coor = projection([d[0].log, d[0].lat]); return 'translate(' + coor[0] + ',' + coor[1] + ')'; }); //通過定的點(diǎn)給svg的g元素添加circle元素,并填充顏色畫圓。 location.append('circle') .attr('r', 5) .attr('fill', '#e91e63') .attr('class', 'location'); //添加鼠標(biāo)互動(dòng) var tooltip = d3.select('#tooltip'); //給svg的g標(biāo)簽添加鼠標(biāo)效果,鼠標(biāo)一上去出現(xiàn)tooltip文字,并將圓圈放大二倍,且伴隨著延時(shí)動(dòng)畫;鼠標(biāo)移走也是同樣相反的動(dòng)畫 location.on('mouseover', function (d) { tooltip.html('當(dāng)前城市:' + d[0].name) .style('left', d3.event.pageX + 20 + 'px') .style('top', d3.event.pageY + 20 + 'px') .style('opacity', 1); d3.select(this).select('circle').transition() .duration(150) .attr('r', 8); }).on('mouseout', function () { tooltip.style('opacity', 0); d3.select(this) .select('circle') .transition() .duration(150) .attr('r', 5); }); //添加文字 location.append('g').append('text').text(d => d[0].name).attr('font-size', 15) .attr('transform', 'translate(-8,2)').attr('fill', 'white') var zoom = d3.zoom() .scaleExtent([1, 8]) .on("zoom", zoomed); function zoomed() { g.attr('transform', d3.event.transform) } svg.call(zoom) }) </script> </html>