1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 | 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× | /* eslint no-param-reassign: [2, {"props": false}] */ /* eslint no-use-before-define: ["error", { "functions": false }] */ import d3 from 'd3'; import { d3TimeFormatPreset, } from '../modules/utils'; import { getColorFromScheme } from '../modules/colors'; import './partition.css'; d3.hierarchy = require('d3-hierarchy').hierarchy; d3.partition = require('d3-hierarchy').partition; function init(root) { // Compute dx, dy, x, y for each node and // return an array of nodes in breadth-first order const flat = []; const dy = 1.0 / (root.height + 1); let prev = null; root.each((n) => { n.y = dy * n.depth; n.dy = dy; if (!n.parent) { n.x = 0; n.dx = 1; } else { n.x = prev.depth === n.parent.depth ? 0 : prev.x + prev.dx; n.dx = n.weight / n.parent.sum * n.parent.dx; } prev = n; flat.push(n); }); return flat; } // This vis is based on // http://mbostock.github.io/d3/talk/20111018/partition.html function partitionVis(slice, payload) { const data = payload.data; const fd = slice.formData; const div = d3.select(slice.selector); const metrics = fd.metrics || []; // Chart options const logScale = fd.log_scale || false; const chartType = fd.time_series_option || 'not_time'; const hasTime = ['adv_anal', 'time_series'].indexOf(chartType) >= 0; const format = d3.format(fd.number_format); const timeFormat = d3TimeFormatPreset(fd.date_time_format); div.selectAll('*').remove(); d3.selectAll('.nvtooltip').remove(); const tooltip = d3 .select('body') .append('div') .attr('class', 'nvtooltip') .style('opacity', 0) .style('top', 0) .style('left', 0) .style('position', 'fixed'); function drawVis(i, dat) { const datum = dat[i]; const w = slice.width(); const h = slice.height() / data.length; const x = d3.scale.linear().range([0, w]); const y = d3.scale.linear().range([0, h]); const viz = div .append('div') .attr('class', 'chart') .style('width', w + 'px') .style('height', h + 'px') .append('svg:svg') .attr('width', w) .attr('height', h); // Add padding between multiple visualizations if (i !== data.length - 1 && data.length > 1) { viz.style('padding-bottom', '3px'); } if (i !== 0 && data.length > 1) { viz.style('padding-top', '3px'); } const root = d3.hierarchy(datum); function hasDateNode(n) { return metrics.indexOf(n.data.name) >= 0 && hasTime; } // node.name is the metric/group name // node.disp is the display value // node.value determines sorting order // node.weight determines partition height // node.sum is the sum of children weights root.eachAfter((n) => { n.disp = n.data.val; n.value = n.disp < 0 ? -n.disp : n.disp; n.weight = n.value; n.name = n.data.name; // If the parent is a metric and we still have // the time column, perform a date-time format if (n.parent && hasDateNode(n.parent)) { // Format timestamp values n.weight = fd.equal_date_size ? 1 : n.value; n.value = n.name; n.name = timeFormat(n.name); } if (logScale) n.weight = Math.log(n.weight + 1); n.disp = n.disp && !isNaN(n.disp) && isFinite(n.disp) ? format(n.disp) : ''; }); // Perform sort by weight root.sort((a, b) => { const v = b.value - a.value; if (v === 0) { return b.name > a.name ? 1 : -1; } return v; }); // Prune data based on partition limit and threshold // both are applied at the same time if (fd.partition_threshold && fd.partition_threshold >= 0) { // Compute weight sums as we go root.each((n) => { n.sum = n.children ? n.children.reduce((a, v) => a + v.weight, 0) || 1 : 1; if (n.children) { // Dates are not ordered by weight if (hasDateNode(n)) { if (fd.equal_date_size) { return; } const removeIndices = []; // Keep at least one child for (let j = 1; j < n.children.length; j++) { if (n.children[j].weight / n.sum < fd.partition_threshold) { removeIndices.push(j); } } for (let j = removeIndices.length - 1; j >= 0; j--) { n.children.splice(removeIndices[j], 1); } } else { // Find first child that falls below the threshold let j; for (j = 1; j < n.children.length; j++) { if (n.children[j].weight / n.sum < fd.partition_threshold) { break; } } n.children = n.children.slice(0, j); } } }); } if (fd.partition_limit && fd.partition_limit >= 0) { root.each((n) => { if (n.children && n.children.length > fd.partition_limit) { if (!hasDateNode(n)) { n.children = n.children.slice(0, fd.partition_limit); } } }); } // Compute final weight sums root.eachAfter((n) => { n.sum = n.children ? n.children.reduce((a, v) => a + v.weight, 0) || 1 : 1; }); const verboseMap = slice.datasource.verbose_map; function getCategory(depth) { if (!depth) { return 'Metric'; } if (hasTime && depth === 1) { return 'Date'; } const col = fd.groupby[depth - (hasTime ? 2 : 1)]; return verboseMap[col] || col; } function getAncestors(d) { const ancestors = [d]; let node = d; while (node.parent) { ancestors.push(node.parent); node = node.parent; } return ancestors; } function positionAndPopulate(tip, d) { let t = '<table>'; if (!fd.rich_tooltip) { t += ( '<thead><tr><td colspan="3">' + `<strong class='x-value'>${getCategory(d.depth)}</strong>` + '</td></tr></thead><tbody>' ); t += ( '<tr class="emph">' + '<td class="legend-color-guide" style="opacity: 0.75">' + `<div style='border: thin solid grey; background-color: ${d.color};'` + '></div>' + '</td>' + `<td>${d.name}</td>` + `<td>${d.disp}</td>` + '</tr>' ); } else { const nodes = getAncestors(d); nodes.forEach((n) => { const atNode = n.depth === d.depth; t += '<tbody>'; t += ( `<tr class='${atNode ? 'emph' : ''}'>` + `<td class='legend-color-guide' style='opacity: ${atNode ? '1' : '0.75'}'>` + '<div ' + `style='border: 2px solid ${atNode ? 'black' : 'transparent'};` + `background-color: ${n.color};'` + '></div>' + '</td>' + `<td>${n.name}</td>` + `<td>${n.disp}</td>` + `<td>${getCategory(n.depth)}</td>` + '</tr>' ); }); } t += '</tbody></table>'; tip.html(t) .style('left', (d3.event.pageX + 13) + 'px') .style('top', (d3.event.pageY - 10) + 'px'); } const g = viz .selectAll('g') .data(init(root)) .enter() .append('svg:g') .attr('transform', d => `translate(${x(d.y)},${y(d.x)})`) .on('click', click) .on('mouseover', (d) => { tooltip .interrupt() .transition() .duration(100) .style('opacity', 0.9); positionAndPopulate(tooltip, d); }) .on('mousemove', (d) => { positionAndPopulate(tooltip, d); }) .on('mouseout', () => { tooltip .interrupt() .transition() .duration(250) .style('opacity', 0); }); let kx = w / root.dx; let ky = h / 1; g.append('svg:rect') .attr('width', root.dy * kx) .attr('height', d => d.dx * ky); g.append('svg:text') .attr('transform', transform) .attr('dy', '0.35em') .style('opacity', d => d.dx * ky > 12 ? 1 : 0) .text((d) => { if (!d.disp) { return d.name; } return `${d.name}: ${d.disp}`; }); // Apply color scheme g.selectAll('rect') .style('fill', (d) => { d.color = getColorFromScheme(d.name, fd.color_scheme); return d.color; }); // Zoom out when clicking outside vis // d3.select(window) // .on('click', () => click(root)); // Keep text centered in its division function transform(d) { return `translate(8,${d.dx * ky / 2})`; } // When clicking a subdivision, the vis will zoom in to it function click(d) { if (!d.children) { if (d.parent) { // Clicking on the rightmost level should zoom in return click(d.parent); } return false; } kx = (d.y ? w - 40 : w) / (1 - d.y); ky = h / d.dx; x.domain([d.y, 1]).range([d.y ? 40 : 0, w]); y.domain([d.x, d.x + d.dx]); const t = g .transition() .duration(d3.event.altKey ? 7500 : 750) .attr('transform', nd => `translate(${x(nd.y)},${y(nd.x)})`); t.select('rect') .attr('width', d.dy * kx) .attr('height', nd => nd.dx * ky); t.select('text') .attr('transform', transform) .style('opacity', nd => nd.dx * ky > 12 ? 1 : 0); d3.event.stopPropagation(); return true; } } for (let i = 0; i < data.length; i++) { drawVis(i, data); } } module.exports = partitionVis; |