|
# DATA |
|
|
|
links = [ |
|
{a: 'Italy', b: 'Italian'}, |
|
{a: 'Germany', b: 'German'}, |
|
{a: 'France', b: 'French'}, |
|
{a: 'Spain', b: 'Spanish'}, |
|
{a: 'Switzerland', b: 'Italian'}, |
|
{a: 'Switzerland', b: 'German'}, |
|
{a: 'Switzerland', b: 'French'} |
|
] |
|
|
|
a_ids = (d3.set (links.map (d) -> d.a)).values() |
|
b_ids = (d3.set (links.map (d) -> d.b)).values() |
|
|
|
# LAYOUT |
|
|
|
# try to fit this rectangle with the matrix |
|
matrix_w = 400 |
|
matrix_h = 400 |
|
|
|
a_n = a_ids.length |
|
b_n = b_ids.length |
|
|
|
cellsize = Math.min matrix_w/b_n, matrix_h/a_n # beware! a is y and b is x! |
|
|
|
a_ids.sort() |
|
b_ids.sort() |
|
|
|
x = d3.scaleBand() |
|
.domain b_ids # beware! a is y and b is x! |
|
.range [-cellsize*b_n/2, cellsize*b_n/2] # centered in (0,0) |
|
|
|
y = d3.scaleBand() |
|
.domain a_ids |
|
.range [-cellsize*a_n/2, cellsize*a_n/2] |
|
|
|
# VIS |
|
|
|
svg = d3.select 'svg' |
|
width = svg.node().getBoundingClientRect().width |
|
height = svg.node().getBoundingClientRect().height |
|
|
|
# center the vis on (0,0) |
|
vis = svg.append 'g' |
|
.attrs |
|
transform: "translate(#{width/2},#{height/2})" |
|
|
|
# draw a background |
|
background = vis.append 'rect' |
|
.attrs |
|
class: 'background' |
|
x: x.range()[0] |
|
y: y.range()[0] |
|
width: x.range()[1]-x.range()[0] |
|
height: y.range()[1]-y.range()[0] |
|
|
|
# draw cells |
|
cells = vis.selectAll '.cell' |
|
.data links, (d) -> "#{d.a}--#{d.b}" |
|
|
|
en_cells = cells.enter().append 'rect' |
|
.attrs |
|
class: 'cell' |
|
|
|
all_cells = en_cells.merge cells |
|
|
|
all_cells |
|
.attrs |
|
x: (d) -> x(d.b) + 0.5 |
|
y: (d) -> y(d.a) + 0.5 |
|
width: x.bandwidth() - 1 |
|
height: y.bandwidth() - 1 |
|
|
|
cells.exit().remove() |