all files / src/visualizations/ heatmap.js

8.57% Statements 12/140
0% Branches 0/60
0% Functions 0/17
8.76% Lines 12/137
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                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
import d3 from 'd3';
// eslint-disable-next-line no-unused-vars
import d3legend from 'd3-svg-legend';
import d3tip from 'd3-tip';
 
import { colorScalerFactory } from '../modules/colors';
import '../../stylesheets/d3tip.css';
import './heatmap.css';
 
function cmp(a, b) {
  return a > b ? 1 : -1;
}
 
// Inspired from http://bl.ocks.org/mbostock/3074470
// https://jsfiddle.net/cyril123/h0reyumq/
function heatmapVis(slice, payload) {
  const data = payload.data.records;
  const fd = slice.formData;
 
  const margin = {
    top: 10,
    right: 10,
    bottom: 35,
    left: 35,
  };
  const valueFormatter = d3.format(fd.y_axis_format);
 
  // Dynamically adjusts  based on max x / y category lengths
  function adjustMargins() {
    const pixelsPerCharX = 4.5; // approx, depends on font size
    const pixelsPerCharY = 6; // approx, depends on font size
    let longestX = 1;
    let longestY = 1;
    let datum;
 
    for (let i = 0; i < data.length; i++) {
      datum = data[i];
      longestX = Math.max(longestX, datum.x.toString().length || 1);
      longestY = Math.max(longestY, datum.y.toString().length || 1);
    }
 
    if (fd.left_margin === 'auto') {
      margin.left = Math.ceil(Math.max(margin.left, pixelsPerCharY * longestY));
      if (fd.show_legend) {
        margin.left += 40;
      }
    } else {
      margin.left = fd.left_margin;
    }
    if (fd.bottom_margin === 'auto') {
      margin.bottom = Math.ceil(Math.max(margin.bottom, pixelsPerCharX * longestX));
    } else {
      margin.bottom = fd.bottom_margin;
    }
  }
 
  function ordScale(k, rangeBands, sortMethod) {
    let domain = {};
    const actualKeys = {};  // hack to preserve type of keys when number
    data.forEach((d) => {
      domain[d[k]] = (domain[d[k]] || 0) + d.v;
      actualKeys[d[k]] = d[k];
    });
    // Not usgin object.keys() as it converts to strings
    const keys = Object.keys(actualKeys).map(s => actualKeys[s]);
    if (sortMethod === 'alpha_asc') {
      domain = keys.sort(cmp);
    } else if (sortMethod === 'alpha_desc') {
      domain = keys.sort(cmp).reverse();
    } else if (sortMethod === 'value_desc') {
      domain = Object.keys(domain).sort((a, b) => domain[a] > domain[b] ? -1 : 1);
    } else if (sortMethod === 'value_asc') {
      domain = Object.keys(domain).sort((a, b) => domain[b] > domain[a] ? -1 : 1);
    }
 
    if (k === 'y' && rangeBands) {
      domain.reverse();
    }
 
    if (rangeBands) {
      return d3.scale.ordinal().domain(domain).rangeBands(rangeBands);
    }
    return d3.scale.ordinal().domain(domain).range(d3.range(domain.length));
  }
 
  slice.container.html('');
  const matrix = {};
 
  adjustMargins();
 
  const width = slice.width();
  const height = slice.height();
  const hmWidth = width - (margin.left + margin.right);
  const hmHeight = height - (margin.bottom + margin.top);
  const fp = d3.format('.2%');
 
  const xScale = ordScale('x', null, fd.sort_x_axis);
  const yScale = ordScale('y', null, fd.sort_y_axis);
  const xRbScale = ordScale('x', [0, hmWidth], fd.sort_x_axis);
  const yRbScale = ordScale('y', [hmHeight, 0], fd.sort_y_axis);
  const X = 0;
  const Y = 1;
  const heatmapDim = [xRbScale.domain().length, yRbScale.domain().length];
 
  const minBound = fd.y_axis_bounds[0] || 0;
  const maxBound = fd.y_axis_bounds[1] || 1;
  const colorScaler = colorScalerFactory(fd.linear_color_scheme, null, null, [minBound, maxBound]);
 
  const scale = [
    d3.scale.linear()
    .domain([0, heatmapDim[X]])
    .range([0, hmWidth]),
    d3.scale.linear()
    .domain([0, heatmapDim[Y]])
    .range([0, hmHeight]),
  ];
 
  const container = d3.select(slice.selector);
 
  const canvas = container.append('canvas')
    .attr('width', heatmapDim[X])
    .attr('height', heatmapDim[Y])
    .style('width', hmWidth + 'px')
    .style('height', hmHeight + 'px')
    .style('image-rendering', fd.canvas_image_rendering)
    .style('left', margin.left + 'px')
    .style('top', margin.top + 'px')
    .style('position', 'absolute');
 
  const svg = container.append('svg')
    .attr('width', width)
    .attr('height', height)
    .style('position', 'relative');
 
  if (fd.show_values) {
    const cells = svg.selectAll('rect')
      .data(data)
      .enter()
      .append('g')
      .attr('transform', `translate(${margin.left}, ${margin.top})`);
 
    cells.append('text')
      .attr('transform', d => `translate(${xRbScale(d.x)}, ${yRbScale(d.y)})`)
      .attr('y', yRbScale.rangeBand() / 2)
      .attr('x', xRbScale.rangeBand() / 2)
      .attr('text-anchor', 'middle')
      .attr('dy', '.35em')
      .text(d => valueFormatter(d.v))
      .attr('font-size', Math.min(yRbScale.rangeBand(), xRbScale.rangeBand()) / 3 + 'px')
      .attr('fill', d => d.v >= payload.data.extents[1] / 2 ? 'white' : 'black');
  }
 
  if (fd.show_legend) {
    const colorLegend = d3.legend.color()
    .labelFormat(valueFormatter)
    .scale(colorScaler)
    .shapePadding(0)
    .cells(50)
    .shapeWidth(10)
    .shapeHeight(3)
    .labelOffset(2);
 
    svg.append('g')
    .attr('transform', 'translate(10, 5)')
    .call(colorLegend);
  }
 
  const tip = d3tip()
    .attr('class', 'd3-tip')
    .offset(function () {
      const k = d3.mouse(this);
      const x = k[0] - (hmWidth / 2);
      return [k[1] - 20, x];
    })
    .html(function () {
      let s = '';
      const k = d3.mouse(this);
      const m = Math.floor(scale[0].invert(k[0]));
      const n = Math.floor(scale[1].invert(k[1]));
      const metric = typeof fd.metric === 'object' ? fd.metric.label : fd.metric;
      if (m in matrix && n in matrix[m]) {
        const obj = matrix[m][n];
        s += '<div><b>' + fd.all_columns_x + ': </b>' + obj.x + '<div>';
        s += '<div><b>' + fd.all_columns_y + ': </b>' + obj.y + '<div>';
        s += '<div><b>' + metric + ': </b>' + valueFormatter(obj.v) + '<div>';
        if (fd.show_perc) {
          s += '<div><b>%: </b>' + fp(fd.normalized ? obj.rank : obj.perc) + '<div>';
        }
        tip.style('display', null);
      } else {
        // this is a hack to hide the tooltip because we have map it to a single <rect>
        // d3-tip toggles opacity and calling hide here is undone by the lib after this call
        tip.style('display', 'none');
      }
      return s;
    });
 
  const rect = svg.append('g')
    .attr('transform', `translate(${margin.left}, ${margin.top})`)
    .append('rect')
    .attr('pointer-events', 'all')
    .on('mousemove', tip.show)
    .on('mouseout', tip.hide)
    .style('fill-opacity', 0)
    .attr('stroke', 'black')
    .attr('width', hmWidth)
    .attr('height', hmHeight);
 
  rect.call(tip);
 
  const xAxis = d3.svg.axis()
    .scale(xRbScale)
    .tickValues(xRbScale.domain().filter(
      function (d, i) {
        return !(i % (parseInt(fd.xscale_interval, 10)));
      }))
    .orient('bottom');
 
  const yAxis = d3.svg.axis()
    .scale(yRbScale)
    .tickValues(yRbScale.domain().filter(
      function (d, i) {
        return !(i % (parseInt(fd.yscale_interval, 10)));
      }))
    .orient('left');
 
  svg.append('g')
    .attr('class', 'x axis')
    .attr('transform', 'translate(' + margin.left + ',' + (margin.top + hmHeight) + ')')
    .call(xAxis)
    .selectAll('text')
    .style('text-anchor', 'end')
    .attr('transform', 'rotate(-45)');
 
  svg.append('g')
    .attr('class', 'y axis')
    .attr('transform', 'translate(' + margin.left + ',' + margin.top + ')')
    .call(yAxis);
 
 
  const context = canvas.node().getContext('2d');
  context.imageSmoothingEnabled = false;
 
  // Compute the pixel colors; scaled by CSS.
  function createImageObj() {
    const imageObj = new Image();
    const image = context.createImageData(heatmapDim[0], heatmapDim[1]);
    const pixs = {};
    data.forEach((d) => {
      const c = d3.rgb(colorScaler(fd.normalized ? d.rank : d.perc));
      const x = xScale(d.x);
      const y = yScale(d.y);
      pixs[x + (y * xScale.domain().length)] = c;
      if (matrix[x] === undefined) {
        matrix[x] = {};
      }
      if (matrix[x][y] === undefined) {
        matrix[x][y] = d;
      }
    });
 
    let p = -1;
    for (let i = 0; i < heatmapDim[0] * heatmapDim[1]; i++) {
      let c = pixs[i];
      let alpha = 255;
      if (c === undefined) {
        c = d3.rgb('#F00');
        alpha = 0;
      }
      image.data[++p] = c.r;
      image.data[++p] = c.g;
      image.data[++p] = c.b;
      image.data[++p] = alpha;
    }
    context.putImageData(image, 0, 0);
    imageObj.src = canvas.node().toDataURL();
  }
  createImageObj();
}
 
module.exports = heatmapVis;