Skip to content

Commit 0b44b87

Browse files
feat(brush): show crosshair cursor when brush enabled (#243)
1 parent ff7340a commit 0b44b87

File tree

4 files changed

+54
-0
lines changed

4 files changed

+54
-0
lines changed

src/components/_container.scss

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,7 @@
1313
}
1414
}
1515
}
16+
17+
.echChart--isBrushEnabled {
18+
cursor: crosshair;
19+
}

src/components/react_canvas/reactive_chart.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import classNames from 'classnames';
12
import { inject, observer } from 'mobx-react';
23
import React from 'react';
34
import { Layer, Rect, Stage } from 'react-konva';
@@ -376,6 +377,10 @@ class Chart extends React.Component<ReactiveChartProps, ReactiveChartState> {
376377
clipHeight: chartDimensions.height,
377378
};
378379

380+
const className = classNames({
381+
'echChart--isBrushEnabled': this.props.chartStore!.isCrosshairCursorVisible.get(),
382+
});
383+
379384
return (
380385
<div
381386
style={{
@@ -395,6 +400,7 @@ class Chart extends React.Component<ReactiveChartProps, ReactiveChartState> {
395400
onClick={() => {
396401
this.props.chartStore!.handleChartClick();
397402
}}
403+
className={className}
398404
>
399405
<Stage
400406
width={parentDimensions.width}

src/state/chart_state.test.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -836,4 +836,34 @@ describe('Chart Store', () => {
836836
expectedTooltipValues.set('seriesKey', 123);
837837
expect(store.legendItemTooltipValues.get()).toEqual(expectedTooltipValues);
838838
});
839+
describe('can determine if crosshair cursor is visible', () => {
840+
const brushEndListener = (): void => {
841+
return;
842+
};
843+
844+
beforeEach(() => {
845+
store.xScale = new ScaleContinuous(ScaleType.Linear, [0, 100], [0, 100]);
846+
});
847+
848+
test('when cursor is outside of chart bounds', () => {
849+
store.cursorPosition.x = -1;
850+
store.cursorPosition.y = -1;
851+
store.onBrushEndListener = brushEndListener;
852+
expect(store.isCrosshairCursorVisible.get()).toBe(false);
853+
});
854+
855+
test('when cursor is within chart bounds and brush enabled', () => {
856+
store.cursorPosition.x = 10;
857+
store.cursorPosition.y = 10;
858+
store.onBrushEndListener = brushEndListener;
859+
expect(store.isCrosshairCursorVisible.get()).toBe(true);
860+
});
861+
862+
test('when cursor is within chart bounds and brush disabled', () => {
863+
store.cursorPosition.x = 10;
864+
store.cursorPosition.y = 10;
865+
store.onBrushEndListener = undefined;
866+
expect(store.isCrosshairCursorVisible.get()).toBe(false);
867+
});
868+
});
839869
});

src/state/chart_state.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,20 @@ export class ChartStore {
221221
this.computeChart();
222222
});
223223

224+
/**
225+
* determine if crosshair cursor should be visible based on cursor position and brush enablement
226+
*/
227+
isCrosshairCursorVisible = computed(() => {
228+
const xPos = this.cursorPosition.x;
229+
const yPos = this.cursorPosition.y;
230+
231+
if (yPos < 0 || xPos < 0) {
232+
return false;
233+
}
234+
235+
return this.isBrushEnabled();
236+
});
237+
224238
/**
225239
* x and y values are relative to the container.
226240
*/

0 commit comments

Comments
 (0)