@gravity-ui/chartkit 4.18.2 → 4.19.1

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.
@@ -1 +1 @@
1
- export { renderTooltip } from './renderTooltip';
1
+ export { getRenderTooltip } from './renderTooltip';
@@ -1 +1 @@
1
- export { renderTooltip } from './renderTooltip';
1
+ export { getRenderTooltip } from './renderTooltip';
@@ -1,2 +1,2 @@
1
1
  import type { TooltipRenderOptions } from '../../types';
2
- export declare const renderTooltip: (data: TooltipRenderOptions) => string;
2
+ export declare const getRenderTooltip: (timeZone?: string) => (data: TooltipRenderOptions) => string;
@@ -12,7 +12,7 @@ const calcOption = (d) => {
12
12
  * Adapter between native Yagr tooltip config and ChartKit
13
13
  * tooltip renderer.
14
14
  */
15
- export const renderTooltip = (data) => {
15
+ export const getRenderTooltip = (timeZone) => (data) => {
16
16
  const cfg = data.yagr.config;
17
17
  const timeMultiplier = cfg.chart.timeMultiplier || 1;
18
18
  const opts = data.options;
@@ -39,7 +39,7 @@ export const renderTooltip = (data) => {
39
39
  : undefined;
40
40
  const tooltipFormatOptions = {
41
41
  activeRowAlwaysFirstInTooltip: rows.length > 1,
42
- tooltipHeader: dateTime({ input: x / timeMultiplier }).format('DD MMMM YYYY HH:mm:ss'),
42
+ tooltipHeader: dateTime({ input: x / timeMultiplier, timeZone }).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,4 +1,4 @@
1
- import type { Yagr, YagrWidgetData, YagrTheme, MinimalValidConfig } from '../types';
1
+ import type { Yagr, YagrWidgetData, YagrTheme, YagrChartOptions, MinimalValidConfig } from '../types';
2
2
  type ShapeYagrConfigArgs = {
3
3
  data: YagrWidgetData['data'];
4
4
  libraryConfig: YagrWidgetData['libraryConfig'];
@@ -18,5 +18,12 @@ export declare const detectClickOutside: (args: {
18
18
  };
19
19
  yagr?: Yagr<MinimalValidConfig> | undefined;
20
20
  }) => (event: MouseEvent) => void;
21
+ /**
22
+ * This function needs to align timezone that uplot is processing.
23
+ * Uplot uses simple new Date() when [processing ticks](https://github.com/leeoniya/uPlot/blob/master/src/opts.js#L177) on axis.
24
+ * It leads that timestamp will be converted to user browser timezone.
25
+ * In this function we artificially add shift diff between browser timezone and user timeozne to reset new Date() affects.
26
+ */
27
+ export declare const getUplotTimezoneAligner: (chart?: YagrChartOptions, timeZone?: string) => (ts: number) => Date;
21
28
  export declare const shapeYagrConfig: (args: ShapeYagrConfigArgs) => MinimalValidConfig;
22
29
  export {};
@@ -2,7 +2,7 @@ import merge from 'lodash/merge';
2
2
  import { dateTime } from '@gravity-ui/date-utils';
3
3
  import { defaults } from '@gravity-ui/yagr';
4
4
  import { settings } from '../../../libs';
5
- import { renderTooltip } from './tooltip';
5
+ import { getRenderTooltip } from './tooltip';
6
6
  const TOOLTIP_HEADER_CLASS_NAME = '_tooltip-header';
7
7
  const TOOLTIP_LIST_CLASS_NAME = '_tooltip-list';
8
8
  export const synchronizeTooltipTablesCellsWidth = (tooltipContainer) => {
@@ -72,20 +72,35 @@ export const detectClickOutside = (args) => (event) => {
72
72
  }
73
73
  }
74
74
  };
75
- const getXAxisFormatter = (msm = 1) => (_, ticks) => {
75
+ const getXAxisFormatter = (msm = 1, timeZone) => (_, ticks) => {
76
76
  const range = (ticks[ticks.length - 1] - ticks[0]) / msm;
77
77
  return ticks.map((rawValue) => {
78
- const d = dateTime({ input: rawValue / msm });
78
+ const d = dateTime({ input: rawValue / msm, timeZone });
79
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');
83
83
  });
84
84
  };
85
+ /**
86
+ * This function needs to align timezone that uplot is processing.
87
+ * Uplot uses simple new Date() when [processing ticks](https://github.com/leeoniya/uPlot/blob/master/src/opts.js#L177) on axis.
88
+ * It leads that timestamp will be converted to user browser timezone.
89
+ * In this function we artificially add shift diff between browser timezone and user timeozne to reset new Date() affects.
90
+ */
91
+ export const getUplotTimezoneAligner = (chart, timeZone) => (ts) => {
92
+ const dt = ts / ((chart === null || chart === void 0 ? void 0 : chart.timeMultiplier) || 1);
93
+ const browserDate = dateTime({ input: dt });
94
+ const browserTimezone = browserDate.utcOffset();
95
+ const timestampRealTimezone = dateTime({ input: dt, timeZone }).utcOffset();
96
+ const uPlotOffset = (browserTimezone - timestampRealTimezone) * 60 * 1000;
97
+ return new Date(browserDate.valueOf() + uPlotOffset);
98
+ };
85
99
  export const shapeYagrConfig = (args) => {
86
100
  var _a, _b;
87
101
  const { data, libraryConfig, theme } = args;
88
102
  const config = Object.assign(Object.assign({}, libraryConfig), { timeline: data.timeline, series: data.graphs });
103
+ const { timeZone } = data;
89
104
  const chart = {
90
105
  appearance: {
91
106
  locale: settings.get('lang'),
@@ -96,7 +111,7 @@ export const shapeYagrConfig = (args) => {
96
111
  config.chart = chart;
97
112
  if ((_a = config.tooltip) === null || _a === void 0 ? void 0 : _a.show) {
98
113
  config.tooltip = config.tooltip || {};
99
- config.tooltip.render = ((_b = config.tooltip) === null || _b === void 0 ? void 0 : _b.render) || renderTooltip;
114
+ config.tooltip.render = ((_b = config.tooltip) === null || _b === void 0 ? void 0 : _b.render) || getRenderTooltip(timeZone);
100
115
  if (!config.tooltip.className) {
101
116
  // "className" property prevent default yagr styles adding
102
117
  config.tooltip.className = 'chartkit-yagr-tooltip';
@@ -113,12 +128,13 @@ export const shapeYagrConfig = (args) => {
113
128
  }
114
129
  config.axes = config.axes || {};
115
130
  const xAxis = config.axes[defaults.DEFAULT_X_SCALE];
131
+ config.editUplotOptions = (opts) => (Object.assign(Object.assign({}, opts), { tzDate: timeZone ? getUplotTimezoneAligner(config.chart, timeZone) : undefined }));
116
132
  if (xAxis && !xAxis.values) {
117
- xAxis.values = getXAxisFormatter(config.chart.timeMultiplier);
133
+ xAxis.values = getXAxisFormatter(config.chart.timeMultiplier, timeZone);
118
134
  }
119
135
  if (!xAxis) {
120
136
  config.axes[defaults.DEFAULT_X_SCALE] = {
121
- values: getXAxisFormatter(config.chart.timeMultiplier),
137
+ values: getXAxisFormatter(config.chart.timeMultiplier, timeZone),
122
138
  };
123
139
  }
124
140
  return config;
@@ -14,6 +14,14 @@ export type YagrWidgetData = {
14
14
  data: {
15
15
  graphs: RawSerieData[];
16
16
  timeline: number[];
17
+ /**
18
+ * Allow to setup timezone for X axis and tooltip's header.
19
+ *
20
+ * Format example: "UTC", "Europe/Moscow".
21
+ *
22
+ * For more examples check [wiki](https://en.wikipedia.org/wiki/List_of_tz_database_time_zones#List)
23
+ */
24
+ timeZone?: string;
17
25
  };
18
26
  libraryConfig: Partial<YagrConfig>;
19
27
  sources?: Record<number, {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gravity-ui/chartkit",
3
- "version": "4.18.2",
3
+ "version": "4.19.1",
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",
@@ -47,7 +47,7 @@
47
47
  ],
48
48
  "dependencies": {
49
49
  "@bem-react/classname": "^1.6.0",
50
- "@gravity-ui/date-utils": "^1.4.1",
50
+ "@gravity-ui/date-utils": "^1.4.2",
51
51
  "@gravity-ui/yagr": "^4.2.3",
52
52
  "afterframe": "^1.0.2",
53
53
  "d3": "^7.8.5",