-
Notifications
You must be signed in to change notification settings - Fork 120
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(interactions): line cursor above the chart, band cursor below (#1453
) (#1457)
- Loading branch information
1 parent
b416ed5
commit ca004a6
Showing
5 changed files
with
201 additions
and
66 deletions.
There are no files selected for viewing
86 changes: 86 additions & 0 deletions
86
packages/charts/src/chart_types/xy_chart/renderer/dom/cursor_band.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import React from 'react'; | ||
import { connect } from 'react-redux'; | ||
|
||
import { Rect } from '../../../../geoms/types'; | ||
import { getTooltipType } from '../../../../specs'; | ||
import { TooltipType } from '../../../../specs/constants'; | ||
import { GlobalChartState } from '../../../../state/chart_state'; | ||
import { getChartRotationSelector } from '../../../../state/selectors/get_chart_rotation'; | ||
import { getChartThemeSelector } from '../../../../state/selectors/get_chart_theme'; | ||
import { getInternalIsInitializedSelector, InitStatus } from '../../../../state/selectors/get_internal_is_intialized'; | ||
import { getSettingsSpecSelector } from '../../../../state/selectors/get_settings_specs'; | ||
import { Rotation } from '../../../../utils/common'; | ||
import { LIGHT_THEME } from '../../../../utils/themes/light_theme'; | ||
import { Theme } from '../../../../utils/themes/theme'; | ||
import { getCursorBandPositionSelector } from '../../state/selectors/get_cursor_band'; | ||
|
||
interface CursorBandProps { | ||
theme: Theme; | ||
chartRotation: Rotation; | ||
cursorPosition?: Rect; | ||
tooltipType: TooltipType; | ||
fromExternalEvent?: boolean; | ||
} | ||
|
||
function canRenderBand(type: TooltipType, visible: boolean, fromExternalEvent?: boolean) { | ||
return visible && (type === TooltipType.Crosshairs || type === TooltipType.VerticalCursor || fromExternalEvent); | ||
} | ||
|
||
class CursorBandComponent extends React.Component<CursorBandProps> { | ||
static displayName = 'CursorBand'; | ||
|
||
render() { | ||
const { | ||
theme: { | ||
crosshair: { band }, | ||
}, | ||
cursorPosition, | ||
tooltipType, | ||
fromExternalEvent, | ||
} = this.props; | ||
const isBand = (cursorPosition?.width ?? 0) > 0 && (cursorPosition?.height ?? 0) > 0; | ||
if (!isBand || !cursorPosition || !canRenderBand(tooltipType, band.visible, fromExternalEvent)) { | ||
return null; | ||
} | ||
const { x, y, width, height } = cursorPosition; | ||
const { fill } = band; | ||
return ( | ||
<svg className="echCrosshair__cursor" width="100%" height="100%"> | ||
<rect {...{ x, y, width, height, fill }} /> | ||
</svg> | ||
); | ||
} | ||
} | ||
|
||
const mapStateToProps = (state: GlobalChartState): CursorBandProps => { | ||
if (getInternalIsInitializedSelector(state) !== InitStatus.Initialized) { | ||
return { | ||
theme: LIGHT_THEME, | ||
chartRotation: 0, | ||
tooltipType: TooltipType.None, | ||
}; | ||
} | ||
const settings = getSettingsSpecSelector(state); | ||
const cursorBandPosition = getCursorBandPositionSelector(state); | ||
const fromExternalEvent = cursorBandPosition?.fromExternalEvent; | ||
const tooltipType = getTooltipType(settings, fromExternalEvent); | ||
|
||
return { | ||
theme: getChartThemeSelector(state), | ||
chartRotation: getChartRotationSelector(state), | ||
cursorPosition: cursorBandPosition, | ||
tooltipType, | ||
fromExternalEvent, | ||
}; | ||
}; | ||
|
||
/** @internal */ | ||
export const CursorBand = connect(mapStateToProps)(CursorBandComponent); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
packages/charts/src/chart_types/xy_chart/renderer/dom/cursor_line.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import React from 'react'; | ||
import { connect } from 'react-redux'; | ||
|
||
import { Rect } from '../../../../geoms/types'; | ||
import { getTooltipType } from '../../../../specs'; | ||
import { TooltipType } from '../../../../specs/constants'; | ||
import { GlobalChartState } from '../../../../state/chart_state'; | ||
import { getChartRotationSelector } from '../../../../state/selectors/get_chart_rotation'; | ||
import { getChartThemeSelector } from '../../../../state/selectors/get_chart_theme'; | ||
import { getInternalIsInitializedSelector, InitStatus } from '../../../../state/selectors/get_internal_is_intialized'; | ||
import { getSettingsSpecSelector } from '../../../../state/selectors/get_settings_specs'; | ||
import { Rotation } from '../../../../utils/common'; | ||
import { LIGHT_THEME } from '../../../../utils/themes/light_theme'; | ||
import { Theme } from '../../../../utils/themes/theme'; | ||
import { getCursorBandPositionSelector } from '../../state/selectors/get_cursor_band'; | ||
|
||
interface CursorLineProps { | ||
theme: Theme; | ||
chartRotation: Rotation; | ||
cursorPosition?: Rect; | ||
tooltipType: TooltipType; | ||
fromExternalEvent?: boolean; | ||
isLine: boolean; | ||
} | ||
|
||
function canRenderBand(type: TooltipType, visible: boolean, fromExternalEvent?: boolean) { | ||
return visible && (type === TooltipType.Crosshairs || type === TooltipType.VerticalCursor || fromExternalEvent); | ||
} | ||
|
||
class CursorLineComponent extends React.Component<CursorLineProps> { | ||
static displayName = 'CursorLine'; | ||
|
||
render() { | ||
const { | ||
theme: { | ||
crosshair: { band, line }, | ||
}, | ||
cursorPosition, | ||
tooltipType, | ||
fromExternalEvent, | ||
isLine, | ||
} = this.props; | ||
|
||
if (!cursorPosition || !canRenderBand(tooltipType, band.visible, fromExternalEvent) || !isLine) { | ||
return null; | ||
} | ||
const { x, y, width, height } = cursorPosition; | ||
const { strokeWidth, stroke, dash } = line; | ||
const strokeDasharray = (dash ?? []).join(' '); | ||
return ( | ||
<svg className="echCrosshair__cursor" width="100%" height="100%"> | ||
<line {...{ x1: x, x2: x + width, y1: y, y2: y + height, strokeWidth, stroke, strokeDasharray }} /> | ||
</svg> | ||
); | ||
} | ||
} | ||
|
||
const mapStateToProps = (state: GlobalChartState): CursorLineProps => { | ||
if (getInternalIsInitializedSelector(state) !== InitStatus.Initialized) { | ||
return { | ||
theme: LIGHT_THEME, | ||
chartRotation: 0, | ||
tooltipType: TooltipType.None, | ||
isLine: false, | ||
}; | ||
} | ||
const settings = getSettingsSpecSelector(state); | ||
const cursorBandPosition = getCursorBandPositionSelector(state); | ||
const fromExternalEvent = cursorBandPosition?.fromExternalEvent; | ||
const tooltipType = getTooltipType(settings, fromExternalEvent); | ||
const isLine = cursorBandPosition?.width === 0 || cursorBandPosition?.height === 0; | ||
|
||
return { | ||
theme: getChartThemeSelector(state), | ||
chartRotation: getChartRotationSelector(state), | ||
cursorPosition: cursorBandPosition, | ||
tooltipType, | ||
fromExternalEvent, | ||
isLine, | ||
}; | ||
}; | ||
|
||
/** @internal */ | ||
export const CursorLine = connect(mapStateToProps)(CursorLineComponent); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters