Skip to content

Commit

Permalink
feat: fix pickDragArea
Browse files Browse the repository at this point in the history
  • Loading branch information
darnautov committed Sep 22, 2020
1 parent d1125c3 commit 483aa99
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 41 deletions.
4 changes: 2 additions & 2 deletions src/chart_types/heatmap/layout/types/viewmodel_types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export interface HeatmapViewModel {
export type PickFunction = (x: Pixels, y: Pixels) => Cell[] | TextBox;

/** @internal */
export type PickDragFunction = (points: [Point, Point]) => Cell[];
export type PickDragFunction = (points: [Point, Point]) => { cells: Cell[]; x: any[]; y: any[] };

/** @internal */
export type PickDragShapeFunction = (
Expand Down Expand Up @@ -107,6 +107,6 @@ export const nullShapeViewModel = (specifiedConfig?: Config): ShapeViewModel =>
config: specifiedConfig || config,
heatmapViewModel: nullHeatmapViewModel,
pickQuads: () => [],
pickDragArea: () => [],
pickDragArea: () => ({ cells: [], x: [], y: [] }),
pickDragShape: () => {},
});
72 changes: 38 additions & 34 deletions src/chart_types/heatmap/layout/viewmodel/viewmodel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,40 +232,6 @@ export function shapeViewModel(
}
return [];
};

/**
* Return selected cells based on drag selection.
* @param start
* @param end
*/
const pickDragArea: PickDragFunction = ([start, end]) => {
const result: Cell[] = [];

const startX = Math.min(start.x, end.x);
const startY = Math.min(start.y, end.y);

const endX = Math.max(start.x, end.x);
const endY = Math.max(start.y, end.y);

const [startPoint] = pickQuads(startX, startY);
result.push(startPoint);

let { x, y } = startPoint;
x += cellWidth + maxTextWidth;

while (y <= endY) {
while (x <= endX) {
result.push(pickQuads(x, y)[0]);
x += cellWidth;
}
// move to the next line
x = startPoint.x + maxTextWidth;
y += cellHeight;
}

return result;
};

/**
* Resolves coordinates and metrics of the selected rect area.
* @param start
Expand All @@ -292,6 +258,44 @@ export function shapeViewModel(
};
};

/**
* Return selected cells based on drag selection.
*/
const pickDragArea: PickDragFunction = (bound) => {
const result = {
cells: [] as Cell[],
x: new Set(),
y: new Set(),
};

const shape = pickDragShape(bound);

let { x, y } = shape;

while (y < shape.height + shape.y) {
result.y.add(yInvertedScale(y));
while (x < shape.width + shape.x) {
const [cell] = pickQuads(x, y);
if (cell) {
result.cells.push(cell);
}
result.x.add(xInvertedScale(x));
x += cellWidth;
}
// move to the next line
x = shape.x;
y += cellHeight;
}

const x = [...result.x];

return {
...result,
x: isXAxisTimeScale ? [x[0], x[x.length - 1]] : x,
y: [...result.y],
};
};

// vertical lines
const xLines = [];
for (let i = 0; i < xValues.length + 1; i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ export function createOnBrushEndCaller(): (state: GlobalChartState) => void {
if (selector === null && state.chartType === ChartTypes.Heatmap) {
selector = createCachedSelector(
[getSpecOrNull, getSettingsSpecSelector, getPickedCells],
(spec, { onBrushEnd, rotation, brushAxis, minBrushDelta }, pickedCells): void => {
if (!spec || !onBrushEnd || pickedCells.length === 0) {
(spec, { onBrushEnd }, pickedCells): void => {
if (!spec || !onBrushEnd || pickedCells === null) {
return;
}

Expand Down
8 changes: 5 additions & 3 deletions src/chart_types/heatmap/state/selectors/picked_shapes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import createCachedSelector from 're-reselect';
import { GlobalChartState } from '../../../../state/chart_state';
import { getChartIdSelector } from '../../../../state/selectors/get_chart_id';
import { getLastDragSelector } from '../../../../state/selectors/get_last_drag';
import { Cell } from '../../layout/types/viewmodel_types';
import { Cell, PickDragFunction } from '../../layout/types/viewmodel_types';
import { TextBox } from '../../layout/viewmodel/viewmodel';
import { geometries } from './geometries';

Expand All @@ -40,9 +40,11 @@ export const getPickedShapes = createCachedSelector([geometries, getCurrentPoint
})(getChartIdSelector);

/** @internal */
export const getPickedCells = createCachedSelector([geometries, getLastDragSelector], (geoms, dragState): Cell[] => {
export const getPickedCells = createCachedSelector([geometries, getLastDragSelector], (geoms, dragState): ReturnType<
PickDragFunction
> | null => {
if (!dragState) {
return [];
return null;
}

const {
Expand Down

0 comments on commit 483aa99

Please sign in to comment.