Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(webapp): sync crosshair in different timelines #1813

Merged
merged 1 commit into from
Jan 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
const defaultOptions = {
syncCrosshairsWith: [],
};

// Enhances the default Plot object with the API from the crosshair plugin
// https://github.com/flot/flot/blob/de34ce947d8ebfb2cac0b682a130ba079d8d654b/source/jquery.flot.crosshair.js#L74
type PlotWithCrosshairsSupport = jquery.flot.plot &
jquery.flot.plotOptions & {
setCrosshair(pos: { x: number; y: number }): void;
clearCrosshair(): void;
};

(function ($) {
function init(plot: PlotWithCrosshairsSupport) {
function getOptions() {
return plot.getOptions() as jquery.flot.plotOptions & {
syncCrosshairsWith: typeof defaultOptions['syncCrosshairsWith'];
};
}

function accessExternalInstance(id: string) {
// Access another flotjs instance
// https://github.com/flot/flot/blob/de34ce947d8ebfb2cac0b682a130ba079d8d654b/source/jquery.flot.js#L969
const p: PlotWithCrosshairsSupport = $(`#${id}`).data('plot');
return p;
}

function onPlotHover(
syncCrosshairsWith: typeof defaultOptions['syncCrosshairsWith'],
e: unknown,
position: { x: number; y: number }
) {
syncCrosshairsWith.forEach((id) =>
accessExternalInstance(id).setCrosshair(position)
);
}

function clearCrosshairs(
syncCrosshairsWith: typeof defaultOptions['syncCrosshairsWith']
) {
syncCrosshairsWith.forEach((id) =>
accessExternalInstance(id).clearCrosshair()
);
}

plot.hooks!.bindEvents!.push(() => {
const options = getOptions();

plot
.getPlaceholder()
.bind('plothover', onPlotHover.bind(null, options.syncCrosshairsWith));

plot
.getPlaceholder()
.bind(
'mouseleave',
clearCrosshairs.bind(null, options.syncCrosshairsWith)
);
});

plot.hooks!.shutdown!.push(() => {
const options = getOptions();

clearCrosshairs(options.syncCrosshairsWith);

plot
.getPlaceholder()
.bind(
'mouseleave',
clearCrosshairs.bind(null, options.syncCrosshairsWith)
);

plot
.getPlaceholder()
.unbind(
'plothover',
onPlotHover.bind(null, options.syncCrosshairsWith)
);
});
}

$.plot.plugins.push({
init,
options: defaultOptions,
name: 'crosshair-sync',
version: '1.0',
});
})(jQuery);
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ import React from 'react';
import ReactFlot from 'react-flot';
import 'react-flot/flot/jquery.flot.time.min';
import './Selection.plugin';
import 'react-flot/flot/jquery.flot.crosshair.min';
import 'react-flot/flot/jquery.flot.crosshair';
import './TimelineChartPlugin';
import './Tooltip.plugin';
import './Annotations.plugin';
import './ContextMenu.plugin';
import './CrosshairSync.plugin';

interface TimelineChartProps {
onSelect: (from: string, until: string) => void;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ type TimelineChartWrapperProps = TimelineDataProps & {

/** What element to render when clicking */
ContextMenu?: (props: ContextMenuProps) => React.ReactNode;

/** The list of timeline IDs (flotjs component) to sync the crosshair with */
syncCrosshairsWith?: string[];
};

class TimelineChartWrapper extends React.Component<
Expand Down Expand Up @@ -150,6 +153,7 @@ class TimelineChartWrapper extends React.Component<
clickable: true,
},
annotations: [],
syncCrosshairsWith: [],
yaxis: {
show: false,
min: 0,
Expand Down Expand Up @@ -210,14 +214,18 @@ class TimelineChartWrapper extends React.Component<
this.state.flotOptions.annotations = this.composeAnnotationsList();
}

// TODO: this only seems to sync props back into the state, which seems unnecessary
componentDidUpdate(prevProps: TimelineChartWrapperProps) {
if (
prevProps.selection !== this.props.selection ||
prevProps.annotations !== this.props.annotations
prevProps.annotations !== this.props.annotations ||
prevProps.syncCrosshairsWith !== this.props.syncCrosshairsWith
) {
const newFlotOptions = this.state.flotOptions;
newFlotOptions.grid.markings = this.plotMarkings();
newFlotOptions.annotations = this.composeAnnotationsList();
newFlotOptions.syncCrosshairsWith = this.props.syncCrosshairsWith;

this.setState({ flotOptions: newFlotOptions });
}
}
Expand Down
12 changes: 12 additions & 0 deletions webapp/javascript/pages/ContinuousComparisonView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,10 @@ function ComparisonApp() {
timelineA={leftTimeline}
timelineB={rightTimeline}
onSelect={handleSelectMain}
syncCrosshairsWith={[
'timeline-chart-left',
'timeline-chart-right',
]}
selection={{
left: {
from: leftFrom,
Expand Down Expand Up @@ -292,6 +296,10 @@ function ComparisonApp() {
id="timeline-chart-left"
data-testid="timeline-left"
selectionWithHandler
syncCrosshairsWith={[
'timeline-chart-double',
'timeline-chart-right',
]}
timelineA={leftTimeline}
selection={{
left: {
Expand Down Expand Up @@ -349,6 +357,10 @@ function ComparisonApp() {
id="timeline-chart-right"
data-testid="timeline-right"
timelineA={rightTimeline}
syncCrosshairsWith={[
'timeline-chart-double',
'timeline-chart-left',
]}
selectionWithHandler
selection={{
right: {
Expand Down
12 changes: 12 additions & 0 deletions webapp/javascript/pages/ContinuousDiffView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,10 @@ function ComparisonDiffApp() {
onSelect={(from, until) => {
dispatch(actions.setFromAndUntil({ from, until }));
}}
syncCrosshairsWith={[
'timeline-chart-left',
'timeline-chart-right',
]}
selection={{
left: {
from: leftFrom,
Expand Down Expand Up @@ -174,6 +178,10 @@ function ComparisonDiffApp() {
key="timeline-chart-left"
id="timeline-chart-left"
timelineA={leftTimeline}
syncCrosshairsWith={[
'timeline-chart-diff',
'timeline-chart-right',
]}
selectionWithHandler
onSelect={(from, until) => {
dispatch(actions.setLeft({ from, until }));
Expand Down Expand Up @@ -209,6 +217,10 @@ function ComparisonDiffApp() {
id="timeline-chart-right"
selectionWithHandler
timelineA={rightTimeline}
syncCrosshairsWith={[
'timeline-chart-diff',
'timeline-chart-left',
]}
onSelect={(from, until) => {
dispatch(actions.setRight({ from, until }));
}}
Expand Down