function Grid(
data,
{ columnLength = 10, squareSize = width / columnLength } = {}
) {
const dataLength = data.length;
const size = squareSize;
const columns =
columnLength !== null && columnLength > 1
? columnLength
: Math.round(width / size);
const height = (dataLength / columns) * size;
const svg = DOM.svg(width, height + size);
const sel = d3.select(svg);
const scaleColumn = d3
.scaleLinear()
.domain([0, columns])
.range([0, columns * size]);
const scaleRow = d3
.scaleLinear()
.domain([0, data.length / columns])
.range([0, height]);
const group = sel
.selectAll("g")
.data(data)
.enter()
.append("g")
.attr("transform", (d, i) => {
const x = i % columns;
const y = Math.floor(i / columns);
return `translate(${scaleColumn(x)}, ${scaleRow(y)})`;
});
group
.append("rect")
.attr("x", 0)
.attr("y", 0)
.attr("width", squareSize)
.attr("height", squareSize)
.attr("stroke-width", 0.2)
.attr("stroke", "#333")
.attr("fill", (d, i) => {
return i >= 0 && i <= airTargetLosses.aircrafts.max
? fill.aircrafts
: i > airTargetLosses.aircrafts.max &&
i <= airTargetLosses.helicopters.max
? fill.helicopters
: i > airTargetLosses.helicopters.max && i <= airTargetLosses.uav.max
? fill.uav
: fill.cruise_missiles;
});
return svg;
}