\");\n", " return(divstyle);\n", " },\n", " \n", " createCanvas: function(){\n", " var width = this.model.get('width'); \n", " var height = this.model.get('height'); \n", " var radius = this.model.get('radius'); \n", " console.log(\"--SIZE--\", width, 'x', height, \" radius\", radius);\n", " var svg = d3.select(\"#d3DemoDiv\")\n", " .append(\"svg\")\n", " .attr(\"id\", \"svg\").attr(\"width\", width).attr(\"height\", height);\n", "\n", " this.svg = svg;\n", " var circleView = this;\n", " \n", " svg.on('click', function() {\n", " var coords = d3.mouse(this);\n", " //debugger;\n", " var radius = circleView.radius;\n", " console.log('--MOUSE--', coords, \" radius:\", radius);\n", " var newCircle = {x: coords[0], y: coords[1], radius: 10 * radius,\n", " borderColor: \"black\", fillColor: \"beige\"};\n", " circleView.circles.push(newCircle);\n", " circleView.drawCircle(newCircle);\n", " //debugger;\n", " circleView.model.set(\"circles\", JSON.stringify(circleView.circles));\n", " circleView.touch();\n", " });\n", " }, \n", "\n", " drawCircle: function(obj){\n", " this.svg.append(\"circle\")\n", " .style(\"stroke\", \"gray\")\n", " .style(\"fill\", \"white\")\n", " .attr(\"r\", obj.radius)\n", " .attr(\"cx\", obj.x)\n", " .attr(\"cy\", obj.y)\n", " .on(\"mouseover\", function(){d3.select(this).style(\"fill\", \"aliceblue\");})\n", " .on(\"mouseout\", function(){d3.select(this).style(\"fill\", \"white\");});\n", " },\n", "\n", " render: function() { \n", " this.$el.append(this.createDiv());\n", " this.listenTo(this.model, 'change:circles', this._circles_changed, this);\n", " this.listenTo(this.model, 'change:radius', this._radius_changed, this);\n", " var circleView = this;\n", " function myFunc(){\n", " circleView.createCanvas()\n", " };\n", " setTimeout(myFunc, 500);\n", " },\n", "\n", " _circles_changed: function() {\n", " var circles = this.model.get(\"circles\");\n", " var newCircle = circles[circles.length-1];\n", " console.log('--DRAW--', this.circles);\n", " this.circles.push(newCircle);\n", " console.log('--LENGTH--', circles.length, \" == \", circles.length);\n", " this.drawCircle(newCircle);\n", " },\n", "\n", " _radius_changed: function() {\n", " console.log('--RADIUS--', this.radius, this.model.get('radius'));\n", " this.radius = this.model.get('radius');\n", " }\n", " });\n", " return {\n", " CircleView : CircleView\n", " };\n", "});\n"], "text/plain": ["
"]}, "metadata": {}, "output_type": "display_data"}], "source": ["%%javascript\n", "\"use strict\";\n", "\n", "require.undef('circle');\n", "\n", "define('circle', [\"@jupyter-widgets/base\", \"d3\"], function(widgets, d3) {\n", " \n", " var CircleView = widgets.DOMWidgetView.extend({\n", "\n", " initialize: function() {\n", " console.log(\"---- initialize, this:\");\n", " console.log(this);\n", " this.circles = [];\n", " this.radius = 1;\n", " },\n", "\n", " createDiv: function(){\n", " var width = this.model.get('width'); \n", " var height = this.model.get('height'); \n", " var divstyle = $(\"\");\n", " return(divstyle);\n", " },\n", " \n", " createCanvas: function(){\n", " var width = this.model.get('width'); \n", " var height = this.model.get('height'); \n", " var radius = this.model.get('radius'); \n", " console.log(\"--SIZE--\", width, 'x', height, \" radius\", radius);\n", " var svg = d3.select(\"#d3DemoDiv\")\n", " .append(\"svg\")\n", " .attr(\"id\", \"svg\").attr(\"width\", width).attr(\"height\", height);\n", "\n", " this.svg = svg;\n", " var circleView = this;\n", " \n", " svg.on('click', function() {\n", " var coords = d3.mouse(this);\n", " //debugger;\n", " var radius = circleView.radius;\n", " console.log('--MOUSE--', coords, \" radius:\", radius);\n", " var newCircle = {x: coords[0], y: coords[1], radius: 10 * radius,\n", " borderColor: \"black\", fillColor: \"beige\"};\n", " circleView.circles.push(newCircle);\n", " circleView.drawCircle(newCircle);\n", " //debugger;\n", " circleView.model.set(\"circles\", JSON.stringify(circleView.circles));\n", " circleView.touch();\n", " });\n", " }, \n", "\n", " drawCircle: function(obj){\n", " this.svg.append(\"circle\")\n", " .style(\"stroke\", \"gray\")\n", " .style(\"fill\", \"white\")\n", " .attr(\"r\", obj.radius)\n", " .attr(\"cx\", obj.x)\n", " .attr(\"cy\", obj.y)\n", " .on(\"mouseover\", function(){d3.select(this).style(\"fill\", \"aliceblue\");})\n", " .on(\"mouseout\", function(){d3.select(this).style(\"fill\", \"white\");});\n", " },\n", "\n", " render: function() { \n", " this.$el.append(this.createDiv());\n", " this.listenTo(this.model, 'change:circles', this._circles_changed, this);\n", " this.listenTo(this.model, 'change:radius', this._radius_changed, this);\n", " var circleView = this;\n", " function myFunc(){\n", " circleView.createCanvas()\n", " };\n", " setTimeout(myFunc, 500);\n", " },\n", "\n", " _circles_changed: function() {\n", " var circles = this.model.get(\"circles\");\n", " var newCircle = circles[circles.length-1];\n", " console.log('--DRAW--', this.circles);\n", " this.circles.push(newCircle);\n", " console.log('--LENGTH--', circles.length, \" == \", circles.length);\n", " this.drawCircle(newCircle);\n", " },\n", "\n", " _radius_changed: function() {\n", " console.log('--RADIUS--', this.radius, this.model.get('radius'));\n", " this.radius = this.model.get('radius');\n", " }\n", " });\n", " return {\n", " CircleView : CircleView\n", " };\n", "});"]}, {"cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [{"data": {"application/vnd.jupyter.widget-view+json": {"model_id": "cecc5402ef9c4967a9b571b5050a594a", "version_major": 2, "version_minor": 2}, "text/plain": ["VBox(children=(IntSlider(value=1, max=10), CircleWidget(height=100, radius=1, width=500)))"]}, "metadata": {}, "output_type": "display_data"}], "source": ["cw = CircleWidget(width=500, height=100)\n", "scale = ipywidgets.IntSlider(1, 0, 10)\n", "box = widgets.VBox([scale, cw])\n", "mylink = ipywidgets.jslink((cw, 'radius'), (scale, 'value'))\n", "box"]}, {"cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [], "source": ["cw.drawCircle(x=30, y=30)"]}, {"cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [], "source": ["scale.value = 2"]}, {"cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [], "source": ["cw.drawCircle(x=60, y=30)"]}, {"cell_type": "code", "execution_count": 21, "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.7.2"}}, "nbformat": 4, "nbformat_minor": 2}