-
Notifications
You must be signed in to change notification settings - Fork 0
/
shinydrawr-d3v5.js
455 lines (373 loc) · 14.4 KB
/
shinydrawr-d3v5.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
// !preview r2d3 data = data_to_json(list(line_data = tibble(x = seq(0, 20, .25), y = exp((x-15)/30)), point_data = tibble(x = seq(0, 20, length.out = 30), y = exp((x-15)/30 + rnorm(30, 0, 0.05))))), options = list(free_draw = FALSE, draw_start = 10, points_end = 15, pin_start = TRUE, x_range = c(0,20), y_range = c(.5,2), line_style = list(strokeWidth = 4), data_line_color = 'steelblue', drawn_line_color = 'steelblue', show_finished = TRUE, shiny_message_loc = 'my_shiny_app', linear = 'true', points = "partial", aspect_ratio = 1, x_by = 0.5), d3_version = "5", dependencies = c('d3-jetpack'),
// Make sure R has the following loaded
// library(tibble)
// data_to_json <- function(data) {jsonlite::toJSON(data, dataframe = "rows", auto_unbox = FALSE, rownames = TRUE)}
// define variable system_font
const system_font = `-apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif, "Apple Color // // Emoji", "Segoe UI Emoji", "Segoe UI Symbol"`;
// ---------------------------------------------------------------------------------------------
// define variable margins
// if options.title == T then set top = 50, else top = 15 (see options list at top, currently, no title defined...)
const margin = {left: 55,
right: 10,
top: options.title ? 40: 10,
bottom: options.title? 25: 55};
// define variable default line attributes
// do not fill the line in (default is filled in black beneath the line)
// stroke provides the color of the line stroke. This is either options.data_line_color (not yet defined) or steelblue
// sets the the stroke width (how thick)
// sets how tojoin the lines? and end the lines? rounded.
// Since we are using Object.assign, we are passing the source options.line_style into the vector of information defined. See top for options list.
const default_line_attrs = Object.assign({
fill: "none",
stroke: options.data_line_color || 'steelblue',
strokeWidth: 4,
strokeLinejoin: "round",
strokeLinecap: "round",
}, options.line_style);
// defines a changing variable called state??
// provides the data from top
// appends the svg group and moves it to the correct location...
// sets the width and height of the plot
// Since we are using object.assign, the options are passed in as the source, this will "overwrite" any information provided in the options???
// Is state like our wrapper?
let state = Object.assign({
line_data: data.line_data,
point_data: data.point_data,
svg: svg.append('g').translate([margin.left, margin.top]).attr("class", "wrapper"),
w: height*options.aspect_ratio - margin.left - margin.right,
h: height - margin.top - margin.bottom,
}, options);
// option variables are passed into state
// console.log(state.y_range)
// To distinguish between code that runs at initialization-time only and
// code that runs when data changes, organize your code so that the code
// which responds to data changes is contained within the r2d3.onRender()
// https://rstudio.github.io/r2d3/articles/advanced_rendering.html
r2d3.onRender(function(data, svg, width, height, options) {
state.line_data = data.line_data;
state.point_data = data.point_data;
state = Object.assign(state, options);
state.options = options;
state.w = height*options.aspect_ratio;
start_drawer(state);
});
// An explicit resize handler
// is this like our bounds??? or does this have to do with scales?
// https://rstudio.github.io/r2d3/articles/advanced_rendering.html
// redraws plot as you resize your browser window
// (box has changed size that we did not do on code end)
r2d3.onResize(function(width, height, options) {
state.w = height*state.options.aspect_ratio - margin.left - margin.right;
state.h = height - margin.top - margin.bottom;
start_drawer(state, reset = false);
});
// Main function that draws current state of viz
// set up scales & draw true line if we decide to do that.
function start_drawer(state, reset = true){
const scales = setup_scales(state);
if(!state.free_draw){
draw_true_line(state, scales, state.draw_start);
}
// Cover hidden portion with a rectangle
// const line_hider = setup_line_hider(state.svg, state.draw_start, scales);
// if we reset (these are points that can be drawn) remove what user has drawn.
if(reset){
// start with the svg object provided by r2d3
// multiassign where setup_drawable_points is grabbing the parameters it needs from state: data, freedraw and draw_start
state.drawable_points = setup_drawable_points(state);
}
// if we have points, we draw user's line.
draw_user_line(state, scales);
draw_rectangle(state, scales);
draw_finished_line(state, scales, state.draw_start);
// draw points for initial portion
if(state.points != "none"){
draw_points(state, scales);
}
// invert from pixle to data scale when they draw their points
const on_drag = function(){
const drag_x = scales.x.invert(d3.event.x);
const drag_y = scales.y.invert(d3.event.y);
fill_in_closest_point(state, drag_x, drag_y);
draw_user_line(state, scales);
draw_rectangle(state, scales);
};
// line_status is set by draw watcher - get user status line
// if some points missing - in progress
// complete line - done
// passes drawn points to shiny when click done
// as long as there are dots (missing spaces) it isn't going to pass data back.
const on_end = function(){
// Check if all points are drawn so we can reveal line
const line_status = get_user_line_status(state);
if(line_status === 'done'){
// User has completed line drawing
//if(state.show_finished) line_hider.reveal();
//if(!state.free_draw) line_hider.reveal();
if(state.show_finished){
draw_finished_line(state, scales, state.draw_start);
}
if(state.shiny_message_loc){
// Make sure shiny is available before sending message
if(typeof Shiny !== 'undefined'){
// Send drawn points off to server
Shiny.onInputChange(
state.shiny_message_loc,
state.drawable_points.map(d => d.y)
);
} else {
console.log(`Sending message to ${state.shiny_message_loc}`);
}
}
}
};
setup_draw_watcher(state.svg, scales, on_drag, on_end);
// Do we have a title?
if(state.title){
state.svg.append('text')
.at({
y: -margin.top/2,
dominantBaseline: 'middle',
fontSize: '1.5rem',
})
.style('font-family', system_font)
.text(state.title);
}
}
function setup_drawable_points({line_data, free_draw, draw_start}){
if(free_draw){
return line_data.map(d => ({x: d.x, y: null}));
} else {
return line_data
.filter(d => d.x >= draw_start)
.map((d,i) => ({
x: d.x,
y: i === 0 ? d.y: null
}));
}
}
function get_user_line_status({drawable_points, free_draw}){
const num_points = drawable_points.length;
const num_filled = d3.sum(drawable_points.map(d => d.y === null ? 0: 1));
const num_starting_filled = free_draw ? 0: 1;
if(num_filled === num_starting_filled){
return 'unstarted';
} else if(num_points === num_filled){
return 'done';
} else {
return 'in_progress';
}
}
// Draw visable portion of line
function draw_true_line({svg, line_data, draw_start}, scales){
var df = line_data.filter(function(d){ return d.x<=draw_start})
state.svg.selectAppend("path.shown_line")
.datum(df)
.at(default_line_attrs)
.attr("d", scales.line_drawer);
}
function draw_points({svg, point_data, points_end, points}, scales){
if(points == "partial"){
var df = point_data.filter(function(d){return (d.x<=points_end)});
} else {
var df = point_data;
}
const dots = state.svg.selectAll("circle").data(df)
dots
.enter().append("circle")
.merge(dots)
.attr("cx", d => scales.x(d.x))
.attr("cy", d => scales.y(d.y))
.attr("r", 2)
.style("fill", "black")
.style("opacity", 0.8)
.style("stroke", "black")
}
function draw_rectangle({svg, drawable_points, line_data, draw_start, width, height, free_draw, x_by}, scales){
if(get_user_line_status(state) === 'unstarted'){
if(free_draw){
var xmin = line_data[0].x
var len = line_data.length - 1
var xmax = line_data[len].x
var drawSpace_start = scales.x(xmin)
var drawSpace_end = scales.x(xmax)
} else {
var drawSpace_start = scales.x(draw_start)
var drawSpace_end = state.w
}
} else {
if(get_user_line_status(state) === 'done'){
var drawSpace_start = scales.x(100)
var drawSpace_end = scales.x(110)
} else {
var df = drawable_points.filter(function(d){return (d.y === null)});
var xmin = df[0].x - x_by;
var len = line_data.length - 1
var xmax = line_data[len].x
var drawSpace_start = scales.x(xmin)
var drawSpace_end = scales.x(xmax)
}
}
const draw_region = state.svg.selectAppend("rect");
draw_region
.attr("x", drawSpace_start)
.attr("width",drawSpace_end - drawSpace_start)
.attr("y", 0)
.attr("height", state.h)
//.style("fill", "#e0f3f3")
.style("fill-opacity", 0.4)
.style("fill", "rgba(255,255,0,.8)")
}
function draw_user_line(state, scales){
const {svg, drawable_points, drawn_line_color} = state;
const user_line = state.svg.selectAppend("path.user_line");
// Only draw line if there's something to draw.
if(get_user_line_status(state) === 'unstarted'){
user_line.remove();
return;
}
// Draws the points the user is drawing with their mouse
user_line
.datum(drawable_points)
.at(default_line_attrs)
.attr('stroke', drawn_line_color)
.attr("d", scales.line_drawer)
.style("stroke-dasharray", ("1, 7"));
}
function draw_finished_line({svg, line_data, draw_start, free_draw}, scales){
if(!free_draw){
var df = line_data.filter(function(d){ return d.x >= draw_start})
} else {
var df = line_data
}
const finished_line = state.svg.selectAppend("path.finished_line")
// Only draw line if there's something to draw.
if(get_user_line_status(state) === 'unstarted'){
finished_line.remove();
return;
}
finished_line
.datum(df)
.at(default_line_attrs)
.attr("d", scales.line_drawer)
.attr("opacity", 0.5)
}
// from state we need drawable_points - from setup_drawable_points() function that modifies state (get all x points bigger than or equal to draw_start and set up with a null), pin_start, and free_draw parameters
// drag_x, drag_y come from on_drag() function
function fill_in_closest_point({drawable_points, pin_start, free_draw}, drag_x, drag_y){
// find closest point on data to draw
let last_dist = Infinity;
let current_dist;
// If nothing triggers break statement than closest point is last point
let closest_index = drawable_points.length - 1;
const starting_index = free_draw ? 0 : (pin_start ? 1: 0);
// for loop to check where closest point to where I am
for(i = starting_index; i < drawable_points.length; i++){
current_dist = Math.abs(drawable_points[i].x - drag_x);
// If distances start going up we've passed the closest point
if(last_dist - current_dist < 0) {
closest_index = i - 1;
break;
}
last_dist = current_dist;
}
drawable_points[closest_index].y = drag_y;
}
function setup_draw_watcher(svg, scales, on_drag, on_end){
// could have called drag_watcher whatever we wanted
// .at is space it begins watching
// .call is "do something" d3.drag() is the mouse action puts listeners to react to user input.
svg.selectAppend('rect.drag_watcher')
.at({
height: scales.y.range()[0],
width: scales.x.range()[1],
fill: 'grey',
fillOpacity: 0,
})
.call(
d3.drag()
.on("drag", on_drag)
.on("end", on_end)
);
}
function add_axis_label(label, y_axis = true){
const bump_axis = y_axis ? 'x': 'y';
const axis_label_style = {
[bump_axis]: bump_axis == 'y' ? 3: -2,
textAnchor: 'end',
fontWeight: '500',
fontSize: '0.9rem',
};
return g => {
let last_tick = g.select(".tick:last-of-type");
const no_ticks = last_tick.empty();
if(no_ticks){
last_tick = g.append('g')
.attr('class', 'tick');
}
last_tick.select("line").remove();
last_tick.selectAppend("text")
.at(axis_label_style)
.html(label);
};
}
// Setup scales for visualization
function setup_scales(state){
// multi-assign: x_range, etc. coming from options
const {w, h, line_data, x_range, y_range, x_name, y_name, linear} = state;
// convert x from data scale to pixle scale
const x = d3.scaleLinear()
.domain(x_range || d3.extent(line_data, d => d.x))
.range([0, w]);
//console.log(linear);
if (linear == 'true') {
//console.log('in linear block');
// converts from data linear scale to pixle scale
var y = d3.scaleLinear()
.domain(y_range || d3.extent(line_data, d => d.y))
.range([h, 0]);
} else {
//console.log('in log block');
// converts from data log scale to pixle scale
var y = d3.scaleLog()
.domain(y_range || d3.extent(line_data, d => d.y))
.range([h, 0]).base(10);
}
const xAxis = d3.axisBottom().scale(x).tickSizeOuter(0);
const yAxis = d3.axisLeft().scale(y).tickFormat(d3.format(".4")).tickSizeOuter(0);
const xAxisGrid = d3.axisBottom().scale(x).tickSize(-h).tickFormat('');
const yAxisGrid = d3.axisLeft().scale(y).tickSize(-w).tickFormat('');
// Remove all grid related things first
// remove everything when you convert from linear and log and vise versa.
// want html element <g> that has class axis-grid
state.svg.selectAll("g.x_grid").remove()
state.svg.selectAll("g.y_grid").remove()
// could call axis-grid "fred"
state.svg.selectAll("g.axis-grid").remove()
state.svg.selectAll("path.shown_line").remove()
state.svg.selectAll("circle").remove()
state.svg.selectAppend("g.x_grid")
.attr('class', 'x axis-grid')
.translate([0,h])
.call(xAxisGrid);
state.svg.selectAppend("g.y_grid")
.attr('class', 'y axis-grid')
.call(yAxisGrid);
state.svg.selectAll(".axis-grid .tick")
.style("stroke", "light-grey")
.style("opacity", ".3");
state.svg.selectAppend("g.x_axis")
.translate([0,h])
.call(xAxis);
state.svg.selectAppend("g.y_axis")
.call(yAxis);
const line_drawer = d3.line()
.defined(d => d.y !== null)
.x(d => x(d.x))
.y(d => y(d.y));
return {
x,
y,
line_drawer,
};
}