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

HighlightController #777

Merged
merged 1 commit into from
Jun 22, 2021
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
9 changes: 6 additions & 3 deletions src/client/views/cube-view/cube-view.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ import { Stage } from "../../../common/models/stage/stage";
import { TimeShift } from "../../../common/models/time-shift/time-shift";
import { Timekeeper } from "../../../common/models/timekeeper/timekeeper";
import { VisualizationManifest } from "../../../common/models/visualization-manifest/visualization-manifest";
import { VisualizationProps } from "../../../common/models/visualization-props/visualization-props";
import { VisualizationSettings } from "../../../common/models/visualization-settings/visualization-settings";
import { Binary, Ternary } from "../../../common/utils/functional/functional";
import { Fn } from "../../../common/utils/general/general";
Expand All @@ -62,6 +61,7 @@ import { DragManager } from "../../utils/drag-manager/drag-manager";
import * as localStorage from "../../utils/local-storage/local-storage";
import tabularOptions from "../../utils/tabular-options/tabular-options";
import { getVisualizationComponent } from "../../visualizations";
import { HighlightController } from "../../visualizations/highlight-controller/highlight-controller";
import { CubeContext, CubeContextValue } from "./cube-context";
import { CubeHeaderBar } from "./cube-header-bar/cube-header-bar";
import "./cube-view.scss";
Expand Down Expand Up @@ -732,7 +732,7 @@ export class CubeView extends React.Component<CubeViewProps, CubeViewState> {
private visElement() {
const { essence, visualizationStage: stage, lastRefreshRequestTimestamp } = this.state;
if (!(essence.visResolve.isReady() && stage)) return null;
const visProps: VisualizationProps = {
const visProps = {
refreshRequestTimestamp: lastRefreshRequestTimestamp,
essence,
clicker: this.clicker,
Expand All @@ -743,6 +743,9 @@ export class CubeView extends React.Component<CubeViewProps, CubeViewState> {
}
};

return React.createElement(getVisualizationComponent(essence.visualization), visProps);
return <HighlightController essence={essence} clicker={this.clicker}>
{highlightProps =>
React.createElement(getVisualizationComponent(essence.visualization), { ...visProps, ...highlightProps })}
</HighlightController>;
}
}
46 changes: 26 additions & 20 deletions src/client/visualizations/bar-chart/bar-chart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,13 @@ import * as React from "react";
import { DateRange } from "../../../common/models/date-range/date-range";
import { canBucketByDefault, Dimension } from "../../../common/models/dimension/dimension";
import { findDimensionByName } from "../../../common/models/dimension/dimensions";
import { FilterClause, FixedTimeFilterClause, NumberFilterClause, StringFilterAction, StringFilterClause } from "../../../common/models/filter-clause/filter-clause";
import {
FilterClause,
FixedTimeFilterClause,
NumberFilterClause,
StringFilterAction,
StringFilterClause
} from "../../../common/models/filter-clause/filter-clause";
import { Measure } from "../../../common/models/measure/measure";
import { ConcreteSeries, SeriesDerivation } from "../../../common/models/series/concrete-series";
import { Series } from "../../../common/models/series/series";
Expand All @@ -46,6 +52,7 @@ import { VisMeasureLabel } from "../../components/vis-measure-label/vis-measure-
import { SPLIT, VIS_H_PADDING } from "../../config/constants";
import { classNames, roundToPx } from "../../utils/dom/dom";
import { BaseVisualization, BaseVisualizationState } from "../base-visualization/base-visualization";
import { hasHighlightOn } from "../highlight-controller/highlight-controller";
import "./bar-chart.scss";
import { BarCoordinates } from "./bar-coordinates";
import { BarChart as ImprovedBarChart } from "./improved-bar-chart/bar-chart";
Expand Down Expand Up @@ -252,14 +259,14 @@ export class BarChart extends BaseVisualization<BarChartState> {
};

onClick = (x: number, y: number) => {
const { essence } = this.props;
const { essence, highlight, dropHighlight, saveHighlight } = this.props;

const selectionInfo = this.calculateMousePosition(x, y);

if (!selectionInfo) return;

if (!selectionInfo.coordinates) {
this.dropHighlight();
dropHighlight();
this.setState({ selectionInfo: null });
return;
}
Expand All @@ -272,17 +279,16 @@ export class BarChart extends BaseVisualization<BarChartState> {
const rowHighlight = getFilterFromDatum(splits, path);

const currentSeries = series.get(chartIndex).definition;
if (this.highlightOn(currentSeries.key())) {
const delta = this.getHighlightClauses();
if (rowHighlight.equals(delta)) {
this.dropHighlight();
if (hasHighlightOn(highlight, currentSeries.key())) {
if (rowHighlight.equals(highlight.clauses)) {
dropHighlight();
this.setState({ selectionInfo: null });
return;
}
}

this.setState({ selectionInfo });
this.highlight(rowHighlight, series.get(chartIndex).definition.key());
saveHighlight(rowHighlight, series.get(chartIndex).definition.key());
};

getYExtent(data: Datum[], series: ConcreteSeries): number[] {
Expand Down Expand Up @@ -393,6 +399,7 @@ export class BarChart extends BaseVisualization<BarChartState> {
}

renderSelectionBubble(hoverInfo: BubbleInfo): JSX.Element {
const { dropHighlight, acceptHighlight } = this.props;
const { series, path, chartIndex, segmentLabel, coordinates } = hoverInfo;
const chartStage = this.getSingleChartStage();
const leftOffset = this.getBubbleLeftOffset(coordinates.middleX);
Expand All @@ -403,8 +410,8 @@ export class BarChart extends BaseVisualization<BarChartState> {
return <HighlightModal
left={leftOffset}
top={topOffset}
dropHighlight={this.dropHighlight}
acceptHighlight={this.acceptHighlight}
dropHighlight={dropHighlight}
acceptHighlight={acceptHighlight}
title={segmentLabel}>
{segmentValue}
</HighlightModal>;
Expand All @@ -413,7 +420,6 @@ export class BarChart extends BaseVisualization<BarChartState> {
renderHoverBubble(hoverInfo: BubbleInfo): JSX.Element {
const chartStage = this.getSingleChartStage();
const { series, path, chartIndex, segmentLabel, coordinates } = hoverInfo;
const { essence } = this.props;

const leftOffset = this.getBubbleLeftOffset(coordinates.middleX);
const topOffset = this.getBubbleTopOffset(coordinates.y, chartIndex, chartStage);
Expand Down Expand Up @@ -445,17 +451,17 @@ export class BarChart extends BaseVisualization<BarChartState> {
}

isSelected(path: Datum[], series: Series): boolean {
const { essence } = this.props;
const { essence, highlight } = this.props;
const { splits } = essence;
return this.highlightOn(series.key()) && this.getHighlightClauses().equals(getFilterFromDatum(splits, path));
return hasHighlightOn(highlight, series.key()) && highlight.clauses.equals(getFilterFromDatum(splits, path));
}

isFaded(): boolean {
return this.hasHighlight();
return this.props.highlight !== null;
}

hasAnySelectionGoingOn(): boolean {
return this.hasHighlight();
return this.props.highlight !== null;
}

isHovered(path: Datum[], series: ConcreteSeries): boolean {
Expand Down Expand Up @@ -901,15 +907,15 @@ export class BarChart extends BaseVisualization<BarChartState> {
}

renderInternals(dataset: Dataset) {
const { essence, stage } = this.props;
const { essence, stage, highlight, saveHighlight, acceptHighlight, dropHighlight } = this.props;
const { splits } = essence;
const newVersionSupports = or(Predicates.areExactSplitKinds("time"), Predicates.areExactSplitKinds("*", "time"));
if (newVersionSupports(essence)) {
return <ImprovedBarChart
highlight={this.getHighlight()}
dropHighlight={this.dropHighlight}
acceptHighlight={this.acceptHighlight}
saveHighlight={this.highlight}
highlight={highlight}
dropHighlight={dropHighlight}
acceptHighlight={acceptHighlight}
saveHighlight={saveHighlight}
dataset={dataset}
essence={essence}
stage={stage} />;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,21 @@
* limitations under the License.
*/

import { List } from "immutable";
import { Dataset, Expression } from "plywood";
import * as React from "react";
import { Essence } from "../../../common/models/essence/essence";
import { FilterClause } from "../../../common/models/filter-clause/filter-clause";
import { Timekeeper } from "../../../common/models/timekeeper/timekeeper";
import { Visualization } from "../../../common/models/visualization-manifest/visualization-manifest";
import { DatasetLoad, error, isError, isLoaded, isLoading, loaded, loading, VisualizationProps } from "../../../common/models/visualization-props/visualization-props";
import {
DatasetLoad,
error,
isError,
isLoaded,
isLoading,
loaded,
loading,
VisualizationProps
} from "../../../common/models/visualization-props/visualization-props";
import { debounceWithPromise } from "../../../common/utils/functional/functional";
import makeQuery from "../../../common/utils/query/visualization-query";
import { Loader } from "../../components/loader/loader";
Expand Down Expand Up @@ -163,40 +170,6 @@ export class BaseVisualization<S extends BaseVisualizationState> extends React.C
return null;
}

protected getHighlight(): Highlight | null {
return this.state.highlight;
}

protected hasHighlight(): boolean {
return this.state.highlight !== null;
}

protected highlightOn(key: string): boolean {
const highlight = this.getHighlight();
if (!highlight) return false;
return highlight.key === key;
}

protected getHighlightClauses(): List<FilterClause> {
const highlight = this.getHighlight();
if (!highlight) return null;
return highlight.clauses;
}

protected dropHighlight = () => this.setState({ highlight: null });

protected acceptHighlight = () => {
if (!this.hasHighlight()) return;
const { essence, clicker } = this.props;
clicker.changeFilter(essence.filter.mergeClauses(this.getHighlightClauses()));
this.setState({ highlight: null });
};

protected highlight = (clauses: List<FilterClause>, key: string | null = null) => {
const highlight = new Highlight(clauses, key);
this.setState({ highlight });
};

deriveDatasetState(dataset: Dataset): Partial<S> {
return {};
}
Expand Down
10 changes: 5 additions & 5 deletions src/client/visualizations/heat-map/heat-map.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export class HeatMap extends BaseVisualization<HeatmapState> {
}

renderInternals() {
const { essence, stage } = this.props;
const { essence, stage, highlight, saveHighlight, acceptHighlight, dropHighlight } = this.props;

const { preparedDataset: dataset } = this.state;

Expand All @@ -59,10 +59,10 @@ export class HeatMap extends BaseVisualization<HeatmapState> {
xScale={x}
yScale={y}
colorScale={color}
saveHighlight={this.highlight}
highlight={this.getHighlight()}
acceptHighlight={this.acceptHighlight}
dropHighlight={this.dropHighlight}
saveHighlight={saveHighlight}
highlight={highlight}
acceptHighlight={acceptHighlight}
dropHighlight={dropHighlight}
essence={essence}
/>
</div>;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* Copyright 2017-2021 Allegro.pl
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { List } from "immutable";
import * as React from "react";
import { ReactNode } from "react";
import { Clicker } from "../../../common/models/clicker/clicker";
import { Essence } from "../../../common/models/essence/essence";
import { FilterClause } from "../../../common/models/filter-clause/filter-clause";
import { Binary, Nullary, Unary } from "../../../common/utils/functional/functional";
import { Highlight } from "../base-visualization/highlight";

interface HighlightProps {
dropHighlight: Nullary<void>;
acceptHighlight: Nullary<void>;
highlight: Highlight | null;
saveHighlight: Binary<List<FilterClause>, string | null, void>;
}

interface HighlightControllerProps {
essence: Essence;
clicker: Clicker;
children: Unary<HighlightProps, ReactNode>;
}

interface HighlightControllerState {
highlight: Highlight | null;
}

export function hasHighlightOn(highlight: Highlight | null, key: string): boolean {
if (!highlight) return false;
return highlight.key === key;
}

export class HighlightController extends React.Component<HighlightControllerProps, HighlightControllerState> {
state: HighlightControllerState = { highlight: null };

private dropHighlight = () => this.setState({ highlight: null });

private acceptHighlight = () => {
const { highlight } = this.state;
if (highlight === null) return;
const { essence, clicker } = this.props;
clicker.changeFilter(essence.filter.mergeClauses(highlight.clauses));
this.setState({ highlight: null });
};

private saveHighlight = (clauses: List<FilterClause>, key: string | null = null) => {
const highlight = new Highlight(clauses, key);
this.setState({ highlight });
};

render() {
const { children } = this.props;
const { highlight } = this.state;
const highlightProps = {
dropHighlight: this.dropHighlight,
acceptHighlight: this.acceptHighlight,
saveHighlight: this.saveHighlight,
highlight
};
return children(highlightProps);
}
}
10 changes: 5 additions & 5 deletions src/client/visualizations/line-chart/line-chart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export class LineChart extends BaseVisualization<BaseVisualizationState> {
private chartsRef = React.createRef<HTMLDivElement>();

protected renderInternals(dataset: Dataset): JSX.Element {
const { essence, timekeeper, stage } = this.props;
const { essence, timekeeper, stage, highlight, dropHighlight, acceptHighlight, saveHighlight } = this.props;

const range = calculateXRange(essence, timekeeper, dataset);
if (!range) {
Expand All @@ -52,10 +52,10 @@ export class LineChart extends BaseVisualization<BaseVisualizationState> {
xScale={scale}
chartsContainerRef={this.chartsRef}
essence={essence}
highlight={this.getHighlight()}
dropHighlight={this.dropHighlight}
acceptHighlight={this.acceptHighlight}
saveHighlight={this.highlight}>
highlight={highlight}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image

dropHighlight={dropHighlight}
acceptHighlight={acceptHighlight}
saveHighlight={saveHighlight}>
{interactions => {
return <div className="line-chart-container">
<div className="line-charts" ref={this.chartsRef} style={{ maxHeight }}>
Expand Down
Loading