Skip to content

Commit

Permalink
fix: reset chart horizontalRangeSlider
Browse files Browse the repository at this point in the history
  • Loading branch information
MEsteves22 authored and HQFOX committed May 14, 2024
1 parent 51ed7fb commit 9c483f1
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 17 deletions.
1 change: 1 addition & 0 deletions .storybook/test-runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const excludeStories = [
"Visualizations/Treemap",
"Visualizations/Heatmap",
"Widgets/Code Editor",
"Tests/Visualizations",
];

function getNodesIds(nodes: NodeResult[]) {
Expand Down
1 change: 1 addition & 0 deletions docs/tests/Table.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ const data = [
{ name: "Sarah", email: "a@a.com", var1: "123", var2: "123", test: "123" },
];

/** This was created to test grouped headers with sticky columns */
export const Main: StoryObj = {
render: () => {
const columns = useMemo<HvTableColumnConfig<any, string>[]>(
Expand Down
83 changes: 83 additions & 0 deletions docs/tests/Visualizations.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import { useState } from "react";
import { css } from "@emotion/css";
import { Decorator, StoryObj } from "@storybook/react";
import { expect, userEvent, within } from "@storybook/test";
import { HvButton } from "@hitachivantara/uikit-react-core";
import { HvLineChart } from "@hitachivantara/uikit-react-viz";

const decorator: Decorator = (Story) => (
<div
className={css({
height: 500,
padding: 20,
})}
>
{Story()}
</div>
);

export default {
title: "Tests/Visualizations",
parameters: {
// Enables Chromatic snapshot
chromatic: { disableSnapshot: false },
eyes: { include: true },
},
decorators: [decorator],
};

/** This tests if the horizontal slider resets when removed */
export const SliderReset: StoryObj = {
play: async ({ canvasElement }) => {
const canvas = within(canvasElement);
const hideBtn = canvas.getByRole("button", { name: "Hide" });
await userEvent.click(hideBtn);
const showBtn = canvas.getByRole("button", { name: "Show" });
expect(showBtn).toBeInTheDocument();
},
render: () => {
const [show, setShow] = useState(true);

return (
<>
<HvButton onClick={() => setShow((prev) => !prev)}>
{show ? "Hide" : "Show"}
</HvButton>
<HvLineChart
data={{
Month: [
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December",
],
"Sales Target": [
5929, 2393, 1590, 7817, 4749, 1702, 2381, 2909, 6732, 3098, 2119,
2146,
],
}}
groupBy="Month"
measures="Sales Target"
onOptionChange={(option) => {
if (show) {
option.dataZoom = [
{ ...option.dataZoom[0], end: 80, start: 50 }, // Setting data zoom because it was not possible through "play"
option.dataZoom[1],
];
}
return option;
}}
horizontalRangeSlider={{ show }}
/>
</>
);
},
};
5 changes: 3 additions & 2 deletions packages/viz/src/BaseChart/BaseChart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,9 @@ export const HvBaseChart = forwardRef<ReactECharts, HvBaseChartProps>(

if (!instance) return;

// More info: https://echarts.apache.org/en/api.html#echartsInstance.setOption
instance.setOption(option, {
replaceMerge: ["xAxis", "yAxis", "series", "dataset"],
replaceMerge: ["xAxis", "yAxis", "series", "dataset", "dataZoom"],
});
}, [theme, option]);

Expand All @@ -72,7 +73,7 @@ export const HvBaseChart = forwardRef<ReactECharts, HvBaseChartProps>(
echarts={echarts}
option={initialOption}
theme={theme}
notMerge
notMerge // When true all the current components will be removed and new components will be created according to the new option
style={{
width: width ?? "100%",
height: height ?? "100%",
Expand Down
34 changes: 19 additions & 15 deletions packages/viz/src/hooks/useDataZoom.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,27 @@ interface HvDataZoomHookProps {
showHorizontal?: boolean;
}

export const useDataZoom = ({ showHorizontal }: HvDataZoomHookProps) => {
export const useDataZoom = ({
showHorizontal = false,
}: HvDataZoomHookProps) => {
const option = useMemo<Pick<HvEChartsOption, "dataZoom">>(() => {
return {
dataZoom: [
{
show: showHorizontal ?? false,
type: "slider",
orient: "horizontal",
},
{
show: showHorizontal ?? false,
type: "inside",
orient: "horizontal",
zoomOnMouseWheel: "shift",
moveOnMouseWheel: true,
},
],
dataZoom: showHorizontal
? [
{
show: true,
type: "slider",
orient: "horizontal",
},
{
show: true,
type: "inside",
orient: "horizontal",
zoomOnMouseWheel: "shift",
moveOnMouseWheel: true,
},
]
: [],
};
}, [showHorizontal]);

Expand Down

0 comments on commit 9c483f1

Please sign in to comment.