Kepler.gl is a powerful open source geospatial analysis tool for large-scale data sets.
fix: time range filter histogram bar alignment and animation window padding (#3385)
Xun Lie50397e2b10e
diff --git a/src/components/src/common/animation-control/animation-controller.ts b/src/components/src/common/animation-control/animation-controller.tsindex ae7bc184a..5e17d0bd4 100644--- a/src/components/src/common/animation-control/animation-controller.ts+++ b/src/components/src/common/animation-control/animation-controller.ts@@ -119,14 +119,15 @@ function AnimationControllerFactory(): typeof AnimationControllerType {if (!domain) {return;}- // interim solution while we fully migrate filter and layer controllersconst setTimelineValue = updateAnimation || this.props.setTimelineValue;if (Array.isArray(value)) {+ const windowSize = value[1] - value[0];if (animationWindow === ANIMATION_WINDOW.incremental) {setTimelineValue([value[0], value[0] + 1] as T);} else {- setTimelineValue([domain[0], domain[0] + value[1] - value[0]] as T);+ const startValue = domain[0] - windowSize;+ setTimelineValue([startValue, startValue + windowSize] as T);}} else {setTimelineValue(domain[0] as T);@@ -210,15 +211,16 @@ function AnimationControllerFactory(): typeof AnimationControllerType {if (Array.isArray(value)) {let value0: number;let value1: number;+ const windowSize = value[1] - value[0];if (animationWindow === ANIMATION_WINDOW.incremental) {const lastFrame = value[1] + delta > domain[1];value0 = value[0];value1 = lastFrame ? value[0] + 1 : value[1] + delta;} else {- // use value[0] to display the last item duration as the first itemconst lastFrame = value[0] + delta > domain[1];- value0 = lastFrame ? domain[0] : value[0] + delta;- value1 = value0 + value[1] - value[0];+ const startValue = domain[0] - windowSize;+ value0 = lastFrame ? startValue : value[0] + delta;+ value1 = value0 + windowSize;}return [value0, value1];}diff --git a/src/components/src/common/histogram-plot.tsx b/src/components/src/common/histogram-plot.tsxindex 5e8f3fd33..c7ca39873 100644--- a/src/components/src/common/histogram-plot.tsx+++ b/src/components/src/common/histogram-plot.tsx@@ -20,7 +20,7 @@ const HISTOGRAM_MASK_FGCOLOR = '#000000';const histogramStyle = {highlightW: 0.7,- unHighlightedW: 0.4+ unHighlightedW: 0.7};const HistogramWrapper = styled.svg`@@ -73,7 +73,8 @@ const isBarInRange = (index === list.length - 1? bar.x1 >= filterDomain[1] && filterDomain[1] === filterValue[1]: false;- return (x0Condition || bar.x0 >= filterValue[0]) && (x1Condition || bar.x1 <= filterValue[1]);+ // Check if the bar overlaps with the filter value range (not fully contained)+ return (x0Condition || bar.x1 > filterValue[0]) && (x1Condition || bar.x0 < filterValue[1]);};export type HistogramMaskModeType = {@@ -82,6 +83,8 @@ export type HistogramMaskModeType = {MaskWithOverlay: number;};+const undefinedToZero = (v: number | undefined) => (v ? v : 0);+interface HistogramPlotProps {width: number;height: number;@@ -112,7 +115,6 @@ function HistogramPlotFactory() {brushComponent,breakLines}: HistogramPlotProps) => {- const undefinedToZero = (x: number | undefined) => (x ? x : 0);const groupKeys = useMemo(() =>Object.keys(histogramsByGroup)@@ -130,28 +132,23 @@ function HistogramPlotFactory() {[range, histogramsByGroup, groupKeys]);- const barWidth = useMemo(() => {+ const numBins = useMemo(() => {if (groupKeys.length === 0) return 0;- // find histogramsByGroup with max number of binsconst maxGroup = groupKeys.reduce((accu, key, _idx) => {if (histogramsByGroup[key].length > accu.length) {return histogramsByGroup[key];}return accu;}, histogramsByGroup[groupKeys[0]]);+ return maxGroup.length;+ }, [histogramsByGroup, groupKeys]);- // find the bin for measuring step- const stdBinIdx = maxGroup.length > 1 ? 1 : 0;- const xStep = maxGroup[stdBinIdx].x1 - maxGroup[stdBinIdx].x0;- const maxBins = (domain[1] - domain[0]) / xStep;- if (!maxBins) return 0;- return width / maxBins / (isMasked ? 1 : groupKeys.length);- }, [histogramsByGroup, domain, groupKeys, width, isMasked]);+ const x = useMemo(() => scaleLinear().domain(domain).range([0, width]), [domain, width]);- const x = useMemo(- () => scaleLinear().domain(domain).range([barWidth, width]),- [domain, width, barWidth]- );+ const fallbackBarWidth = useMemo(() => {+ if (numBins === 0) return 0;+ return width / numBins;+ }, [numBins, width]);const y = useMemo(() =>@@ -193,6 +190,10 @@ function HistogramPlotFactory() {const wRatio = inRange? histogramStyle.highlightW: histogramStyle.unHighlightedW;+ const bx0 = x(undefinedToZero(bar.x0));+ const bx1 = x(undefinedToZero(bar.x1));+ const binW = Math.max(bx1 - bx0, 1);+ const drawnW = binW * wRatio;return (<Bar$isOverlay={false}@@ -200,8 +201,8 @@ function HistogramPlotFactory() {$color={HISTOGRAM_MASK_FGCOLOR}key={`mask-${idx}`}height={y(bar[countProp])}- width={barWidth * wRatio}- x={x(bar.x0) + (barWidth * (1 - wRatio)) / 2}+ width={drawnW}+ x={bx0 + (binW - drawnW) / 2}y={height - y(bar[countProp])}/>);@@ -227,14 +228,18 @@ function HistogramPlotFactory() {: y(bar[countProp]);const inRange = isBarInRange(bar, idx, list, domain, value);const wRatio = inRange ? histogramStyle.highlightW : histogramStyle.unHighlightedW;+ const bx0 = x(undefinedToZero(bar.x0));+ const bx1 = x(undefinedToZero(bar.x1));+ const binW = Math.max(bx1 - bx0, 1);+ const drawnW = binW * wRatio;return (<Bar$inRange={inRange}$isOverlay={true}key={`bar-${idx}`}height={maskHeight}- width={barWidth * wRatio}- x={x(bar.x0) + (barWidth * (1 - wRatio)) / 2}+ width={drawnW}+ x={bx0 + (binW - drawnW) / 2}y={height - y(bar[countProp])}/>);@@ -248,7 +253,7 @@ function HistogramPlotFactory() {);})}</HistogramBreakLine>- <g transform={`translate(${isRanged ? 0 : barWidth / 2}, 0)`}>{brushComponent}</g>+ <g transform={`translate(${isRanged ? 0 : fallbackBarWidth / 2}, 0)`}>{brushComponent}</g></HistogramWrapper>);};@@ -268,30 +273,35 @@ function HistogramPlotFactory() {const inRange = isBarInRange(bar, idx, list, domain, value);const wRatio = inRange ? histogramStyle.highlightW : histogramStyle.unHighlightedW;- const startX =- x(undefinedToZero(bar.x0)) + barWidth * i + (barWidth * (1 - wRatio)) / 2;- if (startX > 0 && startX + barWidth * histogramStyle.unHighlightedW <= width) {- return (- <Bar- $isOverlay={false}- $inRange={inRange}- $color={colorsByGroup?.[key]}- key={`bar-${idx}`}- height={y(bar[countProp])}- width={barWidth * wRatio}- x={startX}- rx={1}- ry={1}- y={height - y(bar[countProp])}- />- );- }- return null;+ const bx0 = x(undefinedToZero(bar.x0));+ const bx1 = x(undefinedToZero(bar.x1));+ const binW = Math.max(bx1 - bx0, 1);+ const perGroupW = isMasked ? binW : binW / groupKeys.length;+ const drawnWidth = perGroupW * wRatio;+ const groupOffset =+ groupKeys.length > 1+ ? perGroupW * i + (perGroupW - drawnWidth) / 2+ : (binW - drawnWidth) / 2;+ const startX = bx0 + groupOffset;+ return (+ <Bar+ $isOverlay={false}+ $inRange={inRange}+ $color={colorsByGroup?.[key]}+ key={`bar-${idx}`}+ height={y(bar[countProp])}+ width={drawnWidth}+ x={startX}+ rx={1}+ ry={1}+ y={height - y(bar[countProp])}+ />+ );})}</g>))}</g>- <g transform={`translate(${isRanged ? 0 : barWidth / 2}, 0)`}>{brushComponent}</g>+ <g transform={`translate(${isRanged ? 0 : fallbackBarWidth / 2}, 0)`}>{brushComponent}</g></HistogramWrapper>);};diff --git a/src/components/src/common/line-chart.tsx b/src/components/src/common/line-chart.tsxindex 77d4220c6..9afac6007 100644--- a/src/components/src/common/line-chart.tsx+++ b/src/components/src/common/line-chart.tsx@@ -86,6 +86,7 @@ interface LineChartProps {width: number;timezone?: string | null;timeFormat?: string;+ range?: number[];}const MARGIN = {top: 0, bottom: 0, left: 0, right: 0};@@ -103,12 +104,18 @@ function LineChartFactory() {onMouseMove,width,timezone,- timeFormat+ timeFormat,+ range}: LineChartProps) => {const {yDomain, xDomain} = lineChart || {};// @ts-expect-error seems lineChart.series has ambiguous types. Requires refactoring.const series: {lines: any[]; markers: any[]} = lineChart?.series;+ const effectiveXDomain = useMemo(+ () => (range && range.length === 2 ? range : xDomain),+ [range, xDomain]+ );+const paddedYDomain = useMemo(() =>yDomain && yDomain[0] && yDomain[1]@@ -117,16 +124,16 @@ function LineChartFactory() {[yDomain]);const brushData = useMemo(() => {- return xDomain && paddedYDomain+ return effectiveXDomain && paddedYDomain? [{- x: xDomain[0],+ x: effectiveXDomain[0],y: paddedYDomain[1],customComponent: () => brushComponent}]: [];- }, [xDomain, paddedYDomain, brushComponent]);+ }, [effectiveXDomain, paddedYDomain, brushComponent]);const hintFormatter = useMemo(() => datetimeFormatter(timezone)(timeFormat),@@ -144,7 +151,7 @@ function LineChartFactory() {onMouseMove(null);}}yDomain={paddedYDomain}- xDomain={xDomain}+ xDomain={effectiveXDomain}><HorizontalGridLines tickTotal={3} />{series.lines.map((d, i) => (diff --git a/src/components/src/common/range-plot.tsx b/src/components/src/common/range-plot.tsxindex 536b00004..b1d655aef 100644--- a/src/components/src/common/range-plot.tsx+++ b/src/components/src/common/range-plot.tsx@@ -156,7 +156,7 @@ export default function RangePlotFactory(};return isLineChart(plotType) && lineChart ? (- <LineChartPlot lineChart={lineChart} {...commonProps} />+ <LineChartPlot lineChart={lineChart} range={range} {...commonProps} />) : (<HistogramPlothistogramsByGroup={bins}diff --git a/src/components/src/common/range-slider.tsx b/src/components/src/common/range-slider.tsxindex 0841f8b7b..5a0b934d7 100644--- a/src/components/src/common/range-slider.tsx+++ b/src/components/src/common/range-slider.tsx@@ -153,6 +153,48 @@ export default function RangeSliderFactory((value0, value1) => [value0, value1]);+ _isTimeFilter = () => {+ return typeof this.props.timeFormat === 'string';+ };++ _getDisplayRangeInfo = () => {+ const {range, bins, isRanged} = this.props;+ if (!range) {+ return {displayRange: range, binStep: 0};+ }++ if (!bins || !isRanged) {+ return {displayRange: range, binStep: 0};+ }++ const groupKeys = Object.keys(bins).filter(k => bins[k]?.length > 0);+ if (groupKeys.length === 0) {+ return {displayRange: range, binStep: 0};+ }++ const group = bins[groupKeys[0]];+ const idx = group.length > 1 ? 1 : 0;+ const binStep = group[idx].x1 - group[idx].x0;++ if (binStep <= 0) {+ return {displayRange: range, binStep: 0};+ }++ if (this._isTimeFilter()) {+ const displayRange = [range[0] - binStep, range[1] + binStep];+ return {displayRange, binStep};+ }++ // Non-time range filters: pad by half a bin so edge bars aren't clipped+ const pad = binStep / 2;+ const displayRange = [range[0] - pad, range[1] + pad];+ return {displayRange, binStep: pad};+ };++ _getDisplayRange = () => {+ return this._getDisplayRangeInfo().displayRange;+ };+_roundValToStep = val => {const {range, step} = this.props;if (!range || !step) return;@@ -160,18 +202,20 @@ export default function RangeSliderFactory(};_setRangeVal1 = val => {- const {value0, range, onChange = noop} = this.props;- if (!range) return;+ const {value0, onChange = noop} = this.props;+ const dr = this._getDisplayRange();+ if (!dr) return;const val1 = Number(val);- onChange([value0, clamp([value0, range[1]], this._roundValToStep(val1))]);+ onChange([value0, clamp([value0, dr[1]], this._roundValToStep(val1))]);return true;};_setRangeVal0 = val => {- const {value1, range, onChange = noop} = this.props;- if (!range) return;+ const {value1, onChange = noop} = this.props;+ const dr = this._getDisplayRange();+ if (!dr) return;const val0 = Number(val);- onChange([clamp([range[0], value1], this._roundValToStep(val0)), value1]);+ onChange([clamp([dr[0], value1], this._roundValToStep(val0)), value1]);return true;};@@ -249,7 +293,13 @@ export default function RangeSliderFactory(const plotWidth = Math.max(width - Number(sliderHandleWidth), 0);const hasPlot = plotType?.type;- const value = this.props.plotValue || this.filterValueSelector(this.props);+ const {displayRange} = this._getDisplayRangeInfo();++ const rawValue = this.props.plotValue || this.filterValueSelector(this.props);+ const value =+ displayRange && Array.isArray(rawValue)+ ? (rawValue.map(v => clamp([displayRange[0], displayRange[1]], v)) as typeof rawValue)+ : rawValue;const scaledValue =subAnimations?.length && range? scaleSourceDomainToDestination(value as [number, number], range as [number, number])@@ -275,7 +325,7 @@ export default function RangeSliderFactory(animationWindow={animationWindow}filter={filter}datasets={datasets}- range={range}+ range={displayRange!}value={value}width={plotWidth}isRanged={isRanged}@@ -304,7 +354,7 @@ export default function RangeSliderFactory(<this.props.xAxiswidth={plotWidth}timezone={timezone}- domain={range}+ domain={displayRange}isEnlarged={this.props.isEnlarged}/></div>@@ -312,8 +362,8 @@ export default function RangeSliderFactory(<Slidermarks={this.props.marks}isRanged={isRanged}- minValue={range[0]}- maxValue={range[1]}+ minValue={displayRange![0]}+ maxValue={displayRange![1]}value0={this.props.value0}value1={this.props.value1}step={step}diff --git a/test/browser/components/filters/time-widget-test.js b/test/browser/components/filters/time-widget-test.jsindex 6b2e57b5e..ce773160f 100644--- a/test/browser/components/filters/time-widget-test.js+++ b/test/browser/components/filters/time-widget-test.js@@ -255,7 +255,7 @@ test('Components -> TimeWidget.mount -> test setFilterAnimationTime', t => {// test updateAnimationSpeedt.deepEqual(setFilterAnimationTime.args[0],- [0, 'value', [1474594560000, 1474617600000]],+ [0, 'value', [1474592400000, 1474617600000]],'should call setFilterAnimationTime with new value');