Skip to content

Commit

Permalink
Fix split by y bug
Browse files Browse the repository at this point in the history
  • Loading branch information
David Mesquita-Morris committed Oct 14, 2021
1 parent d8f871f commit 5e30434
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 249 deletions.
2 changes: 1 addition & 1 deletion docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ <h1>Outstanding work</h1>

console.timeEnd('merge');

// console.log(table);
console.log(table);

// render the table in the target element
document.getElementById('tablan').replaceWith(render.table(table, 'tablan', 'landscape'));
Expand Down
6 changes: 3 additions & 3 deletions lib/node/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ export declare function table<TRow extends Row>(cube: Cube<TRow>, axes: Axes<TRo
* @param axes The x and y axes used in the pivot operation to create the cube.
* @param onX A flag to indicate if cells in cube containing multiple values should be split on the x axis (if not, the y axis will be used).
*/
export declare function split<TRow extends Row>(keys: Cube<Cell<TRow>>, axes: Axes<TRow>, onX: boolean): Array<Array<Cell<TRow>>>;
export declare function split<TRow extends Row>(cells: Cube<Cell<TRow>>, axes: Axes<TRow>, onX: boolean): Array<Array<Cell<TRow>>>;
/**
* Merge adjacent cells in a split table on the y and/or x axes.
* @param table A table of Cells created by a previous call to splitX or splitY.
* @param cells A table of Cells created by a previous call to splitX or splitY.
* @param onX A flag to indicate that cells should be merged on the x axis.
* @param onY A flag to indicate that cells should be merged on the y axis.
*/
export declare function merge<TRow extends Row>(table: Array<Array<Cell<TRow>>>, onX: boolean, onY: boolean): void;
export declare function merge<TRow extends Row>(cells: Array<Array<Cell<TRow>>>, onX: boolean, onY: boolean): void;
46 changes: 23 additions & 23 deletions lib/node/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ exports.merge = exports.split = exports.table = void 0;
function table(cube, axes, getKey, onX) {
const identity = { index: 0 };
// convert the source data to keys and remove resulting duplicates
const keys = cube.map(row => row.map(table => table.length ? tableCells(table, getKey, identity) : [{ text: '', style: 'empty', index: [], source: [], rows: 1, cols: 1 }]));
const cells = cube.map(row => row.map(table => table.length ? tableCells(table, getKey, identity) : [{ text: '', style: 'empty', index: [], source: [], rows: 1, cols: 1 }]));
// create the resultant table
return split(keys, axes, onX);
return split(cells, axes, onX);
}
exports.table = table;
/**
Expand All @@ -22,47 +22,47 @@ exports.table = table;
* @param axes The x and y axes used in the pivot operation to create the cube.
* @param onX A flag to indicate if cells in cube containing multiple values should be split on the x axis (if not, the y axis will be used).
*/
function split(keys, axes, onX) {
function split(cells, axes, onX) {
// calcuate the x and y splits required
const xSplits = axes.x.map((_, iX) => onX ? leastCommonMultiple(keys, row => row[iX].length) : 1);
const ySplits = keys.map(row => onX ? 1 : leastCommonMultiple(row, table => table.length));
const xSplits = axes.x.map((_, iX) => onX ? leastCommonMultiple(cells, row => row[iX].length) : 1);
const ySplits = cells.map(row => onX ? 1 : leastCommonMultiple(row, table => table.length));
// iterate and expand the y axis based on the split data
return expand(keys, ySplits, (row, ySplit, ysi, iY) => {
return expand(cells, ySplits, (row, ySplit, ysi, iY) => {
// iterate and expand the x axis based on the split data
return expand(row, xSplits, (values, xSplit, xsi) => {
return expand(row, xSplits, (cell, xSplit, xsi) => {
// generate the cube cells
return { ...values[Math.floor(values.length * (ysi + xsi) / (xSplit * ySplit))] };
return { ...cell[Math.floor(cell.length * (ysi + xsi) / (xSplit * ySplit))] };
// generate the y axis row header cells
}, axes.y[iY].map(criterion => axis(criterion, 'y')));
// generate the x axis column header rows
}, axes.x[0].map((_, iY) => {
}, axes.x[0].map((_, iC) => {
// iterate and expand the x axis
return expand(axes.x, xSplits, xPoint => {
// generate the x axis cells
return axis(xPoint[iY], 'x');
return axis(xPoint[iC], 'x');
// generate the x/y header
}, axes.y[0].map(() => axis({ key: '', value: '' }, 'xy')));
}));
}
exports.split = split;
/**
* Merge adjacent cells in a split table on the y and/or x axes.
* @param table A table of Cells created by a previous call to splitX or splitY.
* @param cells A table of Cells created by a previous call to splitX or splitY.
* @param onX A flag to indicate that cells should be merged on the x axis.
* @param onY A flag to indicate that cells should be merged on the y axis.
*/
function merge(table, onX, onY) {
function merge(cells, onX, onY) {
let next;
forEachRev(table, (row, iY) => {
forEachRev(row, (value, iX) => {
if (onY && iY && (next = table[iY - 1][iX]) && keyEquals(next, value) && next.cols === value.cols) {
next.rows += value.rows;
mergeContext(next, value);
forEachRev(cells, (row, iY) => {
forEachRev(row, (cell, iX) => {
if (onY && iY && (next = cells[iY - 1][iX]) && keyEquals(next, cell) && next.cols === cell.cols) {
next.rows += cell.rows;
mergeContext(next, cell);
row.splice(iX, 1);
}
else if (onX && iX && (next = row[iX - 1]) && keyEquals(next, value) && next.rows === value.rows) {
next.cols += value.cols;
mergeContext(next, value);
else if (onX && iX && (next = row[iX - 1]) && keyEquals(next, cell) && next.rows === cell.rows) {
next.cols += cell.cols;
mergeContext(next, cell);
row.splice(iX, 1);
}
});
Expand All @@ -73,11 +73,11 @@ exports.merge = merge;
* Merges the context of two adjacent cells.
* @hidden
*/
function mergeContext(next, value) {
value.index.forEach((index, i) => {
function mergeContext(next, cell) {
cell.index.forEach((index, i) => {
if (!next.index.includes(index)) {
next.index.push(index);
next.source.push(value.source[i]);
next.source.push(cell.source[i]);
}
});
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@steelbreeze/landscape",
"version": "3.4.1",
"version": "3.4.2",
"description": "Landscape map viewpoint visualisation",
"main": "lib/node/index.js",
"module": "lib/node/index.js",
Expand Down
198 changes: 0 additions & 198 deletions src/index.old

This file was deleted.

Loading

0 comments on commit 5e30434

Please sign in to comment.