Skip to content

Commit 8cafe5e

Browse files
committed
Optimize ScatterPlot drag and drop
1 parent 32002db commit 8cafe5e

File tree

1 file changed

+38
-9
lines changed

1 file changed

+38
-9
lines changed

examples/plotting.ipynb

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636
"\n",
3737
" self.color = color\n",
3838
" self.scheme = scheme\n",
39+
" \n",
40+
" self.background_color = '#f7f7f7'\n",
3941
"\n",
4042
" self.init_plot(x, y)\n",
4143
"\n",
@@ -75,7 +77,7 @@
7577
" background = self[0]\n",
7678
"\n",
7779
" # Draw background\n",
78-
" background.fill_style = '#f7f7f7'\n",
80+
" background.fill_style = self.background_color\n",
7981
" background.global_alpha = 0.3\n",
8082
" background.fill_rect(drawarea_min_x, drawarea_min_y, drawarea_max_x, drawarea_max_y)\n",
8183
" background.global_alpha = 1\n",
@@ -130,10 +132,13 @@
130132
"\n",
131133
" self.n_marks = min(x.shape[0], y.shape[0], size.shape[0], color.shape[0])\n",
132134
"\n",
135+
" # Index of the dragged point\n",
136+
" self.i_mark = -1\n",
137+
"\n",
133138
" self[2].on_mouse_down(self.mouse_down_handler)\n",
134139
" self[2].on_mouse_move(self.mouse_move_handler)\n",
135140
" self[2].on_mouse_up(self.mouse_up_handler)\n",
136-
" \n",
141+
"\n",
137142
" self.draw()\n",
138143
"\n",
139144
" def draw(self):\n",
@@ -161,6 +166,8 @@
161166
" plot_layer.restore()\n",
162167
"\n",
163168
" def mouse_down_handler(self, pixel_x, pixel_y):\n",
169+
" plot_layer = self[1]\n",
170+
"\n",
164171
" for idx in range(self.n_marks):\n",
165172
" mark_x = self.x[idx]\n",
166173
" mark_y = self.y[idx]\n",
@@ -170,18 +177,40 @@
170177
" pixel_y > self.scale_y(mark_y) - mark_size and pixel_y < self.scale_y(mark_y) + mark_size):\n",
171178
" self.i_mark = idx\n",
172179
" self.dragging = True\n",
180+
"\n",
181+
" with hold_canvas(plot_layer):\n",
182+
" plot_layer.fill_style = self.background_color\n",
183+
" plot_layer.stroke_style = self.colormap(self.color[self.i_mark])\n",
184+
"\n",
185+
" plot_layer.fill_arc(self.scale_x(mark_x), self.scale_y(mark_y), mark_size, 0, 2 * pi)\n",
186+
" plot_layer.stroke_arc(self.scale_x(mark_x), self.scale_y(mark_y), mark_size, 0, 2 * pi)\n",
173187
" break\n",
174-
" \n",
188+
"\n",
175189
" def mouse_move_handler(self, pixel_x, pixel_y):\n",
176-
" if self.dragging:\n",
190+
" if self.dragging and self.i_mark != -1:\n",
191+
" interaction_layer = self[2]\n",
192+
"\n",
177193
" unscaled_x = self.unscale_x(pixel_x)\n",
178194
" unscaled_y = self.unscale_y(pixel_y)\n",
179-
" self.x[self.i_mark] = unscaled_x\n",
180-
" self.y[self.i_mark] = unscaled_y\n",
181-
" self.draw()\n",
182-
" \n",
195+
"\n",
196+
" with hold_canvas(interaction_layer):\n",
197+
" interaction_layer.clear()\n",
198+
" interaction_layer.fill_style = self.colormap(self.color[self.i_mark])\n",
199+
" interaction_layer.stroke_style = self.stroke_color\n",
200+
"\n",
201+
" self.x[self.i_mark] = unscaled_x\n",
202+
" self.y[self.i_mark] = unscaled_y\n",
203+
"\n",
204+
" interaction_layer.fill_arc(pixel_x, pixel_y, self.sizes[self.i_mark], 0, 2 * pi)\n",
205+
" interaction_layer.stroke_arc(pixel_x, pixel_y, self.sizes[self.i_mark], 0, 2 * pi)\n",
206+
"\n",
183207
" def mouse_up_handler(self, pixel_x, pixel_y):\n",
184-
" self.dragging = False"
208+
" self.dragging = False\n",
209+
"\n",
210+
" self.draw()\n",
211+
"\n",
212+
" interaction_layer = self[2]\n",
213+
" interaction_layer.clear()"
185214
]
186215
},
187216
{

0 commit comments

Comments
 (0)