todidnt 0.2.0 → 0.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,56 @@
1
+ .d3-tip {
2
+ line-height: 1;
3
+ font-weight: bold;
4
+ font-size: 0.8em;
5
+ padding: 8px 10px;
6
+ background: rgba(0, 0, 0, 0.8);
7
+ color: #fff;
8
+ border-radius: 2px;
9
+ pointer-events: none;
10
+ }
11
+
12
+ /* Creates a small triangle extender for the tooltip */
13
+ .d3-tip:after {
14
+ box-sizing: border-box;
15
+ display: inline;
16
+ font-size: 10px;
17
+ width: 100%;
18
+ line-height: 1;
19
+ color: rgba(0, 0, 0, 0.8);
20
+ position: absolute;
21
+ pointer-events: none;
22
+ }
23
+
24
+ /* Northward tooltips */
25
+ .d3-tip.n:after {
26
+ content: "\25BC";
27
+ margin: -3px 0 0 0;
28
+ top: 100%;
29
+ left: 0;
30
+ text-align: center;
31
+ }
32
+
33
+ /* Eastward tooltips */
34
+ .d3-tip.e:after {
35
+ content: "\25C0";
36
+ margin: -4px 0 0 0;
37
+ top: 50%;
38
+ left: -8px;
39
+ }
40
+
41
+ /* Southward tooltips */
42
+ .d3-tip.s:after {
43
+ content: "\25B2";
44
+ margin: 0 0 1px 0;
45
+ top: -8px;
46
+ left: 0;
47
+ text-align: center;
48
+ }
49
+
50
+ /* Westward tooltips */
51
+ .d3-tip.w:after {
52
+ content: "\25B6";
53
+ margin: -4px 0 0 -1px;
54
+ top: 50%;
55
+ left: 100%;
56
+ }
@@ -1,4 +1,5 @@
1
- <script src='http://d3js.org/d3.v3.min.js'></script>
1
+ <script src='js/vendor/d3.min.js'></script>
2
+ <script src='js/vendor/d3-tip.js'></script>
2
3
  <script type='text/javascript'>
3
4
  PRELOADED_DATA = <%= data.to_json %>;
4
5
  </script>
@@ -36,6 +36,11 @@ var yAxis = d3.svg.axis()
36
36
  .orient("left")
37
37
  .tickFormat(d3.format(".2s"));
38
38
 
39
+ var tip = d3.tip()
40
+ .attr('class', 'd3-tip')
41
+ .offset([-10, 0])
42
+ .html(function(d) { return d.name + " - " + (d.y1 - d.y0); });
43
+
39
44
  // Transform data
40
45
 
41
46
  var authors = PRELOADED_DATA['authors'];
@@ -63,6 +68,8 @@ var svg = d3.select("section.content").append("svg")
63
68
  .append("g")
64
69
  .attr("transform", "translate(" + margin.left + "," + margin.top + ")");
65
70
 
71
+ svg.call(tip);
72
+
66
73
  x.domain(data.map(function(d) { return d.date; }));
67
74
  y.domain([0, d3.max(data, function(d) { return d.total; })]);
68
75
 
@@ -93,7 +100,9 @@ dates.selectAll("rect")
93
100
  .attr("width", x.rangeBand())
94
101
  .attr("y", function(d) { return y(d.y1); })
95
102
  .attr("height", function(d) { return y(d.y0) - y(d.y1); })
96
- .style("fill", function(d) { return getColor(d.name); });
103
+ .style("fill", function(d) { return getColor(d.name); })
104
+ .on("mouseover", tip.show)
105
+ .on("mouseout", tip.hide);
97
106
 
98
107
  // Legend
99
108
 
@@ -0,0 +1,293 @@
1
+ // d3.tip
2
+ // Copyright (c) 2013 Justin Palmer
3
+ //
4
+ // Tooltips for d3.js SVG visualizations
5
+
6
+ (function (root, factory) {
7
+ if (typeof define === 'function' && define.amd) {
8
+ // AMD. Register as an anonymous module with d3 as a dependency.
9
+ define(['d3'], factory)
10
+ } else {
11
+ // Browser global.
12
+ root.d3.tip = factory(root.d3)
13
+ }
14
+ }(this, function (d3) {
15
+
16
+ // Public - contructs a new tooltip
17
+ //
18
+ // Returns a tip
19
+ return function() {
20
+ var direction = d3_tip_direction,
21
+ offset = d3_tip_offset,
22
+ html = d3_tip_html,
23
+ node = initNode(),
24
+ svg = null,
25
+ point = null,
26
+ target = null
27
+
28
+ function tip(vis) {
29
+ svg = getSVGNode(vis)
30
+ point = svg.createSVGPoint()
31
+ document.body.appendChild(node)
32
+ }
33
+
34
+ // Public - show the tooltip on the screen
35
+ //
36
+ // Returns a tip
37
+ tip.show = function() {
38
+ var args = Array.prototype.slice.call(arguments)
39
+ if(args[args.length - 1] instanceof SVGElement) target = args.pop()
40
+
41
+ var content = html.apply(this, args),
42
+ poffset = offset.apply(this, args),
43
+ dir = direction.apply(this, args),
44
+ nodel = d3.select(node),
45
+ i = directions.length,
46
+ coords,
47
+ scrollTop = document.documentElement.scrollTop || document.body.scrollTop,
48
+ scrollLeft = document.documentElement.scrollLeft || document.body.scrollLeft
49
+
50
+ nodel.html(content)
51
+ .style({ opacity: 1, 'pointer-events': 'all' })
52
+
53
+ while(i--) nodel.classed(directions[i], false)
54
+ coords = direction_callbacks.get(dir).apply(this)
55
+ nodel.classed(dir, true).style({
56
+ top: (coords.top + poffset[0]) + scrollTop + 'px',
57
+ left: (coords.left + poffset[1]) + scrollLeft + 'px'
58
+ })
59
+
60
+ return tip
61
+ }
62
+
63
+ // Public - hide the tooltip
64
+ //
65
+ // Returns a tip
66
+ tip.hide = function() {
67
+ nodel = d3.select(node)
68
+ nodel.style({ opacity: 0, 'pointer-events': 'none' })
69
+ return tip
70
+ }
71
+
72
+ // Public: Proxy attr calls to the d3 tip container. Sets or gets attribute value.
73
+ //
74
+ // n - name of the attribute
75
+ // v - value of the attribute
76
+ //
77
+ // Returns tip or attribute value
78
+ tip.attr = function(n, v) {
79
+ if (arguments.length < 2 && typeof n === 'string') {
80
+ return d3.select(node).attr(n)
81
+ } else {
82
+ var args = Array.prototype.slice.call(arguments)
83
+ d3.selection.prototype.attr.apply(d3.select(node), args)
84
+ }
85
+
86
+ return tip
87
+ }
88
+
89
+ // Public: Proxy style calls to the d3 tip container. Sets or gets a style value.
90
+ //
91
+ // n - name of the property
92
+ // v - value of the property
93
+ //
94
+ // Returns tip or style property value
95
+ tip.style = function(n, v) {
96
+ if (arguments.length < 2 && typeof n === 'string') {
97
+ return d3.select(node).style(n)
98
+ } else {
99
+ var args = Array.prototype.slice.call(arguments)
100
+ d3.selection.prototype.style.apply(d3.select(node), args)
101
+ }
102
+
103
+ return tip
104
+ }
105
+
106
+ // Public: Set or get the direction of the tooltip
107
+ //
108
+ // v - One of n(north), s(south), e(east), or w(west), nw(northwest),
109
+ // sw(southwest), ne(northeast) or se(southeast)
110
+ //
111
+ // Returns tip or direction
112
+ tip.direction = function(v) {
113
+ if (!arguments.length) return direction
114
+ direction = v == null ? v : d3.functor(v)
115
+
116
+ return tip
117
+ }
118
+
119
+ // Public: Sets or gets the offset of the tip
120
+ //
121
+ // v - Array of [x, y] offset
122
+ //
123
+ // Returns offset or
124
+ tip.offset = function(v) {
125
+ if (!arguments.length) return offset
126
+ offset = v == null ? v : d3.functor(v)
127
+
128
+ return tip
129
+ }
130
+
131
+ // Public: sets or gets the html value of the tooltip
132
+ //
133
+ // v - String value of the tip
134
+ //
135
+ // Returns html value or tip
136
+ tip.html = function(v) {
137
+ if (!arguments.length) return html
138
+ html = v == null ? v : d3.functor(v)
139
+
140
+ return tip
141
+ }
142
+
143
+ function d3_tip_direction() { return 'n' }
144
+ function d3_tip_offset() { return [0, 0] }
145
+ function d3_tip_html() { return ' ' }
146
+
147
+ var direction_callbacks = d3.map({
148
+ n: direction_n,
149
+ s: direction_s,
150
+ e: direction_e,
151
+ w: direction_w,
152
+ nw: direction_nw,
153
+ ne: direction_ne,
154
+ sw: direction_sw,
155
+ se: direction_se
156
+ }),
157
+
158
+ directions = direction_callbacks.keys()
159
+
160
+ function direction_n() {
161
+ var bbox = getScreenBBox()
162
+ return {
163
+ top: bbox.n.y - node.offsetHeight,
164
+ left: bbox.n.x - node.offsetWidth / 2
165
+ }
166
+ }
167
+
168
+ function direction_s() {
169
+ var bbox = getScreenBBox()
170
+ return {
171
+ top: bbox.s.y,
172
+ left: bbox.s.x - node.offsetWidth / 2
173
+ }
174
+ }
175
+
176
+ function direction_e() {
177
+ var bbox = getScreenBBox()
178
+ return {
179
+ top: bbox.e.y - node.offsetHeight / 2,
180
+ left: bbox.e.x
181
+ }
182
+ }
183
+
184
+ function direction_w() {
185
+ var bbox = getScreenBBox()
186
+ return {
187
+ top: bbox.w.y - node.offsetHeight / 2,
188
+ left: bbox.w.x - node.offsetWidth
189
+ }
190
+ }
191
+
192
+ function direction_nw() {
193
+ var bbox = getScreenBBox()
194
+ return {
195
+ top: bbox.nw.y - node.offsetHeight,
196
+ left: bbox.nw.x - node.offsetWidth
197
+ }
198
+ }
199
+
200
+ function direction_ne() {
201
+ var bbox = getScreenBBox()
202
+ return {
203
+ top: bbox.ne.y - node.offsetHeight,
204
+ left: bbox.ne.x
205
+ }
206
+ }
207
+
208
+ function direction_sw() {
209
+ var bbox = getScreenBBox()
210
+ return {
211
+ top: bbox.sw.y,
212
+ left: bbox.sw.x - node.offsetWidth
213
+ }
214
+ }
215
+
216
+ function direction_se() {
217
+ var bbox = getScreenBBox()
218
+ return {
219
+ top: bbox.se.y,
220
+ left: bbox.e.x
221
+ }
222
+ }
223
+
224
+ function initNode() {
225
+ var node = d3.select(document.createElement('div'))
226
+ node.style({
227
+ position: 'absolute',
228
+ top: 0,
229
+ opacity: 0,
230
+ 'pointer-events': 'none',
231
+ 'box-sizing': 'border-box'
232
+ })
233
+
234
+ return node.node()
235
+ }
236
+
237
+ function getSVGNode(el) {
238
+ el = el.node()
239
+ if(el.tagName.toLowerCase() == 'svg')
240
+ return el
241
+
242
+ return el.ownerSVGElement
243
+ }
244
+
245
+ // Private - gets the screen coordinates of a shape
246
+ //
247
+ // Given a shape on the screen, will return an SVGPoint for the directions
248
+ // n(north), s(south), e(east), w(west), ne(northeast), se(southeast), nw(northwest),
249
+ // sw(southwest).
250
+ //
251
+ // +-+-+
252
+ // | |
253
+ // + +
254
+ // | |
255
+ // +-+-+
256
+ //
257
+ // Returns an Object {n, s, e, w, nw, sw, ne, se}
258
+ function getScreenBBox() {
259
+ var targetel = target || d3.event.target,
260
+ bbox = {},
261
+ matrix = targetel.getScreenCTM(),
262
+ tbbox = targetel.getBBox(),
263
+ width = tbbox.width,
264
+ height = tbbox.height,
265
+ x = tbbox.x,
266
+ y = tbbox.y
267
+
268
+ point.x = x
269
+ point.y = y
270
+ bbox.nw = point.matrixTransform(matrix)
271
+ point.x += width
272
+ bbox.ne = point.matrixTransform(matrix)
273
+ point.y += height
274
+ bbox.se = point.matrixTransform(matrix)
275
+ point.x -= width
276
+ bbox.sw = point.matrixTransform(matrix)
277
+ point.y -= height / 2
278
+ bbox.w = point.matrixTransform(matrix)
279
+ point.x += width
280
+ bbox.e = point.matrixTransform(matrix)
281
+ point.x -= width / 2
282
+ point.y -= height / 2
283
+ bbox.n = point.matrixTransform(matrix)
284
+ point.y += height
285
+ bbox.s = point.matrixTransform(matrix)
286
+
287
+ return bbox
288
+ }
289
+
290
+ return tip
291
+ };
292
+
293
+ }));