Skip to content

Commit

Permalink
feat(xy): add onProjectionUpdate listener
Browse files Browse the repository at this point in the history
  • Loading branch information
nickofthyme committed Jun 9, 2021
1 parent f7fbc12 commit 818af54
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 7 deletions.
4 changes: 4 additions & 0 deletions packages/charts/api/charts.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -1545,6 +1545,9 @@ export type ProjectedValues = {
// @public
export type ProjectionClickListener = (values: ProjectedValues) => void;

// @public
export type ProjectionUpdateListener = (values: ProjectedValues) => void;

// @public
export type Ratio = number;

Expand Down Expand Up @@ -1784,6 +1787,7 @@ export interface SettingsSpec extends Spec, LegendSpec {
// (undocumented)
onPointerUpdate?: PointerUpdateListener;
onProjectionClick?: ProjectionClickListener;
onProjectionUpdate?: ProjectionUpdateListener;
// (undocumented)
onRenderChange?: RenderChangeListener;
orderOrdinalBinsBy?: OrderBy;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import { computeSeriesGeometriesSelector } from './compute_series_geometries';
import { getGeometriesIndexKeysSelector } from './get_geometries_index_keys';
import { getOrientedProjectedPointerPositionSelector } from './get_oriented_projected_pointer_position';
import { PointerPosition } from './get_projected_pointer_position';
import { getProjectedScaledValues } from './get_projected_scaled_values';

const getPointerEventSelector = createCachedSelector(
[
Expand All @@ -49,7 +50,7 @@ function getPointerEvent(
xScale: Scale | undefined,
geometriesIndexKeys: any[],
): PointerEvent {
// update che cursorBandPosition based on chart configuration
// update the cursorBandPosition based on chart configuration
if (!xScale) {
return {
chartId,
Expand Down Expand Up @@ -79,7 +80,11 @@ function getPointerEvent(
};
}

function hasPointerEventChanged(prevPointerEvent: PointerEvent, nextPointerEvent: PointerEvent | null) {
function hasPointerEventChanged(
prevPointerEvent: PointerEvent,
nextPointerEvent: PointerEvent | null,
compareValue: boolean,
) {
if (nextPointerEvent && prevPointerEvent.type !== nextPointerEvent.type) {
return true;
}
Expand All @@ -95,7 +100,8 @@ function hasPointerEventChanged(prevPointerEvent: PointerEvent, nextPointerEvent
nextPointerEvent &&
prevPointerEvent.type === PointerEventType.Over &&
nextPointerEvent.type === PointerEventType.Over &&
(prevPointerEvent.value !== nextPointerEvent.value ||
(!compareValue ||
prevPointerEvent.value !== nextPointerEvent.value ||
prevPointerEvent.scale !== nextPointerEvent.scale ||
prevPointerEvent.unit !== nextPointerEvent.unit)
) {
Expand All @@ -106,13 +112,19 @@ function hasPointerEventChanged(prevPointerEvent: PointerEvent, nextPointerEvent

/** @internal */
export function createOnPointerMoveCaller(): (state: GlobalChartState) => void {
let yValuesString: string = '';
let prevPointerEvent: PointerEvent | null = null;
let selector: Selector<GlobalChartState, void> | null = null;
return (state: GlobalChartState) => {
if (selector === null && state.chartType === ChartType.XYAxis) {
selector = createCachedSelector(
[getSettingsSpecSelector, getPointerEventSelector, getChartIdSelector],
(settings: SettingsSpec, nextPointerEvent: PointerEvent, chartId: string): void => {
[getSettingsSpecSelector, getPointerEventSelector, getChartIdSelector, getProjectedScaledValues],
(
{ onPointerUpdate, onProjectionUpdate }: SettingsSpec,
nextPointerEvent: PointerEvent,
chartId: string,
values,
): void => {
if (prevPointerEvent === null) {
prevPointerEvent = {
chartId,
Expand All @@ -125,8 +137,14 @@ export function createOnPointerMoveCaller(): (state: GlobalChartState) => void {
// we have to update the prevPointerEvents before possibly calling the onPointerUpdate
// to avoid a recursive loop of calls caused by the impossibility to update the prevPointerEvent
prevPointerEvent = nextPointerEvent;
if (settings && settings.onPointerUpdate && hasPointerEventChanged(tempPrev, nextPointerEvent)) {
settings.onPointerUpdate(nextPointerEvent);
if (onPointerUpdate && hasPointerEventChanged(tempPrev, nextPointerEvent, true))
onPointerUpdate(nextPointerEvent);

if (onProjectionUpdate && values && hasPointerEventChanged(tempPrev, nextPointerEvent, false)) {
const oldYValues = yValuesString;
yValuesString = values.y.map(({ value }) => value).join(',');

if (oldYValues !== yValuesString) onProjectionUpdate(values);
}
},
)({
Expand Down
11 changes: 11 additions & 0 deletions packages/charts/src/specs/settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,11 @@ export type ProjectedValues = {
* The listener type for click on the projection area.
*/
export type ProjectionClickListener = (values: ProjectedValues) => void;
/**
* @public
* The listener type for mouse move on the projection area.
*/
export type ProjectionUpdateListener = (values: ProjectedValues) => void;

/** @public */
export type ElementClickListener = (
Expand Down Expand Up @@ -496,6 +501,12 @@ export interface SettingsSpec extends Spec, LegendSpec {
* X axis point, and an array of Y values for every groupId used in the chart.
*/
onProjectionClick?: ProjectionClickListener;
/**
* Attach a listener for mouse move on the projection area.
* The listener will be called with the current x value snapped to the closest
* X axis point, and an array of Y values for every groupId used in the chart.
*/
onProjectionUpdate?: ProjectionUpdateListener;
onElementClick?: ElementClickListener;
onElementOver?: ElementOverListener;
onElementOut?: BasicListener;
Expand Down
1 change: 1 addition & 0 deletions stories/interactions/16_cursor_update_action.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ export const Example = () => {
showLegend
showLegendExtra
onPointerUpdate={pointerUpdate}
onProjectionUpdate={action('onProjectionUpdate')}
externalPointerEvents={{
tooltip: { visible: topVisible, placement: topPlacement },
}}
Expand Down

0 comments on commit 818af54

Please sign in to comment.