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 | 1× 1× 1× 1× 1× 1× | /* eslint-disable prefer-rest-params, no-param-reassign */ // Copied and modified from // https://github.com/kmandov/d3-horizon-chart import d3 from 'd3'; import './horizon.css'; const horizonChart = function () { let colors = [ '#313695', '#4575b4', '#74add1', '#abd9e9', '#fee090', '#fdae61', '#f46d43', '#d73027', ]; let height = 30; const y = d3.scale.linear().range([0, height]); let bands = colors.length >> 1; // number of bands in each direction (positive / negative) let width = 1000; let offsetX = 0; let spacing = 0; let mode = 'offset'; let axis; let title; let extent; // the extent is derived from the data, unless explicitly set via .extent([min, max]) let x; let canvas; function my(data) { const horizon = d3.select(this); const step = width / data.length; horizon.append('span') .attr('class', 'title') .text(title); horizon.append('span') .attr('class', 'value'); canvas = horizon.append('canvas'); canvas .attr('width', width) .attr('height', height); const context = canvas.node().getContext('2d'); context.imageSmoothingEnabled = false; // update the y scale, based on the data extents const ext = extent || d3.extent(data, d => d.y); const max = Math.max(-ext[0], ext[1]); y.domain([0, max]); // x = d3.scaleTime().domain[]; axis = d3.svg.axis(x).ticks(5); context.clearRect(0, 0, width, height); // context.translate(0.5, 0.5); // the data frame currently being shown: const startIndex = Math.floor(Math.max(0, -(offsetX / step))); const endIndex = Math.floor(Math.min(data.length, startIndex + (width / step))); // skip drawing if there's no data to be drawn if (startIndex > data.length) { return; } // we are drawing positive & negative bands separately to avoid mutating canvas state // http://www.html5rocks.com/en/tutorials/canvas/performance/ let negative = false; // draw positive bands let value; let bExtents; for (let b = 0; b < bands; b += 1) { context.fillStyle = colors[bands + b]; // Adjust the range based on the current band index. bExtents = (b + 1 - bands) * height; y.range([bands * height + bExtents, bExtents]); // only the current data frame is being drawn i.e. what's visible: for (let i = startIndex; i < endIndex; i++) { value = data[i].y; if (value <= 0) { negative = true; continue; } if (value === undefined) { continue; } context.fillRect(offsetX + i * step, y(value), step + 1, y(0) - y(value)); } } // draw negative bands if (negative) { // mirror the negative bands, by flipping the canvas if (mode === 'offset') { context.translate(0, height); context.scale(1, -1); } for (let b = 0; b < bands; b++) { context.fillStyle = colors[bands - b - 1]; // Adjust the range based on the current band index. bExtents = (b + 1 - bands) * height; y.range([bands * height + bExtents, bExtents]); // only the current data frame is being drawn i.e. what's visible: for (let ii = startIndex; ii < endIndex; ii++) { value = data[ii].y; if (value >= 0) { continue; } context.fillRect(offsetX + ii * step, y(-value), step + 1, y(0) - y(-value)); } } } } my.axis = function (_) { if (!arguments.length) { return axis; } axis = _; return my; }; my.title = function (_) { if (!arguments.length) { return title; } title = _; return my; }; my.canvas = function (_) { if (!arguments.length) { return canvas; } canvas = _; return my; }; // Array of colors representing the number of bands my.colors = function (_) { if (!arguments.length) { return colors; } colors = _; // update the number of bands bands = colors.length >> 1; return my; }; my.height = function (_) { if (!arguments.length) { return height; } height = _; return my; }; my.width = function (_) { if (!arguments.length) { return width; } width = _; return my; }; my.spacing = function (_) { if (!arguments.length) { return spacing; } spacing = _; return my; }; // mirror or offset my.mode = function (_) { if (!arguments.length) { return mode; } mode = _; return my; }; my.extent = function (_) { if (!arguments.length) { return extent; } extent = _; return my; }; my.offsetX = function (_) { if (!arguments.length) { return offsetX; } offsetX = _; return my; }; return my; }; function horizonViz(slice, payload) { const fd = slice.formData; const div = d3.select(slice.selector); div.selectAll('*').remove(); let extent; if (fd.horizon_color_scale === 'overall') { let allValues = []; payload.data.forEach(function (d) { allValues = allValues.concat(d.values); }); extent = d3.extent(allValues, d => d.y); } else if (fd.horizon_color_scale === 'change') { payload.data.forEach(function (series) { const t0y = series.values[0].y; // value at time 0 series.values = series.values.map(d => Object.assign({}, d, { y: d.y - t0y }), ); }); } div.selectAll('.horizon') .data(payload.data) .enter() .append('div') .attr('class', 'horizon') .each(function (d, i) { horizonChart() .height(fd.series_height) .width(slice.width()) .extent(extent) .title(d.key) .call(this, d.values, i); }); } module.exports = horizonViz; |