-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path2.1.2-canvas-map.html
47 lines (36 loc) · 1.15 KB
/
2.1.2-canvas-map.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<!DOCTYPE html>
<meta charset="utf-8">
<body>
<script src="lib/d3.v4.min.js"></script>
<script src="lib/topojson.v1.min.js"></script>
<script>
var width = 1300,
height = 800;
var projection = d3.geoMercator()
.center([100, 37])
.scale(850)
.translate([width/2, height/2]);
var canvas = d3.select("body").append("canvas")
.attr("width", width)
.attr("height", height);
var context = canvas.node().getContext("2d");
var mapPath = d3.geoPath()
.projection(projection)
.context(context);
var mapColor =d3.scaleOrdinal(d3.schemeCategory20);
d3.json("data/china.topojson", function(error, us) {
if (error) throw error;
var countries = topojson.feature(us, us.objects.collection);
countries.features.forEach(drawMapPath);
});
function drawMapPath(d) {
context.beginPath();
var color = mapColor(parseInt(d.properties.id) );
context.fillStyle= color;
context.strokeStyle=color;
context.globalAlpha=0.3;
mapPath(d);
context.stroke();
context.fill();
}
</script>