Initial OpenECOMP policy/engine commit
[policy/engine.git] / ecomp-sdk-app / src / main / webapp / static / fusion / d3 / js / models / lineWithFisheye.js
1
2 nv.models.line = function() {
3   "use strict";
4   //Default Settings
5   var margin = {top: 0, right: 0, bottom: 0, left: 0},
6       width = 960,
7       height = 500,
8       color = nv.utils.defaultColor(), // function that returns colors
9       id = Math.floor(Math.random() * 10000), //Create semi-unique ID incase user doesn't select one
10       getX = function(d) { return d.x }, // accessor to get the x value from a data point
11       getY = function(d) { return d.y }, // accessor to get the y value from a data point
12       clipEdge = false, // if true, masks lines within x and y scale
13       interpolate = "linear"; // controls the line interpolation
14
15
16   var scatter = nv.models.scatter()
17                   .id(id)
18                   .size(16) // default size
19                   .sizeDomain([16,256]), //set to speed up calculation, needs to be unset if there is a custom size accessor
20       //x = scatter.xScale(),
21       //y = scatter.yScale(),
22       x, y,
23       x0, y0, timeoutID;
24
25
26   function chart(selection) {
27     selection.each(function(data) {
28       var availableWidth = width - margin.left - margin.right,
29           availableHeight = height - margin.top - margin.bottom;
30
31       //get the scales inscase scatter scale was set manually
32       x = x || scatter.xScale();
33       y = y || scatter.yScale();
34
35       //store old scales if they exist
36       x0 = x0 || x;
37       y0 = y0 || y;
38
39
40       var wrap = d3.select(this).selectAll('g.nv-wrap.nv-line').data([data]);
41       var wrapEnter = wrap.enter().append('g').attr('class', 'nvd3 nv-wrap nv-line');
42       var defsEnter = wrapEnter.append('defs');
43       var gEnter = wrapEnter.append('g');
44       var g = wrap.select('g')
45
46       wrapEnter.append('g').attr('class', 'nv-scatterWrap');
47       var scatterWrap = wrap.select('.nv-scatterWrap').datum(data);
48
49       gEnter.append('g').attr('class', 'nv-groups');
50
51
52       scatter
53         .width(availableWidth)
54         .height(availableHeight)
55
56       d3.transition(scatterWrap).call(scatter);
57
58
59       wrap.attr('transform', 'translate(' + margin.left + ',' + margin.top + ')');
60
61
62       defsEnter.append('clipPath')
63           .attr('id', 'nv-edge-clip-' + id)
64         .append('rect');
65
66       wrap.select('#nv-edge-clip-' + id + ' rect')
67           .attr('width', availableWidth)
68           .attr('height', availableHeight);
69
70       g   .attr('clip-path', clipEdge ? 'url(#nv-edge-clip-' + id + ')' : '');
71       scatterWrap
72           .attr('clip-path', clipEdge ? 'url(#nv-edge-clip-' + id + ')' : '');
73
74
75
76
77       var groups = wrap.select('.nv-groups').selectAll('.nv-group')
78           .data(function(d) { return d }, function(d) { return d.key });
79       groups.enter().append('g')
80           .style('stroke-opacity', 1e-6)
81           .style('fill-opacity', 1e-6);
82       d3.transition(groups.exit())
83           .style('stroke-opacity', 1e-6)
84           .style('fill-opacity', 1e-6)
85           .remove();
86       groups
87           .attr('class', function(d,i) { return 'nv-group nv-series-' + i })
88           .classed('hover', function(d) { return d.hover })
89           .style('fill', function(d,i){ return color(d, i) })
90           .style('stroke', function(d,i){ return color(d, i) })
91       d3.transition(groups)
92           .style('stroke-opacity', 1)
93           .style('fill-opacity', .5)
94
95
96       var paths = groups.selectAll('path')
97           .data(function(d, i) { return [d.values] });
98       paths.enter().append('path')
99           .attr('class', 'nv-line')
100           .attr('d', d3.svg.line()
101             .interpolate(interpolate)
102             .x(function(d,i) { return x0(getX(d,i)) })
103             .y(function(d,i) { return y0(getY(d,i)) })
104           );
105       d3.transition(groups.exit().selectAll('path'))
106           .attr('d', d3.svg.line()
107             .interpolate(interpolate)
108             .x(function(d,i) { return x(getX(d,i)) })
109             .y(function(d,i) { return y(getY(d,i)) })
110           )
111           .remove(); // redundant? line is already being removed
112       d3.transition(paths)
113           .attr('d', d3.svg.line()
114             .interpolate(interpolate)
115             .x(function(d,i) { return x(getX(d,i)) })
116             .y(function(d,i) { return y(getY(d,i)) })
117           );
118
119
120       //store old scales for use in transitions on update, to animate from old to new positions
121       x0 = x.copy();
122       y0 = y.copy();
123
124     });
125
126     return chart;
127   }
128
129
130   chart.dispatch = scatter.dispatch;
131
132   d3.rebind(chart, scatter, 'interactive', 'size', 'xScale', 'yScale', 'zScale', 'xDomain', 'yDomain', 'xRange', 'yRange', 'sizeDomain', 'forceX', 'forceY', 'forceSize', 'clipVoronoi', 'clipRadius');
133
134   chart.options = nv.utils.optionsFunc.bind(chart);
135   
136   chart.margin = function(_) {
137     if (!arguments.length) return margin;
138     margin = _;
139     return chart;
140   };
141
142   chart.width = function(_) {
143     if (!arguments.length) return width;
144     width = _;
145     return chart;
146   };
147
148   chart.height = function(_) {
149     if (!arguments.length) return height;
150     height = _;
151     return chart;
152   };
153
154   chart.x = function(_) {
155     if (!arguments.length) return getX;
156     getX = _;
157     scatter.x(_);
158     return chart;
159   };
160
161   chart.y = function(_) {
162     if (!arguments.length) return getY;
163     getY = _;
164     scatter.y(_);
165     return chart;
166   };
167
168   chart.clipEdge = function(_) {
169     if (!arguments.length) return clipEdge;
170     clipEdge = _;
171     return chart;
172   };
173
174   chart.color = function(_) {
175     if (!arguments.length) return color;
176     color = nv.utils.getColor(_);
177     scatter.color(color);
178     return chart;
179   };
180
181   chart.id = function(_) {
182     if (!arguments.length) return id;
183     id = _;
184     return chart;
185   };
186
187   chart.interpolate = function(_) {
188     if (!arguments.length) return interpolate;
189     interpolate = _;
190     return chart;
191   };
192
193   chart.defined = function(_) {
194     if (!arguments.length) return defined;
195     defined = _;
196     return chart;
197   };
198
199   return chart;
200 }