function discreteLegend({
labels,
nColumns=1,
colWidth=200,
keySize=32,
margin=5,
textLeft=40,
fontFamily="Helvetica Neue, sans-serif",
fontSize=16
} = {}) {
let keySpace = keySize/3;
let maxKeysPerCol = Math.ceil((labels.length+1)/nColumns)
let height = maxKeysPerCol * (keySize+keySpace+margin)
let textTop = (keySize + keySpace)/2
const svg = d3.create("svg")
.attr("width", width)
.attr("height", height)
.attr("viewBox", [0, 0, width, height])
.style("overflow", "visible")
.style("display", "block");
svg.append("g")
.selectAll("rect")
.data(labels)
.join("rect")
.attr("x", (d, i) => Math.floor(i/maxKeysPerCol) * (colWidth))
.attr("y", (d, i) => (i%maxKeysPerCol) * (keySize+keySpace))
.attr("width", keySize)
.attr("height", keySize)
.attr("fill", d => d.primary);
svg.append("g")
.selectAll("text")
.data(labels)
.join("text")
.attr("x", (d, i) => Math.floor(i/maxKeysPerCol) * (colWidth) + textLeft)
.attr("y", (d, i) => ((i%maxKeysPerCol) * (keySize+keySpace)) + textTop)
.attr("font-family",fontFamily)
.attr("font-size",fontSize)
.attr("fill","black")
.text(d => d.party);
return svg.node();
}