Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 46 additions & 42 deletions Embedded_D3.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -199,21 +199,19 @@
"data": {
"application/javascript": [
"require(['d3'], function(d3){\n",
" //a weird idempotency thing\n",
" $(\"#chart1\").remove();\n",
" //create canvas\n",
" element.append(\"<div id='chart1'></div>\");\n",
" $(\"#chart1\").width(\"960px\");\n",
" $(\"#chart1\").height(\"600px\"); \n",
" var margin = {top: 20, right: 20, bottom: 30, left: 40};\n",
" var width = 880 - margin.left - margin.right;\n",
" var height = 500 - margin.top - margin.bottom;\n",
" var frame_width = $(\"#chart1\").width();\n",
" var frame_height = frame_width * 0.75;\n",
" var margin = {top: 20, right: 20, bottom: 30, left: 45};\n",
" var width = frame_width - margin.left - margin.right;\n",
" var height = frame_height - margin.top - margin.bottom;\n",
" var svg = d3.select(\"#chart1\").append(\"svg\")\n",
" .style(\"position\", \"relative\")\n",
" .style(\"max-width\", \"960px\")\n",
" .attr(\"width\", frame_width + \"px\")\n",
" .attr(\"height\", frame_height + \"px\");\n",
" var canvas = svg.append(\"g\")\n",
" .attr(\"width\", width + \"px\")\n",
" .attr(\"height\", (height + 50) + \"px\")\n",
" .append(\"g\")\n",
" .attr(\"height\", height + \"px\")\n",
" .attr(\"transform\", \"translate(\" + margin.left + \",\" + margin.top + \")\");\n",
" \n",
" //set data\n",
Expand Down Expand Up @@ -241,36 +239,40 @@
" var color = d3.scale.category10(); \n",
" \n",
" // x-axis\n",
" svg.append(\"g\")\n",
" .attr(\"class\", \"x axis\")\n",
" canvas.append(\"g\")\n",
" .attr(\"class\", \"axis x\")\n",
" .attr(\"transform\", \"translate(0,\" + height + \")\")\n",
" .call(xAxis)\n",
" .append(\"text\")\n",
" .attr(\"class\", \"label\")\n",
" .attr(\"x\", width - 80)\n",
" .attr(\"y\", -15)\n",
" .style(\"text-anchor\", \"end\")\n",
" .attr(\"x\", width)\n",
" .attr(\"y\", -5)\n",
" .text(\"Average Headway\");\n",
"\n",
" // y-axis\n",
" svg.append(\"g\")\n",
" .attr(\"class\", \"y axis\")\n",
" canvas.append(\"g\")\n",
" .attr(\"class\", \"axis y\")\n",
" .call(yAxis)\n",
" .append(\"text\")\n",
" .attr(\"class\", \"label\")\n",
" .attr(\"transform\", \"rotate(-90)\")\n",
" .attr(\"y\", 0)\n",
" .attr(\"dy\", \"1em\")\n",
" .style(\"text-anchor\", \"end\")\n",
" .text(\"Average Riders\");\n",
" \n",
" canvas.selectAll('.axis line, .axis path')\n",
" .style({'stroke': 'Black', 'fill': 'none', 'stroke-width': '1px'});\n",
"\n",
" canvas.selectAll('.label')\n",
" .style(\"text-anchor\", \"end\");\n",
" \n",
" //NEW: TOOLTIP. \n",
" var tooltip = d3.select(\"body\").append(\"div\")\n",
" .attr(\"class\", \"tooltip\")\n",
" .style(\"opacity\", 0)\n",
" .style(\"background-color\", \"white\");\n",
" \n",
" svg.selectAll(\".dot\")\n",
" canvas.selectAll(\".dot\")\n",
" .data(data)\n",
" .enter().append(\"circle\")\n",
" .attr(\"class\", \"dot\")\n",
Expand All @@ -292,7 +294,7 @@
" .style(\"opacity\", 0);\n",
" });\n",
" \n",
" var legend = svg.selectAll(\".legend\")\n",
" var legend = canvas.selectAll(\".legend\")\n",
" .data(color.domain()) //stores the color <-> label mappings\n",
" .enter().append(\"g\")\n",
" .attr(\"class\", \"legend\")\n",
Expand Down Expand Up @@ -324,21 +326,19 @@
"source": [
"%%javascript\n",
"require(['d3'], function(d3){\n",
" //a weird idempotency thing\n",
" $(\"#chart1\").remove();\n",
" //create canvas\n",
" element.append(\"<div id='chart1'></div>\");\n",
" $(\"#chart1\").width(\"960px\");\n",
" $(\"#chart1\").height(\"600px\"); \n",
" var margin = {top: 20, right: 20, bottom: 30, left: 40};\n",
" var width = 880 - margin.left - margin.right;\n",
" var height = 500 - margin.top - margin.bottom;\n",
" var frame_width = $(\"#chart1\").width();\n",
" var frame_height = frame_width * 0.75;\n",
" var margin = {top: 20, right: 20, bottom: 30, left: 45};\n",
" var width = frame_width - margin.left - margin.right;\n",
" var height = frame_height - margin.top - margin.bottom;\n",
" var svg = d3.select(\"#chart1\").append(\"svg\")\n",
" .style(\"position\", \"relative\")\n",
" .style(\"max-width\", \"960px\")\n",
" .attr(\"width\", frame_width + \"px\")\n",
" .attr(\"height\", frame_height + \"px\");\n",
" var canvas = svg.append(\"g\")\n",
" .attr(\"width\", width + \"px\")\n",
" .attr(\"height\", (height + 50) + \"px\")\n",
" .append(\"g\")\n",
" .attr(\"height\", height + \"px\")\n",
" .attr(\"transform\", \"translate(\" + margin.left + \",\" + margin.top + \")\");\n",
" \n",
" //set data\n",
Expand Down Expand Up @@ -366,36 +366,40 @@
" var color = d3.scale.category10(); \n",
" \n",
" // x-axis\n",
" svg.append(\"g\")\n",
" .attr(\"class\", \"x axis\")\n",
" canvas.append(\"g\")\n",
" .attr(\"class\", \"axis x\")\n",
" .attr(\"transform\", \"translate(0,\" + height + \")\")\n",
" .call(xAxis)\n",
" .append(\"text\")\n",
" .attr(\"class\", \"label\")\n",
" .attr(\"x\", width - 80)\n",
" .attr(\"y\", -15)\n",
" .style(\"text-anchor\", \"end\")\n",
" .attr(\"x\", width)\n",
" .attr(\"y\", -5)\n",
" .text(\"Average Headway\");\n",
"\n",
" // y-axis\n",
" svg.append(\"g\")\n",
" .attr(\"class\", \"y axis\")\n",
" canvas.append(\"g\")\n",
" .attr(\"class\", \"axis y\")\n",
" .call(yAxis)\n",
" .append(\"text\")\n",
" .attr(\"class\", \"label\")\n",
" .attr(\"transform\", \"rotate(-90)\")\n",
" .attr(\"y\", 0)\n",
" .attr(\"dy\", \"1em\")\n",
" .style(\"text-anchor\", \"end\")\n",
" .text(\"Average Riders\");\n",
" \n",
" canvas.selectAll('.axis line, .axis path')\n",
" .style({'stroke': 'Black', 'fill': 'none', 'stroke-width': '1px'});\n",
"\n",
" canvas.selectAll('.label')\n",
" .style(\"text-anchor\", \"end\");\n",
" \n",
" //NEW: TOOLTIP. \n",
" var tooltip = d3.select(\"body\").append(\"div\")\n",
" .attr(\"class\", \"tooltip\")\n",
" .style(\"opacity\", 0)\n",
" .style(\"background-color\", \"white\");\n",
" \n",
" svg.selectAll(\".dot\")\n",
" canvas.selectAll(\".dot\")\n",
" .data(data)\n",
" .enter().append(\"circle\")\n",
" .attr(\"class\", \"dot\")\n",
Expand All @@ -417,7 +421,7 @@
" .style(\"opacity\", 0);\n",
" });\n",
" \n",
" var legend = svg.selectAll(\".legend\")\n",
" var legend = canvas.selectAll(\".legend\")\n",
" .data(color.domain()) //stores the color <-> label mappings\n",
" .enter().append(\"g\")\n",
" .attr(\"class\", \"legend\")\n",
Expand Down