Skip to content

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

[Enhancement] Create factory for histogram and line chart, add brush handle to range brush (#1274)

Shan Head65170040ed
diff --git a/src/components/common/animation-control/playback-controls.js b/src/components/common/animation-control/playback-controls.js
index 6f4c08b39..2a01764e4 100644
--- a/src/components/common/animation-control/playback-controls.js
+++ b/src/components/common/animation-control/playback-controls.js
@@ -44,16 +44,23 @@ const IconButton = styled(Button)`
function nop() {}
const DEFAULT_BUTTON_HEIGHT = '18px';
+const DEFAULT_ICONS = {
+ reset: Reset,
+ play: Play,
+ pause: Pause
+};
function AnimationPlaybacksFactory() {
const AnimationPlaybacks = ({
isAnimatable,
isAnimating,
buttonStyle,
+ width,
pauseAnimation = nop,
updateAnimationTime = nop,
startAnimation = nop,
- buttonHeight = DEFAULT_BUTTON_HEIGHT
+ buttonHeight = DEFAULT_BUTTON_HEIGHT,
+ playbackIcons = DEFAULT_ICONS
}) => {
const btnStyle = buttonStyle ? {[buttonStyle]: true} : {};
return (
@@ -61,6 +68,7 @@ function AnimationPlaybacksFactory() {
className={classnames('time-range-slider__control', {
disabled: !isAnimatable
})}
+ style={{width: `${width}px`}}
>
<ButtonGroup>
<IconButton
@@ -68,14 +76,18 @@ function AnimationPlaybacksFactory() {
{...btnStyle}
onClick={updateAnimationTime}
>
- <Reset height={buttonHeight} />
+ <playbackIcons.reset height={buttonHeight} />
</IconButton>
<IconButton
{...btnStyle}
className={classnames('playback-control-button', {active: isAnimating})}
onClick={isAnimating ? pauseAnimation : startAnimation}
>
- {isAnimating ? <Pause height={buttonHeight} /> : <Play height={buttonHeight} />}
+ {isAnimating ? (
+ <playbackIcons.pause height={buttonHeight} />
+ ) : (
+ <playbackIcons.play height={buttonHeight} />
+ )}
</IconButton>
</ButtonGroup>
</StyledAnimationControls>
diff --git a/src/components/common/field-selector.js b/src/components/common/field-selector.js
index a3009e5f3..057b46c37 100644
--- a/src/components/common/field-selector.js
+++ b/src/components/common/field-selector.js
@@ -159,6 +159,7 @@ const FieldSelectorFactory = FieldListItemFactory => {
filterOption="name"
fixedOptions={this.props.suggested}
inputTheme={this.props.inputTheme}
+ size={this.props.size}
isError={this.props.error}
selectedItems={this.selectedItemsSelector(this.props)}
erasable={this.props.erasable}
diff --git a/src/components/common/histogram-plot.js b/src/components/common/histogram-plot.js
new file mode 100644
index 000000000..2221ea814
--- /dev/null
+++ b/src/components/common/histogram-plot.js
@@ -0,0 +1,82 @@
+import React, {useMemo} from 'react';
+import {scaleLinear} from 'd3-scale';
+import {max} from 'd3-array';
+import styled from 'styled-components';
+import classnames from 'classnames';
+
+const histogramStyle = {
+ highlightW: 0.7,
+ unHighlightedW: 0.4
+};
+
+const HistogramWrapper = styled.svg`
+ overflow: visible;
+ .histogram-bars {
+ rect {
+ fill: ${props => props.theme.histogramFillOutRange};
+ }
+ rect.in-range {
+ fill: ${props => props.theme.histogramFillInRange};
+ }
+ }
+`;
+
+function HistogramPlotFactory() {
+ const HistogramPlot = ({width, height, margin, isRanged, histogram, value, brushComponent}) => {
+ const domain = useMemo(() => [histogram[0].x0, histogram[histogram.length - 1].x1], [
+ histogram
+ ]);
+ const dataId = Object.keys(histogram[0]).filter(k => k !== 'x0' && k !== 'x1')[0];
+
+ // use 1st for now
+ const getValue = useMemo(() => d => d[dataId], [dataId]);
+
+ const x = useMemo(
+ () =>
+ scaleLinear()
+ .domain(domain)
+ .range([0, width]),
+ [domain, width]
+ );
+
+ const y = useMemo(
+ () =>
+ scaleLinear()
+ .domain([0, max(histogram, getValue)])
+ .range([0, height]),
+ [histogram, height, getValue]
+ );
+
+ const barWidth = width / histogram.length;
+
+ return (
+ <HistogramWrapper width={width} height={height} style={{marginTop: `${margin.top}px`}}>
+ <g className="histogram-bars">
+ {histogram.map(bar => {
+ const inRange = bar.x1 <= value[1] + 1 && bar.x0 >= value[0];
+ const wRatio = inRange ? histogramStyle.highlightW : histogramStyle.unHighlightedW;
+ return (
+ <rect
+ className={classnames({'in-range': inRange})}
+ key={bar.x0}
+ height={y(getValue(bar))}
+ width={barWidth * wRatio}
+ x={x(bar.x0) + (barWidth * (1 - wRatio)) / 2}
+ rx={1}
+ ry={1}
+ y={height - y(getValue(bar))}
+ />
+ );
+ })}
+ </g>
+ <g transform={`translate(${isRanged ? 0 : barWidth / 2}, 0)`}>{brushComponent}</g>
+ </HistogramWrapper>
+ );
+ };
+
+ const EmpptyOrPlot = props =>
+ !props.histogram || !props.histogram.length ? null : <HistogramPlot {...props} />;
+
+ return EmpptyOrPlot;
+}
+export default HistogramPlotFactory;
diff --git a/src/components/common/item-selector/chickleted-input.js b/src/components/common/item-selector/chickleted-input.js
index 2bb659a72..e598cf779 100644
--- a/src/components/common/item-selector/chickleted-input.js
+++ b/src/components/common/item-selector/chickleted-input.js
@@ -83,8 +83,8 @@ const ChickletedInputContainer = styled.div`
props.inputTheme === 'secondary'
? props.theme.secondaryChickletedInput
: props.inputTheme === 'light'
- ? props.theme.chickletedInputLT
- : props.theme.chickletedInput}
+ ? props.theme.chickletedInputLT
+ : props.theme.chickletedInput}
color: ${props =>
props.hasPlaceholder ? props.theme.selectColorPlaceHolder : props.theme.selectColor};
diff --git a/src/components/common/item-selector/item-selector.js b/src/components/common/item-selector/item-selector.js
index f28bcec5e..535636459 100644
--- a/src/components/common/item-selector/item-selector.js
+++ b/src/components/common/item-selector/item-selector.js
@@ -45,7 +45,8 @@ const StyledDropdownSelect = styled.div.attrs({
? props.theme.inputLT
: props.theme.input};
- height: ${props => props.theme.dropdownSelectHeight}px;
+ height: ${props =>
+ props.size === 'small' ? props.theme.inputBoxHeightSmall : props.theme.inputBoxHeight};
.list__item__anchor {
${props => props.theme.dropdownListAnchor};
@@ -269,7 +270,8 @@ class ItemSelector extends Component {
onClick: this._showTypeahead,
onFocus: this._showPopover,
error: this.props.isError,
- inputTheme: this.props.inputTheme
+ inputTheme: this.props.inputTheme,
+ size: this.props.size
};
const intl = this.props.intl;
diff --git a/src/components/common/line-chart.js b/src/components/common/line-chart.js
new file mode 100644
index 000000000..26cb17491
--- /dev/null
+++ b/src/components/common/line-chart.js
@@ -0,0 +1,132 @@
+// Copyright (c) 2020 Uber Technologies, Inc.
+//
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to deal
+// in the Software without restriction, including without limitation the rights
+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
+//
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+// THE SOFTWARE.
+
+import React, {useMemo} from 'react';
+import moment from 'moment';
+import {
+ HorizontalGridLines,
+ LineSeries,
+ XYPlot,
+ CustomSVGSeries,
+ Hint,
+ YAxis,
+ MarkSeries
+} from 'react-vis';
+import styled from 'styled-components';
+import {getTimeWidgetHintFormatter} from 'utils/filter-utils';
+
+const LineChartWrapper = styled.div`
+ .rv-xy-plot {
+ /* important for rendering hint */
+ position: relative;
+ }
+ .rv-xy-plot__inner {
+ /* important to show axis */
+ overflow: visible;
+ }
+
+ .rv-xy-plot__grid-lines__line {
+ stroke: ${props => props.theme.histogramFillOutRange};
+ stroke-dasharray: 1px 4px;
+ }
+
+ .rv-xy-plot__axis__tick__text {
+ font-size: 9px;
+ fill: ${props => props.theme.textColor};
+ }
+`;
+
+const StyledHint = styled.div`
+ background-color: #d3d8e0;
+ border-radius: 2px;
+ color: ${props => props.theme.textColorLT};
+ font-size: 9px;
+ margin: 4px;
+ padding: 3px 6px;
+ pointer-events: none;
+ user-select: none;
+`;
+
+const HintContent = ({x, y, format}) => (
+ <StyledHint>
+ <div className="hint--x">{format(x)}</div>
+ <div className="row">{y}</div>
+ </StyledHint>
+);
+
+const MARGIN = {top: 0, bottom: 0, left: 0, right: 0};
+function LineChartFactory() {
+ const LineChart = ({
+ brushComponent,
+ brushing,
+ color,
+ enableChartHover,
+ height,
+ hoveredDP,
+ isEnlarged,
+ lineChart,
+ margin,
+ onMouseMove,
+ width
+ }) => {
+ const {xDomain, series, yDomain} = lineChart;
+ const hintFormatter = useMemo(() => {
+ return getTimeWidgetHintFormatter(xDomain);
+ }, [xDomain]);
+
+ const brushData = useMemo(() => {
+ return [{x: series[0].x, y: yDomain[1], customComponent: () => brushComponent}];
+ }, [series, yDomain, brushComponent]);
+
+ return (
+ <LineChartWrapper style={{marginTop: `${margin.top}px`}}>
+ <XYPlot
+ xType="time"
+ width={width}
+ height={height}
+ margin={MARGIN}
+ onMouseLeave={() => {
+ onMouseMove(null);
+ }}
+ >
+ <HorizontalGridLines tickTotal={3} />
+ <LineSeries
+ style={{fill: 'none'}}
+ strokeWidth={2}
+ color={color}
+ data={series}
+ onNearestX={enableChartHover ? onMouseMove : null}
+ />
+ <MarkSeries data={hoveredDP ? [hoveredDP] : []} color={color} size={3} />
+ <CustomSVGSeries data={brushData} />
+ {isEnlarged && <YAxis tickTotal={3} />}
+ {hoveredDP && enableChartHover && !brushing ? (
+ <Hint value={hoveredDP}>
+ <HintContent {...hoveredDP} format={val => moment.utc(val).format(hintFormatter)} />
+ </Hint>
+ ) : null}
+ </XYPlot>
+ </LineChartWrapper>
+ );
+ };
+ return LineChart;
+}
+
+export default LineChartFactory;
diff --git a/src/components/common/range-brush.js b/src/components/common/range-brush.js
index 9265cbc9c..b74ee81c7 100644
--- a/src/components/common/range-brush.js
+++ b/src/components/common/range-brush.js
@@ -20,7 +20,7 @@
import React, {Component, createRef} from 'react';
import PropTypes from 'prop-types';
-import styled from 'styled-components';
+import styled, {withTheme} from 'styled-components';
import {event, select} from 'd3-selection';
import {brushX} from 'd3-brush';
import {normalizeSliderValue} from 'utils/data-utils';
@@ -43,6 +43,19 @@ function moveRight(startSel, selection) {
return Boolean(startSel0 === sel0);
}
+// style brush resize handle
+// https://github.com/crossfilter/crossfilter/blob/gh-pages/index.html#L466
+const getHandlePath = props => {
+ return function brushResizePath(d) {
+ const e = Number(d.type === 'e');
+ const x = e ? 1 : -1;
+ const h = 39;
+ const w = 4.5;
+ const y = (props.height - h) / 2;
+ return `M${0.5 * x},${y}c${2.5 * x},0,${w * x},2,${w * x},${w}v${h - w * 2}c0,2.5,${-2 *
+ x},${w},${-w * x},${w}V${y}z`;
+ };
+};
function RangeBrushFactory() {
class RangeBrush extends Component {
@@ -65,6 +78,7 @@ function RangeBrushFactory() {
// distinguish between the uses.
//
// We don't use state because that would trigger another `componentDidUpdate`
+ const {theme, isRanged, onMouseoverHandle, onMouseoutHandle} = this.props;
this.brushing = false;
this.moving = false;
@@ -98,6 +112,23 @@ function RangeBrushFactory() {
});
this.root.call(this.brush);
+ const brushResizePath = getHandlePath(this.props);
+ this.handle = this.root
+ .selectAll('.handle--custom')
+ .data([{type: 'w'}, {type: 'e'}])
+ .enter()
+ .append('path')
+ .attr('class', 'handle--custom')
+ .attr('display', isRanged ? null : 'none')
+ .attr('fill', theme ? theme.sliderHandleColor : '#D3D8E0')
+ .attr('cursor', 'ew-resize')
+ .attr('d', brushResizePath)
+ .on('mouseover', () => {
+ if (onMouseoverHandle) onMouseoverHandle();
+ })
+ .on('mouseout', () => {
+ if (onMouseoutHandle) onMouseoutHandle();
+ });
const {
value: [val0, val1]
@@ -126,6 +157,10 @@ function RangeBrushFactory() {
this._move(val0, val1);
}
}
+
+ if (!this.props.isRanged) {
+ this.handle.attr('display', 'none');
+ }
}
rootContainer = createRef();
@@ -150,6 +185,10 @@ function RangeBrushFactory() {
this.brush.move(this.root, [scale(val0), scale(val0) + 1]);
} else {
this.brush.move(this.root, [scale(val0), scale(val1)]);
+
+ this.handle
+ .attr('display', null)
+ .attr('transform', (d, i) => `translate(${[i === 0 ? scale(val0) : scale(val1), 0]})`);
}
}
}
@@ -174,7 +213,7 @@ function RangeBrushFactory() {
d1 = normalizeSliderValue(d1, min, step, marks);
if (isRanged) this._move(d0, d1);
- else this._move(right ? d1 : d0);
+ else this._move(...(right ? [d1, d1] : [d0, d0]));
if (isRanged) this._onBrush(d0, d1);
else this._onBrush(right ? d1 : d0);
@@ -199,13 +238,12 @@ function RangeBrushFactory() {
render() {
const {isRanged} = this.props;
-
return (
<StyledG className="kg-range-slider__brush" isRanged={isRanged} ref={this.rootContainer} />
);
}
}
- return RangeBrush;
+ return withTheme(RangeBrush);
}
export default RangeBrushFactory;
diff --git a/src/components/common/range-plot.js b/src/components/common/range-plot.js
index c6e7ef458..a1a8eb27b 100644
--- a/src/components/common/range-plot.js
+++ b/src/components/common/range-plot.js
@@ -18,201 +18,120 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
-import React, {Component} from 'react';
+import React, {useState, useCallback} from 'react';
import PropTypes from 'prop-types';
-import {scaleLinear} from 'd3-scale';
-import moment from 'moment';
-import {max} from 'd3-array';
-import {createSelector} from 'reselect';
-import {LineSeries, XYPlot, CustomSVGSeries, Hint, MarkSeries} from 'react-vis';
-import styled from 'styled-components';
-import classnames from 'classnames';
-
+import styled, {withTheme} from 'styled-components';
import RangeBrushFactory from './range-brush';
-import {getTimeWidgetHintFormatter} from 'utils/filter-utils';
-
-const chartMargin = {top: 8, bottom: 0, left: 0, right: 0};
-const chartH = 52;
-const containerH = 68;
-const histogramStyle = {
- highlightW: 0.7,
- unHighlightedW: 0.4
-};
-
-RangePlotFactory.deps = [RangeBrushFactory];
+import HistogramPlotFactory from './histogram-plot';
+import LineChartFactory from './line-chart';
-export default function RangePlotFactory(RangeBrush) {
- class RangePlot extends Component {
- static propTypes = {
- value: PropTypes.arrayOf(PropTypes.number).isRequired,
- histogram: PropTypes.arrayOf(
- PropTypes.shape({
- x0: PropTypes.number,
- x1: PropTypes.number
- })
- ),
- lineChart: PropTypes.object,
- plotType: PropTypes.string,
- isEnlarged: PropTypes.bool,
- onBlur: PropTypes.func,
- width: PropTypes.number.isRequired
- };
-
- state = {
- hoveredDP: null
- };
+const StyledRangePlot = styled.div`
+ margin-bottom: ${props => props.theme.sliderBarHeight}px;
+ display: flex;
+ position: 'relative';
+`;
- domainSelector = props => props.lineChart && props.lineChart.xDomain;
- hintFormatter = createSelector(this.domainSelector, domain =>
- getTimeWidgetHintFormatter(domain)
+RangePlotFactory.deps = [RangeBrushFactory, HistogramPlotFactory, LineChartFactory];
+
+export default function RangePlotFactory(RangeBrush, HistogramPlot, LineChart) {
+ const RangePlot = ({
+ onBrush,
+ range,
+ value,
+ width,
+ plotType,
+ lineChart,
+ histogram,
+ isEnlarged,
+ isRanged,
+ theme,
+ ...chartProps
+ }) => {
+ const [brushing, setBrushing] = useState(false);
+ const [hoveredDP, onMouseMove] = useState(null);
+ const [enableChartHover, setEnableChartHover] = useState(false);
+ const height = isEnlarged ? theme.rangePlotHLarge : theme.rangePlotH;
+
+ const onBrushStart = useCallback(() => {
+ setBrushing(true);
+ onMouseMove(null);
+ setEnableChartHover(false);
+ }, [setBrushing, onMouseMove, setEnableChartHover]);
+
+ const onBrushEnd = useCallback(() => {
+ setBrushing(false);
+ setEnableChartHover(true);
+ }, [setBrushing, setEnableChartHover]);
+
+ const onMouseoverHandle = useCallback(() => {
+ onMouseMove(null);
+ setEnableChartHover(false);
+ }, [onMouseMove, setEnableChartHover]);
+
+ const onMouseoutHandle = useCallback(() => {
+ setEnableChartHover(true);
+ }, [setEnableChartHover]);
+
+ const brushComponent = (
+ <RangeBrush
+ onBrush={onBrush}
+ onBrushStart={onBrushStart}
+ onBrushEnd={onBrushEnd}
+ range={range}
+ value={value}
+ width={width}
+ height={height}
+ isRanged={isRanged}
+ onMouseoverHandle={onMouseoverHandle}
+ onMouseoutHandle={onMouseoutHandle}
+ {...chartProps}
+ />
);
- onMouseMove = hoveredDP => {
- this.setState({hoveredDP});
+ const commonProps = {
+ width,
+ value,
+ height,
+ margin: isEnlarged ? theme.rangePlotMarginLarge : theme.rangePlotMargin,
+ brushComponent,
+ brushing,
+ isEnlarged,
+ enableChartHover,
+ onMouseMove,
+ hoveredDP,
+ isRanged,
+ ...chartProps
};
- render() {
- const {onBrush, range, value, width, plotType, lineChart, histogram} = this.props;
- const domain = [histogram[0].x0, histogram[histogram.length - 1].x1];
-
- const brushComponent = (
- <RangeBrush domain={domain} onBrush={onBrush} range={range} value={value} width={width} />
- );
-
- return (
- <div
- style={{
- height: `${containerH}px`,
- position: 'relative'
- }}
- >
- {plotType === 'lineChart' ? (
- <LineChart
- hoveredDP={this.state.hoveredDP}
- width={width}
- height={containerH}
- margin={chartMargin}
- children={brushComponent}
- onMouseMove={this.onMouseMove}
- yDomain={lineChart.yDomain}
- hintFormat={this.hintFormatter(this.props)}
- data={lineChart.series}
- />
- ) : (
- <Histogram
- width={width}
- height={chartH}
- value={value}
- margin={chartMargin}
- histogram={histogram}
- brushComponent={brushComponent}
- />
- )}
- </div>
- );
- }
- }
- return RangePlot;
+ return (
+ <StyledRangePlot
+ style={{
+ height: `${isEnlarged ? theme.rangePlotContainerHLarge : theme.rangePlotContainerH}px`
+ }}
+ className="kg-range-slider__plot"
+ >
+ {plotType === 'lineChart' && lineChart ? (
+ <LineChart lineChart={lineChart} {...commonProps} />
+ ) : (
+ <HistogramPlot histogram={histogram} {...commonProps} />
+ )}
+ </StyledRangePlot>
+ );
+ };
+
+ RangePlot.propTypes = {
+ value: PropTypes.arrayOf(PropTypes.number).isRequired,
+ histogram: PropTypes.arrayOf(
+ PropTypes.shape({
+ x0: PropTypes.number,
+ x1: PropTypes.number
+ })
+ ),
+ lineChart: PropTypes.object,
+ plotType: PropTypes.string,
+ isEnlarged: PropTypes.bool,
+ onBlur: PropTypes.func,
+ width: PropTypes.number.isRequired
+ };
+ return withTheme(RangePlot);
}
-
-const Histogram = ({width, height, margin, histogram, value, brushComponent}) => {
- const domain = [histogram[0].x0, histogram[histogram.length - 1].x1];
- const barWidth = width / histogram.length;
-
- const x = scaleLinear()
- .domain(domain)
- .range([0, width]);
-
- const y = scaleLinear()
- .domain([0, max(histogram, d => d.count)])
- .range([0, height]);
-
- return (
- <HistogramWrapper width={width} height={height} style={{marginTop: `${margin.top}px`}}>
- <g className="histogram-bars">
- {histogram.map(bar => {
- const inRange = bar.x0 >= value[0] && bar.x1 <= value[1];
- const wRatio = inRange ? histogramStyle.highlightW : histogramStyle.unHighlightedW;
-
- return (
- <rect
- className={classnames({'in-range': inRange})}
- key={bar.x0}
- height={y(bar.count)}
- width={barWidth * wRatio}
- x={x(bar.x0) + (barWidth * (1 - wRatio)) / 2}
- rx={1}
- ry={1}
- y={height - y(bar.count)}
- />
- );
- })}
- </g>
- {brushComponent}
- </HistogramWrapper>
- );
-};
-
-const LineChartWrapper = styled.div`
- .rv-xy-plot__inner path {
- fill: none;
- stroke-width: 1.5;
- }
-`;
-
-const HistogramWrapper = styled.svg`
- .histogram-bars {
- rect {
- fill: ${props => props.theme.histogramFillOutRange};
- }
- rect.in-range {
- fill: ${props => props.theme.histogramFillInRange};
- }
- }
-`;
-const LineChart = ({
- width,
- height,
- yDomain,
- hintFormat,
- hoveredDP,
- margin,
- color,
- data,
- onMouseMove,
- children
-}) => {
- const brushData = [{x: data[0].x, y: yDomain[1], customComponent: () => children}];
-
- return (
- <LineChartWrapper>
- <XYPlot width={width} height={height} margin={{...margin, bottom: 12}}>
- <LineSeries strokeWidth={2} color={color} data={data} onNearestX={onMouseMove} />
- <MarkSeries data={hoveredDP ? [hoveredDP] : []} color={color} size={3} />
- <CustomSVGSeries data={brushData} />
- {hoveredDP ? (
- <Hint value={hoveredDP}>
- <HintContent {...hoveredDP} format={val => moment.utc(val).format(hintFormat)} />
- </Hint>
- ) : null}
- </XYPlot>
- </LineChartWrapper>
- );
-};
-
-const StyledHint = styled.div`
- background-color: #d3d8e0;
- border-radius: 2px;
- color: ${props => props.theme.textColorLT};
- font-size: 9px;
- margin: 4px;
- padding: 3px 6px;
- pointer-events: none;
- user-select: none;
-`;
-const HintContent = ({x, y, format}) => (
- <StyledHint>
- <div className="hint--x">{format(x)}</div>
- <div className="row">{y}</div>
- </StyledHint>
-);
diff --git a/src/components/common/range-slider.js b/src/components/common/range-slider.js
index edddfecb9..53344e66f 100644
--- a/src/components/common/range-slider.js
+++ b/src/components/common/range-slider.js
@@ -39,11 +39,11 @@ const SliderInput = styled(Input)`
const SliderWrapper = styled.div`
display: flex;
position: relative;
- align-items: center;
+ align-items: ${props => (!props.isRanged && props.showInput ? 'center' : 'flex-start')};
`;
const RangeInputWrapper = styled.div`
- margin-top: 6px;
+ margin-top: 12px;
display: flex;
justify-content: space-between;
`;
@@ -200,12 +200,22 @@ export default function RangeSliderFactory(RangePlot) {
);
}
+ // eslint-disable-next-line complexity
render() {
- const {isRanged, showInput, histogram, range, onChange, sliderHandleWidth, step} = this.props;
+ const {
+ isRanged,
+ showInput,
+ histogram,
+ lineChart,
+ range,
+ onChange,
+ sliderHandleWidth,
+ step
+ } = this.props;
- const height = isRanged && showInput ? '16px' : '24px';
const {width} = this.state;
const plotWidth = Math.max(width - sliderHandleWidth, 0);
+ const renderPlot = (histogram && histogram.length) || lineChart;
return (
<div
@@ -213,7 +223,7 @@ export default function RangeSliderFactory(RangePlot) {
style={{width: '100%', padding: `0 ${sliderHandleWidth / 2}px`}}
ref={this.sliderContainer}
>
- {histogram && histogram.length ? (
+ {renderPlot ? (
<RangePlot
histogram={histogram}
lineChart={this.props.lineChart}
@@ -222,14 +232,26 @@ export default function RangeSliderFactory(RangePlot) {
onBrush={(val0, val1) => onChange([val0, val1])}
marks={this.props.marks}
range={range}
- value={this.filterValueSelector(this.props)}
+ value={this.props.plotValue || this.filterValueSelector(this.props)}
width={plotWidth}
isRanged={isRanged}
step={step}
/>
) : null}
- <SliderWrapper style={{height}} className="kg-range-slider__slider">
- {this.props.xAxis ? <this.props.xAxis width={plotWidth} domain={range} /> : null}
+ <SliderWrapper
+ className="kg-range-slider__slider"
+ isRanged={isRanged}
+ showInput={showInput}
+ >
+ {this.props.xAxis ? (
+ <div style={{height: '30px'}}>
+ <this.props.xAxis
+ width={plotWidth}
+ domain={range}
+ isEnlarged={this.props.isEnlarged}
+ />
+ </div>
+ ) : null}
<Slider
marks={this.props.marks}
showValues={false}
@@ -247,7 +269,6 @@ export default function RangeSliderFactory(RangePlot) {
}}
enableBarDrag
/>
-
{!isRanged && showInput ? this._renderInput('value1') : null}
</SliderWrapper>
{isRanged && showInput ? (
diff --git a/src/components/common/slider/slider.js b/src/components/common/slider/slider.js
index 4eda98b6d..14cd41377 100644
--- a/src/components/common/slider/slider.js
+++ b/src/components/common/slider/slider.js
@@ -31,7 +31,6 @@ function noop() {}
const StyledRangeSlider = styled.div`
position: relative;
- margin-bottom: 12px;
background-color: ${props => props.theme.sliderBarBgd};
${props => `${props.vertical ? 'width' : 'height'}: ${props.theme.sliderBarHeight}px`};
${props => `${props.vertical ? 'height' : 'width'}: 100%`};
@@ -39,7 +38,6 @@ const StyledRangeSlider = styled.div`
const SliderWrapper = styled.div`
flex-grow: 1;
- margin-top: ${props => props.theme.sliderMarginTop}px;
`;
export default class Slider extends Component {
diff --git a/src/components/common/styled-components.js b/src/components/common/styled-components.js
index 436457ca4..0fc2d5953 100644
--- a/src/components/common/styled-components.js
+++ b/src/components/common/styled-components.js
@@ -155,7 +155,7 @@ export const SidePanelDivider = styled.div.attrs({
export const Tooltip = styled(ReactTooltip)`
&.__react_component_tooltip {
font-size: ${props => props.theme.tooltipFontSize};
- font-weight: 500;
+ font-weight: 400;
padding: 7px 18px;
box-shadow: ${props => props.theme.tooltipBoxShadow};
diff --git a/src/components/common/time-range-slider.js b/src/components/common/time-range-slider.js
index 6918c59ec..5f168067a 100644
--- a/src/components/common/time-range-slider.js
+++ b/src/components/common/time-range-slider.js
@@ -26,7 +26,6 @@ import styled from 'styled-components';
import {createSelector} from 'reselect';
import {Minus} from 'components/common/icons';
-import {SelectTextBold, SelectText} from 'components/common/styled-components';
import RangeSliderFactory from 'components/common/range-slider';
import TimeSliderMarkerFactory from 'components/common/time-slider-marker';
import PlaybackControlsFactory from 'components/common/animation-control/playback-controls';
@@ -50,10 +49,6 @@ const StyledSliderContainer = styled.div`
.playback-control-button {
padding: 9px 12px;
}
-
- .kg-range-slider__slider .kg-slider {
- margin-top: ${props => props.theme.sliderMarginTopIsTime}px;
- }
`;
TimeRangeSliderFactory.deps = [
@@ -187,12 +182,12 @@ export default function TimeRangeSliderFactory(
const TimeValueWrapper = styled.div`
display: flex;
align-items: center;
- font-size: 11px;
+ font-size: ${props => props.theme.timeTitleFontSize};
justify-content: ${props => (props.isEnlarged ? 'center' : 'space-between')};
.horizontal-bar {
padding: 0 12px;
- color: ${props => props.theme.titleTextColor};
+ color: ${props => props.theme.textColor};
}
.time-value {
@@ -201,7 +196,7 @@ const TimeValueWrapper = styled.div`
align-items: flex-start;
span {
- color: ${props => props.theme.titleTextColor};
+ color: ${props => props.theme.textColor};
}
}
@@ -228,13 +223,9 @@ const TimeValue = ({value, split}) => (
{split ? (
value
.split(' ')
- .map((v, i) => (
- <div key={i}>
- {i === 0 ? <SelectText>{v}</SelectText> : <SelectTextBold>{v}</SelectTextBold>}
- </div>
- ))
+ .map((v, i) => <div key={i}>{i === 0 ? <span>{v}</span> : <span>{v}</span>}</div>)
) : (
- <SelectTextBold>{value}</SelectTextBold>
+ <span>{value}</span>
)}
</div>
);
diff --git a/src/components/common/time-slider-marker.js b/src/components/common/time-slider-marker.js
index 290593c4e..069c53049 100644
--- a/src/components/common/time-slider-marker.js
+++ b/src/components/common/time-slider-marker.js
@@ -18,22 +18,25 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
-import React, {Component, createRef} from 'react';
+import React, {useRef, useEffect, useMemo} from 'react';
import PropTypes from 'prop-types';
import {scaleUtc} from 'd3-scale';
import {select} from 'd3-selection';
import {axisBottom} from 'd3-axis';
-import {createSelector} from 'reselect';
import styled from 'styled-components';
-const MIN_TICK_WIDTH = 50;
+const MIN_TICK_WIDTH_LARGE = 80;
+const MIN_TICK_WIDTH_SMALL = 50;
+const HEIGHT = 30;
const TimeSliderContainer = styled.svg`
pointer-events: none;
position: absolute;
+ top: 0;
+ overflow: visible;
.axis text {
- font-size: 9px;
+ font-size: ${props => props.theme.axisFontSize};
fill: ${props => props.theme.textColor};
}
@@ -51,7 +54,7 @@ const TimeSliderContainer = styled.svg`
.value {
fill: ${props => props.theme.textColor};
- font-size: 10px;
+ font-size: ${props => props.theme.axisFontSize};
&.start {
text-anchor: start;
@@ -63,65 +66,49 @@ const TimeSliderContainer = styled.svg`
}
`;
-const height = 30;
-
function TimeSliderMarkerFactory() {
- class TimeSliderMarker extends Component {
- static propTypes = {
- domain: PropTypes.arrayOf(PropTypes.any).isRequired,
- width: PropTypes.number.isRequired
- };
-
- componentDidMount() {
- this._updateAxis(this.scaleSelector(this.props));
- }
-
- componentDidUpdate(prevProps) {
- if (this.scaleSelector(this.props) !== this.scaleSelector(prevProps)) {
- this._updateAxis(this.scaleSelector(this.props));
- }
+ function updateAxis(scale, width, xAxisRef, isEnlarged) {
+ if (!scale) {
+ return;
}
- xAxis = createRef();
+ // TODO: pass in ticks if interval is defined
+ const ticks = Math.floor(width / (isEnlarged ? MIN_TICK_WIDTH_LARGE : MIN_TICK_WIDTH_SMALL));
- domainSelector = props => props.domain;
- widthSelector = props => props.width;
- scaleSelector = createSelector(this.domainSelector, this.widthSelector, (domain, width) =>
- Array.isArray(domain)
- ? scaleUtc()
- .domain(domain)
- .range([0, width])
- : null
- );
-
- _updateAxis(scale) {
- if (!scale) {
- return;
- }
+ const xAxis = axisBottom(scale)
+ .ticks(ticks)
+ .tickSize(0)
+ .tickPadding(12);
- // TODO: pass in ticks if interval is defined
- const ticks = Math.floor(this.props.width / MIN_TICK_WIDTH);
+ select(xAxisRef.current).call(xAxis);
+ }
- const xAxis = axisBottom(scale)
- .ticks(ticks)
- .tickSize(8)
- .tickPadding(6);
+ const TimeSliderMarker = ({width, domain, isEnlarged = true, height = HEIGHT}) => {
+ const xAxisRef = useRef();
+ const scale = useMemo(
+ () =>
+ Array.isArray(domain)
+ ? scaleUtc()
+ .domain(domain)
+ .range([0, width])
+ : null,
+ [domain, width]
+ );
- select(this.xAxis.current).call(xAxis);
- }
+ useEffect(() => {
+ updateAxis(scale, width, xAxisRef, isEnlarged);
+ }, [scale, width, xAxisRef, isEnlarged]);
+ return (
+ <TimeSliderContainer className="time-slider-marker" width={width} height={height}>
+ <g className="x axis" ref={xAxisRef} transform="translate(0, 0)" />
+ </TimeSliderContainer>
+ );
+ };
- render() {
- return (
- <TimeSliderContainer
- className="time-slider-marker"
- width={this.props.width}
- height={height}
- >
- <g className="x axis" ref={this.xAxis} transform="translate(0, 0)" />
- </TimeSliderContainer>
- );
- }
- }
+ TimeSliderMarker.propTypes = {
+ domain: PropTypes.arrayOf(PropTypes.any).isRequired,
+ width: PropTypes.number.isRequired
+ };
return TimeSliderMarker;
}
diff --git a/src/components/filters/time-widget.js b/src/components/filters/time-widget.js
index bc1889a48..6377871ea 100644
--- a/src/components/filters/time-widget.js
+++ b/src/components/filters/time-widget.js
@@ -37,7 +37,7 @@ import FieldSelectorFactory from '../common/field-selector';
const TOP_SECTION_HEIGHT = '36px';
const TimeBottomWidgetInner = styled(BottomWidgetInner)`
- padding: 6px 32px 16px 32px;
+ padding: 6px 32px 24px 32px;
`;
const TopSectionWrapper = styled.div`
display: flex;
diff --git a/src/components/index.js b/src/components/index.js
index 301da1cca..ffb04191a 100644
--- a/src/components/index.js
+++ b/src/components/index.js
@@ -109,6 +109,8 @@ export {default as AnimationPlaybacksFactory} from './common/animation-control/p
export {default as FloatingTimeDisplayFactory} from './common/animation-control/floating-time-display';
export {default as AnimationSpeedSliderFactory} from './common/animation-control/animation-speed-slider';
export {default as RangePlotFactory} from './common/range-plot';
+export {default as HistogramPlotFactory} from './common/histogram-plot';
+export {default as LineChartFactory} from './common/line-chart';
export {default as RangeBrushFactory} from './common/range-brush';
export {FieldListItemFactoryFactory} from './common/field-selector';
export {default as TimeSliderMarkerFactory} from './common/time-slider-marker';
diff --git a/src/components/side-panel/filter-panel/filter-panel.js b/src/components/side-panel/filter-panel/filter-panel.js
index 97bfb659f..9dc35db12 100644
--- a/src/components/side-panel/filter-panel/filter-panel.js
+++ b/src/components/side-panel/filter-panel/filter-panel.js
@@ -36,10 +36,6 @@ import PolygonFilterPanelFactory from 'components/filters/filter-panels/polygon-
const StyledFilterPanel = styled.div`
margin-bottom: 12px;
border-radius: 1px;
-
- .filter-panel__filter {
- margin-top: 24px;
- }
`;
FilterPanelFactory.deps = [
diff --git a/src/reducers/vis-state.js b/src/reducers/vis-state.js
index a8ea57fa8..967b36c7c 100644
--- a/src/reducers/vis-state.js
+++ b/src/reducers/vis-state.js
@@ -76,7 +76,7 @@ const actionHandler = {
[ActionTypes.RESET_MAP_CONFIG]: visStateUpdaters.resetMapConfigUpdater,
[ActionTypes.SET_FILTER]: visStateUpdaters.setFilterUpdater,
-
+
[ActionTypes.SET_TIME_ANIMATION]: visStateUpdaters.setTimeAnmationUpdater,
[ActionTypes.SET_FILTER_PLOT]: visStateUpdaters.setFilterPlotUpdater,
diff --git a/src/styles/base.js b/src/styles/base.js
index 3c435dd82..bfe870d21 100644
--- a/src/styles/base.js
+++ b/src/styles/base.js
@@ -307,6 +307,7 @@ export const sliderInputFontSize = '10px';
export const sliderInputPadding = '4px 6px';
export const sliderMarginTopIsTime = -12;
export const sliderMarginTop = 12;
+export const sliderMarginBottom = 12;
// Geocoder
export const geocoderWidth = 360;
@@ -318,6 +319,14 @@ export const geocoderInputHeight = 36;
export const rangeBrushBgd = '#3A414C';
export const histogramFillInRange = activeColor;
export const histogramFillOutRange = sliderBarColor;
+export const axisFontSize = '10px';
+export const timeTitleFontSize = '10px';
+export const rangePlotMargin = {top: 12, bottom: 0, left: 0, right: 0};
+export const rangePlotMarginLarge = {top: 18, bottom: 0, left: 0, right: 0};
+export const rangePlotH = 62;
+export const rangePlotContainerH = 78;
+export const rangePlotHLarge = 102;
+export const rangePlotContainerHLarge = 120;
// Notification
export const notificationColors = {
@@ -739,9 +748,7 @@ const dropdownScrollBar = css`
`;
const dropdownScrollBarLT = css`
- ${dropdownScrollBar}
-
- ::-webkit-scrollbar-corner {
+ ${dropdownScrollBar} ::-webkit-scrollbar-corner {
background: ${props => props.theme.dropdownListBgdLT};
}
@@ -761,7 +768,6 @@ const dropdownScrollBarLT = css`
}
`;
-
const dropdownListAnchor = css`
color: ${props => props.theme.selectColor};
padding-left: 3px;
@@ -1289,6 +1295,7 @@ export const theme = {
sliderInputWidth,
sliderMarginTopIsTime,
sliderMarginTop,
+ sliderMarginBottom,
// Geocoder
geocoderWidth,
@@ -1300,6 +1307,14 @@ export const theme = {
rangeBrushBgd,
histogramFillInRange,
histogramFillOutRange,
+ axisFontSize,
+ timeTitleFontSize,
+ rangePlotMargin,
+ rangePlotMarginLarge,
+ rangePlotH,
+ rangePlotHLarge,
+ rangePlotContainerH,
+ rangePlotContainerHLarge,
// Notifications
notificationColors,
diff --git a/test/browser/components/common/index.js b/test/browser/components/common/index.js
index e9ce90d44..9ebd17d76 100644
--- a/test/browser/components/common/index.js
+++ b/test/browser/components/common/index.js
@@ -22,3 +22,4 @@ import './file-uploader-test';
import './color-legend-test';
import './range-slider-test';
import './item-selector-test';
+import './range-plot-test';
diff --git a/test/browser/components/common/range-plot-test.js b/test/browser/components/common/range-plot-test.js
new file mode 100644
index 000000000..78546cc21
--- /dev/null
+++ b/test/browser/components/common/range-plot-test.js
@@ -0,0 +1,60 @@
+// Copyright (c) 2020 Uber Technologies, Inc.
+//
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to deal
+// in the Software without restriction, including without limitation the rights
+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
+//
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+// THE SOFTWARE.
+
+/* eslint-disable no-unused-vars */
+import React from 'react';
+import test from 'tape';
+import {mountWithTheme} from 'test/helpers/component-utils';
+import {appInjector} from 'components/container';
+import {RangePlotFactory} from 'components';
+
+const RangePlot = appInjector.get(RangePlotFactory);
+
+test('Components -> RangePlot.render', t => {
+ const props = {
+ histogram: [],
+ isEnlarged: true,
+ isRanged: true,
+ onBrush: () => {},
+ plotType: 'histogram',
+ range: [1421315219000, 1421348744000],
+ value: [1421315219000, 1421348744000],
+ width: 137
+ };
+ t.doesNotThrow(() => {
+ const wrapper = mountWithTheme(<RangePlot {...props} />);
+ }, 'Show not fail without histogram');
+
+ // cant test D3 in jsDom for now
+ // props.histogram = [
+ // {count: 20, x0: 1421315219000, x1: 1421315500000},
+ // {count: 0, x0: 1421315500000, x1: 1421316000000},
+ // {count: 0, x0: 1421316000000, x1: 1421316500000},
+ // {count: 0, x0: 1421316500000, x1: 1421317000000},
+ // {count: 0, x0: 1421317000000, x1: 1421317500000},
+ // {count: 21, x0: 1421317500000, x1: 1421318000000}
+ // ];
+
+ t.doesNotThrow(() => {
+ const wrapper = mountWithTheme(<RangePlot {...props} />);
+ }, 'Show not fail render histogram');
+
+ t.end();
+});