-
Notifications
You must be signed in to change notification settings - Fork 7
/
bandline.js
229 lines (201 loc) · 7.09 KB
/
bandline.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
/**
* Bandline renderer
*
* Implementation of Stephen Few's bandlines
*
* Copyright Robert Monfera
* Design: Stephen Few
* Design documentation: https://www.perceptualedge.com/articles/visual_business_intelligence/introducing_bandlines.pdf
*/
function defined(x) {
return !isNaN(x) && isFinite(x) && x !== null
}
function rectanglePath(xr, yr) {
return d3.svg.line()([[xr[0], yr[0]], [xr[1], yr[0]], [xr[1], yr[1]], [xr[0], yr[1]]]) + 'Z'
}
function bandLinePath(valueAccessor, xScale, yScaler, d) {
var drawer = d3.svg.line().defined(compose(defined, property(1)))
return drawer(valueAccessor(d).map(function(s, i) {return [xScale(i), yScaler(d)(s)]}))
}
function bandData(bands, yScaler, d) {
var yScale = yScaler(d)
return bands.map(function(band, i) {
return {key: i, value: band, yScale: yScale}
})
}
function renderBands(root, bands, yScaler, xRanger, yRanger) {
bind(bind(root, 'bands'), 'band', 'path', bandData.bind(0, bands, yScaler))
.transition().duration(duration)
.attr('class', function(d, i) {return 'band s' + i})
.attr('d', function(d) {return rectanglePath(xRanger(d), yRanger(d))})
}
function pointData(valueAccessor, d) {
return valueAccessor(d).map(function(value, i) {return {key: i, value: value, dd: d}}).filter(compose(defined, value))
}
function renderPoints(root, valueAccessor, pointStyleAccessor, rScale, xSpec, ySpec) {
bind(root, 'valuePoints', 'g', pointData.bind(0, valueAccessor))
.entered
.attr('transform', translate(xSpec, ySpec))
root['valuePoints']
.transition().duration(duration)
.attr('transform', translate(xSpec, ySpec))
bind(root['valuePoints'], 'point', 'circle')
.attr('class', function(d) {return 'point ' + pointStyleAccessor(d.value)})
.attr('r', function(d) {return rScale(pointStyleAccessor(d.value))})
root['valuePoints'].exit().remove()
}
function valuesExtent(valueAccessor, d) {
return d3.extent(valueAccessor(d).filter(defined))
}
function sparkStripBoxPath(valueAccessor, xScale, yRange, d) {
var midY = d3.mean(yRange)
var halfHeight = (yRange[1] - yRange[0]) / 2
return rectanglePath(
valuesExtent(valueAccessor, d).map(xScale),
[midY - halfHeight / 2, midY + halfHeight / 2]
)
}
function renderExtent(root, valueAccessor, xScale, yRange) {
bind(root, 'valueBox', 'path')
.transition().duration(duration)
.attr('d', sparkStripBoxPath.bind(0, valueAccessor, xScale, yRange))
}
function renderValueLine(root, valueAccessor, xScale, yScaler) {
bind(root, 'valueLine', 'path')
.transition().duration(duration)
.attr('d', bandLinePath.bind(0, valueAccessor, xScale, yScaler))
}
function renderAxis(root, yAxis, xScale, yScaler) {
bind(root, 'axes', 'g', yAxis ? du.repeat : [])
.each(function(d) {
yAxis.scale(yScaler(d))(d3.select(this).transition().duration(duration))
})
.entered
.attr('transform', translateX(xScale.range()[['left', 'right'].indexOf(yAxis && yAxis.orient())]))
}
function bandLine() {
function renderBandLine(root) {
var bandLine = bind(root, 'bandLine')
renderBands(bandLine, _bands, _yScalerOfBandLine, constant(_xScaleOfBandLine.range()), function (d) {
return d.value.map(d.yScale)
})
renderValueLine(bandLine, _valueAccessor, _xScaleOfBandLine, _yScalerOfBandLine)
renderAxis(bandLine, _yAxis, _xScaleOfBandLine, _yScalerOfBandLine)
renderPoints(bandLine, _valueAccessor, _pointStyleAccessor, _rScaleOfBandLine, compose(_xScaleOfBandLine, key), function (d) {
return _yScalerOfBandLine(d.dd)(d.value)
})
}
function renderSparkStrip(root) {
var sparkStrip = bind(root, 'sparkStrip')
renderBands(sparkStrip, _bands, _yScalerOfSparkStrip, function (d) {
return d.value.map(_xScaleOfSparkStrip)
}, constant(_yRange))
renderExtent(sparkStrip, _valueAccessor, _xScaleOfSparkStrip, _yRange)
renderPoints(sparkStrip, _valueAccessor, _pointStyleAccessor, _rScaleOfSparkStrip, compose(_xScaleOfSparkStrip, value), _yScalerOfSparkStrip())
}
function yScalerOfBandLineCalc() {
return function (d) {
return d3.scale.linear().domain(valuesExtent(_valueAccessor, d)).range(_yRange).clamp(true)
}
}
var _bands = [[0, 0.25], [0.25, 0.5], [0.5, 0.75], [0.75, 1]]
var bands = function(spec) {
if(spec !== void(0)) {
_bands = spec
return obj
} else {
return bands
}
}
var _valueAccessor = value
var valueAccessor = function(spec) {
if(spec !== void(0)) {
_valueAccessor = spec
_yScalerOfBandLine = yScalerOfBandLineCalc()
return obj
} else {
return _valueAccessor
}
}
var _xScaleOfBandLine = d3.scale.linear()
var xScaleOfBandLine = function(spec) {
if(spec !== void(0)) {
_xScaleOfBandLine = spec
return obj
} else {
return _xScaleOfBandLine
}
}
var _xScaleOfSparkStrip = d3.scale.linear()
var xScaleOfSparkStrip = function(spec) {
if(spec !== void(0)) {
_xScaleOfSparkStrip = spec
return obj
} else {
return _xScaleOfSparkStrip
}
}
var _rScaleOfBandLine = constant(2)
var rScaleOfBandLine = function(spec) {
if(spec !== void(0)) {
_rScaleOfBandLine = spec
return obj
} else {
return _rScaleOfBandLine
}
}
var _rScaleOfSparkStrip = constant(2)
var rScaleOfSparkStrip = function(spec) {
if(spec !== void(0)) {
_rScaleOfSparkStrip = spec
return obj
} else {
return _rScaleOfSparkStrip
}
}
var _yRange = [0, 1]
var _yScalerOfSparkStrip
var _yScalerOfBandLine
var yRange = function(spec) {
if(spec !== void(0)) {
_yRange = spec
_yScalerOfSparkStrip = constant(d3.mean(_yRange))
_yScalerOfBandLine = yScalerOfBandLineCalc()
return obj
} else {
return _yRange
}
}
var _yAxis = false
var yAxis = function(spec) {
if(spec !== void(0)) {
_yAxis = spec
return obj
} else {
return _yAxis
}
}
var _pointStyleAccessor = constant('normal')
var pointStyleAccessor = function(spec) {
if(spec !== void(0)) {
_pointStyleAccessor = spec
return obj
} else {
return _pointStyleAccessor
}
}
var obj = {
renderBandLine: renderBandLine,
renderSparkStrip: renderSparkStrip,
bands: bands,
valueAccessor: valueAccessor,
xScaleOfBandLine: xScaleOfBandLine,
xScaleOfSparkStrip: xScaleOfSparkStrip,
rScaleOfBandLine: rScaleOfBandLine,
rScaleOfSparkStrip: rScaleOfSparkStrip,
yRange: yRange,
yAxis: yAxis,
pointStyleAccessor: pointStyleAccessor
}
return obj
}