Initial OpenECOMP policy/engine commit
[policy/engine.git] / ecomp-sdk-app / src / main / webapp / static / fusion / d3 / js / models / distribution.js
1
2 nv.models.distribution = 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 = 400 //technically width or height depending on x or y....
10     , size = 8
11     , axis = 'x' // 'x' or 'y'... horizontal or vertical
12     , getData = function(d) { return d[axis] }  // defaults d.x or d.y
13     , color = nv.utils.defaultColor()
14     , scale = d3.scale.linear()
15     , domain
16     ;
17
18   //============================================================
19
20
21   //============================================================
22   // Private Variables
23   //------------------------------------------------------------
24
25   var scale0;
26
27   //============================================================
28
29
30   function chart(selection) {
31     selection.each(function(data) {
32       var availableLength = width - (axis === 'x' ? margin.left + margin.right : margin.top + margin.bottom),
33           naxis = axis == 'x' ? 'y' : 'x',
34           container = d3.select(this);
35
36
37       //------------------------------------------------------------
38       // Setup Scales
39
40       scale0 = scale0 || scale;
41
42       //------------------------------------------------------------
43
44
45       //------------------------------------------------------------
46       // Setup containers and skeleton of chart
47
48       var wrap = container.selectAll('g.nv-distribution').data([data]);
49       var wrapEnter = wrap.enter().append('g').attr('class', 'nvd3 nv-distribution');
50       var gEnter = wrapEnter.append('g');
51       var g = wrap.select('g');
52
53       wrap.attr('transform', 'translate(' + margin.left + ',' + margin.top + ')')
54
55       //------------------------------------------------------------
56
57
58       var distWrap = g.selectAll('g.nv-dist')
59           .data(function(d) { return d }, function(d) { return d.key });
60
61       distWrap.enter().append('g');
62       distWrap
63           .attr('class', function(d,i) { return 'nv-dist nv-series-' + i })
64           .style('stroke', function(d,i) { return color(d, i) });
65
66       var dist = distWrap.selectAll('line.nv-dist' + axis)
67           .data(function(d) { return d.values })
68       dist.enter().append('line')
69           .attr(axis + '1', function(d,i) { return scale0(getData(d,i)) })
70           .attr(axis + '2', function(d,i) { return scale0(getData(d,i)) })
71       distWrap.exit().selectAll('line.nv-dist' + axis)
72           .transition()
73           .attr(axis + '1', function(d,i) { return scale(getData(d,i)) })
74           .attr(axis + '2', function(d,i) { return scale(getData(d,i)) })
75           .style('stroke-opacity', 0)
76           .remove();
77       dist
78           .attr('class', function(d,i) { return 'nv-dist' + axis + ' nv-dist' + axis + '-' + i })
79           .attr(naxis + '1', 0)
80           .attr(naxis + '2', size);
81       dist
82           .transition()
83           .attr(axis + '1', function(d,i) { return scale(getData(d,i)) })
84           .attr(axis + '2', function(d,i) { return scale(getData(d,i)) })
85
86
87       scale0 = scale.copy();
88
89     });
90
91     return chart;
92   }
93
94
95   //============================================================
96   // Expose Public Variables
97   //------------------------------------------------------------
98   chart.options = nv.utils.optionsFunc.bind(chart);
99   
100   chart.margin = function(_) {
101     if (!arguments.length) return margin;
102     margin.top    = typeof _.top    != 'undefined' ? _.top    : margin.top;
103     margin.right  = typeof _.right  != 'undefined' ? _.right  : margin.right;
104     margin.bottom = typeof _.bottom != 'undefined' ? _.bottom : margin.bottom;
105     margin.left   = typeof _.left   != 'undefined' ? _.left   : margin.left;
106     return chart;
107   };
108
109   chart.width = function(_) {
110     if (!arguments.length) return width;
111     width = _;
112     return chart;
113   };
114
115   chart.axis = function(_) {
116     if (!arguments.length) return axis;
117     axis = _;
118     return chart;
119   };
120
121   chart.size = function(_) {
122     if (!arguments.length) return size;
123     size = _;
124     return chart;
125   };
126
127   chart.getData = function(_) {
128     if (!arguments.length) return getData;
129     getData = d3.functor(_);
130     return chart;
131   };
132
133   chart.scale = function(_) {
134     if (!arguments.length) return scale;
135     scale = _;
136     return chart;
137   };
138
139   chart.color = function(_) {
140     if (!arguments.length) return color;
141     color = nv.utils.getColor(_);
142     return chart;
143   };
144   //============================================================
145
146
147   return chart;
148 }