Skip to content

Commit

Permalink
Added filterable prop to TimeSeriesWidget (#808)
Browse files Browse the repository at this point in the history
  • Loading branch information
padawannn authored Nov 30, 2023
1 parent 345892e commit 8818dd2
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Not released

- Added filterable prop to TimeSeriesWidget [#808](https://github.com/CartoDB/carto-react/pull/808)
- Fix dataSources store type [#807](https://github.com/CartoDB/carto-react/pull/807)

## 2.3
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ function TimeSeriesWidgetUI({
fitHeight,
showControls,
animation,
filterable,
onTimelineUpdate,
timeWindow,
timelinePosition,
Expand Down Expand Up @@ -98,6 +99,7 @@ function TimeSeriesWidgetUI({
fitHeight={fitHeight}
showControls={showControls}
animation={animation}
filterable={filterable}
palette={palette}
showLegend={showLegend}
selectedCategories={selectedCategories}
Expand Down Expand Up @@ -141,6 +143,7 @@ TimeSeriesWidgetUI.propTypes = {
height: PropTypes.string,
fitHeight: PropTypes.bool,
animation: PropTypes.bool,
filterable: PropTypes.bool,
isPlaying: PropTypes.bool,
onPlay: PropTypes.func,
isPaused: PropTypes.bool,
Expand All @@ -164,6 +167,7 @@ TimeSeriesWidgetUI.defaultProps = {
tooltipFormatter: defaultTooltipFormatter,
formatter: (value) => value,
animation: true,
filterable: true,
isPlaying: false,
isPaused: false,
showControls: true,
Expand All @@ -189,6 +193,7 @@ function TimeSeriesWidgetUIContent({
fitHeight,
showControls,
animation,
filterable,
palette,
selectedCategories,
onSelectedCategoriesChange,
Expand Down Expand Up @@ -325,7 +330,7 @@ function TimeSeriesWidgetUIContent({
</Typography>
</Box>

{showClearButton && (
{filterable && showClearButton && (
<Link
variant='caption'
style={{ cursor: 'pointer' }}
Expand All @@ -338,7 +343,9 @@ function TimeSeriesWidgetUIContent({
</>
);

const controls = showControls && <TimeSeriesControls data={data} stepSize={stepSize} />;
const controls = filterable && showControls && (
<TimeSeriesControls data={data} stepSize={stepSize} />
);

const chart = (
<TimeSeriesChart
Expand All @@ -355,6 +362,7 @@ function TimeSeriesWidgetUIContent({
height={height}
fitHeight={fitHeight}
animation={animation}
filterable={filterable}
selectedCategories={selectedCategories}
onCategoryClick={onSelectedCategoriesChange && handleCategoryClick}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export default function TimeSeriesChart({
height: heightProp,
fitHeight,
animation,
filterable,
selectedCategories,
onCategoryClick
}) {
Expand Down Expand Up @@ -165,7 +166,8 @@ export default function TimeSeriesChart({
useTimeSeriesInteractivity({
echartsInstance,
data,
canSelectLines: Boolean(onCategoryClick)
canSelectLines: Boolean(onCategoryClick),
filterable
});

const seriesOptions = useMemo(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ let initialTimeWindow = null;
export default function useTimeSeriesInteractivity({
echartsInstance,
data,
canSelectLines
canSelectLines,
filterable
}) {
const theme = useTheme();
const { isPlaying, isPaused, setIsPaused, timeWindow, setTimeWindow, stop } =
Expand Down Expand Up @@ -41,6 +42,7 @@ export default function useTimeSeriesInteractivity({
// Echarts events
useEffect(() => {
function clickEvent(params) {
if (!filterable) return;
// params target is specified if we hit data-line or point, not time selection is only for background hits
if (canSelectLines && params.target) return;

Expand Down Expand Up @@ -69,11 +71,14 @@ export default function useTimeSeriesInteractivity({
stop,
timeWindow.length,
updateTimelineByCoordinate,
canSelectLines
canSelectLines,
filterable
]);

useEffect(() => {
function mouseDownEvent(params) {
if (!filterable) return;

if (params.target?.type === 'ec-line') {
setIsMarkLineSelected(true);
updateCursor('grabbing');
Expand Down Expand Up @@ -106,7 +111,7 @@ export default function useTimeSeriesInteractivity({
}

return addEventWithCleanUp(zr, 'mousedown', mouseDownEvent);
}, [zr, echartsInstance, timeWindow, updateCursor]);
}, [zr, echartsInstance, timeWindow, updateCursor, filterable]);

useEffect(() => {
function mouseMoveEvent(params) {
Expand Down Expand Up @@ -185,6 +190,12 @@ export default function useTimeSeriesInteractivity({
updateCursor
]);

useEffect(() => {
if (!filterable && timeWindow.length) {
setTimeWindow([]);
}
}, [filterable, setTimeWindow, timeWindow.length]);

// markLine in echarts
const timelineOptions = useMemo(() => {
if (timeWindow.length !== 1) return undefined;
Expand Down
2 changes: 2 additions & 0 deletions packages/react-widgets/src/widgets/TimeSeriesWidget.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ export type TimeSeriesWidgetProps = {
tooltipFormatter?: Function;
formatter?: Function;

filterable?: boolean;

height?: string;
fitHeihgt?: boolean;
stableHeight?: boolean;
Expand Down
5 changes: 5 additions & 0 deletions packages/react-widgets/src/widgets/TimeSeriesWidget.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ const EMPTY_ARRAY = [];
* @param {boolean=} [props.stableHeight] - If specified, error and no-data state will maintain same height as normal widget in loading/loaded state.
* @param {boolean} [props.showControls] - Enable/disable animation controls (play, pause, stop, speed). True by default.
* @param {boolean} [props.animation] - Enable/disable widget animations on data updates. Enabled by default.
* * @param {boolean} [props.filterable] - Enable/disable widget filtering capabilities. Enabled by default.
* @param {boolean} [props.global] - Enable/disable the viewport filtering in the data fetching.
* @param {function=} [props.onError] - Function to handle error messages from the widget.
* @param {Function=} [props.onStateChange] - Callback to handle state updates of widgets
Expand Down Expand Up @@ -122,6 +123,7 @@ function TimeSeriesWidget({
stableHeight,
showControls,
animation,
filterable,
isPlaying,
onPlay,
isPaused,
Expand Down Expand Up @@ -411,6 +413,7 @@ function TimeSeriesWidget({
fitHeight={fitHeight}
showControls={showControls}
animation={animation}
filterable={filterable}
isPlaying={isPlaying}
onPlay={onPlay}
isPaused={isPaused}
Expand Down Expand Up @@ -484,6 +487,7 @@ TimeSeriesWidget.propTypes = {
fitHeight: PropTypes.bool,
stableHeight: PropTypes.bool,
animation: PropTypes.bool,
filterable: PropTypes.bool,
isPlaying: PropTypes.bool,
onPlay: PropTypes.func,
isPaused: PropTypes.bool,
Expand All @@ -508,6 +512,7 @@ TimeSeriesWidget.defaultProps = {
tooltip: true,
formatter: (value) => value,
animation: true,
filterable: true,
isPlaying: false,
isPaused: false,
timeWindow: [],
Expand Down

0 comments on commit 8818dd2

Please sign in to comment.