{"id":3307,"date":"2014-01-08T16:31:26","date_gmt":"2014-01-08T07:31:26","guid":{"rendered":"https:\/\/gunmagisgeek.com\/wordpress\/?p=3307"},"modified":"2017-02-03T01:19:42","modified_gmt":"2017-02-02T16:19:42","slug":"post-3307","status":"publish","type":"post","link":"https:\/\/gunmagisgeek.com\/blog\/d3-js\/3307","title":{"rendered":"\u3010D3.js\u3011\u304a\u3055\u3089\u3044\uff11\u56de\u76ee\u3000\u68d2\u30b0\u30e9\u30d5"},"content":{"rendered":"<p>\u6628\u5e74\u672b\u3001\u30d6\u30ed\u30b0\u30ec\u30a4\u30a2\u30a6\u30c8\u306e\u30ea\u30cb\u30e5\u30fc\u30a2\u30eb\u306e\u305f\u3081\u306b\u53bb\u5e74\u66f8\u3044\u305f\u8a18\u4e8b\u3092\u898b\u76f4\u3057\u3066\u3044\u305f\u306e\u3067\u3059\u304c\u3001\u3088\u304f\u3088\u304f\u8003\u3048\u3066\u307f\u308b\u3068\u68d2\u30b0\u30e9\u30d5\u3068\u304b\u6298\u308c\u7dda\u30b0\u30e9\u30d5\u3068\u304bD3\u3092\u4f7f\u3063\u305f\u57fa\u672c\u7684\u306a\u30b0\u30e9\u30d5\u306e\u4f5c\u6210\u65b9\u6cd5\u306b\u3064\u3044\u3066\u8a18\u4e8b\u3092\u66f8\u3044\u305f\u3053\u3068\u304c\u7121\u304b\u3063\u305f\u306a\u3001\u3068\u3044\u3046\u3053\u3068\u306b\u6c17\u4ed8\u3044\u305f\u306e\u3067\u6539\u3081\u3066D3\u306b\u3064\u3044\u3066\u304a\u3055\u3089\u3044\u3057\u3066\u3044\u304f\u3053\u3068\u306b\u6c7a\u3081\u307e\u3057\u305f\u3002<\/p>\n<h2>\u30e9\u30a4\u30d6\u30e9\u30ea\u3092\u8aad\u307f\u8fbc\u3080<\/h2>\n<p> \u307e\u305a\u57fa\u672c\u3067\u3059\u304c\u3001D3\u30e9\u30a4\u30d6\u30e9\u30ea\u3092\u8aad\u307f\u8fbc\u307f\u307e\u3059\u3002<\/p>\n<pre class=\"lang:xhtml decode:true \" >&lt;script src=\"http:\/\/d3js.org\/d3.v3.min.js\"&gt;&lt;\/script&gt;<\/pre>\n<h2>\u30c7\u30fc\u30bf\u30bb\u30c3\u30c8\u3092\u7528\u610f\u3059\u308b<\/h2>\n<p> \u4eca\u56de\u4f7f\u7528\u3059\u308b\u30c7\u30fc\u30bf\u30bb\u30c3\u30c8\u306f\u4ee5\u4e0b\u3002\u3053\u306e\u30c7\u30fc\u30bf\u3092\u68d2\u30b0\u30e9\u30d5\u3068\u3057\u3066\u8868\u793a\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"lang:js decode:true \" >var dataSet = [\r\n\t{name:\"a\", val1: 1000, val2: 500 },\r\n\t{name:\"a\", val1: 2000, val2: 400 },\r\n\t{name:\"a\", val1: 3000, val2: 300 },\r\n\t{name:\"a\", val1: 4000, val2: 200 },\r\n\t{name:\"a\", val1: 5000, val2: 100 }  \r\n];<\/pre>\n<h2>SVG\u3092\u4f7f\u3063\u3066\u68d2\u30b0\u30e9\u30d5(\u6a2a)\u3092\u4f5c\u6210\u3059\u308b<\/h2>\n<h3>svg\u8981\u7d20\u3092\u8a2d\u7f6e\u3059\u308b<\/h3>\n<p>\u307e\u305a\u306f\u3001body\u8981\u7d20\u306e\u306a\u304b\u306bsvg\u8981\u7d20\u3092\u8a2d\u7f6e\u3057\u307e\u3059\u3002<br \/>\n\u3053\u306esvg\u30bf\u30b0\u304c\u4eca\u56de\u30b0\u30e9\u30d5\u304c\u8868\u793a\u3055\u308c\u308b\u30b9\u30c6\u30fc\u30b8\u3068\u306a\u308a\u307e\u3059\u3002<br \/>\n\u9ad8\u3055200px\u3001\u5e45300px\u306b\u6307\u5b9a\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n<pre class=\"lang:js decode:true \" >var svgW = 300; \t\/\/\u30b9\u30c6\u30fc\u30b8\u306e\u5e45\r\nvar svgH = 200;\t\/\/\u30b9\u30c6\u30fc\u30b8\u306e\u9ad8\u3055\r\n\r\nvar svg = d3.select('body')\r\n\t.append('svg') \/\/svg\u8981\u7d20\u3092body\u8981\u7d20\u306e\u4e2d\u306b\u8ffd\u52a0\r\n\t.attr({\r\n\t\twidth: svgW,\r\n\t\theight: svgH,\r\n\t});\r\n<\/pre>\n<h3>\u5024\u3092\u6b63\u898f\u5316\u3059\u308bscale\u95a2\u6570\u3092\u4f5c\u6210\u3059\u308b<\/h3>\n<p>\u30c7\u30fc\u30bf\u306e\u5024\u3092\u6b63\u898f\u5316\u3059\u308bscale\u95a2\u6570\u3092\u4f5c\u6210\u3057\u307e\u3059\u3002<br \/>\n\u4eca\u56de\u4f5c\u6210\u3057\u305fscale\u95a2\u6570\u306f\u300c\u30bc\u30ed\u301c\u30c7\u30fc\u30bf(val1)\u306e\u6700\u5927\u5024\u300d\u9593\u306e\u5024\u3092\u3001\u300c\u30bc\u30ed\u301c\u30b9\u30c6\u30fc\u30b8\u306e\u9ad8\u3055\u300d\u307e\u3067\u306e\u5024\u306b\u5909\u63db\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"lang:js decode:true \" >var scale = d3.scale.linear()\r\n\t.domain([0, d3.max(dataSet, function(d){ return d.val1 })])\t\/\/\u6b63\u898f\u5316\u3055\u308c\u308b\u5024\u306e\u7bc4\u56f2\u3092\u6307\u5b9a(0\u301cval1\u306e\u6700\u5927\u5024)\r\n\t.range([0, svgH]);\t\/\/\u51fa\u529b\u5024\u306e\u7bc4\u56f2\u3092\u6307\u5b9a(0\u301c\u30b9\u30c6\u30fc\u30b8\u306e\u9ad8\u3055)<\/pre>\n<h3>\u30c7\u30fc\u30bf\u30bb\u30c3\u30c8\u3092\u675f\u7e1b\u3057rect\u8981\u7d20\u3092\u8ffd\u52a0\u3057\u307e\u3059<\/h3>\n<p>svg\u8981\u7d20\u306b\u5bfe\u3057\u3066\u67b6\u7a7a\u306erect\u8981\u7d20\u3092\u9078\u629e\u3057(\u3053\u306e\u6642\u70b9\u3067\u306f\u307e\u3060\u30c9\u30ad\u30e5\u30e1\u30f3\u30c8\u306brect\u8981\u7d20\u306f\u5b58\u5728\u3057\u306a\u3044)\u3001\u30c7\u30fc\u30bf\u30bb\u30c3\u30c8\u3092\u675f\u7e1b\u3057\u3066\u8db3\u308a\u306a\u3044rect\u8981\u7d20\u3092\u8ffd\u52a0\u3057\u307e\u3059\u3002<br \/>\nattr\u30e1\u30bd\u30c3\u30c9\u3092\u4f7f\u3063\u3066\u5404rect\u8981\u7d20\u306b\u5c5e\u6027\u5024\u3092\u8a2d\u5b9a\u3001y\u5c5e\u6027(\u7e26\u4f4d\u7f6e)\u306f\u30c7\u30fc\u30bf\u30bb\u30c3\u30c8\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u5143\u306b\u6307\u5b9a\u3057\u3001width\u5c5e\u6027(\u5e45)\u306f\u30c7\u30fc\u30bf\u30bb\u30c3\u30c8\u306eval1\u306e\u5024\u3092\u6b63\u898f\u5316\u3057\u3066\u9069\u7528\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n<pre class=\"lang:js decode:true mark:7,8 \" >var barchart = svg.selectAll('rect')\t\/\/rect\u8981\u7d20\u3092\u9078\u629e\r\n\t.data(dataSet)\t\/\/\u30c7\u30fc\u30bf\u30bb\u30c3\u30c8\u3092\u675f\u7e1b\r\n\t.enter()\t\/\/\u30c7\u30fc\u30bf\u30bb\u30c3\u30c8\u306e\u6570\u306b\u5bfe\u3057\u3066\u9078\u629e\u3055\u308c\u305f\u8981\u7d20\u304c\u3044\u304f\u3064\u8db3\u308a\u306a\u3044\u304b\u30c1\u30a7\u30c3\u30af\r\n\t.append('rect')\t\/\/\u8db3\u308a\u306a\u3044\u5206\u306erect\u8981\u7d20\u3092\u8ffd\u52a0\r\n\t.attr({\r\n\t\tx: 0,\r\n\t\ty: function(d, i){ return i * 30 },\t\t\t\/\/\u30c7\u30fc\u30bf\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u6570\u3092y\u5c5e\u6027\u306b\u53cd\u6620\r\n\t\twidth: function(d){ return scale(d.val1) },\t\/\/\u30c7\u30fc\u30bf\u306e\u5024(val1)\u3092\u6b63\u898f\u5316\u3057\u3066width\u5c5e\u6027\u306b\u53cd\u6620\r\n\t\theight: 20,\r\n\t\tfill: \"blue\"\r\n\t});<\/pre>\n<p>\u30bb\u30ec\u30af\u30b7\u30e7\u30f3\u30e1\u30bd\u30c3\u30c9\u3084enter\u30e1\u30bd\u30c3\u30c9\u306e\u52d5\u4f5c\u306b\u3064\u3044\u3066\u3082\u3063\u3068\u8a73\u3057\u304f\u77e5\u308a\u305f\u3044\u65b9\u306f\u4ee5\u4e0b\u3092\u53c2\u7167\u3057\u3066\u304f\u3060\u3055\u3044\u3002<br \/>\n<span class=\"removed_link\" title=\"http:\/\/ja.d3js.info\/mike\/selection\/\">D3 &#8211; \u30bb\u30ec\u30af\u30b7\u30e7\u30f3\u306e\u4ed5\u7d44\u307f<\/span><\/p>\n<p>\u30fb\u5b9f\u884c\u7d50\u679c<br \/>\n\u3053\u3053\u307e\u3067\u306e\u30b3\u30fc\u30c9\u3092\u5b9f\u884c\u3057\u305f\u7d50\u679c\u304c\u4ee5\u4e0b\u3067\u3059\u3002<br \/>\n\u6a2a\u5411\u304d\u306e\u30b0\u30e9\u30d5\u304c\u8868\u793a\u3055\u308c\u307e\u3059\u3002<br \/>\n<img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/gunmagisgeek.com\/blog\/wp-content\/uploads\/2014\/01\/d3barchart000000.jpg\" alt=\"d3barchart000000\" width=\"405\" height=\"139\" class=\"alignnone size-full wp-image-3308\" srcset=\"https:\/\/gunmagisgeek.com\/blog\/wp-content\/uploads\/2014\/01\/d3barchart000000.jpg 405w, https:\/\/gunmagisgeek.com\/blog\/wp-content\/uploads\/2014\/01\/d3barchart000000-300x103.jpg 300w\" sizes=\"auto, (max-width: 405px) 100vw, 405px\" \/><\/p>\n<p>\u6b21\u306f\u3001\u3053\u306e\u30b0\u30e9\u30d5\u3092\u7e26\u306b\u8868\u793a\u3055\u308c\u308b\u3088\u3046\u306b\u4fee\u6b63\u3057\u307e\u3059\u3002<\/p>\n<h2>SVG\u3092\u4f7f\u3063\u3066\u68d2\u30b0\u30e9\u30d5(\u7e26)\u3092\u4f5c\u6210\u3059\u308b<\/h2>\n<h3>\u7e26\u8868\u793a\u306b\u5909\u66f4\u3059\u308b<\/h3>\n<p> y\u5c5e\u6027\u306b\u6307\u5b9a\u3057\u3066\u3044\u305f\u30b3\u30fc\u30eb\u30d0\u30c3\u30af\u3068x\u5c5e\u6027\u306e\u5024\u3092\u5165\u308c\u66ff\u3048\u307e\u3059\u3002<br \/>\n\u307e\u305f\u3001width\u5c5e\u6027\u306b\u6307\u5b9a\u3057\u3066\u3044\u305f\u30b3\u30fc\u30eb\u30d0\u30c3\u30af\u3068height\u5c5e\u6027\u306e\u5024\u3092\u5165\u308c\u66ff\u3048\u307e\u3059\u3002<br \/>\n\u3053\u306e\u5909\u66f4\u306b\u3088\u3063\u3066\u3001\u5404rect\u8981\u7d20\u306b\u8a2d\u5b9a\u3055\u308c\u308b\u5024\u304c\u3001x\u5c5e\u6027(\u6a2a\u4f4d\u7f6e)\u306f\u30c7\u30fc\u30bf\u30bb\u30c3\u30c8\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u5143\u306b\u6307\u5b9a\u3055\u308c\u3001height\u5c5e\u6027(\u9ad8\u3055)\u306f\u30c7\u30fc\u30bf\u30bb\u30c3\u30c8\u306eval1\u306e\u5024\u3092\u6b63\u898f\u5316\u3057\u305f\u5024\u304c\u9069\u7528\u3055\u308c\u308b\u3088\u3046\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<pre class=\"lang:js decode:true mark:6-9\" >var barchart = svg.selectAll('rect')\r\n\t.data(dataSet)\r\n\t.enter()\r\n\t.append('rect')\r\n\t.attr({\r\n\t\tx: function(d, i){ return i * 30 },\t\/\/\u3000\u2193\u3000\u5165\u308c\u66ff\u3048\u307e\u3057\u305f\r\n\t\ty: 0,\t\t\t\t\t\t\t\t\/\/\u3000\u2191\r\n\t\twidth: 20,\t\t\t\t\t\t\t\t\t\t\/\/ \u2193 \u5165\u308c\u66ff\u3048\u307e\u3057\u305f\r\n\t\theight: function(d){ return scale(d.val1)} ,\t\/\/ \u2191\r\n\t\tfill: \"blue\"\r\n\t});<\/pre>\n<p>\u30fb\u5b9f\u884c\u7d50\u679c<br \/>\n\u30b3\u30fc\u30c9\u3092\u4fee\u6b63\u3057\u3066\u5b9f\u884c\u3057\u305f\u7d50\u679c\u304c\u4ee5\u4e0b\u3068\u306a\u308a\u307e\u3059\u3002<br \/>\n\u30b0\u30e9\u30d5\u304c\u4e0a\u304b\u3089\u4e0b\u3078\u5411\u304b\u3063\u3066\u8868\u793a\u3055\u308c\u308b\u3088\u3046\u306b\u306a\u308a\u307e\u3057\u305f\u3002<br \/>\n<img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/gunmagisgeek.com\/blog\/wp-content\/uploads\/2014\/01\/d3barchart000001.jpg\" alt=\"d3barchart000001\" width=\"270\" height=\"259\" class=\"alignnone size-full wp-image-3309\" srcset=\"https:\/\/gunmagisgeek.com\/blog\/wp-content\/uploads\/2014\/01\/d3barchart000001.jpg 270w, https:\/\/gunmagisgeek.com\/blog\/wp-content\/uploads\/2014\/01\/d3barchart000001-156x150.jpg 156w\" sizes=\"auto, (max-width: 270px) 100vw, 270px\" \/><\/p>\n<p>\u5f8c\u307b\u3069\u30b0\u30e9\u30d5\u306e\u5411\u304d\u3092\u53cd\u8ee2(\u4e0b\u304b\u3089\u4e0a\u3078\u5411\u304b\u3063\u3066\u8868\u793a)\u3055\u305b\u307e\u3059\u304c\u3001\u305d\u306e\u524d\u306b\u6b21\u306f\u76ee\u76db\u308a\u8ef8\u3092\u4f5c\u6210\u3057\u307e\u3059\u3002<\/p>\n<h2>\u76ee\u76db\u308a\u3092\u8868\u793a\u3059\u308b<\/h2>\n<h3>\u30de\u30fc\u30b8\u30f3\u3092\u8a2d\u5b9a\u3059\u308b<\/h3>\n<p>\u4eca\u56de\u306f\u76ee\u76db\u308a\u3092\u30b0\u30e9\u30d5\u306e\u5de6\u5074\u306b\u8868\u793a\u3057\u307e\u3059\u3002<br \/>\n\u3053\u306e\u307e\u307e\u3067\u306f\u3001\u76ee\u76db\u308a\u8981\u7d20\u3092\u8868\u793a\u3059\u308b\u30b9\u30da\u30fc\u30b9\u304c\u7121\u3044\u306e\u3067\u307e\u305a\u30de\u30fc\u30b8\u30f3\u3092\u8a2d\u5b9a\u3057\u307e\u3059\u3002 <\/p>\n<pre class=\"lang:js decode:true \" >var xMargin = 50;<\/pre>\n<p>\u30de\u30fc\u30b8\u30f3\u304c\u30b0\u30e9\u30d5\u306b\u53cd\u6620\u3055\u308c\u308b\u3088\u3046\u306b\u30b3\u30fc\u30c9\u3092\u4fee\u6b63\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"lang:js mark:6 decode:true \" >var barchart = svg.selectAll('rect')\r\n\t.data(dataSet)\r\n\t.enter()\r\n\t.append('rect')\r\n\t.attr({\r\n\t\tx: function(d, i){ return i * 30 + xMargin },\t\/\/\u30de\u30fc\u30b8\u30f3\u3092\u30b0\u30e9\u30d5\u306b\u53cd\u6620\r\n\t\ty: 0,\r\n\t\twidth: 20,\r\n\t\theight: function(d){ return scale(d.val1)} ,\r\n\t\tfill: \"blue\"\r\n\t});<\/pre>\n<h3>\u76ee\u76db\u308a\u3092\u4f5c\u6210\u3059\u308b<\/h3>\n<p>\u76ee\u76db\u308a\u3092\u4f5c\u6210\u3059\u308b\u306b\u306f\u3001\u307e\u305ad3.svg.axis\u30e1\u30bd\u30c3\u30c9\u3092\u4f7f\u3063\u3066axis\u30aa\u30d6\u30b8\u30a7\u30af\u30c8(yAxisCall)\u3092\u751f\u6210\u3057\u307e\u3059\u3002<br \/>\n\u305d\u306e\u5f8c\u3001svg\u8981\u7d20\u306bg\u8981\u7d20(\u30b0\u30eb\u30fc\u30d7\u8981\u7d20)\u3092\u4e00\u3064\u8ffd\u52a0\u3057\u3001call\u30e1\u30bd\u30c3\u30c9\u306b\u4f5c\u6210\u3057\u305fyAxisCall\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u6e21\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"lang:js decode:true \" >var yAxisCall = d3.svg.axis()\r\n\t.scale(scale)\t\t\/\/\u30b9\u30b1\u30fc\u30eb\u3092\u9069\u7528\u3059\u308b\r\n\t.orient('left')\t\t\/\/\u76ee\u76db\u308a\u306e\u8868\u793a\u4f4d\u7f6e\u3092\u5de6\u5074\u306b\u6307\u5b9a\r\n\r\nvar yAxis = svg.append('g')\r\n\t.attr({\r\n\t\t\"class\": \"axis\",\t\/\/class\u3092\u6307\u5b9a\u3057\u3066\u304a\u304f\r\n\t\t\"transform\": \"translate(\" + [xMargin, 0] + \")\" \r\n\t})\r\n\t.call(yAxisCall);\t\/\/yAxisCall\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u9069\u7528<\/pre>\n<p>\u30fb\u5b9f\u884c\u7d50\u679c<br \/>\n\u4fee\u6b63\u3057\u305f\u30b3\u30fc\u30c9\u3092\u5b9f\u884c\u3057\u305f\u7d50\u679c\u304c\u4ee5\u4e0b\u3068\u306a\u308a\u307e\u3059\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/gunmagisgeek.com\/blog\/wp-content\/uploads\/2014\/01\/d3barchart000002.jpg\" alt=\"d3barchart000002\" width=\"270\" height=\"259\" class=\"alignnone size-full wp-image-3310\" srcset=\"https:\/\/gunmagisgeek.com\/blog\/wp-content\/uploads\/2014\/01\/d3barchart000002.jpg 270w, https:\/\/gunmagisgeek.com\/blog\/wp-content\/uploads\/2014\/01\/d3barchart000002-156x150.jpg 156w\" sizes=\"auto, (max-width: 270px) 100vw, 270px\" \/><\/p>\n<h3>\u76ee\u76db\u308a\u3092\u30c7\u30b6\u30a4\u30f3\u3059\u308b<\/h3>\n<p>\u8868\u793a\u3055\u308c\u3066\u3044\u308b\u76ee\u76db\u308a\u3092\u30b9\u30bf\u30a4\u30eb\u30b7\u30fc\u30c8\u3092\u4f7f\u7528\u3057\u3066\u898b\u305f\u76ee\u3092\u5909\u66f4\u3057\u307e\u3059\u3002<br \/>\n\u524d\u56de\u3001attr\u30e1\u30bd\u30c3\u30c9\u3092\u4f7f\u3063\u3066\u6307\u5b9a\u3057\u305f\u30af\u30e9\u30b9\u540d\u3092\u5143\u306bpaht\u8981\u7d20\u3068line\u8981\u7d20\u306b\u30b9\u30bf\u30a4\u30eb\u3092\u9069\u7528\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"lang:css decode:true \" >.axis path, .axis line{\r\n    fill:none;\r\n    stroke:black;\r\n}\r\n.axis text {\r\n    fill:black;\r\n}<\/pre>\n<p>\u30fb\u5b9f\u884c\u7d50\u679c<br \/>\n\u4e0a\u8a18\u306e\u30b9\u30bf\u30a4\u30eb\u3092\u9069\u7528\u3057\u305f\u7d50\u679c\u304c\u4ee5\u4e0b\u3068\u306a\u308a\u307e\u3059\u3002<br \/>\n<img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/gunmagisgeek.com\/blog\/wp-content\/uploads\/2014\/01\/d3barchart000003.jpg\" alt=\"d3barchart000003\" width=\"270\" height=\"259\" class=\"alignnone size-full wp-image-3311\" srcset=\"https:\/\/gunmagisgeek.com\/blog\/wp-content\/uploads\/2014\/01\/d3barchart000003.jpg 270w, https:\/\/gunmagisgeek.com\/blog\/wp-content\/uploads\/2014\/01\/d3barchart000003-156x150.jpg 156w\" sizes=\"auto, (max-width: 270px) 100vw, 270px\" \/><\/p>\n<p>\u6b21\u306f\u3044\u3088\u3044\u3088\u3001\u30b0\u30e9\u30d5\u306e\u4e0a\u4e0b\u3092\u53cd\u8ee2\u3055\u305b\u307e\u3059\u3002<\/p>\n<h2>\u53cd\u8ee2\u3059\u308b<\/h2>\n<h3>\u30b9\u30b1\u30fc\u30eb\u3092\u53cd\u8ee2\u3055\u305b\u308b<\/h3>\n<p>\u5148\u305a\u306fscale\u95a2\u6570\u306erange(\u51fa\u529b\u7bc4\u56f2)\u3092\u53cd\u8ee2\u3057\u307e\u3059\u3002<br \/>\n\u3053\u306e\u4fee\u6b63\u306b\u3088\u3063\u3066\u5927\u304d\u3044\u5024\u306e\u30c7\u30fc\u30bf\u307b\u3069scale\u95a2\u6570\u3092\u9069\u7528\u3059\u308b\u3068\u3001\u5c0f\u3055\u3044\u6570\u5024\u306b\u6b63\u898f\u5316\u3055\u308c\u3066\u51fa\u529b\u3055\u308c\u307e\u3059\u3002<\/p>\n<pre class=\"lang:js mark:3 decode:true \" >var scale = d3.scale.linear()\r\n\t.domain([0, d3.max(dataSet, function(d){ return d.val1 })])\r\n\t.range([svgH, 0]);<\/pre>\n<p>\u30fb\u5b9f\u884c\u7d50\u679c<br \/>\nyAxisCall\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306f\u51fa\u529b\u3059\u308b\u76ee\u76db\u308a\u306e\u5024\u3092\u8aad\u307f\u8fbc\u3093\u3067\u3044\u308bscale\u95a2\u6570\u3092\u5143\u306b\u751f\u6210\u3057\u3066\u3044\u308b\u306e\u3067\u3001scale\u95a2\u6570\u3092\u4fee\u6b63\u3057\u305f\u3053\u3068\u306b\u3088\u3063\u3066\u51fa\u529b\u3055\u308c\u308b\u76ee\u76db\u308a\u3082\u53cd\u8ee2\u3055\u308c\u4e0b\u306b\u884c\u304f\u307b\u3069\u5c11\u306a\u3044\u5024\u304c\u8868\u793a\u3055\u308c\u308b\u3088\u3046\u306b\u306a\u308a\u307e\u3059\u3002<br \/>\n<img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/gunmagisgeek.com\/blog\/wp-content\/uploads\/2014\/01\/d3barchart000004.jpg\" alt=\"d3barchart000004\" width=\"270\" height=\"259\" class=\"alignnone size-full wp-image-3312\" srcset=\"https:\/\/gunmagisgeek.com\/blog\/wp-content\/uploads\/2014\/01\/d3barchart000004.jpg 270w, https:\/\/gunmagisgeek.com\/blog\/wp-content\/uploads\/2014\/01\/d3barchart000004-156x150.jpg 156w\" sizes=\"auto, (max-width: 270px) 100vw, 270px\" \/><\/p>\n<h3>\u30b0\u30e9\u30d5\u306e\u9ad8\u3055\u3092\u53cd\u8ee2\u3055\u305b\u308b<\/h3>\n<p>\u4e0a\u8a18\u306e\u5b9f\u884c\u7d50\u679c\u3092\u3088\u304f\u898b\u308b\u3068\u30b0\u30e9\u30d5\u306e\u5024\u304c\u5b9f\u969b\u306e\u30c7\u30fc\u30bf\u306e\u5024\u3068\u53cd\u5bfe\u306b\u306a\u3063\u3066\u3044\u308b\u3053\u3068\u306b\u6c17\u4ed8\u304f\u3068\u601d\u3044\u307e\u3059\u3002<br \/>\n\u305d\u3053\u3067\u3001attr\u30e1\u30bd\u30c3\u30c9\u3067\u9069\u7528\u3057\u3066\u3044\u308brect\u8981\u7d20\u306eheight\u5c5e\u6027\u3092\u300c\u6b63\u898f\u5316\u3057\u305f\u30c7\u30fc\u30bf\u306e\u5024\u300d\u304b\u3089\u300c\u30b9\u30c6\u30fc\u30b8\u306e\u9ad8\u3055\uff0d\u6b63\u898f\u5316\u3057\u305f\u30c7\u30fc\u30bf\u306e\u5024\u300d\u306b\u5909\u66f4\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"lang:js mark:9 decode:true \" >var barchart = svg.selectAll('rect')\r\n\t.data(dataSet)\r\n\t.enter()\r\n\t.append('rect')\r\n\t.attr({\r\n\t\tx: function(d, i){ return i * 30 + xMargin },\r\n\t\ty: 0,\r\n\t\twidth: 20,\r\n\t\theight: function(d){ return svgH - scale(d.val1)} ,\t\/\/(\u30b9\u30c6\u30fc\u30b8\u306e\u9ad8\u3055\uff0d\u6b63\u898f\u5316\u3057\u305f\u30c7\u30fc\u30bf\u306e\u5024)\u3092\u53cd\u6620\r\n\t\tfill: \"blue\"\r\n\t});<\/pre>\n<p>\u30fb\u5b9f\u884c\u7d50\u679c<br \/>\n\u4fee\u6b63\u3057\u305f\u30b3\u30fc\u30c9\u3092\u5b9f\u884c\u3057\u305f\u7d50\u679c\u304c\u4ee5\u4e0b\u3067\u3059\u3002<br \/>\n<img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/gunmagisgeek.com\/blog\/wp-content\/uploads\/2014\/01\/d3barchart000005.jpg\" alt=\"d3barchart000005\" width=\"270\" height=\"259\" class=\"alignnone size-full wp-image-3313\" srcset=\"https:\/\/gunmagisgeek.com\/blog\/wp-content\/uploads\/2014\/01\/d3barchart000005.jpg 270w, https:\/\/gunmagisgeek.com\/blog\/wp-content\/uploads\/2014\/01\/d3barchart000005-156x150.jpg 156w\" sizes=\"auto, (max-width: 270px) 100vw, 270px\" \/><\/p>\n<h3>\u30b0\u30e9\u30d5\u306e\u8868\u793a\u4f4d\u7f6e(\u7e26)\u3092\u53cd\u8ee2\u3055\u305b\u308b<\/h3>\n<p>\u6700\u5f8c\u306e\u4ed5\u4e0a\u3052\u3068\u3057\u3066\u3001\u5404rect\u8981\u7d20\u306ey\u5c5e\u6027\u3092\u8abf\u6574\u3057\u30b0\u30e9\u30d5\u3092\u4e0b\u65b9\u5411\u306b\u63c3\u3048\u307e\u3059\u3002<\/p>\n<pre class=\"lang:js mark:7 decode:true \" >var barchart = svg.selectAll('rect')\r\n\t.data(dataSet)\r\n\t.enter()\r\n\t.append('rect')\r\n\t.attr({\r\n\t\tx: function(d, i){ return i * 30 + xMargin },\r\n\t\ty: function(d){ return scale(d.val1)},\t\t\/\/\u7e26\u4f4d\u7f6e\u3092\u4e0b\u65b9\u5411\u306b\u63c3\u3048\u308b\r\n\t\twidth: 20,\r\n\t\theight: function(d){ return svgH - scale(d.val1)} ,\r\n\t\tfill: \"blue\"\r\n\t});<\/pre>\n<p>\u30fb\u5b9f\u884c\u7d50\u679c<br \/>\n\u3053\u308c\u3067\u3072\u3068\u307e\u305a\u68d2\u30b0\u30e9\u30d5(\u7e26)\u306e\u5b8c\u6210\u3067\u3059<br \/>\n<img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/gunmagisgeek.com\/blog\/wp-content\/uploads\/2014\/01\/d3barchart000006.jpg\" alt=\"d3barchart000006\" width=\"270\" height=\"259\" class=\"alignnone size-full wp-image-3314\" srcset=\"https:\/\/gunmagisgeek.com\/blog\/wp-content\/uploads\/2014\/01\/d3barchart000006.jpg 270w, https:\/\/gunmagisgeek.com\/blog\/wp-content\/uploads\/2014\/01\/d3barchart000006-156x150.jpg 156w\" sizes=\"auto, (max-width: 270px) 100vw, 270px\" \/><\/p>\n<h2>\u52d5\u7684\u306b\u30b0\u30e9\u30d5\u3092\u5909\u5316\u3055\u305b\u308b<\/h2>\n<p>\u3064\u3044\u3067\u306a\u306e\u3067\u3001\u30a4\u30d9\u30f3\u30c8\u5b9f\u884c\u6642\u306b\u30b0\u30e9\u30d5\u3092\u52d5\u7684\u306b\u5909\u5316\u3059\u308b\u3088\u3046\u306b\u3057\u3066\u307f\u307e\u3059\u3002<br \/>\n\u4eca\u56de\u306f\u3001body\u8981\u7d20\u4e0a\u3067\u30af\u30ea\u30c3\u30af\u3055\u308c\u305f\u3089\u3001\u8868\u793a\u3059\u308b\u30b0\u30e9\u30d5\u3092val1\u304b\u3089val2\u306e\u30b0\u30e9\u30d5\u3078\u3068\u5909\u5316\u3055\u305b\u3066\u307f\u307e\u3059\u3002<\/p>\n<h3>\u30a4\u30d9\u30f3\u30c8\u3092\u8a2d\u7f6e\u3059\u308b<\/h3>\n<p>\u307e\u305a\u306f\u3001body\u8981\u7d20\u306b\u30a4\u30d9\u30f3\u30c8\u3092\u8a2d\u7f6e\u3057\u30a2\u30e9\u30fc\u30c8\u304c\u6b63\u3057\u304f\u5b9f\u884c\u3055\u308c\u308b\u304b\u30c6\u30b9\u30c8\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"lang:js decode:true \" >d3.select('body').on('click', function(){\r\n\talert('test');\r\n});<\/pre>\n<p>\u6b63\u5e38\u306b\u52d5\u4f5c\u3059\u308b\u3053\u3068\u304c\u78ba\u8a8d\u3067\u304d\u305f\u3089alert\u306f\u524a\u9664\u3057\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n<h3>\u30b9\u30b1\u30fc\u30eb\u3092\u66f4\u65b0\u3059\u308b<\/h3>\n<p>\u6b63\u898f\u5316\u3059\u308b\u5bfe\u8c61\u3092\u30c7\u30fc\u30bf\u30bb\u30c3\u30c8\u306eval1\u304b\u3089val2\u3078\u3068\u5909\u66f4\u3059\u308b\u305f\u3081\u3001\u30b9\u30b1\u30fc\u30eb\u306e\u9069\u7528\u7bc4\u56f2\u3092\u300c\u30bc\u30ed\u304b\u3089val2\u306e\u6700\u5927\u5024\u300d\u3078\u3068\u66f4\u65b0\u3057\u307e\u3059\u3002 <\/p>\n<pre class=\"lang:js mark:2 decode:true \" >d3.select('body').on('click', function(){\r\n\tscale.domain([0, d3.max(dataSet, function(d){ return d.val2 })]);\t\/\/\u6b63\u898f\u5316\u3055\u308c\u308b\u5024\u306e\u7bc4\u56f2\u3092\u6307\u5b9a(0\u301cval2\u306e\u6700\u5927\u5024)\r\n});<\/pre>\n<h3>\u76ee\u76db\u308a\u3092\u66f4\u65b0\u3059\u308b<\/h3>\n<p>\u3055\u3089\u306b\u3001\u76ee\u76db\u308a\u306e\u8868\u8a18\u3092val1\u306b\u5bfe\u3059\u308b\u3082\u306e\u304b\u3089val2\u306b\u5408\u308f\u305b\u305f\u3082\u306e\u3078\u5909\u66f4\u3059\u308b\u306e\u305f\u3081\u306b\u3001xAxis\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306b\u518d\u5ea6yAxisCal\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u6e21\u3057\u3066\u66f4\u65b0\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"lang:js mark:3 decode:true \" >d3.select('body').on('click', function(){\r\n\tscale.domain([0, d3.max(dataSet, function(d){ return d.val2 })]);\r\n\tyAxis.call(yAxisCall);\r\n});<\/pre>\n<h3>\u68d2\u30b0\u30e9\u30d5\u3092\u66f4\u65b0\u3059\u308b<\/h3>\n<p>\u6700\u5f8c\u306b\u5404rect\u8981\u7d20\u306ey\u5c5e\u6027\u5024\u30fbheight\u5c5e\u6027\u3092\u66f4\u65b0\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"lang:js mark:4-7 decode:true \" >d3.select('body').on('click', function(){\r\n\tscale.domain([0, d3.max(dataSet, function(d){ return d.val2 })]);\r\n\tyAxis.call(yAxisCall);\r\n\tbarchart.attr({\r\n\t\ty: function(d){ return scale(d.val2)},\r\n\t\theight: function(d){ return svgH - scale(d.val2)} , \r\n\t});\r\n});<\/pre>\n<p>\u30fb\u5b9f\u884c\u7d50\u679c<br \/>\n\u30af\u30ea\u30c3\u30af\u30a4\u30d9\u30f3\u30c8\u767a\u751f\u5f8c\u306e\u5b9f\u884c\u7d50\u679c\u304c\u4ee5\u4e0b\u3067\u3059\u3002<br \/>\n\u30b0\u30e9\u30d5\u306e\u5185\u5bb9\u304c\u30c7\u30fc\u30bf\u30bb\u30c3\u30c8\u306eval2\u306e\u5024\u3092\u5143\u306b\u3057\u305f\u3082\u306e\u306b\u5909\u66f4\u3055\u308c\u3001\u76ee\u76db\u308a\u3082\u305d\u308c\u306b\u5408\u308f\u305b\u305f\u5024\u306b\u5909\u66f4\u3055\u308c\u3066\u3044\u307e\u3059\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/gunmagisgeek.com\/blog\/wp-content\/uploads\/2014\/01\/d3barchart000008.jpg\" alt=\"d3barchart000008\" width=\"270\" height=\"259\" class=\"alignnone size-full wp-image-3315\" srcset=\"https:\/\/gunmagisgeek.com\/blog\/wp-content\/uploads\/2014\/01\/d3barchart000008.jpg 270w, https:\/\/gunmagisgeek.com\/blog\/wp-content\/uploads\/2014\/01\/d3barchart000008-156x150.jpg 156w\" sizes=\"auto, (max-width: 270px) 100vw, 270px\" \/><\/p>\n<h3>\u30a2\u30cb\u30e1\u30fc\u30b7\u30e7\u30f3\u3055\u305b\u308b<\/h3>\n<p>val1\u30b0\u30e9\u30d5\u304b\u3089val2\u30b0\u30e9\u30d5\u3078\u306e\u5909\u66f4\u306e\u969b\u306b\u30a2\u30cb\u30e1\u30fc\u30b7\u30e7\u30f3\u3055\u308c\u308b\u3088\u3046\u306b\u30b3\u30fc\u30c9\u3092\u4fee\u6b63\u3057\u307e\u3059\u3002<br \/>\ntransition\u30e1\u30bd\u30c3\u30c9\u3092\u9593\u306b\u631f\u3080\u3060\u3051\u306a\u306e\u3067\u7c21\u5358\u3067\u3059\u3002 <\/p>\n<pre class=\"lang:js decode:true \" >d3.select('body').on('click', function(){\r\n\tscale.domain([0, d3.max(dataSet, function(d){ return d.val2 })]);\r\n\tyAxis.transition().call(yAxisCall);\t\/\/\u76ee\u76db\u308a\u8868\u793a\u66f4\u65b0\u6642\u306b\u30a2\u30cb\u30e1\u30fc\u30b7\u30e7\u30f3\u3055\u305b\u308b\r\n\tbarchart\r\n\t\t.transition()\t\/\/\u30b0\u30e9\u30d5\u8868\u793a\u66f4\u65b0\u6642\u306b\u30a2\u30cb\u30e1\u30fc\u30b7\u30e7\u30f3\u3055\u305b\u308b\r\n\t\t.attr({\r\n\t\t\ty: function(d){ return scale(d.val2)},\r\n\t\t\theight: function(d){ return svgH - scale(d.val2)} , \r\n\t\t});\r\n});<\/pre>\n<p>\u4ee5\u4e0a\u3067\u3059\u3002<br \/>\n\u6b21\u56de\u306f\u3001\u6298\u308c\u7dda\u30b0\u30e9\u30d5\u3092\u304a\u3055\u3089\u3044\u3057\u307e\u3059\u3002<\/p>\n<hr \/>\n<p><strong>\u30b5\u30f3\u30d7\u30eb\u30b3\u30fc\u30c9<\/strong><\/p>\n<pre class=\"lang:js decode:true \" >var dataSet = [\r\n\t{name:\"a\", val1: 1000, val2: 500 },\r\n\t{name:\"a\", val1: 2000, val2: 400 },\r\n\t{name:\"a\", val1: 3000, val2: 300 },\r\n\t{name:\"a\", val1: 4000, val2: 200 },\r\n\t{name:\"a\", val1: 5000, val2: 100 }  \r\n];\r\n\r\nvar svgW = 300; \t\/\/\u30b9\u30c6\u30fc\u30b8\u306e\u5e45\r\nvar svgH = 200;\t\/\/\u30b9\u30c6\u30fc\u30b8\u306e\u9ad8\u3055\r\nvar xMargin = 50; \/\/\u30de\u30fc\u30b8\u30f3\r\n\r\nvar svg = d3.select('body')\r\n\t.append('svg') \/\/svg\u8981\u7d20\u3092body\u8981\u7d20\u306e\u4e2d\u306b\u8ffd\u52a0\r\n\t.attr({\r\n\t\twidth: svgW,\r\n\t\theight: svgH,\r\n\t});\r\n\r\nvar scale = d3.scale.linear()\r\n\t.domain([0, d3.max(dataSet, function(d){ return d.val1 })])\t\/\/\u6b63\u898f\u5316\u3055\u308c\u308b\u5024\u306e\u7bc4\u56f2\u3092\u6307\u5b9a(0\u301cval1\u306e\u6700\u5927\u5024)\r\n\t.range([svgH, 0]);\t\/\/\u51fa\u529b\u5024\u306e\u7bc4\u56f2\u3092\u6307\u5b9a(\u30b9\u30c6\u30fc\u30b8\u306e\u9ad8\u3055\u301c0)\r\n\r\nvar barchart = svg.selectAll('rect')\r\n\t.data(dataSet)\r\n\t.enter()\r\n\t.append('rect')\r\n\t.attr({\r\n\t\tx: function(d, i){ return i * 30 + xMargin },\t\/\/\u30de\u30fc\u30b8\u30f3\u3092\u30b0\u30e9\u30d5\u306b\u53cd\u6620\r\n\t\ty: function(d){ return scale(d.val1)},\t\t\/\/\u7e26\u4f4d\u7f6e\u3092\u4e0b\u65b9\u5411\u306b\u63c3\u3048\u308b\r\n\t\twidth: 20,\r\n\theight: function(d){ return svgH - scale(d.val1)} ,\t\/\/(\u30b9\u30c6\u30fc\u30b8\u306e\u9ad8\u3055\uff0d\u6b63\u898f\u5316\u3057\u305f\u30c7\u30fc\u30bf\u306e\u5024)\u3092\u53cd\u6620\r\n\t\tfill: \"blue\"\r\n\t});\r\n\r\nvar yAxisCall = d3.svg.axis()\r\n\t.scale(scale)\t\t\/\/\u30b9\u30b1\u30fc\u30eb\u3092\u9069\u7528\u3059\u308b\r\n\t.orient('left')\t\t\/\/\u76ee\u76db\u308a\u306e\u8868\u793a\u4f4d\u7f6e\u3092\u5de6\u5074\u306b\u6307\u5b9a\r\n\r\nvar yAxis = svg.append('g')\r\n\t.attr({\r\n\t\t\"class\": \"axis\",\t\/\/class\u3092\u6307\u5b9a\u3057\u3066\u304a\u304f\r\n\t\t\"transform\": \"translate(\" + [xMargin, 0] + \")\" \r\n\t})\r\n\t.call(yAxisCall);\t\/\/yAxisCall\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u9069\u7528\r\n\r\nd3.select('body').on('click', function(){\r\n\tscale.domain([0, d3.max(dataSet, function(d){ return d.val2 })]);\r\n\tyAxis.transition().call(yAxisCall);\t\/\/\u76ee\u76db\u308a\u8868\u793a\u66f4\u65b0\u6642\u306b\u30a2\u30cb\u30e1\u30fc\u30b7\u30e7\u30f3\u3055\u305b\u308b\r\n\tbarchart\r\n\t\t.transition()\t\/\/\u30b0\u30e9\u30d5\u8868\u793a\u66f4\u65b0\u6642\u306b\u30a2\u30cb\u30e1\u30fc\u30b7\u30e7\u30f3\u3055\u305b\u308b\r\n\t\t.attr({\r\n\t\t\ty: function(d){ return scale(d.val2)},\r\n\t\t\theight: function(d){ return svgH - scale(d.val2)} , \r\n\t\t});\r\n});\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u6628\u5e74\u672b\u3001\u30d6\u30ed\u30b0\u30ec\u30a4\u30a2\u30a6\u30c8\u306e\u30ea\u30cb\u30e5\u30fc\u30a2\u30eb\u306e\u305f\u3081\u306b\u53bb\u5e74&hellip;<\/p>\n","protected":false},"author":1,"featured_media":3314,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18],"tags":[],"class_list":["post-3307","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-d3-js","has-post-thumbnail-archive"],"_links":{"self":[{"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/posts\/3307","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=3307"}],"version-history":[{"count":0,"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/posts\/3307\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/media\/3314"}],"wp:attachment":[{"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/media?parent=3307"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/categories?post=3307"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/tags?post=3307"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}