Last active
January 27, 2019 14:13
-
-
Save oshikiri/82200fb98ce03383cb8f06f142fff5fd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": { | |
"ExecuteTime": { | |
"end_time": "2019-01-27T13:40:26.861948Z", | |
"start_time": "2019-01-27T13:40:26.661608Z" | |
} | |
}, | |
"outputs": [], | |
"source": [ | |
"import pandas as pd\n", | |
"from IPython.display import IFrame, HTML\n", | |
"import html" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Fetch and preprocess json" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": { | |
"ExecuteTime": { | |
"end_time": "2019-01-27T13:40:27.594409Z", | |
"start_time": "2019-01-27T13:40:26.863333Z" | |
} | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/html": [ | |
"<div>\n", | |
"<style scoped>\n", | |
" .dataframe tbody tr th:only-of-type {\n", | |
" vertical-align: middle;\n", | |
" }\n", | |
"\n", | |
" .dataframe tbody tr th {\n", | |
" vertical-align: top;\n", | |
" }\n", | |
"\n", | |
" .dataframe thead th {\n", | |
" text-align: right;\n", | |
" }\n", | |
"</style>\n", | |
"<table border=\"1\" class=\"dataframe\">\n", | |
" <thead>\n", | |
" <tr style=\"text-align: right;\">\n", | |
" <th></th>\n", | |
" <th>dow</th>\n", | |
" <th>hour</th>\n", | |
" <th>contributions</th>\n", | |
" </tr>\n", | |
" </thead>\n", | |
" <tbody>\n", | |
" <tr>\n", | |
" <th>0</th>\n", | |
" <td>0</td>\n", | |
" <td>0</td>\n", | |
" <td>7</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>1</th>\n", | |
" <td>0</td>\n", | |
" <td>1</td>\n", | |
" <td>0</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>2</th>\n", | |
" <td>0</td>\n", | |
" <td>2</td>\n", | |
" <td>1</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>3</th>\n", | |
" <td>0</td>\n", | |
" <td>3</td>\n", | |
" <td>0</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>4</th>\n", | |
" <td>0</td>\n", | |
" <td>4</td>\n", | |
" <td>0</td>\n", | |
" </tr>\n", | |
" </tbody>\n", | |
"</table>\n", | |
"</div>" | |
], | |
"text/plain": [ | |
" dow hour contributions\n", | |
"0 0 0 7\n", | |
"1 0 1 0\n", | |
"2 0 2 1\n", | |
"3 0 3 0\n", | |
"4 0 4 0" | |
] | |
}, | |
"execution_count": 2, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"user, repository = 'd3', 'd3'\n", | |
"\n", | |
"data = pd.read_json(f'https://api.github.com/repos/{user}/{repository}/stats/punch_card')\n", | |
"data.columns = ['dow', 'hour', 'contributions']\n", | |
"data.head()" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Save to local" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": { | |
"ExecuteTime": { | |
"end_time": "2019-01-27T13:40:27.601637Z", | |
"start_time": "2019-01-27T13:40:27.596964Z" | |
} | |
}, | |
"outputs": [], | |
"source": [ | |
"data.to_json('./data/d3-contributions.json', orient='records')" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## d3.js" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": { | |
"ExecuteTime": { | |
"end_time": "2019-01-27T13:40:27.717324Z", | |
"start_time": "2019-01-27T13:40:27.603945Z" | |
} | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"<!DOCTYPE html>\r\n", | |
"<meta charset=\"utf-8\">\r\n", | |
"<body>\r\n", | |
"<style>\r\n", | |
" div.tooltip {\r\n", | |
" position: absolute;\r\n", | |
" text-align: center;\r\n", | |
" width: 60px;\r\n", | |
" height: 28px;\r\n", | |
" padding: 2px;\r\n", | |
" font: 12px sans-serif;\r\n", | |
" background: rgb(255, 255, 255);\r\n", | |
" border: 0px;\r\n", | |
" border-radius: 8px;\r\n", | |
" pointer-events: none;\r\n", | |
" }\r\n", | |
"</style>\r\n", | |
"<script src=\"https://d3js.org/d3.v5.min.js\"></script>\r\n", | |
"<script>\r\n", | |
" const margin = {top: 20, right: 20, bottom: 50, left: 50};\r\n", | |
" const width = 600 - margin.left - margin.right;\r\n", | |
" const height = 200 - margin.top - margin.bottom;\r\n", | |
"\r\n", | |
" const xScale = d3.scaleLinear().range([0, width]);\r\n", | |
" const yScale = d3.scaleLinear().rangeRound([0, height]);\r\n", | |
" const rScale = d3.scaleSqrt().range([0, 10]);\r\n", | |
"\r\n", | |
" const dayOfWeek = [\"Sun\", \"Mon\", \"Tue\", \"Wed\", \"Thr\", \"Fri\", \"Sat\"];\r\n", | |
"\r\n", | |
" const svg = d3.select(\"body\")\r\n", | |
" .append(\"svg\")\r\n", | |
" .attr(\"width\", width + margin.left + margin.right)\r\n", | |
" .attr(\"height\", height + margin.top + margin.bottom)\r\n", | |
" .append(\"g\")\r\n", | |
" .attr(\"transform\", `translate(${margin.left}, ${margin.top})`);\r\n", | |
" \r\n", | |
" const dataPromise = d3.json(\"/files/contributions.json\"); // inject:contributions\r\n", | |
" dataPromise.then(data => {\r\n", | |
" xScale.domain([-0.5, 23.5]);\r\n", | |
" yScale.domain([-0.5, 6.5]);\r\n", | |
" rScale.domain([0, d3.max(data, d => d.contributions)]);\r\n", | |
"\r\n", | |
" const div = d3.select(\"body\").append(\"div\")\r\n", | |
" .attr(\"class\", \"tooltip\")\r\n", | |
" .style(\"opacity\", 0);\r\n", | |
"\r\n", | |
" svg.selectAll(\"dot\")\r\n", | |
" .data(data)\r\n", | |
" .enter().append(\"circle\")\r\n", | |
" .attr(\"r\", d => rScale(d.contributions))\r\n", | |
" .attr(\"cx\", d => xScale(d.hour))\r\n", | |
" .attr(\"cy\", d => yScale(d.dow))\r\n", | |
" .on('mouseover', d => {\r\n", | |
" div.transition()\r\n", | |
" .duration(200)\r\n", | |
" .style(\"opacity\", .9);\r\n", | |
" div.html(`${dayOfWeek[d.dow]}, ${d.hour}<br />${d.contributions}`)\r\n", | |
" .style(\"left\", (d3.event.pageX) + \"px\")\r\n", | |
" .style(\"top\", (d3.event.pageY - 28) + \"px\");\r\n", | |
" })\r\n", | |
" .on(\"mouseout\", d => {\r\n", | |
" div.transition()\r\n", | |
" .duration(500)\r\n", | |
" .style(\"opacity\", 0);\r\n", | |
" });\r\n", | |
"\r\n", | |
" svg.append(\"g\")\r\n", | |
" .attr(\"transform\", `translate(0, ${height})`)\r\n", | |
" .call(d3.axisBottom(xScale).ticks(24));\r\n", | |
"\r\n", | |
" svg.append(\"g\")\r\n", | |
" .call(\r\n", | |
" d3.axisLeft(yScale)\r\n", | |
" .ticks(7)\r\n", | |
" .tickFormat((d, i) => dayOfWeek[d])\r\n", | |
" );\r\n", | |
"\r\n", | |
" svg.append(\"text\")\r\n", | |
" .attr(\"transform\", `translate(${width / 2}, ${(height + margin.top + 15)})`)\r\n", | |
" .style(\"text-anchor\", \"middle\")\r\n", | |
" .style(\"font-size\", \"12px\")\r\n", | |
" .text(\"hour\");\r\n", | |
"\r\n", | |
" svg.append(\"text\")\r\n", | |
" .attr(\"transform\", \"rotate(-90)\")\r\n", | |
" .attr(\"y\", 0 - margin.left)\r\n", | |
" .attr(\"x\", 0 - (height / 2))\r\n", | |
" .attr(\"dy\", \"1em\")\r\n", | |
" .style(\"text-anchor\", \"middle\")\r\n", | |
" .style(\"font-size\", \"12px\")\r\n", | |
" .text(\"day of week\"); \r\n", | |
" });\r\n", | |
"</script>\r\n", | |
"</body>\r\n" | |
] | |
} | |
], | |
"source": [ | |
"!cat punchcard.html" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### 単にiframeに描画" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"metadata": { | |
"ExecuteTime": { | |
"end_time": "2019-01-27T13:40:27.727939Z", | |
"start_time": "2019-01-27T13:40:27.720754Z" | |
} | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/html": [ | |
"\n", | |
" <iframe\n", | |
" width=\"650\"\n", | |
" height=\"250\"\n", | |
" src=\"punchcard.html\"\n", | |
" frameborder=\"0\"\n", | |
" allowfullscreen\n", | |
" ></iframe>\n", | |
" " | |
], | |
"text/plain": [ | |
"<IPython.lib.display.IFrame at 0x7f0e10001518>" | |
] | |
}, | |
"execution_count": 5, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"IFrame(src=\"punchcard.html\", width=650, height=250)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### JSON を HTML に埋め込んでからiframeに描画" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"metadata": { | |
"ExecuteTime": { | |
"end_time": "2019-01-27T13:40:27.734422Z", | |
"start_time": "2019-01-27T13:40:27.730245Z" | |
} | |
}, | |
"outputs": [], | |
"source": [ | |
"with open('punchcard.html', mode='r') as f:\n", | |
" punchcard_html = ''.join(f.readlines())\n", | |
"\n", | |
"json_str = data.to_json(orient='records')\n", | |
"punchcard_html_injected = punchcard_html.replace(\n", | |
" 'const dataPromise = d3.json(\"/files/contributions.json\");',\n", | |
" f'const dataPromise = Promise.resolve({json_str});')" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 7, | |
"metadata": { | |
"ExecuteTime": { | |
"end_time": "2019-01-27T13:40:27.740967Z", | |
"start_time": "2019-01-27T13:40:27.736217Z" | |
} | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/html": [ | |
"\n", | |
" <iframe\n", | |
" width=\"650\"\n", | |
" height=\"250\"\n", | |
" src=\"data:text/html;charset=utf-8,<!DOCTYPE html>\n", | |
"<meta charset="utf-8">\n", | |
"<body>\n", | |
"<style>\n", | |
" div.tooltip {\n", | |
" position: absolute;\n", | |
" text-align: center;\n", | |
" width: 60px;\n", | |
" height: 28px;\n", | |
" padding: 2px;\n", | |
" font: 12px sans-serif;\n", | |
" background: rgb(255, 255, 255);\n", | |
" border: 0px;\n", | |
" border-radius: 8px;\n", | |
" pointer-events: none;\n", | |
" }\n", | |
"</style>\n", | |
"<script src="https://d3js.org/d3.v5.min.js"></script>\n", | |
"<script>\n", | |
" const margin = {top: 20, right: 20, bottom: 50, left: 50};\n", | |
" const width = 600 - margin.left - margin.right;\n", | |
" const height = 200 - margin.top - margin.bottom;\n", | |
"\n", | |
" const xScale = d3.scaleLinear().range([0, width]);\n", | |
" const yScale = d3.scaleLinear().rangeRound([0, height]);\n", | |
" const rScale = d3.scaleSqrt().range([0, 10]);\n", | |
"\n", | |
" const dayOfWeek = ["Sun", "Mon", "Tue", "Wed", "Thr", "Fri", "Sat"];\n", | |
"\n", | |
" const svg = d3.select("body")\n", | |
" .append("svg")\n", | |
" .attr("width", width + margin.left + margin.right)\n", | |
" .attr("height", height + margin.top + margin.bottom)\n", | |
" .append("g")\n", | |
" .attr("transform", `translate(${margin.left}, ${margin.top})`);\n", | |
" \n", | |
" const dataPromise = Promise.resolve([{"dow":0,"hour":0,"contributions":7},{"dow":0,"hour":1,"contributions":0},{"dow":0,"hour":2,"contributions":1},{"dow":0,"hour":3,"contributions":0},{"dow":0,"hour":4,"contributions":0},{"dow":0,"hour":5,"contributions":0},{"dow":0,"hour":6,"contributions":0},{"dow":0,"hour":7,"contributions":9},{"dow":0,"hour":8,"contributions":7},{"dow":0,"hour":9,"contributions":26},{"dow":0,"hour":10,"contributions":30},{"dow":0,"hour":11,"contributions":31},{"dow":0,"hour":12,"contributions":19},{"dow":0,"hour":13,"contributions":24},{"dow":0,"hour":14,"contributions":13},{"dow":0,"hour":15,"contributions":19},{"dow":0,"hour":16,"contributions":22},{"dow":0,"hour":17,"contributions":23},{"dow":0,"hour":18,"contributions":14},{"dow":0,"hour":19,"contributions":16},{"dow":0,"hour":20,"contributions":19},{"dow":0,"hour":21,"contributions":16},{"dow":0,"hour":22,"contributions":18},{"dow":0,"hour":23,"contributions":25},{"dow":1,"hour":0,"contributions":6},{"dow":1,"hour":1,"contributions":3},{"dow":1,"hour":2,"contributions":2},{"dow":1,"hour":3,"contributions":2},{"dow":1,"hour":4,"contributions":6},{"dow":1,"hour":5,"contributions":3},{"dow":1,"hour":6,"contributions":1},{"dow":1,"hour":7,"contributions":2},{"dow":1,"hour":8,"contributions":14},{"dow":1,"hour":9,"contributions":33},{"dow":1,"hour":10,"contributions":27},{"dow":1,"hour":11,"contributions":50},{"dow":1,"hour":12,"contributions":35},{"dow":1,"hour":13,"contributions":38},{"dow":1,"hour":14,"contributions":32},{"dow":1,"hour":15,"contributions":44},{"dow":1,"hour":16,"contributions":31},{"dow":1,"hour":17,"contributions":30},{"dow":1,"hour":18,"contributions":19},{"dow":1,"hour":19,"contributions":6},{"dow":1,"hour":20,"contributions":14},{"dow":1,"hour":21,"contributions":29},{"dow":1,"hour":22,"contributions":20},{"dow":1,"hour":23,"contributions":18},{"dow":2,"hour":0,"contributions":13},{"dow":2,"hour":1,"contributions":2},{"dow":2,"hour":2,"contributions":7},{"dow":2,"hour":3,"contributions":1},{"dow":2,"hour":4,"contributions":0},{"dow":2,"hour":5,"contributions":0},{"dow":2,"hour":6,"contributions":2},{"dow":2,"hour":7,"contributions":4},{"dow":2,"hour":8,"contributions":31},{"dow":2,"hour":9,"contributions":28},{"dow":2,"hour":10,"contributions":46},{"dow":2,"hour":11,"contributions":45},{"dow":2,"hour":12,"contributions":33},{"dow":2,"hour":13,"contributions":73},{"dow":2,"hour":14,"contributions":54},{"dow":2,"hour":15,"contributions":43},{"dow":2,"hour":16,"contributions":49},{"dow":2,"hour":17,"contributions":36},{"dow":2,"hour":18,"contributions":21},{"dow":2,"hour":19,"contributions":14},{"dow":2,"hour":20,"contributions":13},{"dow":2,"hour":21,"contributions":31},{"dow":2,"hour":22,"contributions":21},{"dow":2,"hour":23,"contributions":18},{"dow":3,"hour":0,"contributions":11},{"dow":3,"hour":1,"contributions":5},{"dow":3,"hour":2,"contributions":5},{"dow":3,"hour":3,"contributions":0},{"dow":3,"hour":4,"contributions":1},{"dow":3,"hour":5,"contributions":1},{"dow":3,"hour":6,"contributions":1},{"dow":3,"hour":7,"contributions":6},{"dow":3,"hour":8,"contributions":22},{"dow":3,"hour":9,"contributions":47},{"dow":3,"hour":10,"contributions":80},{"dow":3,"hour":11,"contributions":86},{"dow":3,"hour":12,"contributions":55},{"dow":3,"hour":13,"contributions":36},{"dow":3,"hour":14,"contributions":42},{"dow":3,"hour":15,"contributions":54},{"dow":3,"hour":16,"contributions":40},{"dow":3,"hour":17,"contributions":40},{"dow":3,"hour":18,"contributions":33},{"dow":3,"hour":19,"contributions":18},{"dow":3,"hour":20,"contributions":28},{"dow":3,"hour":21,"contributions":32},{"dow":3,"hour":22,"contributions":29},{"dow":3,"hour":23,"contributions":24},{"dow":4,"hour":0,"contributions":16},{"dow":4,"hour":1,"contributions":0},{"dow":4,"hour":2,"contributions":1},{"dow":4,"hour":3,"contributions":0},{"dow":4,"hour":4,"contributions":0},{"dow":4,"hour":5,"contributions":0},{"dow":4,"hour":6,"contributions":1},{"dow":4,"hour":7,"contributions":5},{"dow":4,"hour":8,"contributions":19},{"dow":4,"hour":9,"contributions":53},{"dow":4,"hour":10,"contributions":52},{"dow":4,"hour":11,"contributions":34},{"dow":4,"hour":12,"contributions":39},{"dow":4,"hour":13,"contributions":28},{"dow":4,"hour":14,"contributions":43},{"dow":4,"hour":15,"contributions":64},{"dow":4,"hour":16,"contributions":34},{"dow":4,"hour":17,"contributions":50},{"dow":4,"hour":18,"contributions":11},{"dow":4,"hour":19,"contributions":19},{"dow":4,"hour":20,"contributions":18},{"dow":4,"hour":21,"contributions":18},{"dow":4,"hour":22,"contributions":25},{"dow":4,"hour":23,"contributions":18},{"dow":5,"hour":0,"contributions":9},{"dow":5,"hour":1,"contributions":2},{"dow":5,"hour":2,"contributions":0},{"dow":5,"hour":3,"contributions":0},{"dow":5,"hour":4,"contributions":0},{"dow":5,"hour":5,"contributions":0},{"dow":5,"hour":6,"contributions":1},{"dow":5,"hour":7,"contributions":2},{"dow":5,"hour":8,"contributions":14},{"dow":5,"hour":9,"contributions":35},{"dow":5,"hour":10,"contributions":53},{"dow":5,"hour":11,"contributions":27},{"dow":5,"hour":12,"contributions":27},{"dow":5,"hour":13,"contributions":18},{"dow":5,"hour":14,"contributions":45},{"dow":5,"hour":15,"contributions":35},{"dow":5,"hour":16,"contributions":42},{"dow":5,"hour":17,"contributions":24},{"dow":5,"hour":18,"contributions":23},{"dow":5,"hour":19,"contributions":15},{"dow":5,"hour":20,"contributions":19},{"dow":5,"hour":21,"contributions":21},{"dow":5,"hour":22,"contributions":22},{"dow":5,"hour":23,"contributions":13},{"dow":6,"hour":0,"contributions":24},{"dow":6,"hour":1,"contributions":6},{"dow":6,"hour":2,"contributions":0},{"dow":6,"hour":3,"contributions":0},{"dow":6,"hour":4,"contributions":0},{"dow":6,"hour":5,"contributions":0},{"dow":6,"hour":6,"contributions":0},{"dow":6,"hour":7,"contributions":1},{"dow":6,"hour":8,"contributions":10},{"dow":6,"hour":9,"contributions":36},{"dow":6,"hour":10,"contributions":30},{"dow":6,"hour":11,"contributions":29},{"dow":6,"hour":12,"contributions":34},{"dow":6,"hour":13,"contributions":31},{"dow":6,"hour":14,"contributions":30},{"dow":6,"hour":15,"contributions":27},{"dow":6,"hour":16,"contributions":19},{"dow":6,"hour":17,"contributions":18},{"dow":6,"hour":18,"contributions":23},{"dow":6,"hour":19,"contributions":7},{"dow":6,"hour":20,"contributions":17},{"dow":6,"hour":21,"contributions":19},{"dow":6,"hour":22,"contributions":17},{"dow":6,"hour":23,"contributions":13}]); // inject:contributions\n", | |
" dataPromise.then(data => {\n", | |
" xScale.domain([-0.5, 23.5]);\n", | |
" yScale.domain([-0.5, 6.5]);\n", | |
" rScale.domain([0, d3.max(data, d => d.contributions)]);\n", | |
"\n", | |
" const div = d3.select("body").append("div")\n", | |
" .attr("class", "tooltip")\n", | |
" .style("opacity", 0);\n", | |
"\n", | |
" svg.selectAll("dot")\n", | |
" .data(data)\n", | |
" .enter().append("circle")\n", | |
" .attr("r", d => rScale(d.contributions))\n", | |
" .attr("cx", d => xScale(d.hour))\n", | |
" .attr("cy", d => yScale(d.dow))\n", | |
" .on('mouseover', d => {\n", | |
" div.transition()\n", | |
" .duration(200)\n", | |
" .style("opacity", .9);\n", | |
" div.html(`${dayOfWeek[d.dow]}, ${d.hour}<br />${d.contributions}`)\n", | |
" .style("left", (d3.event.pageX) + "px")\n", | |
" .style("top", (d3.event.pageY - 28) + "px");\n", | |
" })\n", | |
" .on("mouseout", d => {\n", | |
" div.transition()\n", | |
" .duration(500)\n", | |
" .style("opacity", 0);\n", | |
" });\n", | |
"\n", | |
" svg.append("g")\n", | |
" .attr("transform", `translate(0, ${height})`)\n", | |
" .call(d3.axisBottom(xScale).ticks(24));\n", | |
"\n", | |
" svg.append("g")\n", | |
" .call(\n", | |
" d3.axisLeft(yScale)\n", | |
" .ticks(7)\n", | |
" .tickFormat((d, i) => dayOfWeek[d])\n", | |
" );\n", | |
"\n", | |
" svg.append("text")\n", | |
" .attr("transform", `translate(${width / 2}, ${(height + margin.top + 15)})`)\n", | |
" .style("text-anchor", "middle")\n", | |
" .style("font-size", "12px")\n", | |
" .text("hour");\n", | |
"\n", | |
" svg.append("text")\n", | |
" .attr("transform", "rotate(-90)")\n", | |
" .attr("y", 0 - margin.left)\n", | |
" .attr("x", 0 - (height / 2))\n", | |
" .attr("dy", "1em")\n", | |
" .style("text-anchor", "middle")\n", | |
" .style("font-size", "12px")\n", | |
" .text("day of week"); \n", | |
" });\n", | |
"</script>\n", | |
"</body>\n", | |
"\"\n", | |
" frameborder=\"0\"\n", | |
" allowfullscreen\n", | |
" ></iframe>\n", | |
" " | |
], | |
"text/plain": [ | |
"<IPython.lib.display.IFrame at 0x7f0e44258438>" | |
] | |
}, | |
"execution_count": 7, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"IFrame(f'data:text/html;charset=utf-8,{html.escape(punchcard_html_injected)}', width=650, height=250)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## vega-lite" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### JSON を HTML に埋め込んでからiframeに描画" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 8, | |
"metadata": { | |
"ExecuteTime": { | |
"end_time": "2019-01-27T13:40:27.749655Z", | |
"start_time": "2019-01-27T13:40:27.742630Z" | |
} | |
}, | |
"outputs": [], | |
"source": [ | |
"dow = [\"Sun\", \"Mon\", \"Tue\", \"Wed\", \"Thr\", \"Fri\", \"Sat\"]\n", | |
"json_str = (\n", | |
" data\n", | |
" .assign(dow = lambda d: d.dow.map(lambda i: dow[i]))\n", | |
" .rename(columns={'dow': 'day of week'})\n", | |
" .to_json(orient='records')\n", | |
")\n", | |
"\n", | |
"with open('2019-01-08_try_js_vis_tools/vega-lite/index.html', mode='r') as f:\n", | |
" vegalite_html = ''.join(f.readlines())" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 9, | |
"metadata": { | |
"ExecuteTime": { | |
"end_time": "2019-01-27T13:40:27.752770Z", | |
"start_time": "2019-01-27T13:40:27.750848Z" | |
} | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"<!DOCTYPE html>\n", | |
"<head>\n", | |
" <title>Vega Lite Bar Chart</title>\n", | |
" <meta charset=\"utf-8\">\n", | |
"\n", | |
" <script src=\"https://cdn.jsdelivr.net/npm/vega@4.3.0/build/vega.js\"></script>\n", | |
" <script src=\"https://cdn.jsdelivr.net/npm/vega-lite@3.0.0-rc10/build/vega-lite.js\"></script>\n", | |
" <script src=\"https://cdn.jsdelivr.net/npm/vega-embed@3.24.1/build/vega-embed.js\"></script>\n", | |
"\n", | |
"</head>\n", | |
"<body>\n", | |
" <!-- Container for the visualization -->\n", | |
" <div id=\"vis\"></div>\n", | |
"\n", | |
" <script>\n", | |
" // Assign the specification to a local variable vlSpec.\n", | |
" var vlSpec = {\n", | |
" \"$schema\": \"https://vega.github.io/schema/vega-lite/v3.json\",\n", | |
" \"data\": { \"url\": \"../../data/d3-contributions.json\"},\n", | |
" \"mark\": { \"type\": \"circle\", \"color\": \"black\" },\n", | |
"\n", | |
" \"encoding\": {\n", | |
" \"y\": {\n", | |
" \"field\": \"day of week\",\n", | |
" \"type\": \"nominal\",\n", | |
" \"sort\": [\"Sun\", \"Mon\", \"Tue\", \"Wed\", \"Thr\", \"Fri\", \"Sat\"]\n", | |
" },\n", | |
" \"x\": {\n", | |
" \"field\": \"hour\",\n", | |
" \"type\": \"nominal\"\n", | |
" },\n", | |
" \"size\": {\n", | |
" \"field\": \"contributions\",\n", | |
" \"type\": \"quantitative\",\n", | |
" \"aggregate\": \"sum\"\n", | |
" }\n", | |
" }\n", | |
" };\n", | |
"\n", | |
" // Embed the visualization in the container with id `vis`\n", | |
" vegaEmbed(\"#vis\", vlSpec, {actions: false});\n", | |
" </script>\n", | |
"</body>\n", | |
"</html>\n", | |
"\n" | |
] | |
} | |
], | |
"source": [ | |
"print(vegalite_html)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 10, | |
"metadata": { | |
"ExecuteTime": { | |
"end_time": "2019-01-27T13:40:27.758624Z", | |
"start_time": "2019-01-27T13:40:27.754982Z" | |
} | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/html": [ | |
"\n", | |
" <iframe\n", | |
" width=\"700\"\n", | |
" height=\"220\"\n", | |
" src=\"data:text/html;charset=utf-8,<!DOCTYPE html>\n", | |
"<head>\n", | |
" <title>Vega Lite Bar Chart</title>\n", | |
" <meta charset="utf-8">\n", | |
"\n", | |
" <script src="https://cdn.jsdelivr.net/npm/vega@4.3.0/build/vega.js"></script>\n", | |
" <script src="https://cdn.jsdelivr.net/npm/vega-lite@3.0.0-rc10/build/vega-lite.js"></script>\n", | |
" <script src="https://cdn.jsdelivr.net/npm/vega-embed@3.24.1/build/vega-embed.js"></script>\n", | |
"\n", | |
"</head>\n", | |
"<body>\n", | |
" <!-- Container for the visualization -->\n", | |
" <div id="vis"></div>\n", | |
"\n", | |
" <script>\n", | |
" // Assign the specification to a local variable vlSpec.\n", | |
" var vlSpec = {\n", | |
" "$schema": "https://vega.github.io/schema/vega-lite/v3.json",\n", | |
" "data": { "values": [{"day of week":"Sun","hour":0,"contributions":7},{"day of week":"Sun","hour":1,"contributions":0},{"day of week":"Sun","hour":2,"contributions":1},{"day of week":"Sun","hour":3,"contributions":0},{"day of week":"Sun","hour":4,"contributions":0},{"day of week":"Sun","hour":5,"contributions":0},{"day of week":"Sun","hour":6,"contributions":0},{"day of week":"Sun","hour":7,"contributions":9},{"day of week":"Sun","hour":8,"contributions":7},{"day of week":"Sun","hour":9,"contributions":26},{"day of week":"Sun","hour":10,"contributions":30},{"day of week":"Sun","hour":11,"contributions":31},{"day of week":"Sun","hour":12,"contributions":19},{"day of week":"Sun","hour":13,"contributions":24},{"day of week":"Sun","hour":14,"contributions":13},{"day of week":"Sun","hour":15,"contributions":19},{"day of week":"Sun","hour":16,"contributions":22},{"day of week":"Sun","hour":17,"contributions":23},{"day of week":"Sun","hour":18,"contributions":14},{"day of week":"Sun","hour":19,"contributions":16},{"day of week":"Sun","hour":20,"contributions":19},{"day of week":"Sun","hour":21,"contributions":16},{"day of week":"Sun","hour":22,"contributions":18},{"day of week":"Sun","hour":23,"contributions":25},{"day of week":"Mon","hour":0,"contributions":6},{"day of week":"Mon","hour":1,"contributions":3},{"day of week":"Mon","hour":2,"contributions":2},{"day of week":"Mon","hour":3,"contributions":2},{"day of week":"Mon","hour":4,"contributions":6},{"day of week":"Mon","hour":5,"contributions":3},{"day of week":"Mon","hour":6,"contributions":1},{"day of week":"Mon","hour":7,"contributions":2},{"day of week":"Mon","hour":8,"contributions":14},{"day of week":"Mon","hour":9,"contributions":33},{"day of week":"Mon","hour":10,"contributions":27},{"day of week":"Mon","hour":11,"contributions":50},{"day of week":"Mon","hour":12,"contributions":35},{"day of week":"Mon","hour":13,"contributions":38},{"day of week":"Mon","hour":14,"contributions":32},{"day of week":"Mon","hour":15,"contributions":44},{"day of week":"Mon","hour":16,"contributions":31},{"day of week":"Mon","hour":17,"contributions":30},{"day of week":"Mon","hour":18,"contributions":19},{"day of week":"Mon","hour":19,"contributions":6},{"day of week":"Mon","hour":20,"contributions":14},{"day of week":"Mon","hour":21,"contributions":29},{"day of week":"Mon","hour":22,"contributions":20},{"day of week":"Mon","hour":23,"contributions":18},{"day of week":"Tue","hour":0,"contributions":13},{"day of week":"Tue","hour":1,"contributions":2},{"day of week":"Tue","hour":2,"contributions":7},{"day of week":"Tue","hour":3,"contributions":1},{"day of week":"Tue","hour":4,"contributions":0},{"day of week":"Tue","hour":5,"contributions":0},{"day of week":"Tue","hour":6,"contributions":2},{"day of week":"Tue","hour":7,"contributions":4},{"day of week":"Tue","hour":8,"contributions":31},{"day of week":"Tue","hour":9,"contributions":28},{"day of week":"Tue","hour":10,"contributions":46},{"day of week":"Tue","hour":11,"contributions":45},{"day of week":"Tue","hour":12,"contributions":33},{"day of week":"Tue","hour":13,"contributions":73},{"day of week":"Tue","hour":14,"contributions":54},{"day of week":"Tue","hour":15,"contributions":43},{"day of week":"Tue","hour":16,"contributions":49},{"day of week":"Tue","hour":17,"contributions":36},{"day of week":"Tue","hour":18,"contributions":21},{"day of week":"Tue","hour":19,"contributions":14},{"day of week":"Tue","hour":20,"contributions":13},{"day of week":"Tue","hour":21,"contributions":31},{"day of week":"Tue","hour":22,"contributions":21},{"day of week":"Tue","hour":23,"contributions":18},{"day of week":"Wed","hour":0,"contributions":11},{"day of week":"Wed","hour":1,"contributions":5},{"day of week":"Wed","hour":2,"contributions":5},{"day of week":"Wed","hour":3,"contributions":0},{"day of week":"Wed","hour":4,"contributions":1},{"day of week":"Wed","hour":5,"contributions":1},{"day of week":"Wed","hour":6,"contributions":1},{"day of week":"Wed","hour":7,"contributions":6},{"day of week":"Wed","hour":8,"contributions":22},{"day of week":"Wed","hour":9,"contributions":47},{"day of week":"Wed","hour":10,"contributions":80},{"day of week":"Wed","hour":11,"contributions":86},{"day of week":"Wed","hour":12,"contributions":55},{"day of week":"Wed","hour":13,"contributions":36},{"day of week":"Wed","hour":14,"contributions":42},{"day of week":"Wed","hour":15,"contributions":54},{"day of week":"Wed","hour":16,"contributions":40},{"day of week":"Wed","hour":17,"contributions":40},{"day of week":"Wed","hour":18,"contributions":33},{"day of week":"Wed","hour":19,"contributions":18},{"day of week":"Wed","hour":20,"contributions":28},{"day of week":"Wed","hour":21,"contributions":32},{"day of week":"Wed","hour":22,"contributions":29},{"day of week":"Wed","hour":23,"contributions":24},{"day of week":"Thr","hour":0,"contributions":16},{"day of week":"Thr","hour":1,"contributions":0},{"day of week":"Thr","hour":2,"contributions":1},{"day of week":"Thr","hour":3,"contributions":0},{"day of week":"Thr","hour":4,"contributions":0},{"day of week":"Thr","hour":5,"contributions":0},{"day of week":"Thr","hour":6,"contributions":1},{"day of week":"Thr","hour":7,"contributions":5},{"day of week":"Thr","hour":8,"contributions":19},{"day of week":"Thr","hour":9,"contributions":53},{"day of week":"Thr","hour":10,"contributions":52},{"day of week":"Thr","hour":11,"contributions":34},{"day of week":"Thr","hour":12,"contributions":39},{"day of week":"Thr","hour":13,"contributions":28},{"day of week":"Thr","hour":14,"contributions":43},{"day of week":"Thr","hour":15,"contributions":64},{"day of week":"Thr","hour":16,"contributions":34},{"day of week":"Thr","hour":17,"contributions":50},{"day of week":"Thr","hour":18,"contributions":11},{"day of week":"Thr","hour":19,"contributions":19},{"day of week":"Thr","hour":20,"contributions":18},{"day of week":"Thr","hour":21,"contributions":18},{"day of week":"Thr","hour":22,"contributions":25},{"day of week":"Thr","hour":23,"contributions":18},{"day of week":"Fri","hour":0,"contributions":9},{"day of week":"Fri","hour":1,"contributions":2},{"day of week":"Fri","hour":2,"contributions":0},{"day of week":"Fri","hour":3,"contributions":0},{"day of week":"Fri","hour":4,"contributions":0},{"day of week":"Fri","hour":5,"contributions":0},{"day of week":"Fri","hour":6,"contributions":1},{"day of week":"Fri","hour":7,"contributions":2},{"day of week":"Fri","hour":8,"contributions":14},{"day of week":"Fri","hour":9,"contributions":35},{"day of week":"Fri","hour":10,"contributions":53},{"day of week":"Fri","hour":11,"contributions":27},{"day of week":"Fri","hour":12,"contributions":27},{"day of week":"Fri","hour":13,"contributions":18},{"day of week":"Fri","hour":14,"contributions":45},{"day of week":"Fri","hour":15,"contributions":35},{"day of week":"Fri","hour":16,"contributions":42},{"day of week":"Fri","hour":17,"contributions":24},{"day of week":"Fri","hour":18,"contributions":23},{"day of week":"Fri","hour":19,"contributions":15},{"day of week":"Fri","hour":20,"contributions":19},{"day of week":"Fri","hour":21,"contributions":21},{"day of week":"Fri","hour":22,"contributions":22},{"day of week":"Fri","hour":23,"contributions":13},{"day of week":"Sat","hour":0,"contributions":24},{"day of week":"Sat","hour":1,"contributions":6},{"day of week":"Sat","hour":2,"contributions":0},{"day of week":"Sat","hour":3,"contributions":0},{"day of week":"Sat","hour":4,"contributions":0},{"day of week":"Sat","hour":5,"contributions":0},{"day of week":"Sat","hour":6,"contributions":0},{"day of week":"Sat","hour":7,"contributions":1},{"day of week":"Sat","hour":8,"contributions":10},{"day of week":"Sat","hour":9,"contributions":36},{"day of week":"Sat","hour":10,"contributions":30},{"day of week":"Sat","hour":11,"contributions":29},{"day of week":"Sat","hour":12,"contributions":34},{"day of week":"Sat","hour":13,"contributions":31},{"day of week":"Sat","hour":14,"contributions":30},{"day of week":"Sat","hour":15,"contributions":27},{"day of week":"Sat","hour":16,"contributions":19},{"day of week":"Sat","hour":17,"contributions":18},{"day of week":"Sat","hour":18,"contributions":23},{"day of week":"Sat","hour":19,"contributions":7},{"day of week":"Sat","hour":20,"contributions":17},{"day of week":"Sat","hour":21,"contributions":19},{"day of week":"Sat","hour":22,"contributions":17},{"day of week":"Sat","hour":23,"contributions":13}]},\n", | |
" "mark": { "type": "circle", "color": "black" },\n", | |
"\n", | |
" "encoding": {\n", | |
" "y": {\n", | |
" "field": "day of week",\n", | |
" "type": "nominal",\n", | |
" "sort": ["Sun", "Mon", "Tue", "Wed", "Thr", "Fri", "Sat"]\n", | |
" },\n", | |
" "x": {\n", | |
" "field": "hour",\n", | |
" "type": "nominal"\n", | |
" },\n", | |
" "size": {\n", | |
" "field": "contributions",\n", | |
" "type": "quantitative",\n", | |
" "aggregate": "sum"\n", | |
" }\n", | |
" }\n", | |
" };\n", | |
"\n", | |
" // Embed the visualization in the container with id `vis`\n", | |
" vegaEmbed("#vis", vlSpec, {actions: false});\n", | |
" </script>\n", | |
"</body>\n", | |
"</html>\n", | |
"\"\n", | |
" frameborder=\"0\"\n", | |
" allowfullscreen\n", | |
" ></iframe>\n", | |
" " | |
], | |
"text/plain": [ | |
"<IPython.lib.display.IFrame at 0x7f0e0f5a6780>" | |
] | |
}, | |
"execution_count": 10, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"vegalite_html_injected = vegalite_html.replace(\n", | |
" '{ \"url\": \"../../data/d3-contributions.json\"}',\n", | |
" '{ \"values\": ' + json_str + '}')\n", | |
"IFrame(f'data:text/html;charset=utf-8,{html.escape(vegalite_html_injected)}', width=700, height=220)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"ExecuteTime": { | |
"end_time": "2019-01-10T11:31:43.977399Z", | |
"start_time": "2019-01-10T11:31:43.967836Z" | |
} | |
}, | |
"outputs": [], | |
"source": [] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3", | |
"language": "python", | |
"name": "python3" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.6.6" | |
}, | |
"toc": { | |
"nav_menu": {}, | |
"number_sections": true, | |
"sideBar": true, | |
"skip_h1_title": false, | |
"toc_cell": false, | |
"toc_position": {}, | |
"toc_section_display": "block", | |
"toc_window_display": true | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment