Initial OpenECOMP policy/engine commit
[policy/engine.git] / ecomp-sdk-app / src / main / webapp / app / fusion / external / d3 / js / models / discreteBarChart.js
1
2 nv.models.discreteBarChart = function() {
3   "use strict";
4   //============================================================
5   // Public Variables with Default Settings
6   //------------------------------------------------------------
7
8   var discretebar = nv.models.discreteBar()
9     , xAxis = nv.models.axis()
10     , yAxis = nv.models.axis()
11     ;
12
13   var margin = {top: 15, right: 10, bottom: 50, left: 60}
14     , width = null
15     , height = null
16     , color = nv.utils.getColor()
17     , showXAxis = true
18     , showYAxis = true
19     , rightAlignYAxis = false
20     , staggerLabels = false
21     , tooltips = true
22     , tooltip = function(key, x, y, e, graph) {
23         return '<h3>' + x + '</h3>' +
24                '<p>' +  y + '</p>'
25       }
26     , x
27     , y
28     , noData = "No Data Available."
29     , dispatch = d3.dispatch('tooltipShow', 'tooltipHide', 'beforeUpdate')
30     , transitionDuration = 250
31     ;
32
33   xAxis
34     .orient('bottom')
35     .highlightZero(false)
36     .showMaxMin(false)
37     .tickFormat(function(d) { return d })
38     ;
39   yAxis
40     .orient((rightAlignYAxis) ? 'right' : 'left')
41     .tickFormat(d3.format(',.1f'))
42     ;
43
44   //============================================================
45
46
47   //============================================================
48   // Private Variables
49   //------------------------------------------------------------
50
51   var showTooltip = function(e, offsetElement) {
52     var left = e.pos[0] + ( offsetElement.offsetLeft || 0 ),
53         top = e.pos[1] + ( offsetElement.offsetTop || 0),
54         x = xAxis.tickFormat()(discretebar.x()(e.point, e.pointIndex)),
55         y = yAxis.tickFormat()(discretebar.y()(e.point, e.pointIndex)),
56         content = tooltip(e.series.key, x, y, e, chart);
57
58     nv.tooltip.show([left, top], content, e.value < 0 ? 'n' : 's', null, offsetElement);
59   };
60
61   //============================================================
62
63
64   function chart(selection) {
65     selection.each(function(data) {
66       var container = d3.select(this),
67           that = this;
68
69       var availableWidth = (width  || parseInt(container.style('width')) || 960)
70                              - margin.left - margin.right,
71           availableHeight = (height || parseInt(container.style('height')) || 400)
72                              - margin.top - margin.bottom;
73
74
75       chart.update = function() { 
76         dispatch.beforeUpdate(); 
77         container.transition().duration(transitionDuration).call(chart); 
78       };
79       chart.container = this;
80
81
82       //------------------------------------------------------------
83       // Display No Data message if there's nothing to show.
84
85       if (!data || !data.length || !data.filter(function(d) { return d.values.length }).length) {
86         var noDataText = container.selectAll('.nv-noData').data([noData]);
87
88         noDataText.enter().append('text')
89           .attr('class', 'nvd3 nv-noData')
90           .attr('dy', '-.7em')
91           .style('text-anchor', 'middle');
92
93         noDataText
94           .attr('x', margin.left + availableWidth / 2)
95           .attr('y', margin.top + availableHeight / 2)
96           .text(function(d) { return d });
97
98         return chart;
99       } else {
100         container.selectAll('.nv-noData').remove();
101       }
102
103       //------------------------------------------------------------
104
105
106       //------------------------------------------------------------
107       // Setup Scales
108
109       x = discretebar.xScale();
110       y = discretebar.yScale().clamp(true);
111
112       //------------------------------------------------------------
113
114
115       //------------------------------------------------------------
116       // Setup containers and skeleton of chart
117
118       var wrap = container.selectAll('g.nv-wrap.nv-discreteBarWithAxes').data([data]);
119       var gEnter = wrap.enter().append('g').attr('class', 'nvd3 nv-wrap nv-discreteBarWithAxes').append('g');
120       var defsEnter = gEnter.append('defs');
121       var g = wrap.select('g');
122
123       gEnter.append('g').attr('class', 'nv-x nv-axis');
124       gEnter.append('g').attr('class', 'nv-y nv-axis');
125       gEnter.append('g').attr('class', 'nv-barsWrap');
126
127       g.attr('transform', 'translate(' + margin.left + ',' + margin.top + ')');
128
129       if (rightAlignYAxis) {
130           g.select(".nv-y.nv-axis")
131               .attr("transform", "translate(" + availableWidth + ",0)");
132       }
133
134       //------------------------------------------------------------
135
136
137       //------------------------------------------------------------
138       // Main Chart Component(s)
139
140       discretebar
141         .width(availableWidth)
142         .height(availableHeight);
143
144
145       var barsWrap = g.select('.nv-barsWrap')
146           .datum(data.filter(function(d) { return !d.disabled }))
147
148       barsWrap.transition().call(discretebar);
149
150       //------------------------------------------------------------
151
152
153
154       defsEnter.append('clipPath')
155           .attr('id', 'nv-x-label-clip-' + discretebar.id())
156         .append('rect');
157
158       g.select('#nv-x-label-clip-' + discretebar.id() + ' rect')
159           .attr('width', x.rangeBand() * (staggerLabels ? 2 : 1))
160           .attr('height', 16)
161           .attr('x', -x.rangeBand() / (staggerLabels ? 1 : 2 ));
162
163
164       //------------------------------------------------------------
165       // Setup Axes
166
167       if (showXAxis) {
168           xAxis
169             .scale(x)
170             .ticks( availableWidth / 100 )
171             .tickSize(-availableHeight, 0);
172
173           g.select('.nv-x.nv-axis')
174               .attr('transform', 'translate(0,' + (y.range()[0] + ((discretebar.showValues() && y.domain()[0] < 0) ? 16 : 0)) + ')');
175           //d3.transition(g.select('.nv-x.nv-axis'))
176           g.select('.nv-x.nv-axis').transition()
177               .call(xAxis);
178
179
180           var xTicks = g.select('.nv-x.nv-axis').selectAll('g');
181
182           if (staggerLabels) {
183             xTicks
184                 .selectAll('text')
185                 .attr('transform', function(d,i,j) { return 'translate(0,' + (j % 2 == 0 ? '5' : '17') + ')' })
186           }
187       }
188
189       if (showYAxis) {
190           yAxis
191             .scale(y)
192             .ticks( availableHeight / 36 )
193             .tickSize( -availableWidth, 0);
194
195           g.select('.nv-y.nv-axis').transition()
196               .call(yAxis);
197       }
198
199       //------------------------------------------------------------
200
201
202       //============================================================
203       // Event Handling/Dispatching (in chart's scope)
204       //------------------------------------------------------------
205
206       dispatch.on('tooltipShow', function(e) {
207         if (tooltips) showTooltip(e, that.parentNode);
208       });
209
210       //============================================================
211
212
213     });
214
215     return chart;
216   }
217
218   //============================================================
219   // Event Handling/Dispatching (out of chart's scope)
220   //------------------------------------------------------------
221
222   discretebar.dispatch.on('elementMouseover.tooltip', function(e) {
223     e.pos = [e.pos[0] +  margin.left, e.pos[1] + margin.top];
224     dispatch.tooltipShow(e);
225   });
226
227   discretebar.dispatch.on('elementMouseout.tooltip', function(e) {
228     dispatch.tooltipHide(e);
229   });
230
231   dispatch.on('tooltipHide', function() {
232     if (tooltips) nv.tooltip.cleanup();
233   });
234
235   //============================================================
236
237
238   //============================================================
239   // Expose Public Variables
240   //------------------------------------------------------------
241
242   // expose chart's sub-components
243   chart.dispatch = dispatch;
244   chart.discretebar = discretebar;
245   chart.xAxis = xAxis;
246   chart.yAxis = yAxis;
247
248   d3.rebind(chart, discretebar, 'x', 'y', 'xDomain', 'yDomain', 'xRange', 'yRange', 'forceX', 'forceY', 'id', 'showValues', 'valueFormat');
249
250   chart.options = nv.utils.optionsFunc.bind(chart);
251   
252   chart.margin = function(_) {
253     if (!arguments.length) return margin;
254     margin.top    = typeof _.top    != 'undefined' ? _.top    : margin.top;
255     margin.right  = typeof _.right  != 'undefined' ? _.right  : margin.right;
256     margin.bottom = typeof _.bottom != 'undefined' ? _.bottom : margin.bottom;
257     margin.left   = typeof _.left   != 'undefined' ? _.left   : margin.left;
258     return chart;
259   };
260
261   chart.width = function(_) {
262     if (!arguments.length) return width;
263     width = _;
264     return chart;
265   };
266
267   chart.height = function(_) {
268     if (!arguments.length) return height;
269     height = _;
270     return chart;
271   };
272
273   chart.color = function(_) {
274     if (!arguments.length) return color;
275     color = nv.utils.getColor(_);
276     discretebar.color(color);
277     return chart;
278   };
279
280   chart.showXAxis = function(_) {
281     if (!arguments.length) return showXAxis;
282     showXAxis = _;
283     return chart;
284   };
285
286   chart.showYAxis = function(_) {
287     if (!arguments.length) return showYAxis;
288     showYAxis = _;
289     return chart;
290   };
291
292   chart.rightAlignYAxis = function(_) {
293     if(!arguments.length) return rightAlignYAxis;
294     rightAlignYAxis = _;
295     yAxis.orient( (_) ? 'right' : 'left');
296     return chart;
297   };
298
299   chart.staggerLabels = function(_) {
300     if (!arguments.length) return staggerLabels;
301     staggerLabels = _;
302     return chart;
303   };
304
305   chart.tooltips = function(_) {
306     if (!arguments.length) return tooltips;
307     tooltips = _;
308     return chart;
309   };
310
311   chart.tooltipContent = function(_) {
312     if (!arguments.length) return tooltip;
313     tooltip = _;
314     return chart;
315   };
316
317   chart.noData = function(_) {
318     if (!arguments.length) return noData;
319     noData = _;
320     return chart;
321   };
322
323   chart.transitionDuration = function(_) {
324     if (!arguments.length) return transitionDuration;
325     transitionDuration = _;
326     return chart;
327   };
328
329   //============================================================
330
331
332   return chart;
333 }