function Table(options, dimensions) {
const { data, cell = { height: 30 }, text = {} } = options;
const keys = Array.from(new Set(data.flatMap(Object.keys)));
const table = [keys, ...data.map((d) => keys.map((key) => d[key]))];
const cells = table.flatMap((row, y) =>
row.map((col, x) => ({ key: col, x, y }))
);
const { height } = dimensions;
const rows = data.length + 1;
const expectedHeight = cell.height * rows;
const range = expectedHeight / height;
return [
{
type: "cell",
data: cells,
encode: {
x: "x",
y: "y"
},
scale: {
y: { range: [0, range] }
},
axis: false,
style: {
fill: (d) =>
d.y === 0 ? "#e1eafd" : d.y % 2 === 1 ? "#f6f8fe" : "#ffffff",
...cell
},
tooltip: false,
animate: false
},
{
type: "text",
data: cells,
encode: {
x: "x",
y: "y",
text: "key"
},
style: {
fontWeight: (d) => (d.y === 0 ? "bold" : undefined),
...text
},
tooltip: false,
animate: false
}
];
}