回答編集履歴

4

余談を追加

2018/01/15 04:59

投稿

umyu
umyu

スコア5846

test CHANGED
@@ -105,3 +105,263 @@
105
105
  ■参考情報
106
106
 
107
107
  [d3-drag#event_on](https://github.com/d3/d3-drag#event_on)
108
+
109
+
110
+
111
+ --
112
+
113
+ 218/01/15コメント欄の要望を元に追加
114
+
115
+ ■案1)質問文に合わせた形に
116
+
117
+ ```HTML
118
+
119
+ <!DOCTYPE html>
120
+
121
+ <html lang="ja">
122
+
123
+ <head>
124
+
125
+ <meta charset="utf8">
126
+
127
+ <style>
128
+
129
+ #canvas {
130
+
131
+ background: #666;
132
+
133
+ }
134
+
135
+ </style>
136
+
137
+ <script src="http://d3js.org/d3.v3.min.js"></script>
138
+
139
+ <script src="http://code.jquery.com/jquery-2.1.4.min.js"></script>
140
+
141
+ <script type="text/javascript">
142
+
143
+ $(function(){
144
+
145
+ var svg = d3.select("#svg");
146
+
147
+      // dragイベント
148
+
149
+ var drag = d3.behavior.drag()
150
+
151
+ .on("drag", function(d) {
152
+
153
+ d.x = d3.event.x;
154
+
155
+ d.y = d3.event.y;
156
+
157
+ d3.select(this).attr("x", d.x).attr("y", d.y);
158
+
159
+ });
160
+
161
+      // 矩形を表示する
162
+
163
+ var dataSet = [{x:100,y:100,w:100,h:100},{x:250,y:100,w:100,h:100}];
164
+
165
+ svg.append("g")
166
+
167
+ .append('rect')
168
+
169
+ .data([dataSet[0]])
170
+
171
+ .attr('x', function(d,i){
172
+
173
+ return d.x;
174
+
175
+ })
176
+
177
+ .attr('y', function(d,i){
178
+
179
+ return d.y;
180
+
181
+ })
182
+
183
+ .attr('width', function(d,i){
184
+
185
+ return d.w;
186
+
187
+ })
188
+
189
+ .attr('height', function(d,i){
190
+
191
+ return d.h;
192
+
193
+ })
194
+
195
+ .call(drag);
196
+
197
+
198
+
199
+      // もう一つ矩形を表示する
200
+
201
+ svg.append("g")
202
+
203
+ .append('rect')
204
+
205
+ .data([dataSet[1]])
206
+
207
+ .attr('x', function(d,i){
208
+
209
+ return d.x;
210
+
211
+ })
212
+
213
+ .attr('y', function(d,i){
214
+
215
+ return d.y;
216
+
217
+ })
218
+
219
+ .attr('width', function(d,i){
220
+
221
+ return d.w;
222
+
223
+ })
224
+
225
+ .attr('height', function(d,i){
226
+
227
+ return d.h;
228
+
229
+ })
230
+
231
+ .call(drag);
232
+
233
+ });
234
+
235
+ </script>
236
+
237
+ </head>
238
+
239
+ <body>
240
+
241
+ <svg id="svg" width="1000" height="1000"></svg>
242
+
243
+ </body>
244
+
245
+ </html>
246
+
247
+ ```
248
+
249
+ ■案2)関数化(drawRect)
250
+
251
+ ```HTML
252
+
253
+ <!DOCTYPE html>
254
+
255
+ <html lang="ja">
256
+
257
+ <head>
258
+
259
+ <meta charset="utf8">
260
+
261
+ <style>
262
+
263
+ #canvas {
264
+
265
+ background: #666;
266
+
267
+ }
268
+
269
+ </style>
270
+
271
+ <script src="http://d3js.org/d3.v3.min.js"></script>
272
+
273
+ <script src="http://code.jquery.com/jquery-2.1.4.min.js"></script>
274
+
275
+ <script type="text/javascript">
276
+
277
+ $(function(){
278
+
279
+ var svg = d3.select("#svg");
280
+
281
+      // dragイベント
282
+
283
+ var drag = d3.behavior.drag()
284
+
285
+ .on("drag", function(d) {
286
+
287
+ d.x = d3.event.x;
288
+
289
+ d.y = d3.event.y;
290
+
291
+ d3.select(this).attr("x", d.x).attr("y", d.y);
292
+
293
+ });
294
+
295
+ function drawRect(dataSet, index){
296
+
297
+ svg.append("g")
298
+
299
+ .append('rect')
300
+
301
+ .data([dataSet[index]])
302
+
303
+ .attr('x', function(d,i){
304
+
305
+ return d.x;
306
+
307
+ })
308
+
309
+ .attr('y', function(d,i){
310
+
311
+ return d.y;
312
+
313
+ })
314
+
315
+ .attr('width', function(d,i){
316
+
317
+ return d.w;
318
+
319
+ })
320
+
321
+ .attr('height', function(d,i){
322
+
323
+ return d.h;
324
+
325
+ })
326
+
327
+ .call(drag);
328
+
329
+ }
330
+
331
+     
332
+
333
+ var dataSet = [{x:100,y:100,w:100,h:100},{x:250,y:100,w:100,h:100}];
334
+
335
+ // 矩形を表示する
336
+
337
+ drawRect(dataSet, 0);
338
+
339
+ // もう一つ矩形を表示する
340
+
341
+ drawRect(dataSet, 1);
342
+
343
+ });
344
+
345
+ </script>
346
+
347
+ </head>
348
+
349
+ <body>
350
+
351
+ <svg id="svg" width="1000" height="1000"></svg>
352
+
353
+ </body>
354
+
355
+ </html>
356
+
357
+ ```
358
+
359
+
360
+
361
+ ■余談
362
+
363
+ そもそも論ですが、質問文のコードを以下コードに変更すると分かるのですが、
364
+
365
+ 質問文のコードは2個描画していなく1個目の座標を2回描画しています。。
366
+
367
+ var dataSet = [{x:100,y:100,w:100,h:100},{x:250,y:100,w:100,h:100}];

3

behavior.dragのdragイベントより不要なパラメータを削除

2018/01/15 04:59

投稿

umyu
umyu

スコア5846

test CHANGED
@@ -32,7 +32,7 @@
32
32
 
33
33
  var drag = d3.behavior.drag()
34
34
 
35
- .on("drag", function(d,i) {
35
+ .on("drag", function(d) {
36
36
 
37
37
  d.x = d3.event.x;
38
38
 

2

参考情報を追加

2018/01/14 20:34

投稿

umyu
umyu

スコア5846

test CHANGED
@@ -99,3 +99,9 @@
99
99
  </html>
100
100
 
101
101
  ```
102
+
103
+
104
+
105
+ ■参考情報
106
+
107
+ [d3-drag#event_on](https://github.com/d3/d3-drag#event_on)

1

サンプルデータを追加

2018/01/14 18:59

投稿

umyu
umyu

スコア5846

test CHANGED
@@ -46,7 +46,7 @@
46
46
 
47
47
       // 矩形を表示する
48
48
 
49
- var dataSet = [{x:100,y:100,w:100,h:100}, {x:250,y:100,w:100,h:100}];
49
+ var dataSet = [{x:100,y:100,w:100,h:100}, {x:100,y:100,w:100,h:100}, {x:250,y:100,w:100,h:100}];
50
50
 
51
51
  svg.selectAll("rects")
52
52