@gravity-ui/chartkit 3.0.0-beta.10 → 3.0.0-beta.15

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.
@@ -8,7 +8,6 @@ declare const ChartKitComponent: <T extends keyof import("../types").ChartKitWid
8
8
  export declare const ChartKit: <T extends keyof import("../types").ChartKitWidget>(props: {
9
9
  type: T;
10
10
  data: import("../types").ChartKitWidget[T]["data"];
11
- pluginRef?: import("../types").ChartKitWidget[T]["pluginRef"] | undefined;
12
11
  id?: string | undefined;
13
12
  isMobile?: boolean | undefined;
14
13
  onLoad?: ((data?: import("../types").ChartKitOnLoadData<T> | undefined) => void) | undefined;
@@ -17,7 +16,7 @@ export declare const ChartKit: <T extends keyof import("../types").ChartKitWidge
17
16
  onError?: import("../types").ChartKitOnError | undefined;
18
17
  renderError?: import("../types").RenderError | undefined;
19
18
  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) & {
19
+ } & (Omit<import("../types").ChartKitWidget[T], "data" | "widget"> extends infer T_1 ? { [key in keyof T_1]: import("../types").ChartKitWidget[T][key]; } : never) & {
21
20
  ref?: React.ForwardedRef<ChartKitRef | undefined> | undefined;
22
21
  }) => ReturnType<typeof ChartKitComponent>;
23
22
  export {};
@@ -3,7 +3,6 @@ import type { ChartKitWidgetRef } from '../../../types';
3
3
  declare const HighchartsWidget: React.ForwardRefExoticComponent<{
4
4
  type: "highcharts";
5
5
  data: import("../types").HighchartsWidgetData;
6
- pluginRef?: undefined;
7
6
  id?: string | undefined;
8
7
  isMobile?: boolean | undefined;
9
8
  onLoad?: ((data?: import("../../../types").ChartKitOnLoadData<"highcharts"> | undefined) => void) | undefined;
@@ -4,7 +4,6 @@ import './IndicatorWidget.css';
4
4
  declare const IndicatorWidget: React.ForwardRefExoticComponent<{
5
5
  type: "indicator";
6
6
  data: import("../types").IndicatorWidgetData;
7
- pluginRef?: undefined;
8
7
  id?: string | undefined;
9
8
  isMobile?: boolean | undefined;
10
9
  onLoad?: ((data?: import("../../../types").ChartKitOnLoadData<"indicator"> | undefined) => void) | undefined;
@@ -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,
@@ -12,14 +13,12 @@ export default {
12
13
  const LineTemplate = () => {
13
14
  const [shown, setShown] = React.useState(false);
14
15
  const chartkitRef = React.useRef();
15
- // Example of usage pluginRef property
16
- const yagrPluginRef = React.useRef(null);
17
16
  if (!shown) {
18
17
  settings.set({ plugins: [YagrPlugin] });
19
18
  return React.createElement(Button, { onClick: () => setShown(true) }, "Show chart");
20
19
  }
21
20
  return (React.createElement("div", { style: { height: 300, width: '100%' } },
22
- React.createElement(ChartKit, { ref: chartkitRef, id: "1", type: "yagr", data: line10, pluginRef: yagrPluginRef })));
21
+ React.createElement(ChartKit, { ref: chartkitRef, id: "1", type: "yagr", data: line10 })));
23
22
  };
24
23
  const UpdatesTemplate = () => {
25
24
  const [shown, setShown] = React.useState(false);
@@ -39,5 +38,52 @@ const UpdatesTemplate = () => {
39
38
  React.createElement(Button, { onClick: onStartUpdates }, "Start Updates"),
40
39
  React.createElement(Button, { onClick: () => setState(getNewConfig()) }, "Once")));
41
40
  };
41
+ function Tooltip({ yagr }) {
42
+ const [x, setX] = useState(null);
43
+ const [visible, setVisible] = useState(false);
44
+ const tooltipRef = React.useRef(null);
45
+ useEffect(() => {
46
+ var _a, _b, _c;
47
+ if (!yagr) {
48
+ return;
49
+ }
50
+ const onChange = (_, data) => {
51
+ var _a, _b;
52
+ setX((_a = data.data) === null || _a === void 0 ? void 0 : _a.x);
53
+ setVisible(data.state.visible);
54
+ if (data.state.visible && tooltipRef.current && ((_b = data.data) === null || _b === void 0 ? void 0 : _b.anchor)) {
55
+ placement(tooltipRef.current, data.data.anchor, 'right', {
56
+ xOffset: 24,
57
+ yOffset: 24,
58
+ });
59
+ }
60
+ };
61
+ (_a = yagr.plugins.tooltip) === null || _a === void 0 ? void 0 : _a.on('render', onChange);
62
+ (_b = yagr.plugins.tooltip) === null || _b === void 0 ? void 0 : _b.on('show', onChange);
63
+ (_c = yagr.plugins.tooltip) === null || _c === void 0 ? void 0 : _c.on('hide', onChange);
64
+ }, [yagr]);
65
+ if (!visible) {
66
+ return null;
67
+ }
68
+ return (React.createElement("div", { style: {
69
+ zIndex: 1000,
70
+ backgroundColor: 'white',
71
+ padding: 8,
72
+ pointerEvents: 'none',
73
+ }, ref: tooltipRef }, x));
74
+ }
75
+ const CustomTooltipImpl = () => {
76
+ const [shown, setShown] = React.useState(false);
77
+ const chartkitRef = React.useRef();
78
+ const [state, setState] = React.useState(line10);
79
+ if (!shown) {
80
+ settings.set({ plugins: [YagrPlugin] });
81
+ return React.createElement(Button, { onClick: () => setShown(true) }, "Show chart");
82
+ }
83
+ return (React.createElement("div", { style: { height: 300, width: '100%' } },
84
+ React.createElement(ChartKit, { ref: chartkitRef, id: "1", type: "yagr", data: state, tooltip: (props) => React.createElement(Tooltip, Object.assign({}, props)) }),
85
+ React.createElement(Button, { onClick: () => setState(getNewConfig()) }, "Change data")));
86
+ };
42
87
  export const Line = LineTemplate.bind({});
43
88
  export const Updates = UpdatesTemplate.bind({});
89
+ 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,5 +1,4 @@
1
1
  import React from 'react';
2
- import { YagrReactRef } from '@gravity-ui/yagr/dist/react';
3
2
  import type { ChartKitWidgetRef } from '../../../types';
4
3
  import './polyfills';
5
4
  import '@gravity-ui/yagr/dist/index.css';
@@ -7,7 +6,6 @@ import './YagrWidget.css';
7
6
  declare const YagrWidget: React.ForwardRefExoticComponent<{
8
7
  type: "yagr";
9
8
  data: import("../types").YagrWidgetData;
10
- pluginRef?: React.MutableRefObject<YagrReactRef | null> | undefined;
11
9
  id?: string | undefined;
12
10
  isMobile?: boolean | undefined;
13
11
  onLoad?: ((data?: import("../../../types").ChartKitOnLoadData<"yagr"> | undefined) => void) | undefined;
@@ -16,7 +14,9 @@ declare const YagrWidget: React.ForwardRefExoticComponent<{
16
14
  onError?: import("../../../types").ChartKitOnError | undefined;
17
15
  renderError?: import("../../../types").RenderError | undefined;
18
16
  renderPluginLoader?: (() => React.ReactNode) | undefined;
19
- } & {} & {
17
+ } & {
18
+ tooltip?: (<T extends import("../types").CustomTooltipProps = import("../types").CustomTooltipProps>(props: T) => React.ReactNode) | undefined;
19
+ } & {
20
20
  id: string;
21
21
  } & React.RefAttributes<ChartKitWidgetRef | undefined>>;
22
22
  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, tooltip, } = 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,16 @@ 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
+ tooltip &&
71
+ yagr &&
72
+ tooltip({
73
+ yagr,
74
+ }),
75
+ React.createElement(YagrComponent, { ref: yagrRef, id: id, config: config, debug: debug, onChartLoad: handleChartLoading })));
79
76
  });
80
77
  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.tooltip),
12
+ }), [data, libraryConfig, theme, props.tooltip]);
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,15 @@
1
- import type { RawSerieData, YagrConfig } from '@gravity-ui/yagr';
1
+ import type { MinimalValidConfig, RawSerieData, YagrConfig } from '@gravity-ui/yagr';
2
+ import type Yagr from '@gravity-ui/yagr';
3
+ import { ChartKitProps } from 'src/types';
2
4
  export type { default as Yagr } from '@gravity-ui/yagr';
3
5
  export type { YagrReactRef } from '@gravity-ui/yagr/dist/react';
4
6
  export * from '@gravity-ui/yagr/dist/types';
7
+ export interface CustomTooltipProps {
8
+ yagr: Yagr<MinimalValidConfig> | undefined;
9
+ }
10
+ export type YagrWidgetProps = ChartKitProps<'yagr'> & {
11
+ id: string;
12
+ };
5
13
  export type YagrWidgetData = {
6
14
  data: {
7
15
  graphs: RawSerieData[];
@@ -26,8 +26,6 @@ export type ChartKitOnError = (data: {
26
26
  export type ChartKitProps<T extends ChartKitType> = {
27
27
  type: T;
28
28
  data: ChartKitWidget[T]['data'];
29
- /** Plugin component's react ref */
30
- pluginRef?: ChartKitWidget[T]['pluginRef'];
31
29
  id?: string;
32
30
  isMobile?: boolean;
33
31
  onLoad?: (data?: ChartKitOnLoadData<T>) => void;
@@ -42,7 +40,7 @@ export type ChartKitProps<T extends ChartKitType> = {
42
40
  /** Used to render user's plugin loader component */
43
41
  renderPluginLoader?: () => React.ReactNode;
44
42
  } & {
45
- [key in keyof Omit<ChartKitWidget[T], 'data' | 'widget' | 'pluginRef'>]: ChartKitWidget[T][key];
43
+ [key in keyof Omit<ChartKitWidget[T], 'data' | 'widget'>]: ChartKitWidget[T][key];
46
44
  };
47
45
  export type ChartKitPlugin = {
48
46
  type: ChartKitType;
@@ -1,23 +1,21 @@
1
1
  /// <reference types="react" />
2
- import type { Yagr, YagrReactRef, YagrWidgetData } from '../plugins/yagr/types';
2
+ import type { CustomTooltipProps, Yagr, 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 {
6
6
  yagr: {
7
7
  data: YagrWidgetData;
8
8
  widget: Yagr;
9
- pluginRef: React.MutableRefObject<YagrReactRef | null>;
9
+ tooltip?: <T extends CustomTooltipProps = CustomTooltipProps>(props: T) => React.ReactNode;
10
10
  };
11
11
  indicator: {
12
12
  data: IndicatorWidgetData;
13
13
  widget: never;
14
- pluginRef: never;
15
14
  formatNumber?: <T = any>(value: number, options?: T) => string;
16
15
  };
17
16
  highcharts: {
18
17
  data: HighchartsWidgetData;
19
18
  widget: Highcharts.Chart | null;
20
- pluginRef: never;
21
19
  hoistConfigError?: boolean;
22
20
  nonBodyScroll?: boolean;
23
21
  splitTooltip?: boolean;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gravity-ui/chartkit",
3
- "version": "3.0.0-beta.10",
3
+ "version": "3.0.0-beta.15",
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.4.0",
17
+ "@gravity-ui/yagr": "^3.7.2",
18
18
  "bem-cn-lite": "^4.1.0",
19
19
  "highcharts": "^8.2.2",
20
20
  "highcharts-react-official": "^3.0.0",