Simon Hunt | 1a9eff9 | 2014-11-07 11:06:34 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 Open Networking Laboratory |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | /* |
| 18 | Utility functions for D3 visualizations. |
| 19 | |
| 20 | @author Simon Hunt |
| 21 | */ |
| 22 | |
| 23 | (function (onos) { |
| 24 | 'use strict'; |
| 25 | |
Paul Greyson | fcba0e8 | 2014-11-13 10:21:16 -0800 | [diff] [blame] | 26 | function createDragBehavior(force, selectCb, atDragEnd, requireMeta) { |
Simon Hunt | 1a9eff9 | 2014-11-07 11:06:34 -0800 | [diff] [blame] | 27 | var draggedThreshold = d3.scale.linear() |
| 28 | .domain([0, 0.1]) |
| 29 | .range([5, 20]) |
| 30 | .clamp(true), |
| 31 | drag; |
| 32 | |
| 33 | // TODO: better validation of parameters |
| 34 | if (!$.isFunction(selectCb)) { |
| 35 | alert('d3util.createDragBehavior(): selectCb is not a function') |
| 36 | } |
| 37 | if (!$.isFunction(atDragEnd)) { |
| 38 | alert('d3util.createDragBehavior(): atDragEnd is not a function') |
| 39 | } |
Simon Hunt | 01095ff | 2014-11-13 16:37:29 -0800 | [diff] [blame] | 40 | if (!$.isFunction(requireMeta)) { |
| 41 | alert('d3util.createDragBehavior(): requireMeta is not a function') |
| 42 | } |
Simon Hunt | 1a9eff9 | 2014-11-07 11:06:34 -0800 | [diff] [blame] | 43 | |
| 44 | function dragged(d) { |
| 45 | var threshold = draggedThreshold(force.alpha()), |
| 46 | dx = d.oldX - d.px, |
| 47 | dy = d.oldY - d.py; |
| 48 | if (Math.abs(dx) >= threshold || Math.abs(dy) >= threshold) { |
| 49 | d.dragged = true; |
| 50 | } |
| 51 | return d.dragged; |
| 52 | } |
| 53 | |
| 54 | drag = d3.behavior.drag() |
| 55 | .origin(function(d) { return d; }) |
| 56 | .on('dragstart', function(d) { |
Simon Hunt | 01095ff | 2014-11-13 16:37:29 -0800 | [diff] [blame] | 57 | if (requireMeta() ^ !d3.event.sourceEvent.metaKey) { |
Paul Greyson | fcba0e8 | 2014-11-13 10:21:16 -0800 | [diff] [blame] | 58 | d3.event.sourceEvent.stopPropagation(); |
| 59 | |
| 60 | d.oldX = d.x; |
| 61 | d.oldY = d.y; |
| 62 | d.dragged = false; |
| 63 | d.fixed |= 2; |
| 64 | d.dragStarted = true; |
| 65 | } |
Simon Hunt | 1a9eff9 | 2014-11-07 11:06:34 -0800 | [diff] [blame] | 66 | }) |
| 67 | .on('drag', function(d) { |
Simon Hunt | 01095ff | 2014-11-13 16:37:29 -0800 | [diff] [blame] | 68 | if (requireMeta() ^ !d3.event.sourceEvent.metaKey) { |
Paul Greyson | fcba0e8 | 2014-11-13 10:21:16 -0800 | [diff] [blame] | 69 | d.px = d3.event.x; |
| 70 | d.py = d3.event.y; |
| 71 | if (dragged(d)) { |
| 72 | if (!force.alpha()) { |
| 73 | force.alpha(.025); |
| 74 | } |
Simon Hunt | 1a9eff9 | 2014-11-07 11:06:34 -0800 | [diff] [blame] | 75 | } |
| 76 | } |
| 77 | }) |
| 78 | .on('dragend', function(d) { |
Paul Greyson | fcba0e8 | 2014-11-13 10:21:16 -0800 | [diff] [blame] | 79 | if (d.dragStarted) { |
| 80 | d.dragStarted = false; |
| 81 | if (!dragged(d)) { |
| 82 | // consider this the same as a 'click' (selection of node) |
| 83 | selectCb(d, this); // TODO: set 'this' context instead of param |
| 84 | } |
| 85 | d.fixed &= ~6; |
Simon Hunt | 1a9eff9 | 2014-11-07 11:06:34 -0800 | [diff] [blame] | 86 | |
Paul Greyson | fcba0e8 | 2014-11-13 10:21:16 -0800 | [diff] [blame] | 87 | // hook at the end of a drag gesture |
| 88 | atDragEnd(d, this); // TODO: set 'this' context instead of param |
| 89 | } |
Simon Hunt | 1a9eff9 | 2014-11-07 11:06:34 -0800 | [diff] [blame] | 90 | }); |
| 91 | |
| 92 | return drag; |
| 93 | } |
| 94 | |
| 95 | function appendGlow(svg) { |
| 96 | // TODO: parameterize color |
| 97 | |
| 98 | var glow = svg.append('filter') |
| 99 | .attr('x', '-50%') |
| 100 | .attr('y', '-50%') |
| 101 | .attr('width', '200%') |
| 102 | .attr('height', '200%') |
| 103 | .attr('id', 'blue-glow'); |
| 104 | |
| 105 | glow.append('feColorMatrix') |
| 106 | .attr('type', 'matrix') |
| 107 | .attr('values', '0 0 0 0 0 ' + |
| 108 | '0 0 0 0 0 ' + |
| 109 | '0 0 0 0 .7 ' + |
| 110 | '0 0 0 1 0 '); |
| 111 | |
| 112 | glow.append('feGaussianBlur') |
| 113 | .attr('stdDeviation', 3) |
| 114 | .attr('result', 'coloredBlur'); |
| 115 | |
| 116 | glow.append('feMerge').selectAll('feMergeNode') |
| 117 | .data(['coloredBlur', 'SourceGraphic']) |
| 118 | .enter().append('feMergeNode') |
| 119 | .attr('in', String); |
| 120 | } |
| 121 | |
Simon Hunt | 8f40cce | 2014-11-23 15:57:30 -0800 | [diff] [blame] | 122 | // --- Ordinal scales for 7 values. |
| 123 | // TODO: tune colors for light and dark themes |
| 124 | |
Simon Hunt | 95dad92 | 2014-11-24 09:43:31 -0800 | [diff] [blame] | 125 | // blue purple pink mustard cyan green red |
| 126 | var lightNorm = ['#1f77b4', '#9467bd', '#e377c2', '#bcbd22', '#17becf', '#2ca02c', '#d62728'], |
| 127 | lightMute = ['#aec7e8', '#c5b0d5', '#f7b6d2', '#dbdb8d', '#9edae5', '#98df8a', '#ff9896'], |
| 128 | darkNorm = ['#1f77b4', '#9467bd', '#e377c2', '#bcbd22', '#17becf', '#2ca02c', '#d62728'], |
| 129 | darkMute = ['#aec7e8', '#c5b0d5', '#f7b6d2', '#dbdb8d', '#9edae5', '#98df8a', '#ff9896']; |
Simon Hunt | 8f40cce | 2014-11-23 15:57:30 -0800 | [diff] [blame] | 130 | |
| 131 | function cat7() { |
| 132 | var colors = { |
| 133 | light: { |
| 134 | norm: d3.scale.ordinal().range(lightNorm), |
| 135 | mute: d3.scale.ordinal().range(lightMute) |
| 136 | }, |
| 137 | dark: { |
| 138 | norm: d3.scale.ordinal().range(darkNorm), |
| 139 | mute: d3.scale.ordinal().range(darkMute) |
| 140 | } |
| 141 | }, |
| 142 | tcid = 'd3utilTestCard'; |
| 143 | |
| 144 | function get(id, muted, theme) { |
| 145 | // NOTE: since we are lazily assigning domain ids, we need to |
| 146 | // get the color from all 4 scales, to keep the domains |
| 147 | // in sync. |
| 148 | var ln = colors.light.norm(id), |
| 149 | lm = colors.light.mute(id), |
| 150 | dn = colors.dark.norm(id), |
| 151 | dm = colors.dark.mute(id); |
| 152 | if (theme === 'dark') { |
| 153 | return muted ? dm : dn; |
| 154 | } else { |
| 155 | return muted ? lm : ln; |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | function testCard(svg) { |
| 160 | var g = svg.select('g#' + tcid), |
| 161 | dom = d3.range(7), |
| 162 | k, muted, theme, what; |
| 163 | |
| 164 | if (!g.empty()) { |
| 165 | g.remove(); |
| 166 | |
| 167 | } else { |
| 168 | g = svg.append('g') |
| 169 | .attr('id', tcid) |
| 170 | .attr('transform', 'scale(4)translate(20,20)'); |
| 171 | |
| 172 | for (k=0; k<4; k++) { |
| 173 | muted = k%2; |
| 174 | what = muted ? ' muted' : ' normal'; |
| 175 | theme = k < 2 ? 'light' : 'dark'; |
| 176 | dom.forEach(function (id, i) { |
| 177 | var x = i * 20, |
| 178 | y = k * 20, |
| 179 | f = get(id, muted, theme); |
| 180 | g.append('circle').attr({ |
| 181 | cx: x, |
| 182 | cy: y, |
| 183 | r: 5, |
| 184 | fill: f |
| 185 | }); |
| 186 | }); |
| 187 | g.append('rect').attr({ |
| 188 | x: 140, |
| 189 | y: k * 20 - 5, |
| 190 | width: 32, |
| 191 | height: 10, |
| 192 | rx: 2, |
| 193 | fill: '#888' |
| 194 | }); |
| 195 | g.append('text').text(theme + what) |
| 196 | .attr({ |
| 197 | x: 142, |
| 198 | y: k * 20 + 2, |
| 199 | fill: 'white' |
| 200 | }) |
| 201 | .style('font-size', '4pt'); |
| 202 | } |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | return { |
| 207 | testCard: testCard, |
| 208 | get: get |
| 209 | }; |
| 210 | } |
| 211 | |
Simon Hunt | 1a9eff9 | 2014-11-07 11:06:34 -0800 | [diff] [blame] | 212 | // === register the functions as a library |
| 213 | onos.ui.addLib('d3util', { |
| 214 | createDragBehavior: createDragBehavior, |
Simon Hunt | 8f40cce | 2014-11-23 15:57:30 -0800 | [diff] [blame] | 215 | appendGlow: appendGlow, |
| 216 | cat7: cat7 |
Simon Hunt | 1a9eff9 | 2014-11-07 11:06:34 -0800 | [diff] [blame] | 217 | }); |
| 218 | |
| 219 | }(ONOS)); |