From 6c8e67806c0c76401fc437c9a9e7a6fcfc65f6fb Mon Sep 17 00:00:00 2001 From: karolina-siemieniuk-morawska Date: Tue, 12 Dec 2023 09:58:50 +0100 Subject: [PATCH] ui: fix alignment of bar chart and slider * ref: cern-sis/issues-scoap3#252 --- ui/src/components/search/YearFacet.tsx | 64 +++++++++++++++++++------- ui/src/styles/globals.css | 5 ++ 2 files changed, 53 insertions(+), 16 deletions(-) diff --git a/ui/src/components/search/YearFacet.tsx b/ui/src/components/search/YearFacet.tsx index eb063f2dd..60b780c92 100644 --- a/ui/src/components/search/YearFacet.tsx +++ b/ui/src/components/search/YearFacet.tsx @@ -1,10 +1,10 @@ import React, { useEffect, useState } from "react"; -import { FlexibleWidthXYPlot, VerticalBarSeries, Hint } from "react-vis"; -import 'react-vis/dist/style.css'; +import { XYPlot, VerticalBarSeries, Hint } from "react-vis"; import { Button, Card, Slider } from "antd"; import { SliderMarks } from "antd/es/slider"; import { useRouter } from "next/navigation"; import isEqual from "lodash.isequal"; +import "react-vis/dist/style.css"; import { getSearchUrl } from "@/utils/utils"; import { PublicationYear, YearFacetData, Params } from "@/types"; @@ -38,17 +38,25 @@ const YearFacet: React.FC = ({ data, params }) => { getSearchUrl({ ...params, page: 1, - publication_year__range: sliderEndpoints.join("__"), + publication_year__range: resolveYearQuery(), }) ); }, [filters]); + const resolveYearQuery = () => { + if (sliderEndpoints[0] === sliderEndpoints[1]) { + return sliderEndpoints[0]?.toString(); + } + return sliderEndpoints.join("__"); + }; + const mapInitialDataToYears = ( initial: PublicationYear[] ): YearFacetData[] => { return initial?.map((item) => ({ x: new Date(item?.key)?.getFullYear(), y: item?.doc_count, + y0: 0 })); }; @@ -70,16 +78,11 @@ const YearFacet: React.FC = ({ data, params }) => { }; const updateStateAndMarks = (newFilters: YearFacetData[]) => { - setFilters(newFilters); setSliderEndpoints(getSliderEndpoints(newFilters)); setMarks(getMarks(newFilters)); }; - const onBarClick = (value: YearFacetData) => { - updateStateAndMarks([value]); - }; - - const onSliderChange = (data: number[]) => { + const sliderDataToGraphData = (data: number[]) => { const firstIndex = initialData.findIndex( (item: YearFacetData) => item.x === data[0] ); @@ -88,7 +91,24 @@ const YearFacet: React.FC = ({ data, params }) => { ); const range = initialData.slice(firstIndex, lastIndex + 1); - updateStateAndMarks(range); + if (range.length === 1) { + return [range, range].flat(); + } + + return range; + }; + + const onSliderChange = (data: number[]) => { + updateStateAndMarks(sliderDataToGraphData(data)); + }; + + const onSliderAfterChange = (data: number[]) => { + setFilters(sliderDataToGraphData(data)); + }; + + const onBarClick = (value: YearFacetData) => { + updateStateAndMarks([value, value]); + setFilters([value, value]); }; const onBarMouseHover = (bar: YearFacetData) => { @@ -100,6 +120,7 @@ const YearFacet: React.FC = ({ data, params }) => { const resetFilters = () => { setReset(!reset); updateStateAndMarks(initialData); + setFilters(initialData); }; return ( @@ -107,33 +128,44 @@ const YearFacet: React.FC = ({ data, params }) => {
{!isEqual(initialData, filters) && (
-
)} - + {hoveredBar && } + - {hoveredBar && } - +