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 | |
Simon Hunt | 6e18fe3 | 2014-11-29 13:35:41 -0800 | [diff] [blame] | 26 | function isF(f) { |
| 27 | return $.isFunction(f) ? f : null; |
| 28 | } |
| 29 | |
Simon Hunt | c2367d5 | 2014-11-29 19:30:23 -0800 | [diff] [blame] | 30 | // TODO: sensible defaults |
| 31 | function createDragBehavior(force, selectCb, atDragEnd, |
| 32 | dragEnabled, clickEnabled) { |
Simon Hunt | 1a9eff9 | 2014-11-07 11:06:34 -0800 | [diff] [blame] | 33 | var draggedThreshold = d3.scale.linear() |
| 34 | .domain([0, 0.1]) |
| 35 | .range([5, 20]) |
| 36 | .clamp(true), |
Simon Hunt | 6e18fe3 | 2014-11-29 13:35:41 -0800 | [diff] [blame] | 37 | drag, |
| 38 | fSel = isF(selectCb), |
| 39 | fEnd = isF(atDragEnd), |
Simon Hunt | c2367d5 | 2014-11-29 19:30:23 -0800 | [diff] [blame] | 40 | fDEn = isF(dragEnabled), |
| 41 | fCEn = isF(clickEnabled), |
Simon Hunt | 6e18fe3 | 2014-11-29 13:35:41 -0800 | [diff] [blame] | 42 | bad = []; |
Simon Hunt | 1a9eff9 | 2014-11-07 11:06:34 -0800 | [diff] [blame] | 43 | |
Simon Hunt | 6e18fe3 | 2014-11-29 13:35:41 -0800 | [diff] [blame] | 44 | function naf(what) { |
| 45 | return 'd3util.createDragBehavior(): '+ what + ' is not a function'; |
Simon Hunt | 1a9eff9 | 2014-11-07 11:06:34 -0800 | [diff] [blame] | 46 | } |
Simon Hunt | 6e18fe3 | 2014-11-29 13:35:41 -0800 | [diff] [blame] | 47 | |
| 48 | if (!fSel) { |
| 49 | bad.push(naf('selectCb')); |
Simon Hunt | 1a9eff9 | 2014-11-07 11:06:34 -0800 | [diff] [blame] | 50 | } |
Simon Hunt | 6e18fe3 | 2014-11-29 13:35:41 -0800 | [diff] [blame] | 51 | if (!fEnd) { |
| 52 | bad.push(naf('atDragEnd')); |
Simon Hunt | 01095ff | 2014-11-13 16:37:29 -0800 | [diff] [blame] | 53 | } |
Simon Hunt | c2367d5 | 2014-11-29 19:30:23 -0800 | [diff] [blame] | 54 | if (!fDEn) { |
| 55 | bad.push(naf('dragEnabled')); |
| 56 | } |
| 57 | if (!fCEn) { |
| 58 | bad.push(naf('clickEnabled')); |
Simon Hunt | 6e18fe3 | 2014-11-29 13:35:41 -0800 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | if (bad.length) { |
| 62 | alert(bad.join('\n')); |
| 63 | return null; |
| 64 | } |
| 65 | |
Simon Hunt | 1a9eff9 | 2014-11-07 11:06:34 -0800 | [diff] [blame] | 66 | |
| 67 | function dragged(d) { |
| 68 | var threshold = draggedThreshold(force.alpha()), |
| 69 | dx = d.oldX - d.px, |
| 70 | dy = d.oldY - d.py; |
| 71 | if (Math.abs(dx) >= threshold || Math.abs(dy) >= threshold) { |
| 72 | d.dragged = true; |
| 73 | } |
| 74 | return d.dragged; |
| 75 | } |
| 76 | |
| 77 | drag = d3.behavior.drag() |
| 78 | .origin(function(d) { return d; }) |
| 79 | .on('dragstart', function(d) { |
Simon Hunt | c2367d5 | 2014-11-29 19:30:23 -0800 | [diff] [blame] | 80 | if (clickEnabled() || dragEnabled()) { |
Paul Greyson | fcba0e8 | 2014-11-13 10:21:16 -0800 | [diff] [blame] | 81 | d3.event.sourceEvent.stopPropagation(); |
| 82 | |
| 83 | d.oldX = d.x; |
| 84 | d.oldY = d.y; |
| 85 | d.dragged = false; |
| 86 | d.fixed |= 2; |
| 87 | d.dragStarted = true; |
| 88 | } |
Simon Hunt | 1a9eff9 | 2014-11-07 11:06:34 -0800 | [diff] [blame] | 89 | }) |
| 90 | .on('drag', function(d) { |
Simon Hunt | c2367d5 | 2014-11-29 19:30:23 -0800 | [diff] [blame] | 91 | if (dragEnabled()) { |
Paul Greyson | fcba0e8 | 2014-11-13 10:21:16 -0800 | [diff] [blame] | 92 | d.px = d3.event.x; |
| 93 | d.py = d3.event.y; |
| 94 | if (dragged(d)) { |
| 95 | if (!force.alpha()) { |
| 96 | force.alpha(.025); |
| 97 | } |
Simon Hunt | 1a9eff9 | 2014-11-07 11:06:34 -0800 | [diff] [blame] | 98 | } |
| 99 | } |
| 100 | }) |
| 101 | .on('dragend', function(d) { |
Paul Greyson | fcba0e8 | 2014-11-13 10:21:16 -0800 | [diff] [blame] | 102 | if (d.dragStarted) { |
| 103 | d.dragStarted = false; |
| 104 | if (!dragged(d)) { |
Simon Hunt | c2367d5 | 2014-11-29 19:30:23 -0800 | [diff] [blame] | 105 | // consider this the same as a 'click' |
| 106 | // (selection of a node) |
| 107 | if (clickEnabled()) { |
| 108 | selectCb(d, this); // TODO: set 'this' context instead of param |
| 109 | } |
Paul Greyson | fcba0e8 | 2014-11-13 10:21:16 -0800 | [diff] [blame] | 110 | } |
| 111 | d.fixed &= ~6; |
Simon Hunt | 1a9eff9 | 2014-11-07 11:06:34 -0800 | [diff] [blame] | 112 | |
Paul Greyson | fcba0e8 | 2014-11-13 10:21:16 -0800 | [diff] [blame] | 113 | // hook at the end of a drag gesture |
Simon Hunt | c2367d5 | 2014-11-29 19:30:23 -0800 | [diff] [blame] | 114 | if (dragEnabled()) { |
| 115 | atDragEnd(d, this); // TODO: set 'this' context instead of param |
| 116 | } |
Paul Greyson | fcba0e8 | 2014-11-13 10:21:16 -0800 | [diff] [blame] | 117 | } |
Simon Hunt | 1a9eff9 | 2014-11-07 11:06:34 -0800 | [diff] [blame] | 118 | }); |
| 119 | |
| 120 | return drag; |
| 121 | } |
| 122 | |
Simon Hunt | 9f1bced | 2014-12-02 14:36:39 -0800 | [diff] [blame] | 123 | function loadGlow(defs) { |
Simon Hunt | 1a9eff9 | 2014-11-07 11:06:34 -0800 | [diff] [blame] | 124 | // TODO: parameterize color |
| 125 | |
Simon Hunt | 9f1bced | 2014-12-02 14:36:39 -0800 | [diff] [blame] | 126 | var glow = defs.append('filter') |
Simon Hunt | 1a9eff9 | 2014-11-07 11:06:34 -0800 | [diff] [blame] | 127 | .attr('x', '-50%') |
| 128 | .attr('y', '-50%') |
| 129 | .attr('width', '200%') |
| 130 | .attr('height', '200%') |
| 131 | .attr('id', 'blue-glow'); |
| 132 | |
| 133 | glow.append('feColorMatrix') |
| 134 | .attr('type', 'matrix') |
Simon Hunt | 9f1bced | 2014-12-02 14:36:39 -0800 | [diff] [blame] | 135 | .attr('values', |
| 136 | '0 0 0 0 0 ' + |
| 137 | '0 0 0 0 0 ' + |
| 138 | '0 0 0 0 .7 ' + |
| 139 | '0 0 0 1 0 '); |
Simon Hunt | 1a9eff9 | 2014-11-07 11:06:34 -0800 | [diff] [blame] | 140 | |
| 141 | glow.append('feGaussianBlur') |
| 142 | .attr('stdDeviation', 3) |
| 143 | .attr('result', 'coloredBlur'); |
| 144 | |
| 145 | glow.append('feMerge').selectAll('feMergeNode') |
| 146 | .data(['coloredBlur', 'SourceGraphic']) |
| 147 | .enter().append('feMergeNode') |
| 148 | .attr('in', String); |
| 149 | } |
| 150 | |
Simon Hunt | 8f40cce | 2014-11-23 15:57:30 -0800 | [diff] [blame] | 151 | // --- Ordinal scales for 7 values. |
| 152 | // TODO: tune colors for light and dark themes |
Simon Hunt | 925dfc0 | 2014-11-29 12:11:51 -0800 | [diff] [blame] | 153 | // Note: These colors look good on the white background. Still, need to tune for dark. |
Simon Hunt | 8f40cce | 2014-11-23 15:57:30 -0800 | [diff] [blame] | 154 | |
Thomas Vachuska | 1e68bdd | 2014-11-29 13:53:10 -0800 | [diff] [blame] | 155 | // blue brown brick red sea green purple dark teal lime |
| 156 | var lightNorm = ['#3E5780', '#78533B', '#CB4D28', '#018D61', '#8A2979', '#006D73', '#56AF00'], |
| 157 | lightMute = ['#A8B8CC', '#CCB3A8', '#FFC2BD', '#96D6BF', '#D19FCE', '#8FCCCA', '#CAEAA4'], |
Thomas Vachuska | e02e11c | 2014-11-24 16:13:52 -0800 | [diff] [blame] | 158 | |
Thomas Vachuska | 1e68bdd | 2014-11-29 13:53:10 -0800 | [diff] [blame] | 159 | darkNorm = ['#3E5780', '#78533B', '#CB4D28', '#018D61', '#8A2979', '#006D73', '#56AF00'], |
| 160 | darkMute = ['#A8B8CC', '#CCB3A8', '#FFC2BD', '#96D6BF', '#D19FCE', '#8FCCCA', '#CAEAA4']; |
Simon Hunt | 8f40cce | 2014-11-23 15:57:30 -0800 | [diff] [blame] | 161 | |
| 162 | function cat7() { |
| 163 | var colors = { |
| 164 | light: { |
| 165 | norm: d3.scale.ordinal().range(lightNorm), |
| 166 | mute: d3.scale.ordinal().range(lightMute) |
| 167 | }, |
| 168 | dark: { |
| 169 | norm: d3.scale.ordinal().range(darkNorm), |
| 170 | mute: d3.scale.ordinal().range(darkMute) |
| 171 | } |
| 172 | }, |
| 173 | tcid = 'd3utilTestCard'; |
| 174 | |
| 175 | function get(id, muted, theme) { |
| 176 | // NOTE: since we are lazily assigning domain ids, we need to |
| 177 | // get the color from all 4 scales, to keep the domains |
| 178 | // in sync. |
| 179 | var ln = colors.light.norm(id), |
| 180 | lm = colors.light.mute(id), |
| 181 | dn = colors.dark.norm(id), |
| 182 | dm = colors.dark.mute(id); |
| 183 | if (theme === 'dark') { |
| 184 | return muted ? dm : dn; |
| 185 | } else { |
| 186 | return muted ? lm : ln; |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | function testCard(svg) { |
| 191 | var g = svg.select('g#' + tcid), |
| 192 | dom = d3.range(7), |
| 193 | k, muted, theme, what; |
| 194 | |
| 195 | if (!g.empty()) { |
| 196 | g.remove(); |
| 197 | |
| 198 | } else { |
| 199 | g = svg.append('g') |
| 200 | .attr('id', tcid) |
| 201 | .attr('transform', 'scale(4)translate(20,20)'); |
| 202 | |
| 203 | for (k=0; k<4; k++) { |
| 204 | muted = k%2; |
| 205 | what = muted ? ' muted' : ' normal'; |
| 206 | theme = k < 2 ? 'light' : 'dark'; |
| 207 | dom.forEach(function (id, i) { |
| 208 | var x = i * 20, |
| 209 | y = k * 20, |
| 210 | f = get(id, muted, theme); |
| 211 | g.append('circle').attr({ |
| 212 | cx: x, |
| 213 | cy: y, |
| 214 | r: 5, |
| 215 | fill: f |
| 216 | }); |
| 217 | }); |
| 218 | g.append('rect').attr({ |
| 219 | x: 140, |
| 220 | y: k * 20 - 5, |
| 221 | width: 32, |
| 222 | height: 10, |
| 223 | rx: 2, |
| 224 | fill: '#888' |
| 225 | }); |
| 226 | g.append('text').text(theme + what) |
| 227 | .attr({ |
| 228 | x: 142, |
| 229 | y: k * 20 + 2, |
| 230 | fill: 'white' |
| 231 | }) |
| 232 | .style('font-size', '4pt'); |
| 233 | } |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | return { |
| 238 | testCard: testCard, |
| 239 | get: get |
| 240 | }; |
| 241 | } |
| 242 | |
Simon Hunt | 1a9eff9 | 2014-11-07 11:06:34 -0800 | [diff] [blame] | 243 | // === register the functions as a library |
| 244 | onos.ui.addLib('d3util', { |
| 245 | createDragBehavior: createDragBehavior, |
Simon Hunt | 9f1bced | 2014-12-02 14:36:39 -0800 | [diff] [blame] | 246 | loadGlow: loadGlow, |
Simon Hunt | 8f40cce | 2014-11-23 15:57:30 -0800 | [diff] [blame] | 247 | cat7: cat7 |
Simon Hunt | 1a9eff9 | 2014-11-07 11:06:34 -0800 | [diff] [blame] | 248 | }); |
| 249 | |
| 250 | }(ONOS)); |