Skip to content

Commit

Permalink
fix(crosshair): hide horizontal line when the pointer is outside chart (
Browse files Browse the repository at this point in the history
#484)

This commit fix the visibility of the crosshair horizontal line, hiding
it when the pointer projected position is outside the chart area.

fix #483
  • Loading branch information
markov00 authored Dec 5, 2019
1 parent 20ade8e commit 654d929
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 6 deletions.
3 changes: 2 additions & 1 deletion .playground/playgroud.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
AnnotationDomainTypes,
BarSeries,
RectAnnotation,
TooltipType,
} from '../src';
import { KIBANA_METRICS } from '../src/utils/data_samples/test_dataset_kibana';
export class Playground extends React.Component {
Expand Down Expand Up @@ -49,7 +50,7 @@ export class Playground extends React.Component {
<button onClick={this.onSnapshot}>Snapshot</button>
<div className="chart">
<Chart ref={this.chartRef}>
<Settings theme={LIGHT_THEME} showLegend={true} />
<Settings theme={LIGHT_THEME} showLegend={true} tooltip={TooltipType.Crosshairs} />
<Axis
id={getAxisId('time')}
position={Position.Bottom}
Expand Down
12 changes: 12 additions & 0 deletions src/chart_types/xy_chart/crosshair/crosshair_line.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { getCursorLinePosition } from './crosshair_utils';

describe('Crosshair line position', () => {
it('shuld not compute line position for outside pointer coordinates', () => {
const linePos = getCursorLinePosition(0, { width: 100, height: 100, left: 0, top: 0 }, { x: -1, y: -1 });
expect(linePos).toBeUndefined();
});
it('shuld compute line position for inside pointer coordinates', () => {
const linePos = getCursorLinePosition(0, { width: 100, height: 100, left: 0, top: 0 }, { x: 50, y: 50 });
expect(linePos).toBeDefined();
});
});
6 changes: 5 additions & 1 deletion src/chart_types/xy_chart/crosshair/crosshair_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,11 @@ export function getCursorLinePosition(
chartRotation: Rotation,
chartDimensions: Dimensions,
projectedPointerPosition: { x: number; y: number },
): Dimensions {
): Dimensions | undefined {
const { x, y } = projectedPointerPosition;
if (x < 0 || y < 0) {
return void 0;
}
const { left, top, width, height } = chartDimensions;
const isHorizontalRotated = isHorizontalRotation(chartRotation);
if (isHorizontalRotated) {
Expand Down
4 changes: 1 addition & 3 deletions src/chart_types/xy_chart/renderer/dom/crosshair.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ interface CrosshairProps {
theme: Theme;
chartRotation: Rotation;
cursorBandPosition?: Dimensions;
cursorLinePosition: Dimensions;
cursorLinePosition?: Dimensions;
tooltipType: TooltipType;
}

Expand Down Expand Up @@ -100,8 +100,6 @@ const mapStateToProps = (state: GlobalChartState): CrosshairProps => {
return {
theme: LIGHT_THEME,
chartRotation: 0,
cursorBandPosition: { top: 0, left: 0, width: 0, height: 0 },
cursorLinePosition: { top: 0, left: 0, width: 0, height: 0 },
tooltipType: TooltipType.None,
};
}
Expand Down
3 changes: 2 additions & 1 deletion src/chart_types/xy_chart/state/selectors/get_cursor_line.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ import { getSettingsSpecSelector } from '../../../../state/selectors/get_setting
import { computeChartDimensionsSelector } from './compute_chart_dimensions';
import { getProjectedPointerPositionSelector } from './get_projected_pointer_position';
import { getChartIdSelector } from '../../../../state/selectors/get_chart_id';
import { Dimensions } from '../../../../utils/dimensions';

export const getCursorLinePositionSelector = createCachedSelector(
[computeChartDimensionsSelector, getSettingsSpecSelector, getProjectedPointerPositionSelector],
(chartDimensions, settingsSpec, projectedPointerPosition) => {
(chartDimensions, settingsSpec, projectedPointerPosition): Dimensions | undefined => {
return getCursorLinePosition(settingsSpec.rotation, chartDimensions.chartDimensions, projectedPointerPosition);
},
)(getChartIdSelector);

0 comments on commit 654d929

Please sign in to comment.