Skip to content

Kepler.gl is a powerful open source geospatial analysis tool for large-scale data sets.

[chore] timeline refactoring (#2802)

Igor Dykhta6f35313f7034
diff --git a/src/components/src/common/field-selector.tsx b/src/components/src/common/field-selector.tsx
index e317a51d7..1220e2b6f 100644
--- a/src/components/src/common/field-selector.tsx
+++ b/src/components/src/common/field-selector.tsx
@@ -66,6 +66,7 @@ export type FieldType = string | TooltipField | Field;
export type FieldValue = string | {name: string} | string[] | {name: string}[];
export type FieldSelectorProps<Option extends MinimalField> = {
+ id?: string;
fields: Option[];
onSelect: (
items:
diff --git a/src/components/src/common/range-slider-timeline-panel.tsx b/src/components/src/common/range-slider-timeline-panel.tsx
index 1faf07708..178b08799 100644
--- a/src/components/src/common/range-slider-timeline-panel.tsx
+++ b/src/components/src/common/range-slider-timeline-panel.tsx
@@ -4,8 +4,8 @@
import React, {useMemo} from 'react';
import RangeSliderTimelineFactory from './range-slider-timeline';
-function RangeSliderTimelinePanelFactory(RangeSliderTimeline) {
- const RangeSliderTimelinePanel = ({timelines, scaledValue, style}) => {
+function RangeSliderSubAnimationPanelFactory(RangeSliderTimeline) {
+ const RangeSliderSubAnimationPanel = ({subAnimations, scaledValue, style}) => {
const containerStyle = useMemo(
() => ({
display: 'flex',
@@ -18,16 +18,16 @@ function RangeSliderTimelinePanelFactory(RangeSliderTimeline) {
return (
<div style={containerStyle}>
- {timelines.map((timeline, index) => (
- <RangeSliderTimeline key={index} timeline={timeline} scaledValue={scaledValue} />
+ {subAnimations.map((subAnimation, index) => (
+ <RangeSliderTimeline key={index} subAnimation={subAnimation} scaledValue={scaledValue} />
))}
</div>
);
};
- return RangeSliderTimelinePanel;
+ return RangeSliderSubAnimationPanel;
}
-RangeSliderTimelinePanelFactory.deps = [RangeSliderTimelineFactory];
+RangeSliderSubAnimationPanelFactory.deps = [RangeSliderTimelineFactory];
-export default RangeSliderTimelinePanelFactory;
+export default RangeSliderSubAnimationPanelFactory;
diff --git a/src/components/src/common/range-slider-timeline.tsx b/src/components/src/common/range-slider-timeline.tsx
index cb8db8c3c..f8dad7505 100644
--- a/src/components/src/common/range-slider-timeline.tsx
+++ b/src/components/src/common/range-slider-timeline.tsx
@@ -41,8 +41,8 @@ const TIMELINE_INDICATOR_STYLE: CSSProperties = {
};
function RangeSliderTimelineFactory() {
- const RangeSliderTimeline = ({timeline, scaledValue, style}) => {
- const {startTime, endTime, syncMode, Icon, label} = timeline;
+ const RangeSliderTimeline = ({subAnimation, scaledValue, style}) => {
+ const {startTime, endTime, syncMode, Icon, label} = subAnimation;
const progressStyle: CSSProperties = {
left: `${startTime}%`,
diff --git a/src/components/src/common/range-slider.tsx b/src/components/src/common/range-slider.tsx
index 36f19ee46..f56a66652 100644
--- a/src/components/src/common/range-slider.tsx
+++ b/src/components/src/common/range-slider.tsx
@@ -8,7 +8,7 @@ import styled from 'styled-components';
import RangePlotFactory from './range-plot';
import Slider from './slider/slider';
import {Input} from './styled-components';
-import RangeSliderTimelinePanelFactory from '../common/range-slider-timeline-panel';
+import RangeSliderSubAnimationPanelFactory from '../common/range-slider-timeline-panel';
import {
observeDimensions,
unobserveDimensions,
@@ -66,7 +66,7 @@ interface RangeSliderProps {
step?: number;
sliderHandleWidth?: number;
xAxis?: ElementType;
- timelines?: any[];
+ subAnimations?: any[];
timelineLabel?: string;
timezone?: string | null;
@@ -88,11 +88,11 @@ interface RangeSliderProps {
const RANGE_SLIDER_TIMELINE_PANEL_STYLE = {marginLeft: '-32px'};
-RangeSliderFactory.deps = [RangePlotFactory, RangeSliderTimelinePanelFactory];
+RangeSliderFactory.deps = [RangePlotFactory, RangeSliderSubAnimationPanelFactory];
export default function RangeSliderFactory(
RangePlot: ReturnType<typeof RangePlotFactory>,
- RangeSliderTimelinePanel: ReturnType<typeof RangeSliderTimelinePanelFactory>
+ RangeSliderSubAnimationPanel: ReturnType<typeof RangeSliderSubAnimationPanelFactory>
): ComponentType<RangeSliderProps> {
class RangeSlider extends Component<RangeSliderProps> {
static defaultProps = {
@@ -239,7 +239,7 @@ export default function RangeSliderFactory(
timeFormat,
playbackControlWidth,
setFilterPlot,
- timelines,
+ subAnimations: subAnimations,
animationWindow,
filter,
datasets
@@ -251,7 +251,7 @@ export default function RangeSliderFactory(
const value = this.props.plotValue || this.filterValueSelector(this.props);
const scaledValue =
- timelines?.length && range
+ subAnimations?.length && range
? scaleSourceDomainToDestination(value as [number, number], range as [number, number])
: [0, 0];
const commonPadding = `${Number(sliderHandleWidth) / 2}px`;
@@ -287,9 +287,9 @@ export default function RangeSliderFactory(
style={{paddingLeft: commonPadding}}
/>
) : null}
- {timelines?.length ? (
- <RangeSliderTimelinePanel
- timelines={timelines}
+ {subAnimations?.length ? (
+ <RangeSliderSubAnimationPanel
+ subAnimations={subAnimations}
scaledValue={scaledValue}
style={RANGE_SLIDER_TIMELINE_PANEL_STYLE}
/>
diff --git a/src/components/src/common/slider/slider.tsx b/src/components/src/common/slider/slider.tsx
index cc9ee6694..309d2d3ca 100644
--- a/src/components/src/common/slider/slider.tsx
+++ b/src/components/src/common/slider/slider.tsx
@@ -50,7 +50,7 @@ type SliderProps = {
enableBarDrag: boolean;
showTooltip: boolean;
vertical: boolean;
- marks?: number[];
+ marks?: number[] | null;
classSet?: {[key: string]: boolean};
disabled: boolean;
className?: string;
diff --git a/src/components/src/common/timeline-slider.tsx b/src/components/src/common/timeline-slider.tsx
index 958d0e77b..6af31dfe7 100644
--- a/src/components/src/common/timeline-slider.tsx
+++ b/src/components/src/common/timeline-slider.tsx
@@ -72,7 +72,7 @@ function TimelineSliderFactory() {
}) => {
const onThrottleUpdate = useMemo(() => throttle(setTimelineValue, 20), [setTimelineValue]);
- const {step, domain, value, timeFormat, defaultTimeFormat, timezone, animationWindow} =
+ const {step, domain, value, timeFormat, defaultTimeFormat, timezone, animationWindow, marks} =
timeline;
const isRanged = useMemo(
@@ -141,6 +141,7 @@ function TimelineSliderFactory() {
onSlider1Change={onSlider1Change}
value0={value0}
value1={value1}
+ marks={marks}
/>
</SliderWrapper>
{timeEnd ? (
diff --git a/src/components/src/filters/time-widget-top.tsx b/src/components/src/filters/time-widget-top.tsx
index 3fba9dbbd..304aa2484 100644
--- a/src/components/src/filters/time-widget-top.tsx
+++ b/src/components/src/filters/time-widget-top.tsx
@@ -122,6 +122,7 @@ function TimeWidgetTopFactory(FieldSelector: ReturnType<typeof FieldSelectorFact
<FieldSelector
fields={yAxisFields}
placement="top"
+ id="selected-time-widget-field"
value={filter.yAxis ? filter.yAxis.name : null}
onSelect={_setFilterPlotYAxis}
placeholder="placeholder.yAxis"
diff --git a/src/types/reducers.d.ts b/src/types/reducers.d.ts
index 132514545..4e68bbb89 100644
--- a/src/types/reducers.d.ts
+++ b/src/types/reducers.d.ts
@@ -270,6 +270,7 @@ export type Timeline = {
defaultTimeFormat?: null | string;
timeFormat?: null | string;
timezone?: null | string;
+ timeBins?: null | Record<string, any>;
animationWindow?: null | Filter['animationWindow'];
marks?: null | number[];
} & Record<string, any>;
diff --git a/src/utils/src/data-utils.ts b/src/utils/src/data-utils.ts
index f34364233..b253499a4 100644
--- a/src/utils/src/data-utils.ts
+++ b/src/utils/src/data-utils.ts
@@ -181,7 +181,7 @@ export function normalizeSliderValue(
val: number,
minValue: number,
step: number,
- marks?: number[]
+ marks?: number[] | null
): number {
if (marks && marks.length) {
// Use in slider, given a number and an array of numbers, return the nears number from the array
diff --git a/src/utils/src/time.ts b/src/utils/src/time.ts
index 554386121..7a1121d24 100644
--- a/src/utils/src/time.ts
+++ b/src/utils/src/time.ts
@@ -14,9 +14,16 @@ import {
TickInterval
} from '@kepler.gl/constants';
import {toArray} from '@kepler.gl/common-utils';
-import {AnimationConfig, Timeline, TimeRangeFilter} from '@kepler.gl/types';
+import {AnimationConfig, Timeline, TimeRangeFilter, Filter} from '@kepler.gl/types';
import {getFrequency} from './aggregation';
+import {getBinThresholds} from './plot';
+import {KeplerTableModel} from './types';
+
+export const TIMELINE_MODES = {
+ inner: 'inner',
+ outer: 'outer'
+};
export const TileTimeInterval = {
YEAR: 'Y',
@@ -42,11 +49,6 @@ export const LayerToFilterTimeInterval = {
[TileTimeInterval.YEAR]: INTERVAL['1-year']
};
-export const TIMELINE_MODES = {
- inner: 'inner',
- outer: 'outer'
-};
-
export const SAMPLE_TIMELINE = {
// value: [15], // represent 15% of the all width
value: [5, 15], // represent start at 5% and ends at 15%
@@ -83,37 +85,7 @@ export const getTimelineFromAnimationConfig = (animationConfig: AnimationConfig)
defaultTimeFormat,
timeFormat,
timezone,
- marks: null
- };
-};
-
-export const getTimelineFromFilter = (filter: TimeRangeFilter): Timeline => {
- const {
- value,
- domain,
- speed,
- isAnimating,
- step,
- // @ts-expect-error
- timeSteps,
- defaultTimeFormat,
- timeFormat,
- timezone,
- animationWindow
- } = filter;
-
- return {
- value,
- enableInteraction: true,
- domain,
- speed,
- isAnimating,
- step,
- timeSteps,
- defaultTimeFormat,
- timeFormat,
- timezone,
- animationWindow,
+ timeBins: null,
marks: null
};
};
@@ -221,10 +193,13 @@ function detectInterval(values: number[] = [], domain, maxSteps = 10) {
/**
* mappedValue is saved to dataset.fields.filterProps
- * @param dataset
+ * @param dataset {KeplerTable}
* @param filter
*/
-export function getFilterMappedValue(dataset, filter) {
+export function getFilterMappedValue(
+ dataset: KeplerTableModel<any, any>,
+ filter: Filter
+): Filter['mappedValue'] | null {
const dataId = dataset.id;
const fieldName = filter.name[filter.dataId.indexOf(dataId)];
const field = dataset.getColumnField(fieldName);
@@ -257,7 +232,7 @@ function getDurationUnit(duration) {
return ['milliseconds', 1];
}
-export function intervalToFunction(id) {
+export function intervalToFunction(id: string) {
const [stepStr, interval] = id.split('-');
const step = parseInt(stepStr); // eslint-disable-line radix
if (!step) {
@@ -275,7 +250,16 @@ export function intervalToFunction(id) {
return TIME_INTERVALS[interval].every(step);
}
-export function getInitialInterval(filter, datasets) {
+/**
+ * Get initial interval from filter and datasets
+ * @param filter
+ * @param datasets
+ * @returns
+ */
+export function getInitialInterval(
+ filter: Filter,
+ datasets: Record<string, KeplerTableModel<any, any>>
+): string {
const {domain} = filter;
const mergeMappedValue = filter.dataId.reduce((accu, dataId) => {
const mappedValue = getFilterMappedValue(datasets[dataId], filter);
@@ -287,12 +271,13 @@ export function getInitialInterval(filter, datasets) {
accu.push(mappedValue[i]);
}
return accu;
- }, []);
+ }, [] as any[]);
// check if data has predefined interval
let interval = detectInterval(mergeMappedValue, domain);
if (!interval) {
+ // @ts-expect-error need better types for domain
const [t0, t1] = domain;
interval = getIntervalByTicks(BINS_LARGE, t0, t1);
}
@@ -326,3 +311,42 @@ export function filterIntervalOptions(options, domain) {
return count >= minBins && count <= maxBins;
});
}
+
+/**
+ * Get timeline from filter
+ * @param filter TimeRangeFilter filter
+ * @returns Timeline
+ */
+export const getTimelineFromFilter = (filter: TimeRangeFilter): Timeline => {
+ const {
+ value,
+ domain,
+ speed,
+ isAnimating,
+ step,
+ // @ts-expect-error
+ timeSteps,
+ defaultTimeFormat,
+ timeFormat,
+ timezone,
+ timeBins,
+ animationWindow,
+ plotType
+ } = filter;
+
+ return {
+ value,
+ enableInteraction: true,
+ domain,
+ speed,
+ isAnimating,
+ step,
+ timeSteps,
+ defaultTimeFormat,
+ timeFormat,
+ timezone,
+ timeBins,
+ animationWindow,
+ marks: getBinThresholds(plotType?.interval, domain)
+ };
+};
diff --git a/src/utils/src/types.ts b/src/utils/src/types.ts
index b9a18cee2..17857ca5d 100644
--- a/src/utils/src/types.ts
+++ b/src/utils/src/types.ts
@@ -13,6 +13,7 @@ export interface KeplerTableModel<K, L, F extends Field = any> {
getColumnFilterProps(columnName: string): Field['filterProps'] | null | undefined;
dataContainer: DataContainerInterface;
filterTableCPU(filters: Filter[], layers: L[]): K;
+ getColumnField(fieldName: string): Field | undefined;
gpuFilter: {
filterRange: number[][];
filterValueUpdateTriggers: any;
diff --git a/test/browser/layer-tests/base-layer-sepcs.js b/test/browser/layer-tests/base-layer-specs.js
similarity index 100%
rename from test/browser/layer-tests/base-layer-sepcs.js
rename to test/browser/layer-tests/base-layer-specs.js
diff --git a/test/browser/layer-tests/index.js b/test/browser/layer-tests/index.js
index c1dc511b3..f53493f1c 100644
--- a/test/browser/layer-tests/index.js
+++ b/test/browser/layer-tests/index.js
@@ -1,7 +1,7 @@
// SPDX-License-Identifier: MIT
// Copyright contributors to the kepler.gl project
-import './base-layer-sepcs';
+import './base-layer-specs';
import './arc-layer-specs';
import './cluster-layer-specs';
import './geojson-layer-specs';
diff --git a/test/node/utils/timeline-test.js b/test/node/utils/timeline-test.js
index 5709437ab..5417c8687 100644
--- a/test/node/utils/timeline-test.js
+++ b/test/node/utils/timeline-test.js
@@ -44,6 +44,7 @@ test('#timeline -> getTimelineFromFilter', t => {
],
defaultTimeFormat: 'L LTS',
fieldType: 'timestamp',
+ timeBins: {},
gpuChannel: [0]
};
@@ -60,6 +61,7 @@ test('#timeline -> getTimelineFromFilter', t => {
'defaultTimeFormat',
'timeFormat',
'timezone',
+ 'timeBins',
'animationWindow',
'marks'
],
@@ -93,6 +95,7 @@ test('#timeline -> getTimelineFromAnimationConfig', t => {
'defaultTimeFormat',
'timeFormat',
'timezone',
+ 'timeBins',
'marks'
],
'Should generate the correct keys for animationConfig timeline'