-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathd3.playground.js
129 lines (109 loc) · 3.51 KB
/
d3.playground.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
d3.playground = (function (d, i) {
var turnedOn = false;
// Make a special home for our controls
var holder = d3.select('body')
.append('div')
.style({
display: turnedOn ? 'block' : 'none',
height: '500px',
width: '500px',
position: 'fixed',
top: 0,
left: 0,
'background-color': 'rgba(132,234,12, 0.4)'
});
function turnOn() {
if (turnedOn) return;
holder.style('display', 'block');
}
/* Begin Utilities */
function createSlider(trigger, name, value, control, arg4, arg5,a,r,g,s) {
var min = arg4 || 0;
var max = arg5 || 10;
var step = 1;
var initial = typeof value === 'function' ?
min && max && (max + min) / 2
: value;
var slider = d3.slider()
.value(initial)
.min(min)
.max(max)
.step(step || 1)
holder.append('div').call(slider);
slider.on('slide', trigger)
}
function createTextbox(trigger, name, value, control, a,r,g,s) {
var box = holder.append('div')
.append('center')
.append('input')
.attr('type', 'text')
.attr('value', value.toString())
.on('keyup', function (evt) {
trigger(evt, box.node().value)
})
}
function createCodeArea(trigger, name, value, control, a,r,g,s) {
var initial = typeof value === 'function'
? value.toString() // ECMA5 print out user code as string
: 'function (d, i) { return ' + value + '; }'
var textarea = holder.append('textarea')
.text(initial)
.on('keyup', function (evt) {
var userCode = textarea.node().value
// Not sure this should be here.
// Seems to violate knowing about d3 w/ d, i
trigger(evt, function (d, i) {
var args = [d, i];
try {
// Wrap user code in a IIFE
return eval('(' + userCode + ')(' + args.join(',') + ')')
} catch (e) {
// Do nothing but report
console.error('Value not legal JS', value, e, e.stack)
return undefined;
}
})
})
}
function createColorPicker(trigger, name, value, control, a,r,g,s) {
function rgbToHex(rgb) {
return 'rgb(' +
~~(rgb[0] * 255) + ',' +
~~(rgb[1] * 255) + ',' +
~~(rgb[2] * 255) + ')' ;
}
var textarea = holder.append('input')
.classed('color', true)
.on('change', function () {
trigger(rgbToHex(this.color.rgb))
})
}
/* End Utilities */
function create(trigger, name, value, control, a,r,g,s) {
// Show panel only when in use
turnOn();
switch(control) {
case "code":
createCodeArea.apply(null, arguments)
break;
case "slider":
createSlider.apply(null, arguments)
break;
case "color":
createColorPicker.apply(null, arguments)
break;
case "text":
createTextbox.apply(null, arguments);
break;
default:
console.error.bind(console)('d3.playground::create not behaving', arguments, this, C=window.C&&window.C+1||1); // Testing - Delete me
}
}
function createFromAutocontrol(trigger, args) {
create.apply(null, [].concat.apply([trigger], args))
}
return {
create: create,
createFromAutocontrol: createFromAutocontrol
};
})()