pyk 0.2.6 → 0.2.7

Sign up to get free protection for your applications and to get access to all the features.
Files changed (68) hide show
  1. checksums.yaml +8 -8
  2. data/app/assets/javascripts/lib/chardinjs.min.js +2 -0
  3. data/app/assets/javascripts/lib/crossfilter.js +1383 -1
  4. data/app/assets/javascripts/lib/{d3.js → d3.v3.js} +0 -0
  5. data/app/assets/javascripts/lib/dc.js +3492 -757
  6. data/app/assets/javascripts/lib/jquery.gridster.js +2 -3621
  7. data/app/assets/javascripts/lib/markermanager.js +2 -980
  8. data/app/assets/javascripts/lib/underscore.js +1276 -0
  9. data/app/assets/javascripts/nvd3/lib/colorbrewer.js +302 -0
  10. data/app/assets/javascripts/nvd3/lib/crossfilter.js +1180 -0
  11. data/app/assets/javascripts/nvd3/lib/crossfilter.min.js +1 -0
  12. data/app/assets/javascripts/nvd3/lib/d3.v2.js +7033 -0
  13. data/app/assets/javascripts/nvd3/lib/d3.v2.min.js +4 -0
  14. data/app/assets/javascripts/nvd3/lib/d3.v3.js +8436 -0
  15. data/app/assets/javascripts/nvd3/lib/fisheye.js +86 -0
  16. data/app/assets/javascripts/nvd3/lib/hive.js +80 -0
  17. data/app/assets/javascripts/nvd3/lib/horizon.js +192 -0
  18. data/app/assets/javascripts/nvd3/lib/sankey.js +292 -0
  19. data/app/assets/javascripts/nvd3/nv.d3.js +14312 -0
  20. data/app/assets/javascripts/nvd3/nv.d3.min.js +6 -0
  21. data/app/assets/javascripts/nvd3/src/core.js +122 -0
  22. data/app/assets/javascripts/nvd3/src/interactiveLayer.js +251 -0
  23. data/app/assets/javascripts/nvd3/src/models/axis.js +405 -0
  24. data/app/assets/javascripts/nvd3/src/models/backup/bullet.js +250 -0
  25. data/app/assets/javascripts/nvd3/src/models/backup/bulletChart.js +349 -0
  26. data/app/assets/javascripts/nvd3/src/models/boilerplate.js +104 -0
  27. data/app/assets/javascripts/nvd3/src/models/bullet.js +385 -0
  28. data/app/assets/javascripts/nvd3/src/models/bulletChart.js +343 -0
  29. data/app/assets/javascripts/nvd3/src/models/cumulativeLineChart.js +782 -0
  30. data/app/assets/javascripts/nvd3/src/models/discreteBar.js +349 -0
  31. data/app/assets/javascripts/nvd3/src/models/discreteBarChart.js +333 -0
  32. data/app/assets/javascripts/nvd3/src/models/distribution.js +148 -0
  33. data/app/assets/javascripts/nvd3/src/models/historicalBar.js +331 -0
  34. data/app/assets/javascripts/nvd3/src/models/historicalBarChart.js +419 -0
  35. data/app/assets/javascripts/nvd3/src/models/indentedTree.js +337 -0
  36. data/app/assets/javascripts/nvd3/src/models/legend.js +270 -0
  37. data/app/assets/javascripts/nvd3/src/models/line.js +284 -0
  38. data/app/assets/javascripts/nvd3/src/models/lineChart.js +465 -0
  39. data/app/assets/javascripts/nvd3/src/models/linePlusBarChart.js +433 -0
  40. data/app/assets/javascripts/nvd3/src/models/linePlusBarWithFocusChart.js +658 -0
  41. data/app/assets/javascripts/nvd3/src/models/lineWithFisheye.js +200 -0
  42. data/app/assets/javascripts/nvd3/src/models/lineWithFisheyeChart.js +297 -0
  43. data/app/assets/javascripts/nvd3/src/models/lineWithFocusChart.js +574 -0
  44. data/app/assets/javascripts/nvd3/src/models/multiBar.js +461 -0
  45. data/app/assets/javascripts/nvd3/src/models/multiBarChart.js +524 -0
  46. data/app/assets/javascripts/nvd3/src/models/multiBarHorizontal.js +424 -0
  47. data/app/assets/javascripts/nvd3/src/models/multiBarHorizontalChart.js +434 -0
  48. data/app/assets/javascripts/nvd3/src/models/multiBarTimeSeries.js +384 -0
  49. data/app/assets/javascripts/nvd3/src/models/multiBarTimeSeriesChart.js +405 -0
  50. data/app/assets/javascripts/nvd3/src/models/multiChart.js +452 -0
  51. data/app/assets/javascripts/nvd3/src/models/ohlcBar.js +380 -0
  52. data/app/assets/javascripts/nvd3/src/models/parallelCoordinates.js +239 -0
  53. data/app/assets/javascripts/nvd3/src/models/pie.js +398 -0
  54. data/app/assets/javascripts/nvd3/src/models/pieChart.js +292 -0
  55. data/app/assets/javascripts/nvd3/src/models/scatter.js +674 -0
  56. data/app/assets/javascripts/nvd3/src/models/scatterChart.js +628 -0
  57. data/app/assets/javascripts/nvd3/src/models/scatterPlusLineChart.js +620 -0
  58. data/app/assets/javascripts/nvd3/src/models/sparkline.js +194 -0
  59. data/app/assets/javascripts/nvd3/src/models/sparklinePlus.js +295 -0
  60. data/app/assets/javascripts/nvd3/src/models/stackedArea.js +368 -0
  61. data/app/assets/javascripts/nvd3/src/models/stackedAreaChart.js +629 -0
  62. data/app/assets/javascripts/nvd3/src/tooltip.js +490 -0
  63. data/app/assets/javascripts/nvd3/src/utils.js +152 -0
  64. data/app/assets/javascripts/pyk.js +1 -0
  65. data/app/assets/stylesheets/lib/chardinjs.css +82 -0
  66. data/app/assets/stylesheets/nvd3/nv.d3.css +769 -0
  67. data/app/assets/stylesheets/pyk.css.scss +1 -0
  68. metadata +61 -2
@@ -0,0 +1,424 @@
1
+
2
+ nv.models.multiBarHorizontal = function() {
3
+ "use strict";
4
+ //============================================================
5
+ // Public Variables with Default Settings
6
+ //------------------------------------------------------------
7
+
8
+ var margin = {top: 0, right: 0, bottom: 0, left: 0}
9
+ , width = 960
10
+ , height = 500
11
+ , id = Math.floor(Math.random() * 10000) //Create semi-unique ID in case user doesn't select one
12
+ , x = d3.scale.ordinal()
13
+ , y = d3.scale.linear()
14
+ , getX = function(d) { return d.x }
15
+ , getY = function(d) { return d.y }
16
+ , forceY = [0] // 0 is forced by default.. this makes sense for the majority of bar graphs... user can always do chart.forceY([]) to remove
17
+ , color = nv.utils.defaultColor()
18
+ , barColor = null // adding the ability to set the color for each rather than the whole group
19
+ , disabled // used in conjunction with barColor to communicate from multiBarHorizontalChart what series are disabled
20
+ , stacked = false
21
+ , showValues = false
22
+ , valuePadding = 60
23
+ , valueFormat = d3.format(',.2f')
24
+ , delay = 1200
25
+ , xDomain
26
+ , yDomain
27
+ , xRange
28
+ , yRange
29
+ , dispatch = d3.dispatch('chartClick', 'elementClick', 'elementDblClick', 'elementMouseover', 'elementMouseout')
30
+ ;
31
+
32
+ //============================================================
33
+
34
+
35
+ //============================================================
36
+ // Private Variables
37
+ //------------------------------------------------------------
38
+
39
+ var x0, y0 //used to store previous scales
40
+ ;
41
+
42
+ //============================================================
43
+
44
+
45
+ function chart(selection) {
46
+ selection.each(function(data) {
47
+ var availableWidth = width - margin.left - margin.right,
48
+ availableHeight = height - margin.top - margin.bottom,
49
+ container = d3.select(this);
50
+
51
+
52
+ if (stacked)
53
+ data = d3.layout.stack()
54
+ .offset('zero')
55
+ .values(function(d){ return d.values })
56
+ .y(getY)
57
+ (data);
58
+
59
+
60
+ //add series index to each data point for reference
61
+ data.forEach(function(series, i) {
62
+ series.values.forEach(function(point) {
63
+ point.series = i;
64
+ });
65
+ });
66
+
67
+
68
+
69
+ //------------------------------------------------------------
70
+ // HACK for negative value stacking
71
+ if (stacked)
72
+ data[0].values.map(function(d,i) {
73
+ var posBase = 0, negBase = 0;
74
+ data.map(function(d) {
75
+ var f = d.values[i]
76
+ f.size = Math.abs(f.y);
77
+ if (f.y<0) {
78
+ f.y1 = negBase - f.size;
79
+ negBase = negBase - f.size;
80
+ } else
81
+ {
82
+ f.y1 = posBase;
83
+ posBase = posBase + f.size;
84
+ }
85
+ });
86
+ });
87
+
88
+
89
+
90
+ //------------------------------------------------------------
91
+ // Setup Scales
92
+
93
+ // remap and flatten the data for use in calculating the scales' domains
94
+ var seriesData = (xDomain && yDomain) ? [] : // if we know xDomain and yDomain, no need to calculate
95
+ data.map(function(d) {
96
+ return d.values.map(function(d,i) {
97
+ return { x: getX(d,i), y: getY(d,i), y0: d.y0, y1: d.y1 }
98
+ })
99
+ });
100
+
101
+ x .domain(xDomain || d3.merge(seriesData).map(function(d) { return d.x }))
102
+ .rangeBands(xRange || [0, availableHeight], .1);
103
+
104
+ //y .domain(yDomain || d3.extent(d3.merge(seriesData).map(function(d) { return d.y + (stacked ? d.y0 : 0) }).concat(forceY)))
105
+ y .domain(yDomain || d3.extent(d3.merge(seriesData).map(function(d) { return stacked ? (d.y > 0 ? d.y1 + d.y : d.y1 ) : d.y }).concat(forceY)))
106
+
107
+ if (showValues && !stacked)
108
+ y.range(yRange || [(y.domain()[0] < 0 ? valuePadding : 0), availableWidth - (y.domain()[1] > 0 ? valuePadding : 0) ]);
109
+ else
110
+ y.range(yRange || [0, availableWidth]);
111
+
112
+ x0 = x0 || x;
113
+ y0 = y0 || d3.scale.linear().domain(y.domain()).range([y(0),y(0)]);
114
+
115
+ //------------------------------------------------------------
116
+
117
+
118
+ //------------------------------------------------------------
119
+ // Setup containers and skeleton of chart
120
+
121
+ var wrap = d3.select(this).selectAll('g.nv-wrap.nv-multibarHorizontal').data([data]);
122
+ var wrapEnter = wrap.enter().append('g').attr('class', 'nvd3 nv-wrap nv-multibarHorizontal');
123
+ var defsEnter = wrapEnter.append('defs');
124
+ var gEnter = wrapEnter.append('g');
125
+ var g = wrap.select('g');
126
+
127
+ gEnter.append('g').attr('class', 'nv-groups');
128
+
129
+ wrap.attr('transform', 'translate(' + margin.left + ',' + margin.top + ')');
130
+
131
+ //------------------------------------------------------------
132
+
133
+
134
+
135
+ var groups = wrap.select('.nv-groups').selectAll('.nv-group')
136
+ .data(function(d) { return d }, function(d,i) { return i });
137
+ groups.enter().append('g')
138
+ .style('stroke-opacity', 1e-6)
139
+ .style('fill-opacity', 1e-6);
140
+ groups.exit().transition()
141
+ .style('stroke-opacity', 1e-6)
142
+ .style('fill-opacity', 1e-6)
143
+ .remove();
144
+ groups
145
+ .attr('class', function(d,i) { return 'nv-group nv-series-' + i })
146
+ .classed('hover', function(d) { return d.hover })
147
+ .style('fill', function(d,i){ return color(d, i) })
148
+ .style('stroke', function(d,i){ return color(d, i) });
149
+ groups.transition()
150
+ .style('stroke-opacity', 1)
151
+ .style('fill-opacity', .75);
152
+
153
+
154
+ var bars = groups.selectAll('g.nv-bar')
155
+ .data(function(d) { return d.values });
156
+
157
+ bars.exit().remove();
158
+
159
+
160
+ var barsEnter = bars.enter().append('g')
161
+ .attr('transform', function(d,i,j) {
162
+ return 'translate(' + y0(stacked ? d.y0 : 0) + ',' + (stacked ? 0 : (j * x.rangeBand() / data.length ) + x(getX(d,i))) + ')'
163
+ });
164
+
165
+ barsEnter.append('rect')
166
+ .attr('width', 0)
167
+ .attr('height', x.rangeBand() / (stacked ? 1 : data.length) )
168
+
169
+ bars
170
+ .on('mouseover', function(d,i) { //TODO: figure out why j works above, but not here
171
+ d3.select(this).classed('hover', true);
172
+ dispatch.elementMouseover({
173
+ value: getY(d,i),
174
+ point: d,
175
+ series: data[d.series],
176
+ pos: [ y(getY(d,i) + (stacked ? d.y0 : 0)), x(getX(d,i)) + (x.rangeBand() * (stacked ? data.length / 2 : d.series + .5) / data.length) ],
177
+ pointIndex: i,
178
+ seriesIndex: d.series,
179
+ e: d3.event
180
+ });
181
+ })
182
+ .on('mouseout', function(d,i) {
183
+ d3.select(this).classed('hover', false);
184
+ dispatch.elementMouseout({
185
+ value: getY(d,i),
186
+ point: d,
187
+ series: data[d.series],
188
+ pointIndex: i,
189
+ seriesIndex: d.series,
190
+ e: d3.event
191
+ });
192
+ })
193
+ .on('click', function(d,i) {
194
+ dispatch.elementClick({
195
+ value: getY(d,i),
196
+ point: d,
197
+ series: data[d.series],
198
+ pos: [x(getX(d,i)) + (x.rangeBand() * (stacked ? data.length / 2 : d.series + .5) / data.length), y(getY(d,i) + (stacked ? d.y0 : 0))], // TODO: Figure out why the value appears to be shifted
199
+ pointIndex: i,
200
+ seriesIndex: d.series,
201
+ e: d3.event
202
+ });
203
+ d3.event.stopPropagation();
204
+ })
205
+ .on('dblclick', function(d,i) {
206
+ dispatch.elementDblClick({
207
+ value: getY(d,i),
208
+ point: d,
209
+ series: data[d.series],
210
+ pos: [x(getX(d,i)) + (x.rangeBand() * (stacked ? data.length / 2 : d.series + .5) / data.length), y(getY(d,i) + (stacked ? d.y0 : 0))], // TODO: Figure out why the value appears to be shifted
211
+ pointIndex: i,
212
+ seriesIndex: d.series,
213
+ e: d3.event
214
+ });
215
+ d3.event.stopPropagation();
216
+ });
217
+
218
+
219
+ barsEnter.append('text');
220
+
221
+ if (showValues && !stacked) {
222
+ bars.select('text')
223
+ .attr('text-anchor', function(d,i) { return getY(d,i) < 0 ? 'end' : 'start' })
224
+ .attr('y', x.rangeBand() / (data.length * 2))
225
+ .attr('dy', '.32em')
226
+ .text(function(d,i) { return valueFormat(getY(d,i)) })
227
+ bars.transition()
228
+ .select('text')
229
+ .attr('x', function(d,i) { return getY(d,i) < 0 ? -4 : y(getY(d,i)) - y(0) + 4 })
230
+ } else {
231
+ bars.selectAll('text').text('');
232
+ }
233
+
234
+ bars
235
+ .attr('class', function(d,i) { return getY(d,i) < 0 ? 'nv-bar negative' : 'nv-bar positive'})
236
+
237
+ if (barColor) {
238
+ if (!disabled) disabled = data.map(function() { return true });
239
+ bars
240
+ .style('fill', function(d,i,j) { return d3.rgb(barColor(d,i)).darker( disabled.map(function(d,i) { return i }).filter(function(d,i){ return !disabled[i] })[j] ).toString(); })
241
+ .style('stroke', function(d,i,j) { return d3.rgb(barColor(d,i)).darker( disabled.map(function(d,i) { return i }).filter(function(d,i){ return !disabled[i] })[j] ).toString(); });
242
+ }
243
+
244
+ if (stacked)
245
+ bars.transition()
246
+ .attr('transform', function(d,i) {
247
+ return 'translate(' + y(d.y1) + ',' + x(getX(d,i)) + ')'
248
+ })
249
+ .select('rect')
250
+ .attr('width', function(d,i) {
251
+ return Math.abs(y(getY(d,i) + d.y0) - y(d.y0))
252
+ })
253
+ .attr('height', x.rangeBand() );
254
+ else
255
+ bars.transition()
256
+ .attr('transform', function(d,i) {
257
+ //TODO: stacked must be all positive or all negative, not both?
258
+ return 'translate(' +
259
+ (getY(d,i) < 0 ? y(getY(d,i)) : y(0))
260
+ + ',' +
261
+ (d.series * x.rangeBand() / data.length
262
+ +
263
+ x(getX(d,i)) )
264
+ + ')'
265
+ })
266
+ .select('rect')
267
+ .attr('height', x.rangeBand() / data.length )
268
+ .attr('width', function(d,i) {
269
+ return Math.max(Math.abs(y(getY(d,i)) - y(0)),1)
270
+ });
271
+
272
+
273
+ //store old scales for use in transitions on update
274
+ x0 = x.copy();
275
+ y0 = y.copy();
276
+
277
+ });
278
+
279
+ return chart;
280
+ }
281
+
282
+
283
+ //============================================================
284
+ // Expose Public Variables
285
+ //------------------------------------------------------------
286
+
287
+ chart.dispatch = dispatch;
288
+
289
+ chart.options = nv.utils.optionsFunc.bind(chart);
290
+
291
+ chart.x = function(_) {
292
+ if (!arguments.length) return getX;
293
+ getX = _;
294
+ return chart;
295
+ };
296
+
297
+ chart.y = function(_) {
298
+ if (!arguments.length) return getY;
299
+ getY = _;
300
+ return chart;
301
+ };
302
+
303
+ chart.margin = function(_) {
304
+ if (!arguments.length) return margin;
305
+ margin.top = typeof _.top != 'undefined' ? _.top : margin.top;
306
+ margin.right = typeof _.right != 'undefined' ? _.right : margin.right;
307
+ margin.bottom = typeof _.bottom != 'undefined' ? _.bottom : margin.bottom;
308
+ margin.left = typeof _.left != 'undefined' ? _.left : margin.left;
309
+ return chart;
310
+ };
311
+
312
+ chart.width = function(_) {
313
+ if (!arguments.length) return width;
314
+ width = _;
315
+ return chart;
316
+ };
317
+
318
+ chart.height = function(_) {
319
+ if (!arguments.length) return height;
320
+ height = _;
321
+ return chart;
322
+ };
323
+
324
+ chart.xScale = function(_) {
325
+ if (!arguments.length) return x;
326
+ x = _;
327
+ return chart;
328
+ };
329
+
330
+ chart.yScale = function(_) {
331
+ if (!arguments.length) return y;
332
+ y = _;
333
+ return chart;
334
+ };
335
+
336
+ chart.xDomain = function(_) {
337
+ if (!arguments.length) return xDomain;
338
+ xDomain = _;
339
+ return chart;
340
+ };
341
+
342
+ chart.yDomain = function(_) {
343
+ if (!arguments.length) return yDomain;
344
+ yDomain = _;
345
+ return chart;
346
+ };
347
+
348
+ chart.xRange = function(_) {
349
+ if (!arguments.length) return xRange;
350
+ xRange = _;
351
+ return chart;
352
+ };
353
+
354
+ chart.yRange = function(_) {
355
+ if (!arguments.length) return yRange;
356
+ yRange = _;
357
+ return chart;
358
+ };
359
+
360
+ chart.forceY = function(_) {
361
+ if (!arguments.length) return forceY;
362
+ forceY = _;
363
+ return chart;
364
+ };
365
+
366
+ chart.stacked = function(_) {
367
+ if (!arguments.length) return stacked;
368
+ stacked = _;
369
+ return chart;
370
+ };
371
+
372
+ chart.color = function(_) {
373
+ if (!arguments.length) return color;
374
+ color = nv.utils.getColor(_);
375
+ return chart;
376
+ };
377
+
378
+ chart.barColor = function(_) {
379
+ if (!arguments.length) return barColor;
380
+ barColor = nv.utils.getColor(_);
381
+ return chart;
382
+ };
383
+
384
+ chart.disabled = function(_) {
385
+ if (!arguments.length) return disabled;
386
+ disabled = _;
387
+ return chart;
388
+ };
389
+
390
+ chart.id = function(_) {
391
+ if (!arguments.length) return id;
392
+ id = _;
393
+ return chart;
394
+ };
395
+
396
+ chart.delay = function(_) {
397
+ if (!arguments.length) return delay;
398
+ delay = _;
399
+ return chart;
400
+ };
401
+
402
+ chart.showValues = function(_) {
403
+ if (!arguments.length) return showValues;
404
+ showValues = _;
405
+ return chart;
406
+ };
407
+
408
+ chart.valueFormat= function(_) {
409
+ if (!arguments.length) return valueFormat;
410
+ valueFormat = _;
411
+ return chart;
412
+ };
413
+
414
+ chart.valuePadding = function(_) {
415
+ if (!arguments.length) return valuePadding;
416
+ valuePadding = _;
417
+ return chart;
418
+ };
419
+
420
+ //============================================================
421
+
422
+
423
+ return chart;
424
+ }
@@ -0,0 +1,434 @@
1
+
2
+ nv.models.multiBarHorizontalChart = function() {
3
+ "use strict";
4
+ //============================================================
5
+ // Public Variables with Default Settings
6
+ //------------------------------------------------------------
7
+
8
+ var multibar = nv.models.multiBarHorizontal()
9
+ , xAxis = nv.models.axis()
10
+ , yAxis = nv.models.axis()
11
+ , legend = nv.models.legend().height(30)
12
+ , controls = nv.models.legend().height(30)
13
+ ;
14
+
15
+ var margin = {top: 30, right: 20, bottom: 50, left: 60}
16
+ , width = null
17
+ , height = null
18
+ , color = nv.utils.defaultColor()
19
+ , showControls = true
20
+ , showLegend = true
21
+ , stacked = false
22
+ , tooltips = true
23
+ , tooltip = function(key, x, y, e, graph) {
24
+ return '<h3>' + key + ' - ' + x + '</h3>' +
25
+ '<p>' + y + '</p>'
26
+ }
27
+ , x //can be accessed via chart.xScale()
28
+ , y //can be accessed via chart.yScale()
29
+ , state = { stacked: stacked }
30
+ , defaultState = null
31
+ , noData = 'No Data Available.'
32
+ , dispatch = d3.dispatch('tooltipShow', 'tooltipHide', 'stateChange', 'changeState')
33
+ , controlWidth = function() { return showControls ? 180 : 0 }
34
+ , transitionDuration = 250
35
+ ;
36
+
37
+ multibar
38
+ .stacked(stacked)
39
+ ;
40
+ xAxis
41
+ .orient('left')
42
+ .tickPadding(5)
43
+ .highlightZero(false)
44
+ .showMaxMin(false)
45
+ .tickFormat(function(d) { return d })
46
+ ;
47
+ yAxis
48
+ .orient('bottom')
49
+ .tickFormat(d3.format(',.1f'))
50
+ ;
51
+
52
+ controls.updateState(false);
53
+ //============================================================
54
+
55
+
56
+ //============================================================
57
+ // Private Variables
58
+ //------------------------------------------------------------
59
+
60
+ var showTooltip = function(e, offsetElement) {
61
+ var left = e.pos[0] + ( offsetElement.offsetLeft || 0 ),
62
+ top = e.pos[1] + ( offsetElement.offsetTop || 0),
63
+ x = xAxis.tickFormat()(multibar.x()(e.point, e.pointIndex)),
64
+ y = yAxis.tickFormat()(multibar.y()(e.point, e.pointIndex)),
65
+ content = tooltip(e.series.key, x, y, e, chart);
66
+
67
+ nv.tooltip.show([left, top], content, e.value < 0 ? 'e' : 'w', null, offsetElement);
68
+ };
69
+
70
+ //============================================================
71
+
72
+
73
+ function chart(selection) {
74
+ selection.each(function(data) {
75
+ var container = d3.select(this),
76
+ that = this;
77
+
78
+ var availableWidth = (width || parseInt(container.style('width')) || 960)
79
+ - margin.left - margin.right,
80
+ availableHeight = (height || parseInt(container.style('height')) || 400)
81
+ - margin.top - margin.bottom;
82
+
83
+ chart.update = function() { container.transition().duration(transitionDuration).call(chart) };
84
+ chart.container = this;
85
+
86
+ //set state.disabled
87
+ state.disabled = data.map(function(d) { return !!d.disabled });
88
+
89
+ if (!defaultState) {
90
+ var key;
91
+ defaultState = {};
92
+ for (key in state) {
93
+ if (state[key] instanceof Array)
94
+ defaultState[key] = state[key].slice(0);
95
+ else
96
+ defaultState[key] = state[key];
97
+ }
98
+ }
99
+
100
+ //------------------------------------------------------------
101
+ // Display No Data message if there's nothing to show.
102
+
103
+ if (!data || !data.length || !data.filter(function(d) { return d.values.length }).length) {
104
+ var noDataText = container.selectAll('.nv-noData').data([noData]);
105
+
106
+ noDataText.enter().append('text')
107
+ .attr('class', 'nvd3 nv-noData')
108
+ .attr('dy', '-.7em')
109
+ .style('text-anchor', 'middle');
110
+
111
+ noDataText
112
+ .attr('x', margin.left + availableWidth / 2)
113
+ .attr('y', margin.top + availableHeight / 2)
114
+ .text(function(d) { return d });
115
+
116
+ return chart;
117
+ } else {
118
+ container.selectAll('.nv-noData').remove();
119
+ }
120
+
121
+ //------------------------------------------------------------
122
+
123
+
124
+ //------------------------------------------------------------
125
+ // Setup Scales
126
+
127
+ x = multibar.xScale();
128
+ y = multibar.yScale();
129
+
130
+ //------------------------------------------------------------
131
+
132
+
133
+ //------------------------------------------------------------
134
+ // Setup containers and skeleton of chart
135
+
136
+ var wrap = container.selectAll('g.nv-wrap.nv-multiBarHorizontalChart').data([data]);
137
+ var gEnter = wrap.enter().append('g').attr('class', 'nvd3 nv-wrap nv-multiBarHorizontalChart').append('g');
138
+ var g = wrap.select('g');
139
+
140
+ gEnter.append('g').attr('class', 'nv-x nv-axis');
141
+ gEnter.append('g').attr('class', 'nv-y nv-axis');
142
+ gEnter.append('g').attr('class', 'nv-barsWrap');
143
+ gEnter.append('g').attr('class', 'nv-legendWrap');
144
+ gEnter.append('g').attr('class', 'nv-controlsWrap');
145
+
146
+ //------------------------------------------------------------
147
+
148
+
149
+ //------------------------------------------------------------
150
+ // Legend
151
+
152
+ if (showLegend) {
153
+ legend.width(availableWidth - controlWidth());
154
+
155
+ if (multibar.barColor())
156
+ data.forEach(function(series,i) {
157
+ series.color = d3.rgb('#ccc').darker(i * 1.5).toString();
158
+ })
159
+
160
+ g.select('.nv-legendWrap')
161
+ .datum(data)
162
+ .call(legend);
163
+
164
+ if ( margin.top != legend.height()) {
165
+ margin.top = legend.height();
166
+ availableHeight = (height || parseInt(container.style('height')) || 400)
167
+ - margin.top - margin.bottom;
168
+ }
169
+
170
+ g.select('.nv-legendWrap')
171
+ .attr('transform', 'translate(' + controlWidth() + ',' + (-margin.top) +')');
172
+ }
173
+
174
+ //------------------------------------------------------------
175
+
176
+
177
+ //------------------------------------------------------------
178
+ // Controls
179
+
180
+ if (showControls) {
181
+ var controlsData = [
182
+ { key: 'Grouped', disabled: multibar.stacked() },
183
+ { key: 'Stacked', disabled: !multibar.stacked() }
184
+ ];
185
+
186
+ controls.width(controlWidth()).color(['#444', '#444', '#444']);
187
+ g.select('.nv-controlsWrap')
188
+ .datum(controlsData)
189
+ .attr('transform', 'translate(0,' + (-margin.top) +')')
190
+ .call(controls);
191
+ }
192
+
193
+ //------------------------------------------------------------
194
+
195
+
196
+ wrap.attr('transform', 'translate(' + margin.left + ',' + margin.top + ')');
197
+
198
+
199
+ //------------------------------------------------------------
200
+ // Main Chart Component(s)
201
+
202
+ multibar
203
+ .disabled(data.map(function(series) { return series.disabled }))
204
+ .width(availableWidth)
205
+ .height(availableHeight)
206
+ .color(data.map(function(d,i) {
207
+ return d.color || color(d, i);
208
+ }).filter(function(d,i) { return !data[i].disabled }))
209
+
210
+
211
+ var barsWrap = g.select('.nv-barsWrap')
212
+ .datum(data.filter(function(d) { return !d.disabled }))
213
+
214
+ barsWrap.transition().call(multibar);
215
+
216
+ //------------------------------------------------------------
217
+
218
+
219
+ //------------------------------------------------------------
220
+ // Setup Axes
221
+
222
+ xAxis
223
+ .scale(x)
224
+ .ticks( availableHeight / 24 )
225
+ .tickSize(-availableWidth, 0);
226
+
227
+ g.select('.nv-x.nv-axis').transition()
228
+ .call(xAxis);
229
+
230
+ var xTicks = g.select('.nv-x.nv-axis').selectAll('g');
231
+
232
+ xTicks
233
+ .selectAll('line, text')
234
+ .style('opacity', 1)
235
+
236
+
237
+ yAxis
238
+ .scale(y)
239
+ .ticks( availableWidth / 100 )
240
+ .tickSize( -availableHeight, 0);
241
+
242
+ g.select('.nv-y.nv-axis')
243
+ .attr('transform', 'translate(0,' + availableHeight + ')');
244
+ g.select('.nv-y.nv-axis').transition()
245
+ .call(yAxis);
246
+
247
+ //------------------------------------------------------------
248
+
249
+
250
+
251
+ //============================================================
252
+ // Event Handling/Dispatching (in chart's scope)
253
+ //------------------------------------------------------------
254
+
255
+ legend.dispatch.on('stateChange', function(newState) {
256
+ state = newState;
257
+ dispatch.stateChange(state);
258
+ chart.update();
259
+ });
260
+
261
+ controls.dispatch.on('legendClick', function(d,i) {
262
+ if (!d.disabled) return;
263
+ controlsData = controlsData.map(function(s) {
264
+ s.disabled = true;
265
+ return s;
266
+ });
267
+ d.disabled = false;
268
+
269
+ switch (d.key) {
270
+ case 'Grouped':
271
+ multibar.stacked(false);
272
+ break;
273
+ case 'Stacked':
274
+ multibar.stacked(true);
275
+ break;
276
+ }
277
+
278
+ state.stacked = multibar.stacked();
279
+ dispatch.stateChange(state);
280
+
281
+ chart.update();
282
+ });
283
+
284
+ dispatch.on('tooltipShow', function(e) {
285
+ if (tooltips) showTooltip(e, that.parentNode);
286
+ });
287
+
288
+ // Update chart from a state object passed to event handler
289
+ dispatch.on('changeState', function(e) {
290
+
291
+ if (typeof e.disabled !== 'undefined') {
292
+ data.forEach(function(series,i) {
293
+ series.disabled = e.disabled[i];
294
+ });
295
+
296
+ state.disabled = e.disabled;
297
+ }
298
+
299
+ if (typeof e.stacked !== 'undefined') {
300
+ multibar.stacked(e.stacked);
301
+ state.stacked = e.stacked;
302
+ }
303
+
304
+ selection.call(chart);
305
+ });
306
+ //============================================================
307
+
308
+
309
+ });
310
+
311
+ return chart;
312
+ }
313
+
314
+
315
+ //============================================================
316
+ // Event Handling/Dispatching (out of chart's scope)
317
+ //------------------------------------------------------------
318
+
319
+ multibar.dispatch.on('elementMouseover.tooltip', function(e) {
320
+ e.pos = [e.pos[0] + margin.left, e.pos[1] + margin.top];
321
+ dispatch.tooltipShow(e);
322
+ });
323
+
324
+ multibar.dispatch.on('elementMouseout.tooltip', function(e) {
325
+ dispatch.tooltipHide(e);
326
+ });
327
+ dispatch.on('tooltipHide', function() {
328
+ if (tooltips) nv.tooltip.cleanup();
329
+ });
330
+
331
+ //============================================================
332
+
333
+
334
+ //============================================================
335
+ // Expose Public Variables
336
+ //------------------------------------------------------------
337
+
338
+ // expose chart's sub-components
339
+ chart.dispatch = dispatch;
340
+ chart.multibar = multibar;
341
+ chart.legend = legend;
342
+ chart.xAxis = xAxis;
343
+ chart.yAxis = yAxis;
344
+
345
+ d3.rebind(chart, multibar, 'x', 'y', 'xDomain', 'yDomain', 'xRange', 'yRange', 'forceX', 'forceY', 'clipEdge', 'id', 'delay', 'showValues', 'valueFormat', 'stacked', 'barColor');
346
+
347
+ chart.options = nv.utils.optionsFunc.bind(chart);
348
+
349
+ chart.margin = function(_) {
350
+ if (!arguments.length) return margin;
351
+ margin.top = typeof _.top != 'undefined' ? _.top : margin.top;
352
+ margin.right = typeof _.right != 'undefined' ? _.right : margin.right;
353
+ margin.bottom = typeof _.bottom != 'undefined' ? _.bottom : margin.bottom;
354
+ margin.left = typeof _.left != 'undefined' ? _.left : margin.left;
355
+ return chart;
356
+ };
357
+
358
+ chart.width = function(_) {
359
+ if (!arguments.length) return width;
360
+ width = _;
361
+ return chart;
362
+ };
363
+
364
+ chart.height = function(_) {
365
+ if (!arguments.length) return height;
366
+ height = _;
367
+ return chart;
368
+ };
369
+
370
+ chart.color = function(_) {
371
+ if (!arguments.length) return color;
372
+ color = nv.utils.getColor(_);
373
+ legend.color(color);
374
+ return chart;
375
+ };
376
+
377
+ chart.showControls = function(_) {
378
+ if (!arguments.length) return showControls;
379
+ showControls = _;
380
+ return chart;
381
+ };
382
+
383
+ chart.showLegend = function(_) {
384
+ if (!arguments.length) return showLegend;
385
+ showLegend = _;
386
+ return chart;
387
+ };
388
+
389
+ chart.tooltip = function(_) {
390
+ if (!arguments.length) return tooltip;
391
+ tooltip = _;
392
+ return chart;
393
+ };
394
+
395
+ chart.tooltips = function(_) {
396
+ if (!arguments.length) return tooltips;
397
+ tooltips = _;
398
+ return chart;
399
+ };
400
+
401
+ chart.tooltipContent = function(_) {
402
+ if (!arguments.length) return tooltip;
403
+ tooltip = _;
404
+ return chart;
405
+ };
406
+
407
+ chart.state = function(_) {
408
+ if (!arguments.length) return state;
409
+ state = _;
410
+ return chart;
411
+ };
412
+
413
+ chart.defaultState = function(_) {
414
+ if (!arguments.length) return defaultState;
415
+ defaultState = _;
416
+ return chart;
417
+ };
418
+
419
+ chart.noData = function(_) {
420
+ if (!arguments.length) return noData;
421
+ noData = _;
422
+ return chart;
423
+ };
424
+
425
+ chart.transitionDuration = function(_) {
426
+ if (!arguments.length) return transitionDuration;
427
+ transitionDuration = _;
428
+ return chart;
429
+ };
430
+ //============================================================
431
+
432
+
433
+ return chart;
434
+ }