{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Chapter3\n", "D3.js in Actionの3章の勉強ノートです。\n", "\n", "KernelはPython2を使用します。" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "loaded nvd3 IPython extension\n", "run nvd3.ipynb.initialize_javascript() to set up the notebook\n", "help(nvd3.ipynb.initialize_javascript) for options\n" ] }, { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/javascript": [ "$.getScript(\"https://cdnjs.cloudflare.com/ajax/libs/nvd3/1.7.0/nv.d3.min.js\")" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/javascript": [ "$.getScript(\"https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js\", function() {\n", " $.getScript(\"https://cdnjs.cloudflare.com/ajax/libs/nvd3/1.7.0/nv.d3.min.js\", function() {})});" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "%load_ext sage\n", "from IPython.core.display import HTML\n", "from string import Template\n", "import json\n", "import nvd3\n", "nvd3.ipynb.initialize_javascript(use_remote=True)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## データの可視化\n", "3章では、svgの図形の他にHTMLページや画像を表示する方法を説明しています。\n", "\n", "### W杯のデータ\n", "例題に使用しているのは、以下のようなW杯のデータです。" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Overwriting data/worldcup.csv\n" ] } ], "source": [ "%%writefile data/worldcup.csv\n", "\"team\",\"region\",\"win\",\"loss\",\"draw\",\"points\",\"gf\",\"ga\",\"cs\",\"yc\",\"rc\"\n", "\"Netherlands\",\"UEFA\",6,0,1,18,12,6,2,23,1\n", "\"Spain\",\"UEFA\",6,0,1,18,8,2,5,8,0\n", "\"Germany\",\"UEFA\",5,0,2,15,16,5,3,10,1\n", "\"Argentina\",\"CONMEBOL\",4,0,1,12,10,6,2,8,0\n", "\"Uruguay\",\"CONMEBOL\",3,2,2,11,11,8,3,13,2\n", "\"Brazil\",\"CONMEBOL\",3,1,1,10,9,4,2,9,2\n", "\"Ghana\",\"CAF\",2,2,1,8,5,4,1,12,0\n", "\"Japan\",\"AFC\",2,1,1,7,4,2,2,4,0\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### CSSの定義\n", "最初に使用するCSSとHTMLを定義します。" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Overwriting css/d3ia.css\n" ] } ], "source": [ "%%writefile css/d3ia.css\n", "text {\n", " font-size: 10px;\n", "}\n", "g > text.active {\n", " font-size: 30px;\n", "}\n", "circle {\n", " fill: pink;\n", " stroke: black;\n", " stroke-width: 1px;\n", "}\n", "circle.active {\n", " fill: red;\n", "}\n", "circle.inactive {\n", " fill: gray;\n", "}" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\n", "\n", "\n", "
\n", "
\n", " \n", "
\n", "
\n", "
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "%%HTML\n", "\n", "\n", "\n", "\n", "\n", "\n", "
\n", "
\n", " \n", "
\n", "
\n", "
\n" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "data": { "application/javascript": [ "d3.csv(\"data/worldcup.csv\", function(data) {\n", " overallTeamViz('#ex1', data);\n", "});\n", "\n", "function overallTeamViz(topTag, incomingData) {\n", " // 中央に表示用のキャンバス用g(teamG)と出場国用g(overallG)を作成\n", " createFrame(topTag, incomingData);\n", " \n", " var teamG = d3.select(topTag).selectAll(\"g.overallG\");\n", " // 出場国のCircleとチーム名を表示\n", " createTeamCircle(teamG);\n", " // ボタンアクションをバインディング\n", " bindButtons(topTag, incomingData);\n", " // mouseアクションをバインディング\n", " bindMouseAction(topTag, teamG);\n", "}\n", "\n", "\n", "function createFrame(topTag, incomingData) {\n", " d3.select(topTag).select(\"svg\")\n", " .append(\"g\")\n", " .attr(\"id\", \"teamsG\")\n", " .attr(\"transform\", \"translate(50,300)\")\n", " .selectAll(\"g\")\n", " .data(incomingData)\n", " .enter()\n", " .append(\"g\")\n", " .attr(\"class\", \"overallG\")\n", " .attr(\"transform\", function (d,i) {return \"translate(\" + (i * 50) + \", 0)\"});\n", "}\n", "\n", "function createTeamCircle(teamG) {\n", " // 各チームのCircleを作成\n", " teamG\n", " .append(\"circle\").attr(\"r\", 0)\n", " .transition()\n", " .delay(function(d,i) {return i * 100})\n", " .duration(500)\n", " .attr(\"r\", 40)\n", " .transition()\n", " .duration(500)\n", " .attr(\"r\", 20); \n", " // チームタイトルを表示\n", " teamG\n", " .append(\"text\")\n", " .style(\"text-anchor\", \"middle\")\n", " .attr(\"y\", 30)\n", " .style(\"font-size\", \"10px\")\n", " .text(function(d) {return d.team});\n", "}\n", "\n", "function bindButtons(topTag, incomingData) {\n", " // 最初のデータからキーの配列を取り出し、不要なteamとregionを除いてボタンを生成\n", " var dataKeys = d3.keys(incomingData[0])\n", " .filter(function (el) {return el != \"team\" && el != \"region\"}) \n", " d3.select(topTag).select(\"#controls\").selectAll(\"button.teams\").data(dataKeys).enter().append(\"button\")\n", " .on(\"click\", buttonClick)\n", " .html(function(d) {return d}); \n", " \n", " // ボタンのアクションをバインディング\n", " function buttonClick(datapoint) {\n", " var maxValue = d3.max(incomingData, \n", " function(d) {return parseFloat(d[datapoint])\n", " });\n", " var radiusScale = d3.scale.linear().domain([0,maxValue]).range([2,20]);\n", " d3.select(topTag).selectAll(\"g.overallG\").select(\"circle\").transition().duration(1000).attr(\"r\", \n", " function(d) {return radiusScale(d[datapoint])})\n", " } \n", "}\n", "\n", "function bindMouseAction(topTag, teamG) {\n", " // マウスアクションをバインディング\n", " teamG.on(\"mouseover\", highlightRegion);\n", " teamG.on(\"mouseout\", function() {\n", " d3.select(topTag).selectAll(\"g.overallG\").select(\"circle\").style(\"fill\", \"pink\")\n", " });\n", "\n", " function highlightRegion(d) {\n", " d3.select(topTag).selectAll(\"g.overallG\").select(\"circle\").style(\"fill\", function(p) \n", " {return p.region == d.region ? \"red\" : \"gray\"})\n", " } \n", "}" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "%%javascript\n", "d3.csv(\"data/worldcup.csv\", function(data) {\n", " overallTeamViz('#ex1', data);\n", "});\n", "\n", "function overallTeamViz(topTag, incomingData) {\n", " // 中央に表示用のキャンバス用g(teamG)と出場国用g(overallG)を作成\n", " createFrame(topTag, incomingData);\n", " \n", " var teamG = d3.select(topTag).selectAll(\"g.overallG\");\n", " // 出場国のCircleとチーム名を表示\n", " createTeamCircle(teamG);\n", " // ボタンアクションをバインディング\n", " bindButtons(topTag, incomingData);\n", " // mouseアクションをバインディング\n", " bindMouseAction(topTag, teamG);\n", "}\n", "\n", "\n", "function createFrame(topTag, incomingData) {\n", " d3.select(topTag).select(\"svg\")\n", " .append(\"g\")\n", " .attr(\"id\", \"teamsG\")\n", " .attr(\"transform\", \"translate(50,300)\")\n", " .selectAll(\"g\")\n", " .data(incomingData)\n", " .enter()\n", " .append(\"g\")\n", " .attr(\"class\", \"overallG\")\n", " .attr(\"transform\", function (d,i) {return \"translate(\" + (i * 50) + \", 0)\"});\n", "}\n", "\n", "function createTeamCircle(teamG) {\n", " // 各チームのCircleを作成\n", " teamG\n", " .append(\"circle\").attr(\"r\", 0)\n", " .transition()\n", " .delay(function(d,i) {return i * 100})\n", " .duration(500)\n", " .attr(\"r\", 40)\n", " .transition()\n", " .duration(500)\n", " .attr(\"r\", 20); \n", " // チームタイトルを表示\n", " teamG\n", " .append(\"text\")\n", " .style(\"text-anchor\", \"middle\")\n", " .attr(\"y\", 30)\n", " .style(\"font-size\", \"10px\")\n", " .text(function(d) {return d.team});\n", "}\n", "\n", "function bindButtons(topTag, incomingData) {\n", " // 最初のデータからキーの配列を取り出し、不要なteamとregionを除いてボタンを生成\n", " var dataKeys = d3.keys(incomingData[0])\n", " .filter(function (el) {return el != \"team\" && el != \"region\"}) \n", " d3.select(topTag).select(\"#controls\").selectAll(\"button.teams\").data(dataKeys).enter().append(\"button\")\n", " .on(\"click\", buttonClick)\n", " .html(function(d) {return d}); \n", " \n", " // ボタンのアクションをバインディング\n", " function buttonClick(datapoint) {\n", " var maxValue = d3.max(incomingData, \n", " function(d) {return parseFloat(d[datapoint])\n", " });\n", " var radiusScale = d3.scale.linear().domain([0,maxValue]).range([2,20]);\n", " d3.select(topTag).selectAll(\"g.overallG\").select(\"circle\").transition().duration(1000).attr(\"r\", \n", " function(d) {return radiusScale(d[datapoint])})\n", " } \n", "}\n", "\n", "function bindMouseAction(topTag, teamG) {\n", " // マウスアクションをバインディング\n", " teamG.on(\"mouseover\", highlightRegion);\n", " teamG.on(\"mouseout\", function() {\n", " d3.select(topTag).selectAll(\"g.overallG\").select(\"circle\").style(\"fill\", \"pink\")\n", " });\n", "\n", " function highlightRegion(d) {\n", " d3.select(topTag).selectAll(\"g.overallG\").select(\"circle\").style(\"fill\", function(p) \n", " {return p.region == d.region ? \"red\" : \"gray\"})\n", " } \n", "}\n" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "## コードのメモ\n", "[図3.3](http://bl.ocks.org/emeeks/8ec13f7ffbbf1d3c4bd8)\n", "を機能ごとに分けてみました。\n", "この小さなコードで、すごい表現ができるものだと感心しました。\n", "\n", "3章の説明を簡単にまとめてみます。\n", "\n", "- g要素:SVGの要素をグルーピングするタグ、g要素単位でアニメーションや座標変換を行います\n", "- 描画用のキャンバスフレームをセット(createFrame)\n", "- 各チームのCircleとネームを表示(createTeamCircle)\n", "- ボタンのアクションをバインディング(bindButtons)\n", "- マウスアクションをバインディング(bindMouseAction)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### バイディングされたデータ\n", "teamGの各g要素にバインディングされたデータは、以下のように __data__ 属性に保持されます。\n", "\n", "![バインディングされたデータ](images/data_binding.png)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### イベント処理\n", "\n", "最初は、ボタンイベントをセットしているbindButtonsをみていきましょう。\n", "\n", "dataKeys変数に、キー配列[\"team\",\"region\",\"win\",\"loss\",\"draw\",\"points\",\"gf\",\"ga\",\"cs\",\"yc\",\"rc\"]からteamとregionを除いた配列を取り出します。\n" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "data": { "application/javascript": [ "var dataKeys = [\"team\",\"region\",\"win\",\"loss\",\"draw\",\"points\",\"gf\",\"ga\",\"cs\",\"yc\",\"rc\"]\n", " .filter(function (el) {return el != \"team\" && el != \"region\"}) \n", "element.text(dataKeys);" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "%%javascript\n", "var dataKeys = [\"team\",\"region\",\"win\",\"loss\",\"draw\",\"points\",\"gf\",\"ga\",\"cs\",\"yc\",\"rc\"]\n", " .filter(function (el) {return el != \"team\" && el != \"region\"}) \n", "element.text(dataKeys);" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "その後、配列の各要素を名前とするボタンを生成し、onClick時の関数としてbuttonClickをバインドします。\n", "```javascript\n", " var dataKeys = d3.keys(incomingData[0])\n", " .filter(function (el) {return el != \"team\" && el != \"region\"}) \n", " d3.select(\"#controls\").selectAll(\"button.teams\").data(dataKeys).enter().append(\"button\")\n", " .on(\"click\", buttonClick)\n", " .html(function(d) {return d}); \n", "```\n", "\n", "ボタンクリックのコールバック関数は、datapointで渡されたキーの値から最大値をmaxValueに保持し、各Circleの半径を値の割合に変化させています。\n", "\n", "```javascript\n", " function buttonClick(datapoint) {\n", " var maxValue = d3.max(incomingData, \n", " function(d) {return parseFloat(d[datapoint])\n", " });\n", " var radiusScale = d3.scale.linear().domain([0,maxValue]).range([2,20]);\n", " d3.select(topTag).selectAll(\"g.overallG\").select(\"circle\").transition().duration(1000).attr(\"r\", \n", " function(d) {return radiusScale(d[datapoint])})\n", " } \n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## テンプレートを使ってコードを再利用\n", "原因はわからないのですが、%%javascriptで定義した関数はjupyterのセルで共存することができないので、別のセルで定義した関数を再利用することができません。\n", "\n", "そこで、Templateを使って各関数を定義することにします。" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [], "source": [ "teamViz = '''\n", "function overallTeamViz(topTag, incomingData) {\n", " createFrame(topTag, incomingData);\n", " \n", " var teamG = d3.select(topTag).selectAll(\"g.overallG\");\n", " createTeamCircle(teamG);\n", " bindButtons(topTag, incomingData);\n", " bindMouseAction(topTag, teamG);\n", "}\n", "\n", "function createFrame(topTag, incomingData) {\n", " d3.select(topTag).select(\"svg\")\n", " .append(\"g\")\n", " .attr(\"id\", \"teamsG\")\n", " .attr(\"transform\", \"translate(50,300)\")\n", " .selectAll(\"g\")\n", " .data(incomingData)\n", " .enter()\n", " .append(\"g\")\n", " .attr(\"class\", \"overallG\")\n", " .attr(\"transform\", function (d,i) {return \"translate(\" + (i * 50) + \", 0)\"});\n", "}\n", "\n", "function createTeamCircle(teamG) {\n", " teamG\n", " .append(\"circle\").attr(\"r\", 0)\n", " .transition()\n", " .delay(function(d,i) {return i * 100})\n", " .duration(500)\n", " .attr(\"r\", 40)\n", " .transition()\n", " .duration(500)\n", " .attr(\"r\", 20); \n", " teamG\n", " .append(\"text\")\n", " .style(\"text-anchor\", \"middle\")\n", " .attr(\"y\", 30)\n", " .style(\"font-size\", \"10px\")\n", " .text(function(d) {return d.team});\n", "}\n", "'''\n", "\n", "bindButtons = '''\n", "function bindButtons(topTag, incomingData) {\n", " var dataKeys = d3.keys(incomingData[0])\n", " .filter(function (el) {return el != \"team\" && el != \"region\"}) \n", " d3.select(topTag).select(\"#controls\").selectAll(\"button.teams\").data(dataKeys).enter().append(\"button\")\n", " .on(\"click\", buttonClick)\n", " .html(function(d) {return d}); \n", " \n", " function buttonClick(datapoint) {\n", " var maxValue = d3.max(incomingData, \n", " function(d) {return parseFloat(d[datapoint])\n", " });\n", " var radiusScale = d3.scale.linear().domain([0,maxValue]).range([2,20]);\n", " d3.select(topTag).selectAll(\"g.overallG\").select(\"circle\").transition().duration(1000).attr(\"r\", \n", " function(d) {return radiusScale(d[datapoint])})\n", " } \n", "}\n", "'''\n", "\n", "bindMouseAction = '''\n", "function bindMouseAction(topTag, teamG) {\n", " teamG.on(\"mouseover\", highlightRegion);\n", " teamG.on(\"mouseout\", function() {\n", " d3.select(topTag).selectAll(\"g.overallG\").select(\"circle\").style(\"fill\", \"pink\")\n", " });\n", "\n", " function highlightRegion(d) {\n", " d3.select(topTag).selectAll(\"g.overallG\").select(\"circle\").style(\"fill\", function(p) \n", " {return p.region == d.region ? \"red\" : \"gray\"})\n", " } \n", "}\n", "'''\n", "js_text = Template('''\n", "\n", "\n", "\n", "
\n", "
\n", " \n", "
\n", "
\n", "
\n", "\n", "\n", "''')\n", "\n", "example='ex2'\n", "html_text = js_text.substitute({'example': example, 'teamViz': teamViz, \n", " 'bindButtons': bindButtons, 'bindMouseAction': bindMouseAction, 'extention': \"\"})" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\n", "
\n", "
\n", " \n", "
\n", "
\n", "
\n", "\n", "\n" ], "text/plain": [ "" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "HTML(html_text)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## DOMの操作\n", "バイディングされたデータで見たとおりバインディングされたデータや可視化された要素全てが、DOMの中に保持されています。\n", "\n", "javascriptでこれらのDOM要素にアクセスする方法を見てみましょう。" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": false }, "outputs": [ { "data": { "application/javascript": [ "d3.select('#ex2').select(\"circle\").each(function(d,i) {\n", " console.log(d);console.log(i);console.log(this);\n", "});" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "%%javascript\n", "d3.select('#ex2').select(\"circle\").each(function(d,i) {\n", " console.log(d);console.log(i);console.log(this);\n", "});" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "デベロッパーツールのConsoleには、以下のように表示されます。\n", "\n", "![DOM_this](images/dom_this.png)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "thisは、選択された要素のDOMオブジェクトを参照していることがわかりました。\n", "\n", "また、node()メソッドで選択された要素を取り出すこともできます。\n" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": false }, "outputs": [ { "data": { "application/javascript": [ "console.log(d3.select('#ex2').select(\"circle\").node());" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "%%javascript\n", "console.log(d3.select('#ex2').select(\"circle\").node());" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## カラーマッピング\n", "ボタンをクリックした時に、値によってCircleの半径だけでなく、色も変える例が紹介されています。\n", "\n", "最初にLAB ramp関数を使って数値で色を黄色から青色の間を線形補間して表示する方法を示します。\n", "\n", "```javascript\n", " var ybRamp = d3.scale.linear()\n", " .interpolate(d3.interpolateLab)\n", " .domain([0,maxValue]).range([\"yellow\", \"blue\"]);\n", "```" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "collapsed": true }, "outputs": [], "source": [ "bindButtons = '''\n", "function bindButtons(topTag, incomingData) {\n", " var dataKeys = d3.keys(incomingData[0])\n", " .filter(function (el) {return el != \"team\" && el != \"region\"}) \n", " d3.select(topTag).select(\"#controls\").selectAll(\"button.teams\").data(dataKeys).enter().append(\"button\")\n", " .on(\"click\", buttonClick)\n", " .html(function(d) {return d}); \n", " \n", " function buttonClick(datapoint) {\n", " var maxValue = d3.max(incomingData, function(el) {\n", " return parseFloat(el[datapoint ]); \n", " });\n", " var ybRamp = d3.scale.linear()\n", " .interpolate(d3.interpolateLab)\n", " .domain([0,maxValue]).range([\"yellow\", \"blue\"]);\n", "\n", " var radiusScale = d3.scale.linear().domain([0,maxValue]).range([2,20]); \n", " d3.selectAll(\"g.overallG\").select(\"circle\").transition().duration(1000)\n", " .style(\"fill\", function(p) {\n", " return ybRamp(p[datapoint ])}\n", " ) .attr(\"r\", function(p) {\n", " return radiusScale(p[datapoint ])\n", " });\n", " } \n", "}\n", "'''" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\n", "
\n", "
\n", " \n", "
\n", "
\n", "
\n", "\n", "\n" ], "text/plain": [ "" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "example='ex3'\n", "html_text = js_text.substitute({'example': example, 'teamViz': teamViz, \n", " 'bindButtons': bindButtons, 'bindMouseAction': bindMouseAction, 'extention': \"\"})\n", "HTML(html_text)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "次にcolorbrewerライブラリを使って、赤系統の3色でグルーピングする例を示します。\n", "\n", "```javascript\n", " var colorQuantize = d3.scale.quantize()\n", " .domain([0,maxValue]).range(colorbrewer.Reds[3]);\n", "```" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "collapsed": true }, "outputs": [], "source": [ "bindButtons = '''\n", "function bindButtons(topTag, incomingData) {\n", " var dataKeys = d3.keys(incomingData[0])\n", " .filter(function (el) {return el != \"team\" && el != \"region\"}) \n", " d3.select(topTag).select(\"#controls\").selectAll(\"button.teams\").data(dataKeys).enter().append(\"button\")\n", " .on(\"click\", buttonClick)\n", " .html(function(d) {return d}); \n", " \n", " function buttonClick(datapoint) {\n", " var maxValue = d3.max(incomingData, function(el) {\n", " return parseFloat(el[datapoint ]); \n", " });\n", " var colorQuantize = d3.scale.quantize()\n", " .domain([0,maxValue]).range(colorbrewer.Reds[3]);\n", "\n", " var radiusScale = d3.scale.linear().domain([0,maxValue]).range([2,20]); \n", " d3.selectAll(\"g.overallG\").select(\"circle\").transition().duration(1000)\n", " .style(\"fill\", function(p) {\n", " return colorQuantize(p[datapoint ])}\n", " ) .attr(\"r\", function(p) {\n", " return radiusScale(p[datapoint ])\n", " });\n", " } \n", "}\n", "'''" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\n", "
\n", "
\n", " \n", "
\n", "
\n", "
\n", "\n", "\n" ], "text/plain": [ "" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "example='ex4'\n", "html_text = js_text.substitute({'example': example, 'teamViz': teamViz, \n", " 'bindButtons': bindButtons, 'bindMouseAction': bindMouseAction, 'extention': \"\"})\n", "HTML(html_text)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 画像を図形に表示する\n", "次に画像を図形に表示する例を試してみます。\n", "\n", "画像の表示には、\"xlink:ref\"を使用します。以下の例では、imageタグをtextの前に挿入し、\n", "属性hrefに画像のURIをセットしています。\n", "\n", "![xlink_image](images/xlink_image.png)\n" ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "collapsed": false }, "outputs": [ { "data": { "application/javascript": [ "d3.select('#ex4').selectAll(\"g.overallG\").insert(\"image\", \"text\")\n", " .attr(\"xlink:href\", function(d) {\n", " return \"images/\" + d.team + \".png\";\n", " })\n", " .attr(\"width\", \"45px\").attr(\"height\", \"20px\").attr(\"x\", \"-22\")\n", " .attr(\"y\", \"-10\");" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "%%javascript\n", "d3.select('#ex4').selectAll(\"g.overallG\").insert(\"image\", \"text\")\n", " .attr(\"xlink:href\", function(d) {\n", " return \"images/\" + d.team + \".png\";\n", " })\n", " .attr(\"width\", \"45px\").attr(\"height\", \"20px\").attr(\"x\", \"-22\")\n", " .attr(\"y\", \"-10\");\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### HTMLを表示する\n", "最後に画像にHTMLを表示する例を試してみます。このほかにも3章にはとても興味深い例題が紹介されています。\n", "\n", "ダイアログとして表示するために、以下のCSSを追加します。" ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Appending to css/d3ia.css\n" ] } ], "source": [ "%%writefile -a css/d3ia.css\n", "#modal {\n", " position:fixed;\n", " left:150px;\n", " top:100px;\n", " z-index:1;\n", " background: white;\n", " border: 1px black solid;\n", " box-shadow: 10px 10px 5px #888888;\n", "}\n", " tr {\n", " border: 1px gray solid;\n", "}\n", " td {\n", " font-size: 10px;\n", "}\n", "td.data {\n", " font-weight: 900;\n", " }" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "表示するダイアログのHTMLは、以下のようになります。" ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Overwriting resources/modal.html\n" ] } ], "source": [ "%%writefile resources/modal.html\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
Statistics
Team Name
Region
Wins
Losses
Draws
Points
Goals For
Goals Against
Clean Sheets
Yellow Cards
Red Cards
" ] }, { "cell_type": "code", "execution_count": 18, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\n", "
\n", "
\n", " \n", "
\n", "
\n", "
\n", "\n", "\n" ], "text/plain": [ "" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "example='ex5'\n", "html_text = js_text.substitute({'example': example, 'teamViz': teamViz, \n", " 'bindButtons': bindButtons, 'bindMouseAction': bindMouseAction, \n", " 'extention': \"\"})\n", "HTML(html_text)" ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "collapsed": false }, "outputs": [ { "data": { "application/javascript": [ "var teamG = d3.select('#ex5').selectAll(\"g.overallG\");\n", "teamG.on(\"click\", teamClick);\n", "\n", "d3.text(\"resources/modal.html\", function(data) {\n", " d3.select('#ex5').append(\"div\").attr(\"id\", \"modal\").html(data);\n", "});\n", "\n", "function teamClick(d) {\n", " d3.select('#ex5').selectAll(\"td.data\").data(d3.values(d)).html(function(p) {return p});\n", "} " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "%%javascript\n", "var teamG = d3.select('#ex5').selectAll(\"g.overallG\");\n", "teamG.on(\"click\", teamClick);\n", "\n", "d3.text(\"resources/modal.html\", function(data) {\n", " d3.select('#ex5').append(\"div\").attr(\"id\", \"modal\").html(data);\n", "});\n", "\n", "function teamClick(d) {\n", " d3.select('#ex5').selectAll(\"td.data\").data(d3.values(d)).html(function(p) {return p});\n", "} " ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 2", "language": "python", "name": "python2" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.10" } }, "nbformat": 4, "nbformat_minor": 0 }