@gravity-ui/chartkit 3.0.0-beta.6 → 3.0.0-beta.8

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.
Files changed (30) hide show
  1. package/build/components/ChartKit.d.ts +2 -1
  2. package/build/libs/settings/__tests__/settings.test.js +2 -9
  3. package/build/libs/settings/eventEmitter.d.ts +11 -0
  4. package/build/libs/settings/eventEmitter.js +25 -0
  5. package/build/libs/settings/settings.d.ts +6 -3
  6. package/build/libs/settings/settings.js +8 -13
  7. package/build/plugins/highcharts/renderer/HighchartsWidget.d.ts +1 -0
  8. package/build/plugins/highcharts/renderer/components/HighchartsComponent.js +6 -0
  9. package/build/plugins/highcharts/renderer/helpers/config/config.js +10 -10
  10. package/build/plugins/highcharts/renderer/helpers/config/utils/index.d.ts +33 -0
  11. package/build/plugins/highcharts/renderer/helpers/config/utils/index.js +8 -0
  12. package/build/plugins/highcharts/renderer/helpers/config/utils/setNavigatorDefaultPeriod.js +2 -2
  13. package/build/plugins/highcharts/renderer/helpers/highcharts/highcharts.d.ts +1 -0
  14. package/build/plugins/highcharts/renderer/helpers/highcharts/highcharts.js +5 -2
  15. package/build/plugins/highcharts/renderer/helpers/prepare-data.js +3 -3
  16. package/build/plugins/highcharts/renderer/helpers/tooltip/helpers.d.ts +0 -2
  17. package/build/plugins/highcharts/renderer/helpers/tooltip/helpers.js +0 -9
  18. package/build/plugins/highcharts/renderer/helpers/tooltip/index.js +4 -5
  19. package/build/plugins/highcharts/renderer/helpers/tooltip/types.d.ts +0 -9
  20. package/build/plugins/highcharts/types/widget.d.ts +9 -1
  21. package/build/plugins/indicator/renderer/IndicatorWidget.d.ts +1 -0
  22. package/build/plugins/yagr/__stories__/Yagr.stories.js +3 -1
  23. package/build/plugins/yagr/renderer/YagrWidget.d.ts +2 -0
  24. package/build/plugins/yagr/renderer/YagrWidget.js +13 -12
  25. package/build/plugins/yagr/renderer/tooltip/renderTooltip.js +2 -2
  26. package/build/plugins/yagr/renderer/utils.js +3 -3
  27. package/build/plugins/yagr/types.d.ts +1 -0
  28. package/build/types/index.d.ts +3 -1
  29. package/build/types/widget.d.ts +5 -1
  30. package/package.json +2 -3
@@ -8,6 +8,7 @@ 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;
11
12
  id?: string | undefined;
12
13
  isMobile?: boolean | undefined;
13
14
  onLoad?: ((data?: import("../types").ChartKitOnLoadData<T> | undefined) => void) | undefined;
@@ -16,7 +17,7 @@ export declare const ChartKit: <T extends keyof import("../types").ChartKitWidge
16
17
  onError?: import("../types").ChartKitOnError | undefined;
17
18
  renderError?: import("../types").RenderError | undefined;
18
19
  renderPluginLoader?: (() => React.ReactNode) | undefined;
19
- } & (Omit<import("../types").ChartKitWidget[T], "widget" | "data"> extends infer T_1 ? { [key in keyof T_1]: import("../types").ChartKitWidget[T][key]; } : never) & {
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
21
  ref?: React.ForwardedRef<ChartKitRef | undefined> | undefined;
21
22
  }) => ReturnType<typeof ChartKitComponent>;
22
23
  export {};
@@ -1,13 +1,6 @@
1
- import { DEFAULT_LOCALE_SPECIFICATION, settings } from '../settings';
2
- const resetSettings = () => settings.set({
3
- lang: 'en',
4
- locale: DEFAULT_LOCALE_SPECIFICATION,
5
- });
1
+ import { settings } from '../settings';
2
+ const resetSettings = () => settings.set({ lang: 'en' });
6
3
  describe('libs/settings', () => {
7
- it('Default locale should be equal DEFAULT_LOCALE_SPECIFICATION', () => {
8
- const result = settings.get('locale');
9
- expect(result).toStrictEqual(DEFAULT_LOCALE_SPECIFICATION);
10
- });
11
4
  it('Default lang should be equal to en', () => {
12
5
  const result = settings.get('lang');
13
6
  expect(result).toBe('en');
@@ -0,0 +1,11 @@
1
+ type EventObject<T = unknown> = {
2
+ id: string;
3
+ action: (args: T) => void;
4
+ };
5
+ export declare class EventEmitter<EventsMap = unknown> {
6
+ private events;
7
+ on<MapKey extends keyof EventsMap>(type: MapKey, event: EventObject<EventsMap[MapKey]>): void;
8
+ off<MapKey extends keyof EventsMap>(type: MapKey, eventId: string): void;
9
+ dispatch<MapKey extends keyof EventsMap>(type: MapKey, args: EventsMap[MapKey]): void;
10
+ }
11
+ export {};
@@ -0,0 +1,25 @@
1
+ export class EventEmitter {
2
+ constructor() {
3
+ this.events = {};
4
+ }
5
+ on(type, event) {
6
+ if (this.events[type]) {
7
+ this.events[type].push(event);
8
+ }
9
+ else {
10
+ this.events[type] = [event];
11
+ }
12
+ }
13
+ off(type, eventId) {
14
+ if (this.events[type]) {
15
+ this.events[type] = this.events[type].filter(({ id }) => id !== eventId);
16
+ }
17
+ }
18
+ dispatch(type, args) {
19
+ if (this.events[type]) {
20
+ this.events[type].forEach(({ action }) => {
21
+ action(args);
22
+ });
23
+ }
24
+ }
25
+ }
@@ -1,17 +1,20 @@
1
- import moment from 'moment';
2
1
  import type { ChartKitPlugin, ChartKitLang, ChartKitHolidays } from '../../types';
2
+ import { EventEmitter } from './eventEmitter';
3
3
  interface Settings {
4
4
  plugins: ChartKitPlugin[];
5
5
  lang: ChartKitLang;
6
- locale?: moment.LocaleSpecification;
7
6
  extra?: {
8
7
  holidays?: ChartKitHolidays;
9
8
  };
10
9
  }
11
10
  type SettingKey = keyof Settings;
12
- export declare const DEFAULT_LOCALE_SPECIFICATION: moment.LocaleSpecification;
11
+ type SettingsEventsMap = {
12
+ 'change-lang': ChartKitLang;
13
+ };
14
+ export declare const settingsEventEmitter: EventEmitter<SettingsEventsMap>;
13
15
  declare class ChartKitSettings {
14
16
  private settings;
17
+ constructor();
15
18
  get<T extends SettingKey>(key: T): Settings[T];
16
19
  set(updates: Partial<Settings>): void;
17
20
  }
@@ -1,9 +1,9 @@
1
- import moment from 'moment';
2
1
  import get from 'lodash/get';
3
2
  import merge from 'lodash/merge';
4
3
  import { configure } from '@gravity-ui/uikit';
5
4
  import { i18nFactory } from '../../i18n';
6
- export const DEFAULT_LOCALE_SPECIFICATION = { week: { dow: 1, doy: 7 } };
5
+ import { EventEmitter } from './eventEmitter';
6
+ export const settingsEventEmitter = new EventEmitter();
7
7
  const removeUndefinedValues = (data) => {
8
8
  return Object.entries(data).reduce((acc, [key, value]) => {
9
9
  if (typeof value !== 'undefined') {
@@ -12,12 +12,7 @@ const removeUndefinedValues = (data) => {
12
12
  return acc;
13
13
  }, {});
14
14
  };
15
- const updateLocale = (args) => {
16
- const { lang, locale } = args;
17
- if (locale) {
18
- moment.updateLocale(lang, locale);
19
- }
20
- moment.locale(lang);
15
+ const updateLang = (lang) => {
21
16
  configure({ lang });
22
17
  i18nFactory.setLang(lang);
23
18
  };
@@ -27,19 +22,19 @@ class ChartKitSettings {
27
22
  plugins: [],
28
23
  lang: 'en',
29
24
  };
25
+ updateLang(this.get('lang'));
30
26
  }
31
27
  get(key) {
32
28
  return get(this.settings, key);
33
29
  }
34
30
  set(updates) {
35
31
  const filteredUpdates = removeUndefinedValues(updates);
36
- if (filteredUpdates.lang || filteredUpdates.locale) {
32
+ this.settings = merge(this.settings, filteredUpdates);
33
+ if (filteredUpdates.lang) {
37
34
  const lang = filteredUpdates.lang || this.get('lang');
38
- const locale = filteredUpdates.locale || this.get('locale');
39
- updateLocale({ lang, locale });
35
+ updateLang(lang);
36
+ settingsEventEmitter.dispatch('change-lang', lang);
40
37
  }
41
- this.settings = merge(this.settings, filteredUpdates);
42
38
  }
43
39
  }
44
40
  export const settings = new ChartKitSettings();
45
- settings.set({ locale: DEFAULT_LOCALE_SPECIFICATION });
@@ -3,6 +3,7 @@ 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;
6
7
  id?: string | undefined;
7
8
  isMobile?: boolean | undefined;
8
9
  onLoad?: ((data?: import("../../../types").ChartKitOnLoadData<"highcharts"> | undefined) => void) | undefined;
@@ -3,12 +3,18 @@ import Highcharts from 'highcharts';
3
3
  import HighchartsReact from 'highcharts-react-official';
4
4
  import get from 'lodash/get';
5
5
  import { settings } from '../../../../libs';
6
+ import { settingsEventEmitter } from '../../../../libs/settings/settings';
6
7
  import { markChartPerformance, getChartPerformanceDuration, getRandomCKId } from '../../../../utils';
7
8
  import { getGraph } from '../helpers/graph';
8
9
  import { initHighchartsModules } from '../helpers/init-highcharts-modules';
10
+ import { initHighchartsLangOptions } from '../helpers/highcharts/highcharts';
9
11
  import { withSplitPane } from './withSplitPane/withSplitPane';
10
12
  import './HighchartsComponent.css';
11
13
  const HighcharsReactWithSplitPane = withSplitPane(HighchartsReact);
14
+ settingsEventEmitter.on('change-lang', {
15
+ id: 'hc-lang-handler',
16
+ action: initHighchartsLangOptions,
17
+ });
12
18
  initHighchartsModules();
13
19
  export class HighchartsComponent extends React.PureComponent {
14
20
  constructor() {
@@ -10,13 +10,13 @@ import isNumber from 'lodash/isNumber';
10
10
  import throttle from 'lodash/throttle';
11
11
  import pick from 'lodash/pick';
12
12
  import debounce from 'lodash/debounce';
13
- import moment from 'moment';
13
+ import { dateTime } from '@gravity-ui/date-utils';
14
14
  import { i18n } from '../../../../../i18n';
15
15
  import { formatNumber } from '../../../../shared';
16
16
  import { getCommentsOnLine, drawComments, hideComments, drawOnlyRendererComments, } from '../comments/drawing';
17
17
  import formatTooltip, { TOOLTIP_ROW_CLASS_NAME, SERIES_NAME_DATA_ATTRIBUTE, TOOLTIP_HEADER_CLASS_NAME, TOOLTIP_LIST_CLASS_NAME, TOOLTIP_FOOTER_CLASS_NAME, TOOLTIP_CONTAINER_CLASS_NAME, TOOLTIP_ROW_NAME_CLASS_NANE, } from '../tooltip';
18
18
  import defaultOptions from './options';
19
- import { calculatePrecision, isTooltipShared, isSafari, mergeArrayWithObject, concatStrings, buildNavigatorFallback, addShowInNavigatorToSeries, setNavigatorDefaultPeriod, numberFormat, getFormatOptionsFromLine, checkTooltipPinningAvailability, } from './utils';
19
+ import { calculatePrecision, isTooltipShared, isSafari, mergeArrayWithObject, concatStrings, buildNavigatorFallback, addShowInNavigatorToSeries, setNavigatorDefaultPeriod, numberFormat, getFormatOptionsFromLine, checkTooltipPinningAvailability, getSortedData, } from './utils';
20
20
  import { handleLegendItemClick } from './handleLegendItemClick';
21
21
  import { getChartKitFormattedValue } from './utils/getChartKitFormattedValue';
22
22
  const b = block('chartkit-tooltip');
@@ -381,7 +381,7 @@ function validateCellManipulationConfig(tooltipOptions, property, item) {
381
381
  }
382
382
  }
383
383
  function getTooltip(tooltip, options, comments, holidays) {
384
- var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
384
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j;
385
385
  const serieType = (this.series && this.series.type) || tooltip.chart.options.chart.type;
386
386
  const chart = tooltip.chart;
387
387
  const xAxis = chart.xAxis[0];
@@ -412,7 +412,6 @@ function getTooltip(tooltip, options, comments, holidays) {
412
412
  count: 1,
413
413
  shared: true,
414
414
  unsafe: Boolean(options.unsafe),
415
- sort: (_b = options === null || options === void 0 ? void 0 : options.tooltip) === null || _b === void 0 ? void 0 : _b.sort,
416
415
  };
417
416
  if (typeof options.manageTooltipConfig === 'function') {
418
417
  json = callManageTooltipConfig(options, json, chart);
@@ -440,7 +439,7 @@ function getTooltip(tooltip, options, comments, holidays) {
440
439
  let points = [];
441
440
  let shared;
442
441
  if (this.points) {
443
- points = this.points;
442
+ points = getSortedData(this.points, (_b = options === null || options === void 0 ? void 0 : options.tooltip) === null || _b === void 0 ? void 0 : _b.sort);
444
443
  shared = true;
445
444
  }
446
445
  else {
@@ -459,7 +458,6 @@ function getTooltip(tooltip, options, comments, holidays) {
459
458
  withPercent: false,
460
459
  tooltipHeader: null,
461
460
  unsafe: Boolean(options.unsafe),
462
- sort: (_c = options === null || options === void 0 ? void 0 : options.tooltip) === null || _c === void 0 ? void 0 : _c.sort,
463
461
  };
464
462
  if (isDatetimeXAxis) {
465
463
  const items = this.points || Highcharts.splat(this);
@@ -476,9 +474,9 @@ function getTooltip(tooltip, options, comments, holidays) {
476
474
  }
477
475
  json.tooltipHeader = chart.time.dateFormat(xDateFormat, this.x);
478
476
  }
479
- else if ((_f = (_e = (_d = extendedPoint.point) === null || _d === void 0 ? void 0 : _d.options) === null || _e === void 0 ? void 0 : _e.xFormatted) !== null && _f !== void 0 ? _f : (extendedPoint.x || extendedPoint.x === 0)) {
480
- const customTooltipHeaderFormatter = (_g = options === null || options === void 0 ? void 0 : options.highcharts) === null || _g === void 0 ? void 0 : _g.tooltipHeaderFormatter;
481
- const tooltipHeaderStringValue = String((_k = (_j = (_h = extendedPoint.point) === null || _h === void 0 ? void 0 : _h.options) === null || _j === void 0 ? void 0 : _j.xFormatted) !== null && _k !== void 0 ? _k : extendedPoint.x);
477
+ else if ((_e = (_d = (_c = extendedPoint.point) === null || _c === void 0 ? void 0 : _c.options) === null || _d === void 0 ? void 0 : _d.xFormatted) !== null && _e !== void 0 ? _e : (extendedPoint.x || extendedPoint.x === 0)) {
478
+ const customTooltipHeaderFormatter = (_f = options === null || options === void 0 ? void 0 : options.highcharts) === null || _f === void 0 ? void 0 : _f.tooltipHeaderFormatter;
479
+ const tooltipHeaderStringValue = String((_j = (_h = (_g = extendedPoint.point) === null || _g === void 0 ? void 0 : _g.options) === null || _h === void 0 ? void 0 : _h.xFormatted) !== null && _j !== void 0 ? _j : extendedPoint.x);
482
480
  json.tooltipHeader = customTooltipHeaderFormatter
483
481
  ? customTooltipHeaderFormatter(tooltipHeaderStringValue)
484
482
  : tooltipHeaderStringValue;
@@ -1071,7 +1069,9 @@ function drillOnClick(event, { options, chartType }) {
1071
1069
  drillDownFilter =
1072
1070
  chartType === 'scatter' ? drillDownFilter - 180 * 60 * 1000 : drillDownFilter;
1073
1071
  }
1074
- return isDateTime ? moment(drillDownFilter).format('YYYY-MM-DD') : drillDownFilter;
1072
+ return isDateTime
1073
+ ? dateTime({ input: drillDownFilter }).format('YYYY-MM-DD')
1074
+ : drillDownFilter;
1075
1075
  }
1076
1076
  return filter;
1077
1077
  });
@@ -1,3 +1,4 @@
1
+ import type { HighchartsSortData } from '../../../../types/widget';
1
2
  export { addShowInNavigatorToSeries } from './addShowInNavigatorToSeries';
2
3
  export { buildNavigatorFallback } from './buildNavigatorFallback';
3
4
  export { calculatePrecision } from './calculatePrecision';
@@ -11,3 +12,35 @@ export { isSafari } from './isSafari';
11
12
  export { mergeArrayWithObject } from './mergeArrayWithObject';
12
13
  export { numberFormat } from './numberFormat';
13
14
  export { setNavigatorDefaultPeriod } from './setNavigatorDefaultPeriod';
15
+ export declare const getSortedData: <T extends Record<string, any>>(data: T[], sort?: HighchartsSortData) => (number | T | (() => IterableIterator<T>) | (() => {
16
+ copyWithin: boolean;
17
+ entries: boolean;
18
+ fill: boolean;
19
+ find: boolean;
20
+ findIndex: boolean;
21
+ keys: boolean;
22
+ values: boolean;
23
+ }) | (() => string) | (() => string) | (() => T | undefined) | ((...items: T[]) => number) | {
24
+ (...items: ConcatArray<T>[]): T[];
25
+ (...items: (T | ConcatArray<T>)[]): T[];
26
+ } | ((separator?: string | undefined) => string) | (() => T[]) | (() => T | undefined) | ((start?: number | undefined, end?: number | undefined) => T[]) | ((compareFn?: ((a: T, b: T) => number) | undefined) => T[]) | {
27
+ (start: number, deleteCount?: number | undefined): T[];
28
+ (start: number, deleteCount: number, ...items: T[]): T[];
29
+ } | ((...items: T[]) => number) | ((searchElement: T, fromIndex?: number | undefined) => number) | ((searchElement: T, fromIndex?: number | undefined) => number) | {
30
+ <S extends T>(predicate: (value: T, index: number, array: T[]) => value is S, thisArg?: any): this is S[];
31
+ (predicate: (value: T, index: number, array: T[]) => unknown, thisArg?: any): boolean;
32
+ } | ((predicate: (value: T, index: number, array: T[]) => unknown, thisArg?: any) => boolean) | ((callbackfn: (value: T, index: number, array: T[]) => void, thisArg?: any) => void) | (<U>(callbackfn: (value: T, index: number, array: T[]) => U, thisArg?: any) => U[]) | {
33
+ <S_1 extends T>(predicate: (value: T, index: number, array: T[]) => value is S_1, thisArg?: any): S_1[];
34
+ (predicate: (value: T, index: number, array: T[]) => unknown, thisArg?: any): T[];
35
+ } | {
36
+ (callbackfn: (previousValue: T, currentValue: T, currentIndex: number, array: T[]) => T): T;
37
+ (callbackfn: (previousValue: T, currentValue: T, currentIndex: number, array: T[]) => T, initialValue: T): T;
38
+ <U_1>(callbackfn: (previousValue: U_1, currentValue: T, currentIndex: number, array: T[]) => U_1, initialValue: U_1): U_1;
39
+ } | {
40
+ (callbackfn: (previousValue: T, currentValue: T, currentIndex: number, array: T[]) => T): T;
41
+ (callbackfn: (previousValue: T, currentValue: T, currentIndex: number, array: T[]) => T, initialValue: T): T;
42
+ <U_2>(callbackfn: (previousValue: U_2, currentValue: T, currentIndex: number, array: T[]) => U_2, initialValue: U_2): U_2;
43
+ } | {
44
+ <S_2 extends T>(predicate: (this: void, value: T, index: number, obj: T[]) => value is S_2, thisArg?: any): S_2 | undefined;
45
+ (predicate: (value: T, index: number, obj: T[]) => unknown, thisArg?: any): T | undefined;
46
+ } | ((predicate: (value: T, index: number, obj: T[]) => unknown, thisArg?: any) => number) | ((value: T, start?: number | undefined, end?: number | undefined) => T[]) | ((target: number, start: number, end?: number | undefined) => T[]) | (() => IterableIterator<[number, T]>) | (() => IterableIterator<number>) | (() => IterableIterator<T>) | ((searchElement: T, fromIndex?: number | undefined) => boolean) | (<U_3, This = undefined>(callback: (this: This, value: T, index: number, array: T[]) => U_3 | readonly U_3[], thisArg?: This | undefined) => U_3[]) | (<A, D extends number = 1>(this: A, depth?: D | undefined) => FlatArray<A, D>[]) | ((index: number) => T | undefined))[];
@@ -1,3 +1,4 @@
1
+ import orderBy from 'lodash/orderBy';
1
2
  export { addShowInNavigatorToSeries } from './addShowInNavigatorToSeries';
2
3
  export { buildNavigatorFallback } from './buildNavigatorFallback';
3
4
  export { calculatePrecision } from './calculatePrecision';
@@ -11,3 +12,10 @@ export { isSafari } from './isSafari';
11
12
  export { mergeArrayWithObject } from './mergeArrayWithObject';
12
13
  export { numberFormat } from './numberFormat';
13
14
  export { setNavigatorDefaultPeriod } from './setNavigatorDefaultPeriod';
15
+ export const getSortedData = (data, sort = {}) => {
16
+ const { enabled = true, order = 'desc', iteratee = 'y' } = sort;
17
+ if (!enabled) {
18
+ return [...data];
19
+ }
20
+ return orderBy(data, iteratee, order);
21
+ };
@@ -1,4 +1,4 @@
1
- import moment from 'moment';
1
+ import { dateTime } from '@gravity-ui/date-utils';
2
2
  import { getXAxisThresholdValue } from './getXAxisThresholdValue';
3
3
  const HOUR_IN_MS = 1000 * 60 * 60;
4
4
  const DAY_IN_MS = HOUR_IN_MS * 24;
@@ -18,7 +18,7 @@ export const getDefaultPeriodInMS = (periodSettings, series) => {
18
18
  if (maxXValue === null) {
19
19
  return null;
20
20
  }
21
- const minXValue = moment(maxXValue).subtract(value, period);
21
+ const minXValue = dateTime({ input: maxXValue }).subtract(value, period);
22
22
  const range = maxXValue - minXValue.valueOf();
23
23
  return {
24
24
  minRange,
@@ -2,3 +2,4 @@ export function initHighcharts({ isMobile }: {
2
2
  isMobile: any;
3
3
  }): void;
4
4
  export function initHighchartsMap(): void;
5
+ export function initHighchartsLangOptions(): void;
@@ -113,7 +113,7 @@ Highcharts.setOptions({
113
113
  },
114
114
  },
115
115
  });
116
- function initHighcharts({ isMobile }) {
116
+ function initHighchartsLangOptions() {
117
117
  Highcharts.setOptions({
118
118
  lang: {
119
119
  resetZoom: '⟲',
@@ -159,6 +159,9 @@ function initHighcharts({ isMobile }) {
159
159
  thousandsSep: i18n('highcharts', 'thousands-sep'),
160
160
  },
161
161
  });
162
+ }
163
+ function initHighcharts({ isMobile }) {
164
+ initHighchartsLangOptions();
162
165
  // https://github.com/highcharts/highcharts/issues/11494
163
166
  (function (H) {
164
167
  H.wrap(H.Tooltip.prototype, 'getLabel', function (proceed, ...rest) {
@@ -347,4 +350,4 @@ function initHighchartsMap() {
347
350
  }
348
351
  });
349
352
  }
350
- export { initHighcharts, initHighchartsMap };
353
+ export { initHighcharts, initHighchartsMap, initHighchartsLangOptions };
@@ -1,5 +1,5 @@
1
- import moment from 'moment';
2
1
  import lodashMin from 'lodash/min';
2
+ import { dateTime } from '@gravity-ui/date-utils';
3
3
  import { i18n } from '../../../../i18n';
4
4
  import { ChartKitError, CHARTKIT_ERROR_CODE } from '../../../../libs';
5
5
  import { DEFAULT_LINES_LIMIT } from './constants';
@@ -70,9 +70,9 @@ function removeHolidays(data, options, holidays) {
70
70
  graphsData[i] = [];
71
71
  });
72
72
  data.categories_ms.forEach((ts, i) => {
73
- const datetime = moment(ts).format('YYYYMMDD');
73
+ const key = dateTime({ input: ts }).format('YYYYMMDD');
74
74
  const region = (options.region && options.region.toLowerCase()) || 'tot';
75
- const holiday = holidays.holiday[region][datetime] || holidays.weekend[region][datetime];
75
+ const holiday = holidays.holiday[region][key] || holidays.weekend[region][key];
76
76
  if (!holiday) {
77
77
  timeline.push(ts);
78
78
  data.graphs.forEach((graph, j) => graphsData[j].push(graph.data[i]));
@@ -1,3 +1 @@
1
- import type { TooltipData, TooltipLine } from './types';
2
1
  export declare const escapeHTML: (html?: string) => string;
3
- export declare const getSortedLines: (lines: TooltipLine[], sort?: TooltipData['sort']) => TooltipLine[];
@@ -1,14 +1,5 @@
1
- import orderBy from 'lodash/orderBy';
2
1
  export const escapeHTML = (html = '') => {
3
2
  const elem = document.createElement('span');
4
3
  elem.innerText = html;
5
4
  return elem.innerHTML;
6
5
  };
7
- export const getSortedLines = (lines, sort = {}) => {
8
- // set eneabled to true after https://github.com/gravity-ui/chartkit/issues/171
9
- const { enabled = false, order = 'desc', iteratee = 'originalValue' } = sort;
10
- if (!enabled) {
11
- return [...lines];
12
- }
13
- return orderBy(lines, iteratee, order);
14
- };
@@ -2,7 +2,7 @@
2
2
  import _escape from 'lodash/escape';
3
3
  import { i18n } from '../../../../../i18n';
4
4
  import { renderShapeIcon } from './render-shape-icon';
5
- import { escapeHTML, getSortedLines } from './helpers';
5
+ import { escapeHTML } from './helpers';
6
6
  import './tooltip.css';
7
7
  export const SERIES_NAME_DATA_ATTRIBUTE = 'data-series-name';
8
8
  export const SERIES_IDX_DATA_ATTRIBUTE = 'data-series-idx';
@@ -129,9 +129,8 @@ export const formatTooltip = (data, tooltip, isMobile) => {
129
129
  var _a, _b, _c;
130
130
  const { splitTooltip, activeRowAlwaysFirstInTooltip } = data;
131
131
  const lines = data.lines.slice(0, (tooltip.lastVisibleRowIndex || data.lines.length) + 1);
132
- const sortedLines = getSortedLines(lines, data.sort);
133
- const selectedLineIndex = sortedLines.findIndex(({ selectedSeries }) => selectedSeries);
134
- const selectedLine = sortedLines[selectedLineIndex];
132
+ const selectedLineIndex = lines.findIndex(({ selectedSeries }) => selectedSeries);
133
+ const selectedLine = lines[selectedLineIndex];
135
134
  const withShapes = lines.every((line) => line.seriesShape);
136
135
  const unsafe = data.unsafe;
137
136
  const tooltipHeaderRaw = (_a = data.tooltipHeader) === null || _a === void 0 ? void 0 : _a.trim();
@@ -213,7 +212,7 @@ export const formatTooltip = (data, tooltip, isMobile) => {
213
212
  .join('')}</tr>
214
213
  </thead>`}
215
214
  <tbody class="${TOOLTIP_LIST_CLASS_NAME}">
216
- ${sortedLines
215
+ ${lines
217
216
  .map((line, index) => renderRow(line, getRowRenderConfig(index)))
218
217
  .join('')}
219
218
  </tbody>
@@ -38,15 +38,6 @@ export type TooltipData = {
38
38
  /** Sum of the values of the hidden ("not fit" in the tooltip) rows */
39
39
  hiddenRowsSum?: number | string;
40
40
  unsafe?: boolean;
41
- /** Used to manage tooltip lines sorting */
42
- sort?: {
43
- /** Enable tooltip lines sorting. `false` by default */
44
- enabled?: boolean;
45
- /** The sort orders. `'desc'` by default */
46
- order?: 'asc' | 'desc';
47
- /** The iteratees to sort by key(s) from `TooltipLine`. `'originalValue'` by default */
48
- iteratee?: keyof TooltipLine | keyof TooltipLine[] | ((line: TooltipLine) => TooltipLine[keyof TooltipLine] | TooltipLine[keyof TooltipLine][]);
49
- };
50
41
  };
51
42
  export type TooltipLine = {
52
43
  /** Color displayed in a separate cell */
@@ -27,6 +27,14 @@ export type HighchartsManageTooltipConfigOptions = {
27
27
  xComments?: TooltipData['xComments'];
28
28
  };
29
29
  export type HighchartsManageTooltipConfig = (options: HighchartsManageTooltipConfigOptions, chart: Highcharts.Chart) => HighchartsManageTooltipConfigOptions;
30
+ export type HighchartsSortData = {
31
+ /** Enable tooltip lines sorting. `false` by default */
32
+ enabled?: boolean;
33
+ /** The sort orders. `'desc'` by default */
34
+ order?: 'asc' | 'desc';
35
+ /** The iteratees to sort by key(s) from `TooltipLine`. `'originalValue'` by default */
36
+ iteratee?: keyof TooltipLine | keyof TooltipLine[] | ((line: TooltipLine) => TooltipLine[keyof TooltipLine] | TooltipLine[keyof TooltipLine][]);
37
+ };
30
38
  export type HighchartsWidgetData = {
31
39
  data: (CkHighchartsSeriesOptionsType[] | {
32
40
  graphs: CkHighchartsSeriesOptionsType[];
@@ -95,7 +103,7 @@ export type HighchartsWidgetData = {
95
103
  metaKey?: boolean;
96
104
  };
97
105
  /** Used to manage tooltip lines sorting */
98
- sort?: TooltipData['sort'];
106
+ sort?: HighchartsSortData;
99
107
  };
100
108
  /**
101
109
  * Used to modify tooltip data
@@ -4,6 +4,7 @@ import './IndicatorWidget.css';
4
4
  declare const IndicatorWidget: React.ForwardRefExoticComponent<{
5
5
  type: "indicator";
6
6
  data: import("../types").IndicatorWidgetData;
7
+ pluginRef?: undefined;
7
8
  id?: string | undefined;
8
9
  isMobile?: boolean | undefined;
9
10
  onLoad?: ((data?: import("../../../types").ChartKitOnLoadData<"indicator"> | undefined) => void) | undefined;
@@ -12,12 +12,14 @@ export default {
12
12
  const LineTemplate = () => {
13
13
  const [shown, setShown] = React.useState(false);
14
14
  const chartkitRef = React.useRef();
15
+ // Example of usage pluginRef property
16
+ const yagrPluginRef = React.useRef(null);
15
17
  if (!shown) {
16
18
  settings.set({ plugins: [YagrPlugin] });
17
19
  return React.createElement(Button, { onClick: () => setShown(true) }, "Show chart");
18
20
  }
19
21
  return (React.createElement("div", { style: { height: 300, width: '100%' } },
20
- React.createElement(ChartKit, { ref: chartkitRef, id: "1", type: "yagr", data: line10 })));
22
+ React.createElement(ChartKit, { ref: chartkitRef, id: "1", type: "yagr", data: line10, pluginRef: yagrPluginRef })));
21
23
  };
22
24
  const UpdatesTemplate = () => {
23
25
  const [shown, setShown] = React.useState(false);
@@ -1,4 +1,5 @@
1
1
  import React from 'react';
2
+ import { YagrReactRef } from '@gravity-ui/yagr/dist/react';
2
3
  import type { ChartKitWidgetRef } from '../../../types';
3
4
  import './polyfills';
4
5
  import '@gravity-ui/yagr/dist/index.css';
@@ -6,6 +7,7 @@ import './YagrWidget.css';
6
7
  declare const YagrWidget: React.ForwardRefExoticComponent<{
7
8
  type: "yagr";
8
9
  data: import("../types").YagrWidgetData;
10
+ pluginRef?: React.MutableRefObject<YagrReactRef | null> | undefined;
9
11
  id?: string | undefined;
10
12
  isMobile?: boolean | undefined;
11
13
  onLoad?: ((data?: import("../../../types").ChartKitOnLoadData<"yagr"> | undefined) => void) | undefined;
@@ -1,5 +1,6 @@
1
1
  import React from 'react';
2
2
  import isEmpty from 'lodash/isEmpty';
3
+ import { useForkRef } from '@gravity-ui/uikit';
3
4
  import YagrComponent from '@gravity-ui/yagr/dist/react';
4
5
  import { i18n } from '../../../i18n';
5
6
  import { CHARTKIT_ERROR_CODE, ChartKitError } from '../../../libs';
@@ -9,8 +10,9 @@ import './polyfills';
9
10
  import '@gravity-ui/yagr/dist/index.css';
10
11
  import './YagrWidget.css';
11
12
  const YagrWidget = React.forwardRef((props, forwardedRef) => {
12
- const { id, data: { data }, onLoad, onRender, onChartLoad, } = props;
13
+ const { id, data: { data }, pluginRef, onLoad, onRender, onChartLoad, } = props;
13
14
  const yagrRef = React.useRef(null);
15
+ const handleRef = useForkRef(pluginRef, yagrRef);
14
16
  if (!data || isEmpty(data)) {
15
17
  throw new ChartKitError({
16
18
  code: CHARTKIT_ERROR_CODE.NO_DATA,
@@ -21,18 +23,14 @@ const YagrWidget = React.forwardRef((props, forwardedRef) => {
21
23
  const handleChartLoading = React.useCallback((chart, { renderTime }) => {
22
24
  onLoad === null || onLoad === void 0 ? void 0 : onLoad(Object.assign(Object.assign({}, data), { widget: chart, widgetRendering: renderTime }));
23
25
  onRender === null || onRender === void 0 ? void 0 : onRender({ renderTime });
24
- }, [onLoad, data]);
26
+ }, [onLoad, onRender, data]);
25
27
  const onWindowResize = React.useCallback(() => {
26
28
  if (yagrRef.current) {
27
29
  const chart = yagrRef.current.yagr();
28
30
  if (!chart) {
29
31
  return;
30
32
  }
31
- const root = chart.root;
32
- const height = root.offsetHeight;
33
- const width = root.offsetWidth;
34
- chart.uplot.setSize({ width, height });
35
- chart.uplot.redraw();
33
+ chart.reflow();
36
34
  }
37
35
  }, []);
38
36
  React.useImperativeHandle(forwardedRef, () => ({
@@ -41,25 +39,28 @@ const YagrWidget = React.forwardRef((props, forwardedRef) => {
41
39
  },
42
40
  }), [onWindowResize]);
43
41
  React.useEffect(() => {
44
- var _a, _b, _c, _d;
42
+ var _a, _b, _c, _d, _e, _f;
45
43
  const yagr = (_a = yagrRef.current) === null || _a === void 0 ? void 0 : _a.yagr();
46
44
  if (!yagr) {
47
45
  return;
48
46
  }
47
+ if ((_c = (_b = yagr.config) === null || _b === void 0 ? void 0 : _b.tooltip) === null || _c === void 0 ? void 0 : _c.virtual) {
48
+ return;
49
+ }
49
50
  const handlers = {
50
51
  mouseMove: null,
51
52
  mouseDown: null,
52
53
  };
53
- (_b = yagr.plugins.tooltip) === null || _b === void 0 ? void 0 : _b.on('render', (tooltip) => {
54
+ (_d = yagr.plugins.tooltip) === null || _d === void 0 ? void 0 : _d.on('render', (tooltip) => {
54
55
  synchronizeTooltipTablesCellsWidth(tooltip);
55
56
  });
56
- (_c = yagr.plugins.tooltip) === null || _c === void 0 ? void 0 : _c.on('pin', (tooltip, { actions }) => {
57
+ (_e = yagr.plugins.tooltip) === null || _e === void 0 ? void 0 : _e.on('pin', (tooltip, { actions }) => {
57
58
  handlers.mouseMove = checkFocus({ tooltip, yagr });
58
59
  handlers.mouseDown = detectClickOutside({ tooltip, actions, yagr });
59
60
  document.addEventListener('mousemove', handlers.mouseMove);
60
61
  document.addEventListener('mousedown', handlers.mouseDown);
61
62
  });
62
- (_d = yagr.plugins.tooltip) === null || _d === void 0 ? void 0 : _d.on('unpin', () => {
63
+ (_f = yagr.plugins.tooltip) === null || _f === void 0 ? void 0 : _f.on('unpin', () => {
63
64
  if (handlers.mouseMove) {
64
65
  document.removeEventListener('mousemove', handlers.mouseMove);
65
66
  handlers.mouseMove = null;
@@ -74,6 +75,6 @@ const YagrWidget = React.forwardRef((props, forwardedRef) => {
74
75
  var _a;
75
76
  onChartLoad === null || onChartLoad === void 0 ? void 0 : onChartLoad({ widget: (_a = yagrRef.current) === null || _a === void 0 ? void 0 : _a.yagr() });
76
77
  }, [yagrRef, onChartLoad]);
77
- return (React.createElement(YagrComponent, { ref: yagrRef, id: id, config: config, debug: debug, onChartLoad: handleChartLoading }));
78
+ return (React.createElement(YagrComponent, { ref: handleRef, id: id, config: config, debug: debug, onChartLoad: handleChartLoading }));
78
79
  });
79
80
  export default YagrWidget;
@@ -1,4 +1,4 @@
1
- import moment from 'moment';
1
+ import { dateTime } from '@gravity-ui/date-utils';
2
2
  import { formatTooltip } from './tooltip';
3
3
  const calcOption = (d) => {
4
4
  return typeof d === 'object' && d !== null
@@ -39,7 +39,7 @@ export const renderTooltip = (data) => {
39
39
  : undefined;
40
40
  const tooltipFormatOptions = {
41
41
  activeRowAlwaysFirstInTooltip: rows.length > 1,
42
- tooltipHeader: moment(x / timeMultiplier).format('DD MMMM YYYY HH:mm:ss'),
42
+ tooltipHeader: dateTime({ input: x / timeMultiplier }).format('DD MMMM YYYY HH:mm:ss'),
43
43
  shared: true,
44
44
  lines: rows.map((row, i) => (Object.assign(Object.assign({}, row), { seriesName: row.name || 'Serie ' + (i + 1), seriesColor: row.color, selectedSeries: row.active, seriesIdx: row.seriesIdx, percentValue: typeof row.transformed === 'number' ? row.transformed.toFixed(1) : '' }))),
45
45
  withPercent: calcOption(opts.percent),
@@ -1,5 +1,5 @@
1
- import moment from 'moment';
2
1
  import merge from 'lodash/merge';
2
+ import { dateTime } from '@gravity-ui/date-utils';
3
3
  import { defaults } from '@gravity-ui/yagr';
4
4
  import { settings } from '../../../libs';
5
5
  import { renderTooltip } from './tooltip';
@@ -75,8 +75,8 @@ export const detectClickOutside = (args) => (event) => {
75
75
  const getXAxisFormatter = (msm = 1) => (_, ticks) => {
76
76
  const range = (ticks[ticks.length - 1] - ticks[0]) / msm;
77
77
  return ticks.map((rawValue) => {
78
- const d = moment(rawValue / msm);
79
- if (d.hour() === 0 && d.minutes() === 0 && d.seconds() === 0) {
78
+ const d = dateTime({ input: rawValue / msm });
79
+ if (d.hour() === 0 && d.minute() === 0 && d.second() === 0) {
80
80
  return d.format('DD.MM.YY');
81
81
  }
82
82
  return d.format(range < 300 ? 'HH:mm:ss' : 'HH:mm');
@@ -1,5 +1,6 @@
1
1
  import type { RawSerieData, YagrConfig } from '@gravity-ui/yagr';
2
2
  export type { default as Yagr } from '@gravity-ui/yagr';
3
+ export type { YagrReactRef } from '@gravity-ui/yagr/dist/react';
3
4
  export * from '@gravity-ui/yagr/dist/types';
4
5
  export type YagrWidgetData = {
5
6
  data: {
@@ -26,6 +26,8 @@ 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'];
29
31
  id?: string;
30
32
  isMobile?: boolean;
31
33
  onLoad?: (data?: ChartKitOnLoadData<T>) => void;
@@ -40,7 +42,7 @@ export type ChartKitProps<T extends ChartKitType> = {
40
42
  /** Used to render user's plugin loader component */
41
43
  renderPluginLoader?: () => React.ReactNode;
42
44
  } & {
43
- [key in keyof Omit<ChartKitWidget[T], 'data' | 'widget'>]: ChartKitWidget[T][key];
45
+ [key in keyof Omit<ChartKitWidget[T], 'data' | 'widget' | 'pluginRef'>]: ChartKitWidget[T][key];
44
46
  };
45
47
  export type ChartKitPlugin = {
46
48
  type: ChartKitType;
@@ -1,19 +1,23 @@
1
- import type { Yagr, YagrWidgetData } from '../plugins/yagr/types';
1
+ /// <reference types="react" />
2
+ import type { Yagr, YagrReactRef, YagrWidgetData } from '../plugins/yagr/types';
2
3
  import type { IndicatorWidgetData } from '../plugins/indicator/types';
3
4
  import type { Highcharts, HighchartsWidgetData, StringParams } from '../plugins/highcharts/types';
4
5
  export interface ChartKitWidget {
5
6
  yagr: {
6
7
  data: YagrWidgetData;
7
8
  widget: Yagr;
9
+ pluginRef: React.MutableRefObject<YagrReactRef | null>;
8
10
  };
9
11
  indicator: {
10
12
  data: IndicatorWidgetData;
11
13
  widget: never;
14
+ pluginRef: never;
12
15
  formatNumber?: <T = any>(value: number, options?: T) => string;
13
16
  };
14
17
  highcharts: {
15
18
  data: HighchartsWidgetData;
16
19
  widget: Highcharts.Chart | null;
20
+ pluginRef: never;
17
21
  hoistConfigError?: boolean;
18
22
  nonBodyScroll?: boolean;
19
23
  splitTooltip?: boolean;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gravity-ui/chartkit",
3
- "version": "3.0.0-beta.6",
3
+ "version": "3.0.0-beta.8",
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",
@@ -13,6 +13,7 @@
13
13
  "access": "public"
14
14
  },
15
15
  "dependencies": {
16
+ "@gravity-ui/date-utils": "^1.4.1",
16
17
  "@gravity-ui/yagr": "^3.3.5",
17
18
  "bem-cn-lite": "^4.1.0",
18
19
  "highcharts": "^8.2.2",
@@ -54,7 +55,6 @@
54
55
  "jest": "^28.1.3",
55
56
  "jest-environment-jsdom": "^28.1.2",
56
57
  "lint-staged": "^10.2.7",
57
- "moment": "^2.29.4",
58
58
  "npm-run-all": "^4.1.5",
59
59
  "prettier": "^2.6.0",
60
60
  "react": "^17.0.2",
@@ -72,7 +72,6 @@
72
72
  },
73
73
  "peerDependencies": {
74
74
  "@gravity-ui/uikit": "^4.0.0",
75
- "moment": "^2.19.3",
76
75
  "react": "^16.0.0 || ^17.0.0 || ^18.0.0"
77
76
  },
78
77
  "scripts": {