{"id":2817,"date":"2013-05-06T17:02:11","date_gmt":"2013-05-06T08:02:11","guid":{"rendered":"https:\/\/gunmagisgeek.com\/wordpress\/?p=2817"},"modified":"2018-03-06T14:21:46","modified_gmt":"2018-03-06T05:21:46","slug":"post-2817","status":"publish","type":"post","link":"https:\/\/gunmagisgeek.com\/blog\/d3v4\/2817","title":{"rendered":"\u3010D3.js\u3011\u5730\u56f3\u4e0a\u306b\u30dc\u30ed\u30ce\u30a4\u56f3\u3092\u63cf\u304f"},"content":{"rendered":"<p>\u5404\u9ed2\u3044\u70b9(\u6bcd\u70b9)\u304c\u30ac\u30bd\u30ea\u30f3\u30b9\u30bf\u30f3\u30c9\u306e\u4f4d\u7f6e\u3067\u3059\u3002<br \/>\n<a href=\"http:\/\/bl.ocks.org\/shimizu\/7a738422fdb80818cc32acf39b0a4592\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/gunmagisgeek.com\/blog\/wp-content\/uploads\/2013\/05\/test000012.jpg\" alt=\"D3.js Voronoi Map\" width=\"590\" height=\"390\" class=\"alignnone size-full wp-image-2818\" srcset=\"https:\/\/gunmagisgeek.com\/blog\/wp-content\/uploads\/2013\/05\/test000012.jpg 590w, https:\/\/gunmagisgeek.com\/blog\/wp-content\/uploads\/2013\/05\/test000012-227x150.jpg 227w\" sizes=\"auto, (max-width: 590px) 100vw, 590px\" \/><\/a><\/p>\n<p><a href=\"http:\/\/nlftp.mlit.go.jp\/ksj\/gml\/datalist\/KsjTmplt-P07.html\">\u56fd\u571f\u6570\u5024\u60c5\u5831\u3000\u71c3\u6599\u7d66\u6cb9\u6240\u30c7\u30fc\u30bf(\u70b9)<\/a>\u3092\u4f7f\u3063\u3066\u3001\u9ad8\u5d0e\u5e02\u306e\u5730\u56f3\u4e0a\u306b\u30dc\u30ed\u30ce\u30a4\u56f3\u3092\u66f8\u3044\u3066\u307f\u307e\u3057\u305f\u3002<\/p>\n<p><a href=\"http:\/\/bl.ocks.org\/shimizu\/7a738422fdb80818cc32acf39b0a4592\">example<\/a><\/p>\n<p>\u3010\u53c2\u8003\u3011<br \/>\n<a href=\"http:\/\/www.ics.kagoshima-u.ac.jp\/~fuchida\/edu\/algorithm\/voronoi-diagram\/voronoi-diagram.html\">\u30dc\u30ed\u30ce\u30a4\u56f3\u3068\u306f\u3000<\/a><\/p>\n<h2>\u30b5\u30f3\u30d7\u30eb\u30b3\u30fc\u30c9<\/h2>\n<pre class=\"lang:js decode:true \" >const width = 980;\nconst height = 500;\nconst svg = d3.select(\"svg\");\nconst mapLayer = svg.append(\"g\");\nconst voronoiLayer = svg.append(\"g\");\nconst projection = d3.geoMercator();\n\nconst path = d3.geoPath();\u3000\n\nconst q = d3.queue();\n\nq\n .defer(d3.json, \"takasaki.topojson\")\n .defer(d3.json, \"gasStation.geojson\")\n .await((error, maps2topo, point2geo) => {\n    const maps2geo = topojson.feature(maps2topo, maps2topo.objects.takasaki);\n\n    \/\/\u8aad\u307f\u8fbc\u3093\u3060\u5730\u56f3\u30c7\u30fc\u30bf\u304c\u63cf\u753b\u9818\u57df\u306b\u53ce\u307e\u308b\u3088\u3046\u306b\u81ea\u52d5\u7684\u306b\u30d7\u30ed\u30b8\u30a7\u30af\u30b7\u30e7\u30f3\u3092\u8abf\u6574\u3059\u308b\u3002\n    projection.fitExtent([[0,0],[width, height]], maps2geo);\n    path.projection(projection);\n\n    drawMaps(maps2geo);\n    drawVoronoi(point2geo);\n});\n\n\nfunction drawMaps(geojson){\n    \/\/\u5730\u56f3\u8868\u793a\n    const map =  mapLayer.attr(\"id\", \"map\")\n        .selectAll(\"path\")\n        .data(geojson.features)\n        .enter()\n        .append(\"svg:path\")\n        .attr(\"d\", path)\n        .attr(\"fill\", \"#99ff99\")\n        .attr(\"fill-opacity\", 1)\n        .attr(\"stroke\", \"black\");\n    \n}\n\nfunction drawVoronoi(geojson){\n    const pointdata = geojson.features;\n    const positions = [];\n    \n    pointdata.forEach(d => {\n        positions.push(projection(d.geometry.coordinates)); \/\/\u4f4d\u7f6e\u60c5\u5831\u3092\u30d4\u30af\u30bb\u30eb\u5ea7\u6a19\u306b\u5909\u63db\u3059\u308b\n    });\n    \n    \/\/\u6bcd\u70b9\u8868\u793a\n    voronoiLayer.selectAll(\".point\")\n        .data(positions)\n        .enter()\n        .append(\"circle\")\n        .attr(\"class\", \"point\")\n        .attr(\"transform\", d =>  `translate(${d[0]}, ${d[1]})` )\n        .attr(\"r\", 2);\n    \n    \n    \/\/\u30dc\u30ed\u30ce\u30a4\u5909\u63db\u95a2\u6570\n    const voronoi = d3.voronoi()\n        .extent([[-1, -1],[width+1, height+1]]);\n    \n    \/\/\u30dc\u30ed\u30ce\u30a4\u5883\u754c\u30dd\u30ea\u30b4\u30f3\u30c7\u30fc\u30bf\u3092\u751f\u6210\u3059\u308b\n    const polygons = voronoi(positions).polygons();\n    \n    \/\/\u5883\u754c\u8868\u793a\n    voronoiLayer.selectAll(\".cell\")\n        .data(polygons)\n        .enter()\n        .append(\"path\")\n        .attr(\"class\", \"cell\")\n        .attr(\"fill\", \"none\")\n        .attr(\"stroke\", \"black\")\n        .attr(\"d\", d => `M${d.join(\"L\")}Z` );\n        \n    \n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u5404\u9ed2\u3044\u70b9(\u6bcd\u70b9)\u304c\u30ac\u30bd\u30ea\u30f3\u30b9\u30bf\u30f3\u30c9\u306e\u4f4d\u7f6e\u3067\u3059\u3002 \u56fd&hellip;<\/p>\n","protected":false},"author":1,"featured_media":2818,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[69],"tags":[],"class_list":["post-2817","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-d3v4","has-post-thumbnail-archive"],"_links":{"self":[{"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/posts\/2817","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/comments?post=2817"}],"version-history":[{"count":0,"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/posts\/2817\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/media\/2818"}],"wp:attachment":[{"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/media?parent=2817"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/categories?post=2817"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/tags?post=2817"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}