<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Love web applications &#187; 棒グラフ</title>
	<atom:link href="http://blog.hitsuji.me/tag/bar-graph/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.hitsuji.me</link>
	<description>Webアプリつくるの楽しい！つくるネタ、使うネタ、あとは雑談です。</description>
	<lastBuildDate>Sun, 25 Jan 2015 12:49:45 +0000</lastBuildDate>
	<language>ja</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=4.1.41</generator>
	<item>
		<title>横浜市人口棒グラフに目盛りを追加する</title>
		<link>http://blog.hitsuji.me/add-y-axis-to-bar-graph-using-d3-js/</link>
		<comments>http://blog.hitsuji.me/add-y-axis-to-bar-graph-using-d3-js/#comments</comments>
		<pubDate>Mon, 14 Jul 2014 13:02:27 +0000</pubDate>
		<dc:creator><![CDATA[kapibarawebmaster1515]]></dc:creator>
				<category><![CDATA[可視化]]></category>
		<category><![CDATA[D3.js]]></category>
		<category><![CDATA[棒グラフ]]></category>

		<guid isPermaLink="false">http://blog.hitsuji.me/?p=123</guid>
		<description><![CDATA[こんばんは。だんだん気候が夏に近づいてきましたね。私は暑いのが苦手なので、エアコンに頼りまくりです^^; &#160; 棒グラフに縦軸を追加する 今日は前回の記事「D3.jsで横浜市の人口棒グラフを表・・・<a class="readon" href="http://blog.hitsuji.me/add-y-axis-to-bar-graph-using-d3-js/">続きを読む</a>]]></description>
				<content:encoded><![CDATA[<p>こんばんは。だんだん気候が夏に近づいてきましたね。私は暑いのが苦手なので、エアコンに頼りまくりです^^;</p>
<p>&nbsp;</p>
<h3>棒グラフに縦軸を追加する</h3>
<p>今日は前回の記事「D3.jsで横浜市の人口棒グラフを表示する」で描いた棒グラフについて、縦軸を追加することにします。</p>
<p>&nbsp;</p>
<h4>スケール追加はカンタン</h4>
<p>d3.scale()関数を使えばスケールを簡単に作れるみたいです。実装してみた結果が下のグラフです（画像をクリックすると、グラフのページに飛びます♪）。</p>
<p>&nbsp;</p>
<p><a href="http://static.hitsuji.me/stat-yokohama02/"><img class="alignnone wp-image-125 size-large_for_2x" src="http://blog.hitsuji.me/app/wp-content/uploads/2014/07/yokohama_stat02-1200x666.png" alt="yokohama_stat02" width="600" height="333" /></a></p>
<p>&nbsp;</p>
<p>ソースはこちら。軸を描くコードはたった３行です。</p>
<h6>index.html</h6>
<script src="https://gist.github.com/230711fe512df0da62a0.js"></script><noscript><pre><code class="language-html html">&lt;!DOCTYPE html&gt;
&lt;html lang=&quot;ja&quot;&gt;
&lt;head&gt;
&lt;meta charset=&quot;utf-8&quot;&gt;
&lt;script src=&quot;http://d3js.org/d3.v3.min.js&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;http://d3js.org/topojson.v0.min.js&quot;&gt;&lt;/script&gt;
&lt;style type=&quot;text/css&quot;&gt;
body {
    font-family: 'Lucida Grande','Hiragino Kaku Gothic ProN', Meiryo, sans-serif;
}
h1 {
    font-size: 16px;
}
svg {
    border: solid 1px #aaa;
}
.bar {
    fill: #004E9E;
}
.bar-label {
    fill: #ffffff;
}
.axis path, .axis line {
    fill: none;
    stroke: #000;
    shape-rendering: crispEdges;
}
.axis text {
    font-size: 9px;
}
&lt;/style&gt;
&lt;title&gt;横浜市区別人口&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;h1&gt;横浜市区別人口[千人]&lt;/h1&gt;
&lt;div id=&quot;graph&quot;&gt;&lt;/div&gt;


&lt;script type=&quot;text/javascript&quot;&gt;

    d3.json(&quot;yokohama_stat.json&quot;, function(error, json) {
        if( !error ) {
            main(json);
        }
        return;
    });
    
    function main(stat) {
        
        var svg;
        var graph_width = 800;
        var graph_height = 400;
        var graph_padding = 20;
        var xaxis_margin = 0;
        var yaxis_margin = 25;
        var plotarea_width = graph_width - yaxis_margin - 2 * graph_padding;
        var plotarea_height = graph_height - xaxis_margin - 2 * graph_padding;
        var origin_dx = yaxis_margin + graph_padding;
        var origin_dy = plotarea_height + graph_padding;
        
        var bar_width = 30;
        var bar_space = 10;
        var bar_pitch = bar_width + bar_space;
        var label_padding = 10;
        
        // svgを追加
        svg = d3.select(&quot;#graph&quot;).append(&quot;svg&quot;)
                .attr(&quot;width&quot;, graph_width)
                .attr(&quot;height&quot;, graph_height);
        
        // 横浜市TTLの値を配列から削除する
        stat.splice(0,1);
        
        // 棒グラフを描く
        svg.selectAll(&quot;rect.bar&quot;)
                .data(stat)
                .enter().append(&quot;rect&quot;)
                .attr(&quot;class&quot;,&quot;bar&quot;)
                    .attr(&quot;x&quot;, function(d,i) { return origin_dx + bar_pitch * i + bar_space; })
                    .attr(&quot;y&quot;, function(d,i) { return origin_dy - d.stat['TTL']/1000; })
                    .attr(&quot;width&quot;, bar_width)
                    .attr(&quot;height&quot;, function(d,i) { return d.stat['TTL']/1000; });
                    
        svg.selectAll(&quot;rect.bar&quot;)
                .data(stat)
                .exit()
                .remove();
        
        // ラベルを描く
        svg.selectAll(&quot;.bar-label&quot;)
            .data(stat)
          .enter()
            .append(&quot;text&quot;)
                .attr(&quot;class&quot;, function(d) {
                    return &quot;bar-label&quot;;
                    })
                .attr(&quot;transform&quot;, function(d, i) {
                    return &quot;translate(&quot;+ ( origin_dx + bar_pitch*i + bar_space + bar_width/2) +&quot;,&quot;+ (origin_dy - label_padding) +&quot;)rotate(-90)&quot;;
                 })
                .attr(&quot;dy&quot;, &quot;.35em&quot;)
                .text(function(d) {
                    return d.key;
                });
        svg.selectAll(&quot;rect.bar-label&quot;)
                .data(stat)
                .exit()
                .remove();
        
        // 縦軸を追加する
        var scale = d3.scale.linear().domain([plotarea_height ,0]).range([0, plotarea_height]);
        var axis = d3.svg.axis().scale(scale).orient(&quot;left&quot;).ticks(5);
        svg.append(&quot;g&quot;).attr(&quot;class&quot;,&quot;axis&quot;).attr(&quot;transform&quot;,function(){ return &quot;translate(&quot;+origin_dx+&quot;,&quot;+graph_padding+&quot;)&quot;; }).call(axis);
    
    }
&lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;</code></pre></noscript>
<p>&nbsp;</p>
<p>これで縦軸のスケールが描けました。</p>
<p>ざっくりですが、<a title="横浜市区別人口" href="http://kashika.net/stat-yokohama02/" target="_blank">グラフ</a>の値と<a title="横浜市統計ポータルサイト" href="http://www.city.yokohama.lg.jp/ex/stat/jinko/age/new/age-j.html" target="_blank">元の表</a>の値が合ってることが確認できました♥︎</p>
<p>&nbsp;</p>
<p>次はこの人口データを、以前描いた横浜市地図上で表現することを考えてみようと思います。</p>
<p>それではまた♪</p>
<p>&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.hitsuji.me/add-y-axis-to-bar-graph-using-d3-js/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>D3.jsで横浜市の人口棒グラフを表示する</title>
		<link>http://blog.hitsuji.me/show-bar-graph-of-yokohama-stat-using-d3-js/</link>
		<comments>http://blog.hitsuji.me/show-bar-graph-of-yokohama-stat-using-d3-js/#comments</comments>
		<pubDate>Fri, 11 Jul 2014 02:02:57 +0000</pubDate>
		<dc:creator><![CDATA[kapibarawebmaster1515]]></dc:creator>
				<category><![CDATA[可視化]]></category>
		<category><![CDATA[D3.js]]></category>
		<category><![CDATA[棒グラフ]]></category>

		<guid isPermaLink="false">http://blog.hitsuji.me/?p=117</guid>
		<description><![CDATA[こんにちは。今朝は台風が通過する予報だったのでドキドキして起床しましたが、予想外にも快晴でした。 これがいわゆる台風一過でしょうか。とっても良いお天気です♥︎ &#160; 人口を棒グラフで可視化する・・・<a class="readon" href="http://blog.hitsuji.me/show-bar-graph-of-yokohama-stat-using-d3-js/">続きを読む</a>]]></description>
				<content:encoded><![CDATA[<p>こんにちは。今朝は台風が通過する予報だったのでドキドキして起床しましたが、予想外にも快晴でした。</p>
<p>これがいわゆる台風一過でしょうか。とっても良いお天気です♥︎</p>
<p>&nbsp;</p>
<h3>人口を棒グラフで可視化する</h3>
<p>さて、今日は<a title="Perlで横浜市の区別年齢別人口データを自動入手する" href="http://blog.hitsuji.me/get-stat-of-yokohama-city-population-automatically/">前回の記事「Perlで横浜市の区別年齢別人口データを自動入手する」</a>でWebから入手した横浜市の人口データを棒グラフにしてみたいと思います。</p>
<p>&nbsp;</p>
<h4>D3.jsで棒グラフを描画してみる</h4>
<p>早速グラフを描いてみました。</p>
<p>&nbsp;</p>
<p><a title="tutorials" href="http://ja.d3js.info/alignedleft/tutorials/d3/" target="_blank">D3.jsのチュートリアル</a>などをみると、divを使う方法とsvgを使う方法がありますが、このグラフはsvgを使って描いています。画像をクリックすると、グラフページに飛びます♪</p>
<p>&nbsp;</p>
<p><a href="http://static.hitsuji.me/stat-yokohama01/"><img class="alignnone wp-image-118 size-large_for_2x" src="http://blog.hitsuji.me/app/wp-content/uploads/2014/07/yokohama_stat01-1200x668.png" alt="yokohama_stat01" width="600" height="334" /></a></p>
<p>&nbsp;</p>
<p>コードは次の通りです。</p>
<h6>index.html</h6>
<script src="https://gist.github.com/c48bea307f9755fa254e.js"></script><noscript><pre><code class="language-html html">&lt;!DOCTYPE html&gt;
&lt;html lang=&quot;ja&quot;&gt;
&lt;head&gt;
&lt;meta charset=&quot;utf-8&quot;&gt;
&lt;script src=&quot;http://d3js.org/d3.v3.min.js&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;http://d3js.org/topojson.v0.min.js&quot;&gt;&lt;/script&gt;
&lt;style type=&quot;text/css&quot;&gt;
body {
    font-family: 'Lucida Grande','Hiragino Kaku Gothic ProN', Meiryo, sans-serif;
}
h1 {
    font-size: 16px;
}
svg {
    border: solid 1px #aaa;
}
.bar {
    fill: #004E9E;
}
.bar-label {
    fill: #ffffff;
}
&lt;/style&gt;
&lt;title&gt;横浜市区別人口&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;h1&gt;横浜市区別人口&lt;/h1&gt;
&lt;div id=&quot;graph&quot;&gt;&lt;/div&gt;


&lt;script type=&quot;text/javascript&quot;&gt;
    var graph_width = 800;
    var graph_height = 400;
    var label_padding = 10;
    var svg;
    
    d3.json(&quot;yokohama_stat.json&quot;, function(error, json) {
        if( !error ) {
            main(json);
        }
        return;
    });
    
    function main(stat) {
        
        var bar_width = 30;
        var bar_space = 10;
        var bar_pitch = bar_width + bar_space;
        
        // svgを追加
        svg = d3.select(&quot;#graph&quot;).append(&quot;svg&quot;)
                .attr(&quot;width&quot;, graph_width)
                .attr(&quot;height&quot;, graph_height);
        
        // 横浜市TTLの値を配列から削除する
        stat.splice(0,1);
        
        // 棒グラフを描く
        svg.selectAll(&quot;rect.bar&quot;)
                .data(stat)
                .enter().append(&quot;rect&quot;)
                .attr(&quot;class&quot;,&quot;bar&quot;)
                    .attr(&quot;x&quot;, function(d,i) { return bar_pitch * i + bar_space; })
                    .attr(&quot;y&quot;, function(d,i) { return graph_height - d.stat['TTL']/1000; })
                    .attr(&quot;width&quot;, bar_width)
                    .attr(&quot;height&quot;, function(d,i) { return d.stat['TTL']/1000; });
                    
        svg.selectAll(&quot;rect.bar&quot;)
                .data(stat)
                .exit()
                .remove();
        
        // ラベルを描く
        svg.selectAll(&quot;.bar-label&quot;)
            .data(stat)
          .enter()
            .append(&quot;text&quot;)
                .attr(&quot;class&quot;, function(d) {
                    return &quot;bar-label&quot;;
                    })
                .attr(&quot;transform&quot;, function(d, i) {
                    return &quot;translate(&quot;+ ( bar_pitch*i + bar_space + bar_width/2) +&quot;,&quot;+ (graph_height - label_padding) +&quot;)rotate(-90)&quot;;
                 })
                .attr(&quot;dy&quot;, &quot;.35em&quot;)
                .text(function(d) {
                    return d.key;
                });
        svg.selectAll(&quot;rect.bar-label&quot;)
                .data(stat)
                .exit()
                .remove();
    }
&lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;</code></pre></noscript>
<p>&nbsp;</p>
<p>L56は、読み込んだJSONファイルの0番目の要素に横浜市全体の人口データが入っており、グラフから削除するためにカットしています。</p>
<pre class="command">stat.splice(0,1);</pre>
<p>&nbsp;</p>
<p>L59から棒グラフを描画、</p>
<p>L72から区名を描画する処理を記述しています。</p>
<p>&nbsp;</p>
<p>L82でラベルを-90度回転させる処理は、回転基準アンカーを文字の位置にしたいので、transrate()rotate()で指定しております。（x,yで文字の位置を指定する書き方だと、回転基準が原点0,0になってしまう=&gt;<a title="rotate-x-axis-text-in-d3" href="http://stackoverflow.com/questions/11252753/rotate-x-axis-text-in-d3" target="_blank">stackoverflow</a>）</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>棒グラフが描けました♥︎</p>
<p>データを自動収集するスクリプトと連動させれば、人口グラフを自動更新するサイトが作れそうですね。</p>
<p>&nbsp;</p>
<p>でも・・・このままだと縦軸に単位が入っていないので値が不明です（^^;</p>
<p>次回は、縦方向の目盛りを追加していきます。</p>
<p>&nbsp;</p>
<p>それでは、よい一日をお過ごしください！</p>
<p>&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.hitsuji.me/show-bar-graph-of-yokohama-stat-using-d3-js/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
