Initial OpenECOMP policy/engine commit
[policy/engine.git] / ecomp-sdk-app / src / main / webapp / static / fusion / d3 / js / models / discreteBar.js
1 //TODO: consider deprecating by adding necessary features to multiBar model
2 nv.models.discreteBar = 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     , showValues = false
19     , valueFormat = d3.format(',.2f')
20     , xDomain
21     , yDomain
22     , xRange
23     , yRange
24     , dispatch = d3.dispatch('chartClick', 'elementClick', 'elementDblClick', 'elementMouseover', 'elementMouseout')
25     , rectClass = 'discreteBar'
26     ;
27
28   //============================================================
29
30
31   //============================================================
32   // Private Variables
33   //------------------------------------------------------------
34
35   var x0, y0;
36
37   //============================================================
38
39
40   function chart(selection) {
41     selection.each(function(data) {
42       var availableWidth = width - margin.left - margin.right,
43           availableHeight = height - margin.top - margin.bottom,
44           container = d3.select(this);
45
46
47       //add series index to each data point for reference
48       data.forEach(function(series, i) {
49         series.values.forEach(function(point) {
50           point.series = i;
51         });
52       });
53
54
55       //------------------------------------------------------------
56       // Setup Scales
57
58       // remap and flatten the data for use in calculating the scales' domains
59       var seriesData = (xDomain && yDomain) ? [] : // if we know xDomain and yDomain, no need to calculate
60             data.map(function(d) {
61               return d.values.map(function(d,i) {
62                 return { x: getX(d,i), y: getY(d,i), y0: d.y0 }
63               })
64             });
65
66       x   .domain(xDomain || d3.merge(seriesData).map(function(d) { return d.x }))
67           .rangeBands(xRange || [0, availableWidth], .1);
68
69       y   .domain(yDomain || d3.extent(d3.merge(seriesData).map(function(d) { return d.y }).concat(forceY)));
70
71
72       // If showValues, pad the Y axis range to account for label height
73       if (showValues) y.range(yRange || [availableHeight - (y.domain()[0] < 0 ? 12 : 0), y.domain()[1] > 0 ? 12 : 0]);
74       else y.range(yRange || [availableHeight, 0]);
75
76       //store old scales if they exist
77       x0 = x0 || x;
78       y0 = y0 || y.copy().range([y(0),y(0)]);
79
80       //------------------------------------------------------------
81
82
83       //------------------------------------------------------------
84       // Setup containers and skeleton of chart
85
86       var wrap = container.selectAll('g.nv-wrap.nv-discretebar').data([data]);
87       var wrapEnter = wrap.enter().append('g').attr('class', 'nvd3 nv-wrap nv-discretebar');
88       var gEnter = wrapEnter.append('g');
89       var g = wrap.select('g');
90
91       gEnter.append('g').attr('class', 'nv-groups');
92
93       wrap.attr('transform', 'translate(' + margin.left + ',' + margin.top + ')');
94
95       //------------------------------------------------------------
96
97
98
99       //TODO: by definition, the discrete bar should not have multiple groups, will modify/remove later
100       var groups = wrap.select('.nv-groups').selectAll('.nv-group')
101           .data(function(d) { return d }, function(d) { return d.key });
102       groups.enter().append('g')
103           .style('stroke-opacity', 1e-6)
104           .style('fill-opacity', 1e-6);
105       groups.exit()
106           .transition()
107           .style('stroke-opacity', 1e-6)
108           .style('fill-opacity', 1e-6)
109           .remove();
110       groups
111           .attr('class', function(d,i) { return 'nv-group nv-series-' + i })
112           .classed('hover', function(d) { return d.hover });
113       groups
114           .transition()
115           .style('stroke-opacity', 1)
116           .style('fill-opacity', .75);
117
118
119       var bars = groups.selectAll('g.nv-bar')
120           .data(function(d) { return d.values });
121
122       bars.exit().remove();
123
124
125       var barsEnter = bars.enter().append('g')
126           .attr('transform', function(d,i,j) {
127               return 'translate(' + (x(getX(d,i)) + x.rangeBand() * .05 ) + ', ' + y(0) + ')'
128           })
129           .on('mouseover', function(d,i) { //TODO: figure out why j works above, but not here
130             d3.select(this).classed('hover', true);
131             dispatch.elementMouseover({
132               value: getY(d,i),
133               point: d,
134               series: data[d.series],
135               pos: [x(getX(d,i)) + (x.rangeBand() * (d.series + .5) / data.length), y(getY(d,i))],  // TODO: Figure out why the value appears to be shifted
136               pointIndex: i,
137               seriesIndex: d.series,
138               e: d3.event
139             });
140           })
141           .on('mouseout', function(d,i) {
142             d3.select(this).classed('hover', false);
143             dispatch.elementMouseout({
144               value: getY(d,i),
145               point: d,
146               series: data[d.series],
147               pointIndex: i,
148               seriesIndex: d.series,
149               e: d3.event
150             });
151           })
152           .on('click', function(d,i) {
153             dispatch.elementClick({
154               value: getY(d,i),
155               point: d,
156               series: data[d.series],
157               pos: [x(getX(d,i)) + (x.rangeBand() * (d.series + .5) / data.length), y(getY(d,i))],  // TODO: Figure out why the value appears to be shifted
158               pointIndex: i,
159               seriesIndex: d.series,
160               e: d3.event
161             });
162             d3.event.stopPropagation();
163           })
164           .on('dblclick', function(d,i) {
165             dispatch.elementDblClick({
166               value: getY(d,i),
167               point: d,
168               series: data[d.series],
169               pos: [x(getX(d,i)) + (x.rangeBand() * (d.series + .5) / data.length), y(getY(d,i))],  // TODO: Figure out why the value appears to be shifted
170               pointIndex: i,
171               seriesIndex: d.series,
172               e: d3.event
173             });
174             d3.event.stopPropagation();
175           });
176
177       barsEnter.append('rect')
178           .attr('height', 0)
179           .attr('width', x.rangeBand() * .9 / data.length )
180
181       if (showValues) {
182         barsEnter.append('text')
183           .attr('text-anchor', 'middle')
184           ;
185
186         bars.select('text')
187           .text(function(d,i) { return valueFormat(getY(d,i)) })
188           .transition()
189           .attr('x', x.rangeBand() * .9 / 2)
190           .attr('y', function(d,i) { return getY(d,i) < 0 ? y(getY(d,i)) - y(0) + 12 : -4 })
191
192           ;
193       } else {
194         bars.selectAll('text').remove();
195       }
196
197       bars
198           .attr('class', function(d,i) { return getY(d,i) < 0 ? 'nv-bar negative' : 'nv-bar positive' })
199           .style('fill', function(d,i) { return d.color || color(d,i) })
200           .style('stroke', function(d,i) { return d.color || color(d,i) })
201         .select('rect')
202           .attr('class', rectClass)
203           .transition()
204           .attr('width', x.rangeBand() * .9 / data.length);
205       bars.transition()
206         //.delay(function(d,i) { return i * 1200 / data[0].values.length })
207           .attr('transform', function(d,i) {
208             var left = x(getX(d,i)) + x.rangeBand() * .05,
209                 top = getY(d,i) < 0 ?
210                         y(0) :
211                         y(0) - y(getY(d,i)) < 1 ?
212                           y(0) - 1 : //make 1 px positive bars show up above y=0
213                           y(getY(d,i));
214
215               return 'translate(' + left + ', ' + top + ')'
216           })
217         .select('rect')
218           .attr('height', function(d,i) {
219             return  Math.max(Math.abs(y(getY(d,i)) - y((yDomain && yDomain[0]) || 0)) || 1)
220           });
221
222
223       //store old scales for use in transitions on update
224       x0 = x.copy();
225       y0 = y.copy();
226
227     });
228
229     return chart;
230   }
231
232
233   //============================================================
234   // Expose Public Variables
235   //------------------------------------------------------------
236
237   chart.dispatch = dispatch;
238
239   chart.options = nv.utils.optionsFunc.bind(chart);
240
241   chart.x = function(_) {
242     if (!arguments.length) return getX;
243     getX = _;
244     return chart;
245   };
246
247   chart.y = function(_) {
248     if (!arguments.length) return getY;
249     getY = _;
250     return chart;
251   };
252
253   chart.margin = function(_) {
254     if (!arguments.length) return margin;
255     margin.top    = typeof _.top    != 'undefined' ? _.top    : margin.top;
256     margin.right  = typeof _.right  != 'undefined' ? _.right  : margin.right;
257     margin.bottom = typeof _.bottom != 'undefined' ? _.bottom : margin.bottom;
258     margin.left   = typeof _.left   != 'undefined' ? _.left   : margin.left;
259     return chart;
260   };
261
262   chart.width = function(_) {
263     if (!arguments.length) return width;
264     width = _;
265     return chart;
266   };
267
268   chart.height = function(_) {
269     if (!arguments.length) return height;
270     height = _;
271     return chart;
272   };
273
274   chart.xScale = function(_) {
275     if (!arguments.length) return x;
276     x = _;
277     return chart;
278   };
279
280   chart.yScale = function(_) {
281     if (!arguments.length) return y;
282     y = _;
283     return chart;
284   };
285
286   chart.xDomain = function(_) {
287     if (!arguments.length) return xDomain;
288     xDomain = _;
289     return chart;
290   };
291
292   chart.yDomain = function(_) {
293     if (!arguments.length) return yDomain;
294     yDomain = _;
295     return chart;
296   };
297
298   chart.xRange = function(_) {
299     if (!arguments.length) return xRange;
300     xRange = _;
301     return chart;
302   };
303
304   chart.yRange = function(_) {
305     if (!arguments.length) return yRange;
306     yRange = _;
307     return chart;
308   };
309
310   chart.forceY = function(_) {
311     if (!arguments.length) return forceY;
312     forceY = _;
313     return chart;
314   };
315
316   chart.color = function(_) {
317     if (!arguments.length) return color;
318     color = nv.utils.getColor(_);
319     return chart;
320   };
321
322   chart.id = function(_) {
323     if (!arguments.length) return id;
324     id = _;
325     return chart;
326   };
327
328   chart.showValues = function(_) {
329     if (!arguments.length) return showValues;
330     showValues = _;
331     return chart;
332   };
333
334   chart.valueFormat= function(_) {
335     if (!arguments.length) return valueFormat;
336     valueFormat = _;
337     return chart;
338   };
339
340   chart.rectClass= function(_) {
341     if (!arguments.length) return rectClass;
342     rectClass = _;
343     return chart;
344   };
345   //============================================================
346
347
348   return chart;
349 }