@gravity-ui/chartkit 3.0.0-beta.9 → 3.0.0-beta.zefirka.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -17,7 +17,7 @@ export declare const ChartKit: <T extends keyof import("../types").ChartKitWidge
17
17
  onError?: import("../types").ChartKitOnError | undefined;
18
18
  renderError?: import("../types").RenderError | undefined;
19
19
  renderPluginLoader?: (() => React.ReactNode) | undefined;
20
- } & (Omit<import("../types").ChartKitWidget[T], "widget" | "data" | "pluginRef"> extends infer T_1 ? { [key in keyof T_1]: import("../types").ChartKitWidget[T][key]; } : never) & {
20
+ } & (Omit<import("../types").ChartKitWidget[T], "data" | "widget" | "pluginRef"> extends infer T_1 ? { [key in keyof T_1]: import("../types").ChartKitWidget[T][key]; } : never) & {
21
21
  ref?: React.ForwardedRef<ChartKitRef | undefined> | undefined;
22
22
  }) => ReturnType<typeof ChartKitComponent>;
23
23
  export {};
@@ -3,3 +3,4 @@ declare const _default: import("@storybook/types").ComponentAnnotations<import("
3
3
  export default _default;
4
4
  export declare const Line: import("@storybook/types").AnnotatedStoryFn<import("@storybook/react/dist/types-0a347bb9").R, any>;
5
5
  export declare const Updates: import("@storybook/types").AnnotatedStoryFn<import("@storybook/react/dist/types-0a347bb9").R, any>;
6
+ export declare const CustomTooltip: import("@storybook/types").AnnotatedStoryFn<import("@storybook/react/dist/types-0a347bb9").R, any>;
@@ -1,10 +1,11 @@
1
- import React from 'react';
1
+ import React, { useEffect, useState } from 'react';
2
2
  import { Button } from '@gravity-ui/uikit';
3
3
  import { settings } from '../../../libs';
4
4
  import { YagrPlugin } from '../../../plugins';
5
5
  import { ChartKit } from '../../../components/ChartKit';
6
6
  import { getNewConfig, line10 } from './mocks/line10';
7
7
  import '@gravity-ui/yagr/dist/index.css';
8
+ import placement from '@gravity-ui/yagr/dist/YagrCore/plugins/tooltip/placement';
8
9
  export default {
9
10
  title: 'Plugins/Yagr',
10
11
  component: ChartKit,
@@ -39,5 +40,52 @@ const UpdatesTemplate = () => {
39
40
  React.createElement(Button, { onClick: onStartUpdates }, "Start Updates"),
40
41
  React.createElement(Button, { onClick: () => setState(getNewConfig()) }, "Once")));
41
42
  };
43
+ function Tooltip({ yagr }) {
44
+ const [x, setX] = useState(null);
45
+ const [visible, setVisible] = useState(false);
46
+ const tooltipRef = React.useRef(null);
47
+ useEffect(() => {
48
+ var _a, _b, _c;
49
+ if (!yagr) {
50
+ return;
51
+ }
52
+ const onChange = (_, data) => {
53
+ var _a, _b;
54
+ setX((_a = data.data) === null || _a === void 0 ? void 0 : _a.x);
55
+ setVisible(data.state.visible);
56
+ if (data.state.visible && tooltipRef.current && ((_b = data.data) === null || _b === void 0 ? void 0 : _b.anchor)) {
57
+ placement(tooltipRef.current, data.data.anchor, 'right', {
58
+ xOffset: 24,
59
+ yOffset: 24,
60
+ });
61
+ }
62
+ };
63
+ (_a = yagr.plugins.tooltip) === null || _a === void 0 ? void 0 : _a.on('render', onChange);
64
+ (_b = yagr.plugins.tooltip) === null || _b === void 0 ? void 0 : _b.on('show', onChange);
65
+ (_c = yagr.plugins.tooltip) === null || _c === void 0 ? void 0 : _c.on('hide', onChange);
66
+ }, [yagr]);
67
+ if (!visible) {
68
+ return null;
69
+ }
70
+ return (React.createElement("div", { style: {
71
+ zIndex: 1000,
72
+ backgroundColor: 'white',
73
+ padding: 8,
74
+ pointerEvents: 'none',
75
+ }, ref: tooltipRef }, x));
76
+ }
77
+ const CustomTooltipImpl = () => {
78
+ const [shown, setShown] = React.useState(false);
79
+ const chartkitRef = React.useRef();
80
+ const [state, setState] = React.useState(line10);
81
+ if (!shown) {
82
+ settings.set({ plugins: [YagrPlugin] });
83
+ return React.createElement(Button, { onClick: () => setShown(true) }, "Show chart");
84
+ }
85
+ return (React.createElement("div", { style: { height: 300, width: '100%' } },
86
+ React.createElement(ChartKit, { ref: chartkitRef, id: "1", type: "yagr", data: state, CustomTooltip: Tooltip }),
87
+ React.createElement(Button, { onClick: () => setState(getNewConfig()) }, "Change data")));
88
+ };
42
89
  export const Line = LineTemplate.bind({});
43
90
  export const Updates = UpdatesTemplate.bind({});
91
+ export const CustomTooltip = CustomTooltipImpl.bind({});
@@ -30,6 +30,7 @@ export declare const getNewConfig: () => {
30
30
  id?: string | undefined;
31
31
  scale?: string | undefined;
32
32
  focus?: boolean | undefined;
33
+ title?: string | ((sIdx: number) => string) | undefined;
33
34
  show?: boolean | undefined;
34
35
  color?: string | undefined;
35
36
  spanGaps?: boolean | undefined;
@@ -38,7 +39,6 @@ export declare const getNewConfig: () => {
38
39
  precision?: number | undefined;
39
40
  snapToValues?: false | import("@gravity-ui/yagr/dist/YagrCore/types").SnapToValue | undefined;
40
41
  stackGroup?: number | undefined;
41
- title?: string | ((sIdx: number) => string) | undefined;
42
42
  transform?: ((val: string | number | null, series: import("@gravity-ui/yagr/dist/YagrCore/types").DataSeries[], idx: number) => number | null) | undefined;
43
43
  showInTooltip?: boolean | undefined;
44
44
  postProcess?: ((data: (number | null)[], idx: number, y: import("@gravity-ui/yagr/dist/YagrCore").default<import("@gravity-ui/yagr/dist/YagrCore/types").MinimalValidConfig>) => (number | null)[]) | undefined;
@@ -1,6 +1,7 @@
1
1
  import React from 'react';
2
2
  import { YagrReactRef } from '@gravity-ui/yagr/dist/react';
3
3
  import type { ChartKitWidgetRef } from '../../../types';
4
+ import { Yagr } from '../types';
4
5
  import './polyfills';
5
6
  import '@gravity-ui/yagr/dist/index.css';
6
7
  import './YagrWidget.css';
@@ -16,7 +17,10 @@ declare const YagrWidget: React.ForwardRefExoticComponent<{
16
17
  onError?: import("../../../types").ChartKitOnError | undefined;
17
18
  renderError?: import("../../../types").RenderError | undefined;
18
19
  renderPluginLoader?: (() => React.ReactNode) | undefined;
19
- } & {} & {
20
+ } & {
21
+ CustomTooltip?: React.ComponentType<import("../types").CustomTooltipProps<Yagr<import("@gravity-ui/yagr/dist/YagrCore/types").MinimalValidConfig>>> | undefined;
22
+ } & {
20
23
  id: string;
24
+ CustomTooltip?: React.ComponentType<import("../types").CustomTooltipProps<Yagr<import("@gravity-ui/yagr/dist/YagrCore/types").MinimalValidConfig>>> | undefined;
21
25
  } & React.RefAttributes<ChartKitWidgetRef | undefined>>;
22
26
  export default YagrWidget;
@@ -1,6 +1,5 @@
1
1
  import React from 'react';
2
2
  import isEmpty from 'lodash/isEmpty';
3
- import { useForkRef } from '@gravity-ui/uikit';
4
3
  import YagrComponent from '@gravity-ui/yagr/dist/react';
5
4
  import { i18n } from '../../../i18n';
6
5
  import { CHARTKIT_ERROR_CODE, ChartKitError } from '../../../libs';
@@ -10,27 +9,24 @@ import './polyfills';
10
9
  import '@gravity-ui/yagr/dist/index.css';
11
10
  import './YagrWidget.css';
12
11
  const YagrWidget = React.forwardRef((props, forwardedRef) => {
13
- const { id, data: { data }, pluginRef, onLoad, onRender, onChartLoad, } = props;
12
+ const { id, data: { data }, onLoad, onRender, onChartLoad, CustomTooltip, } = props;
14
13
  const yagrRef = React.useRef(null);
15
- const handleRef = useForkRef(pluginRef, yagrRef);
14
+ const [yagr, setYagr] = React.useState();
16
15
  if (!data || isEmpty(data)) {
17
16
  throw new ChartKitError({
18
17
  code: CHARTKIT_ERROR_CODE.NO_DATA,
19
18
  message: i18n('error', 'label_no-data'),
20
19
  });
21
20
  }
22
- const { config, debug } = useWidgetData(Object.assign(Object.assign({}, props.data), { id }));
21
+ const { config, debug } = useWidgetData(props, id);
23
22
  const handleChartLoading = React.useCallback((chart, { renderTime }) => {
24
23
  onLoad === null || onLoad === void 0 ? void 0 : onLoad(Object.assign(Object.assign({}, data), { widget: chart, widgetRendering: renderTime }));
25
24
  onRender === null || onRender === void 0 ? void 0 : onRender({ renderTime });
26
- }, [onLoad, onRender, data]);
25
+ setYagr(chart);
26
+ }, [onLoad, onRender, data, setYagr]);
27
27
  const onWindowResize = React.useCallback(() => {
28
- if (yagrRef.current) {
29
- const chart = yagrRef.current.yagr();
30
- if (!chart) {
31
- return;
32
- }
33
- chart.reflow();
28
+ if (yagr) {
29
+ yagr.reflow();
34
30
  }
35
31
  }, []);
36
32
  React.useImperativeHandle(forwardedRef, () => ({
@@ -39,28 +35,24 @@ const YagrWidget = React.forwardRef((props, forwardedRef) => {
39
35
  },
40
36
  }), [onWindowResize]);
41
37
  React.useEffect(() => {
42
- var _a, _b, _c, _d, _e, _f;
43
- const yagr = (_a = yagrRef.current) === null || _a === void 0 ? void 0 : _a.yagr();
44
- if (!yagr) {
45
- return;
46
- }
47
- if ((_c = (_b = yagr.config) === null || _b === void 0 ? void 0 : _b.tooltip) === null || _c === void 0 ? void 0 : _c.virtual) {
38
+ var _a, _b, _c, _d, _e;
39
+ if (!yagr || ((_b = (_a = yagr.config) === null || _a === void 0 ? void 0 : _a.tooltip) === null || _b === void 0 ? void 0 : _b.virtual)) {
48
40
  return;
49
41
  }
50
42
  const handlers = {
51
43
  mouseMove: null,
52
44
  mouseDown: null,
53
45
  };
54
- (_d = yagr.plugins.tooltip) === null || _d === void 0 ? void 0 : _d.on('render', (tooltip) => {
46
+ (_c = yagr.plugins.tooltip) === null || _c === void 0 ? void 0 : _c.on('render', (tooltip) => {
55
47
  synchronizeTooltipTablesCellsWidth(tooltip);
56
48
  });
57
- (_e = yagr.plugins.tooltip) === null || _e === void 0 ? void 0 : _e.on('pin', (tooltip, { actions }) => {
49
+ (_d = yagr.plugins.tooltip) === null || _d === void 0 ? void 0 : _d.on('pin', (tooltip, { actions }) => {
58
50
  handlers.mouseMove = checkFocus({ tooltip, yagr });
59
51
  handlers.mouseDown = detectClickOutside({ tooltip, actions, yagr });
60
52
  document.addEventListener('mousemove', handlers.mouseMove);
61
53
  document.addEventListener('mousedown', handlers.mouseDown);
62
54
  });
63
- (_f = yagr.plugins.tooltip) === null || _f === void 0 ? void 0 : _f.on('unpin', () => {
55
+ (_e = yagr.plugins.tooltip) === null || _e === void 0 ? void 0 : _e.on('unpin', () => {
64
56
  if (handlers.mouseMove) {
65
57
  document.removeEventListener('mousemove', handlers.mouseMove);
66
58
  handlers.mouseMove = null;
@@ -70,11 +62,12 @@ const YagrWidget = React.forwardRef((props, forwardedRef) => {
70
62
  handlers.mouseDown = null;
71
63
  }
72
64
  });
73
- }, []);
65
+ }, [yagr]);
74
66
  React.useLayoutEffect(() => {
75
- var _a;
76
- onChartLoad === null || onChartLoad === void 0 ? void 0 : onChartLoad({ widget: (_a = yagrRef.current) === null || _a === void 0 ? void 0 : _a.yagr() });
77
- }, [yagrRef, onChartLoad]);
78
- return (React.createElement(YagrComponent, { ref: handleRef, id: id, config: config, debug: debug, onChartLoad: handleChartLoading }));
67
+ onChartLoad === null || onChartLoad === void 0 ? void 0 : onChartLoad({ widget: yagr });
68
+ }, [yagr, onChartLoad]);
69
+ return (React.createElement(React.Fragment, null,
70
+ CustomTooltip && yagr && React.createElement(CustomTooltip, { yagr: yagr }),
71
+ React.createElement(YagrComponent, { ref: yagrRef, id: id, config: config, debug: debug, onChartLoad: handleChartLoading })));
79
72
  });
80
73
  export default YagrWidget;
@@ -1,8 +1,6 @@
1
1
  import type { YagrChartProps } from '@gravity-ui/yagr/dist/react';
2
- import type { YagrWidgetData, MinimalValidConfig } from '../types';
3
- export declare const useWidgetData: (args: YagrWidgetData & {
4
- id: string;
5
- }) => {
2
+ import type { MinimalValidConfig, YagrWidgetProps } from '../types';
3
+ export declare const useWidgetData: (props: YagrWidgetProps, id: string) => {
6
4
  config: MinimalValidConfig;
7
5
  debug: YagrChartProps['debug'];
8
6
  };
@@ -1,10 +1,15 @@
1
1
  import React from 'react';
2
2
  import { useThemeValue } from '@gravity-ui/uikit';
3
3
  import { shapeYagrConfig } from './utils';
4
- export const useWidgetData = (args) => {
5
- const { id, data, sources, libraryConfig } = args;
4
+ export const useWidgetData = (props, id) => {
5
+ const { data, sources, libraryConfig } = props.data;
6
6
  const theme = useThemeValue();
7
- const config = React.useMemo(() => shapeYagrConfig({ data, libraryConfig, theme }), [data, libraryConfig, theme]);
7
+ const config = React.useMemo(() => shapeYagrConfig({
8
+ data,
9
+ libraryConfig,
10
+ theme,
11
+ customTooltip: Boolean(props.CustomTooltip),
12
+ }), [data, libraryConfig, theme, props.CustomTooltip]);
8
13
  const debug = React.useMemo(() => {
9
14
  const filename = sources
10
15
  ? Object.values(sources)
@@ -3,6 +3,7 @@ type ShapeYagrConfigArgs = {
3
3
  data: YagrWidgetData['data'];
4
4
  libraryConfig: YagrWidgetData['libraryConfig'];
5
5
  theme: YagrTheme;
6
+ customTooltip?: boolean;
6
7
  };
7
8
  export declare const synchronizeTooltipTablesCellsWidth: (tooltipContainer: HTMLElement) => void;
8
9
  export declare const checkFocus: (args: {
@@ -101,6 +101,9 @@ export const shapeYagrConfig = (args) => {
101
101
  // "className" property prevent default yagr styles adding
102
102
  config.tooltip.className = 'chartkit-yagr-tooltip';
103
103
  }
104
+ if (args.customTooltip) {
105
+ config.tooltip.virtual = true;
106
+ }
104
107
  }
105
108
  config.axes = config.axes || {};
106
109
  const xAxis = config.axes[defaults.DEFAULT_X_SCALE];
@@ -1,7 +1,18 @@
1
- import type { RawSerieData, YagrConfig } from '@gravity-ui/yagr';
1
+ /// <reference types="react" />
2
+ import type { MinimalValidConfig, RawSerieData, TooltipOptions, YagrConfig } from '@gravity-ui/yagr';
3
+ import type Yagr from '@gravity-ui/yagr';
4
+ import { ChartKitProps } from 'src/types';
2
5
  export type { default as Yagr } from '@gravity-ui/yagr';
3
6
  export type { YagrReactRef } from '@gravity-ui/yagr/dist/react';
4
7
  export * from '@gravity-ui/yagr/dist/types';
8
+ export interface CustomTooltipProps<T extends Yagr<MinimalValidConfig> = Yagr<MinimalValidConfig>> {
9
+ config?: TooltipOptions;
10
+ yagr: T | undefined;
11
+ }
12
+ export type YagrWidgetProps = ChartKitProps<'yagr'> & {
13
+ id: string;
14
+ CustomTooltip?: React.ComponentType<CustomTooltipProps>;
15
+ };
5
16
  export type YagrWidgetData = {
6
17
  data: {
7
18
  graphs: RawSerieData[];
@@ -1,5 +1,5 @@
1
1
  /// <reference types="react" />
2
- import type { Yagr, YagrReactRef, YagrWidgetData } from '../plugins/yagr/types';
2
+ import type { CustomTooltipProps, Yagr, YagrReactRef, YagrWidgetData } from '../plugins/yagr/types';
3
3
  import type { IndicatorWidgetData } from '../plugins/indicator/types';
4
4
  import type { Highcharts, HighchartsWidgetData, StringParams } from '../plugins/highcharts/types';
5
5
  export interface ChartKitWidget {
@@ -7,6 +7,7 @@ export interface ChartKitWidget {
7
7
  data: YagrWidgetData;
8
8
  widget: Yagr;
9
9
  pluginRef: React.MutableRefObject<YagrReactRef | null>;
10
+ CustomTooltip?: React.ComponentType<CustomTooltipProps>;
10
11
  };
11
12
  indicator: {
12
13
  data: IndicatorWidgetData;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gravity-ui/chartkit",
3
- "version": "3.0.0-beta.9",
3
+ "version": "3.0.0-beta.zefirka.10",
4
4
  "description": "React component used to render charts based on any sources you need",
5
5
  "license": "MIT",
6
6
  "repository": "git@github.com:gravity-ui/ChartKit.git",
@@ -14,7 +14,7 @@
14
14
  },
15
15
  "dependencies": {
16
16
  "@gravity-ui/date-utils": "^1.4.1",
17
- "@gravity-ui/yagr": "^3.3.6",
17
+ "@gravity-ui/yagr": "^3.6.0",
18
18
  "bem-cn-lite": "^4.1.0",
19
19
  "highcharts": "^8.2.2",
20
20
  "highcharts-react-official": "^3.0.0",