Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Sure, you can use the indices of a 2d array to draw a heatmap. I put together a quick example at <a href="http://jsfiddle.net/uArga/4/" rel="nofollow">http://jsfiddle.net/uArga/4/</a>.</p> <p>Assuming that your data looks like this:</p> <pre><code>var data = [[3,5],[9,2]]; </code></pre> <p>First I set up some basic scales to calculate the <code>x</code> and <code>y</code> offsets based on the size of the data and the size of the chart. This makes it easy to resize the chart by changing the <code>height</code> and <code>width</code> without having to manually recalculate how big each of the cells should be.</p> <pre><code>var x = d3.scale.linear() .range([0, width]) .domain([0,data[0].length]); var y = d3.scale.linear() .range([0, height]) .domain([0,data.length]); </code></pre> <p>You can then add each row to the heat map</p> <pre><code>var row = svg.selectAll(".row") .data(data) .enter().append("svg:g") .attr("class", "row"); </code></pre> <p>And then add each cell to the row using a subselect. There might be a cleaner way to do this but I basically just capture the row number alongside the data for each cell. I then use the scales to figure out the <code>x</code>, <code>y</code>, <code>height</code>, and <code>width</code> for each cell.</p> <pre><code>var col = row.selectAll(".cell") .data(function (d,i) {return d.map(function(a){return {value: a, row: i};})}) .enter().append("svg:rect") .attr("class", "cell") .attr("x", function(d, i) { return x(i); }) .attr("y", function(d, i) { return y(d.row); }) .attr("width", x(1)) .attr("height", y(1)) .style("fill", function(d) { return colorScale(d.value); }); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload