Here’s some reusable code to compute and draw a correlation matrix from a set of data in JavaScript. Here’s how it works:

- jeezy‘s correlationMatrix function calculates a correlation matrix for a set of data.
- data2grid sets up a layout for that correlation matrix.
- chroma creates a color scale based on the extents of the correlation matrix (in this case it ignores correlations of 1, because the data set is random and so all correlations are quite low other than when both columns are the same).
- d3 draws the correlation matrix to the DOM.