@gravity-ui/charts 1.35.1 → 1.36.0
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.
- package/dist/cjs/components/AxisX/AxisX.js +18 -8
- package/dist/cjs/components/AxisX/prepare-axis-data.js +12 -0
- package/dist/cjs/components/AxisX/styles.css +7 -2
- package/dist/cjs/components/AxisX/types.d.ts +5 -0
- package/dist/cjs/components/AxisY/AxisY.js +16 -8
- package/dist/cjs/components/AxisY/prepare-axis-data.js +14 -0
- package/dist/cjs/components/AxisY/styles.css +7 -2
- package/dist/cjs/components/AxisY/types.d.ts +5 -0
- package/dist/cjs/components/ChartInner/index.js +2 -0
- package/dist/cjs/components/ChartInner/styles.css +1 -0
- package/dist/cjs/components/ChartInner/useChartInnerHandlers.d.ts +3 -0
- package/dist/cjs/components/ChartInner/useChartInnerHandlers.js +26 -3
- package/dist/cjs/components/ChartInner/useChartInnerProps.d.ts +1 -0
- package/dist/cjs/components/Tooltip/ChartTooltipContent.d.ts +3 -1
- package/dist/cjs/components/Tooltip/ChartTooltipContent.js +2 -2
- package/dist/cjs/components/Tooltip/DefaultTooltipContent/Row.js +1 -1
- package/dist/cjs/components/Tooltip/index.js +5 -2
- package/dist/cjs/components/Tooltip/styles.css +7 -0
- package/dist/cjs/constants/datetime.d.ts +8 -0
- package/dist/cjs/constants/datetime.js +8 -0
- package/dist/cjs/constants/defaults/axis.d.ts +4 -0
- package/dist/cjs/constants/defaults/axis.js +4 -0
- package/dist/cjs/constants/index.d.ts +1 -1
- package/dist/cjs/constants/index.js +1 -1
- package/dist/cjs/hooks/useAxis/types.d.ts +8 -1
- package/dist/cjs/hooks/useAxis/utils.d.ts +1 -1
- package/dist/cjs/hooks/useAxis/x-axis.js +9 -3
- package/dist/cjs/hooks/useAxis/y-axis.js +7 -1
- package/dist/cjs/hooks/useAxisScales/x-scale.d.ts +1 -1
- package/dist/cjs/hooks/useAxisScales/y-scale.d.ts +1 -1
- package/dist/cjs/hooks/useNormalizedOriginalData/index.d.ts +1 -0
- package/dist/cjs/hooks/useTooltip/index.d.ts +3 -1
- package/dist/cjs/hooks/useTooltip/index.js +7 -3
- package/dist/cjs/types/chart/axis.d.ts +17 -3
- package/dist/cjs/types/chart/base.d.ts +1 -1
- package/dist/cjs/types/chart/tooltip.d.ts +5 -1
- package/dist/cjs/utils/chart/axis/common.d.ts +1 -3
- package/dist/cjs/utils/chart/axis/common.js +2 -10
- package/dist/cjs/utils/chart/axis/x-axis.js +3 -2
- package/dist/cjs/utils/chart/format.js +17 -2
- package/dist/cjs/utils/chart/get-hovered-plots.d.ts +14 -0
- package/dist/cjs/utils/chart/get-hovered-plots.js +67 -0
- package/dist/cjs/utils/chart/ticks/datetime.d.ts +8 -0
- package/dist/cjs/utils/chart/ticks/datetime.js +50 -0
- package/dist/cjs/utils/chart/ticks/index.d.ts +6 -0
- package/dist/cjs/utils/chart/ticks/index.js +17 -0
- package/dist/cjs/utils/chart/time.d.ts +1 -0
- package/dist/cjs/utils/chart/time.js +9 -0
- package/dist/esm/components/AxisX/AxisX.js +18 -8
- package/dist/esm/components/AxisX/prepare-axis-data.js +12 -0
- package/dist/esm/components/AxisX/styles.css +7 -2
- package/dist/esm/components/AxisX/types.d.ts +5 -0
- package/dist/esm/components/AxisY/AxisY.js +16 -8
- package/dist/esm/components/AxisY/prepare-axis-data.js +14 -0
- package/dist/esm/components/AxisY/styles.css +7 -2
- package/dist/esm/components/AxisY/types.d.ts +5 -0
- package/dist/esm/components/ChartInner/index.js +2 -0
- package/dist/esm/components/ChartInner/styles.css +1 -0
- package/dist/esm/components/ChartInner/useChartInnerHandlers.d.ts +3 -0
- package/dist/esm/components/ChartInner/useChartInnerHandlers.js +26 -3
- package/dist/esm/components/ChartInner/useChartInnerProps.d.ts +1 -0
- package/dist/esm/components/Tooltip/ChartTooltipContent.d.ts +3 -1
- package/dist/esm/components/Tooltip/ChartTooltipContent.js +2 -2
- package/dist/esm/components/Tooltip/DefaultTooltipContent/Row.js +1 -1
- package/dist/esm/components/Tooltip/index.js +5 -2
- package/dist/esm/components/Tooltip/styles.css +7 -0
- package/dist/esm/constants/datetime.d.ts +8 -0
- package/dist/esm/constants/datetime.js +8 -0
- package/dist/esm/constants/defaults/axis.d.ts +4 -0
- package/dist/esm/constants/defaults/axis.js +4 -0
- package/dist/esm/constants/index.d.ts +1 -1
- package/dist/esm/constants/index.js +1 -1
- package/dist/esm/hooks/useAxis/types.d.ts +8 -1
- package/dist/esm/hooks/useAxis/utils.d.ts +1 -1
- package/dist/esm/hooks/useAxis/x-axis.js +9 -3
- package/dist/esm/hooks/useAxis/y-axis.js +7 -1
- package/dist/esm/hooks/useAxisScales/x-scale.d.ts +1 -1
- package/dist/esm/hooks/useAxisScales/y-scale.d.ts +1 -1
- package/dist/esm/hooks/useNormalizedOriginalData/index.d.ts +1 -0
- package/dist/esm/hooks/useTooltip/index.d.ts +3 -1
- package/dist/esm/hooks/useTooltip/index.js +7 -3
- package/dist/esm/types/chart/axis.d.ts +17 -3
- package/dist/esm/types/chart/base.d.ts +1 -1
- package/dist/esm/types/chart/tooltip.d.ts +5 -1
- package/dist/esm/utils/chart/axis/common.d.ts +1 -3
- package/dist/esm/utils/chart/axis/common.js +2 -10
- package/dist/esm/utils/chart/axis/x-axis.js +3 -2
- package/dist/esm/utils/chart/format.js +17 -2
- package/dist/esm/utils/chart/get-hovered-plots.d.ts +14 -0
- package/dist/esm/utils/chart/get-hovered-plots.js +67 -0
- package/dist/esm/utils/chart/ticks/datetime.d.ts +8 -0
- package/dist/esm/utils/chart/ticks/datetime.js +50 -0
- package/dist/esm/utils/chart/ticks/index.d.ts +6 -0
- package/dist/esm/utils/chart/ticks/index.js +17 -0
- package/dist/esm/utils/chart/time.d.ts +1 -0
- package/dist/esm/utils/chart/time.js +9 -0
- package/package.json +1 -1
- package/dist/cjs/constants/date.d.ts +0 -1
- package/dist/cjs/constants/date.js +0 -1
- package/dist/esm/constants/date.d.ts +0 -1
- package/dist/esm/constants/date.js +0 -1
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import type { DurationInput } from '@gravity-ui/date-utils';
|
|
2
2
|
import type { AXIS_TYPE, DashStyle } from '../../constants';
|
|
3
3
|
import type { FormatNumberOptions } from '../formatter';
|
|
4
|
+
import type { MeaningfulAny } from '../misc';
|
|
4
5
|
import type { BaseTextStyle } from './base';
|
|
5
6
|
import type { ChartBrush } from './brush';
|
|
6
7
|
export type ChartAxisType = (typeof AXIS_TYPE)[keyof typeof AXIS_TYPE];
|
|
@@ -94,6 +95,16 @@ export interface ChartAxisTitle {
|
|
|
94
95
|
*/
|
|
95
96
|
maxRowCount?: number;
|
|
96
97
|
}
|
|
98
|
+
export interface ChartAxisTickMarks {
|
|
99
|
+
/** Enable or disable the tick marks on the axis.
|
|
100
|
+
* @default false
|
|
101
|
+
*/
|
|
102
|
+
enabled?: boolean;
|
|
103
|
+
/** The length of the tick marks in pixels (perpendicular to the axis).
|
|
104
|
+
* @default 6
|
|
105
|
+
*/
|
|
106
|
+
length?: number;
|
|
107
|
+
}
|
|
97
108
|
export interface ChartAxis {
|
|
98
109
|
categories?: string[];
|
|
99
110
|
/** Configure a crosshair that follows either the mouse pointer or the hovered point. */
|
|
@@ -161,6 +172,8 @@ export interface ChartAxis {
|
|
|
161
172
|
plotLines?: AxisPlotLine[];
|
|
162
173
|
/** An array of colored bands stretching across the plot area marking an interval on the axis. */
|
|
163
174
|
plotBands?: AxisPlotBand[];
|
|
175
|
+
/** Small perpendicular marks on the axis line at each tick position. */
|
|
176
|
+
tickMarks?: ChartAxisTickMarks;
|
|
164
177
|
/** Whether axis, including axis title, line, ticks and labels, should be visible. */
|
|
165
178
|
visible?: boolean;
|
|
166
179
|
/** Setting the order of the axis values. It is not applied by default.
|
|
@@ -226,6 +239,8 @@ export interface AxisPlot {
|
|
|
226
239
|
* It is assigned as a data-qa attribute to an element. */
|
|
227
240
|
qa?: string;
|
|
228
241
|
};
|
|
242
|
+
/** Custom data associated with the plot line/band, accessible in tooltip renderer args. */
|
|
243
|
+
custom?: MeaningfulAny;
|
|
229
244
|
}
|
|
230
245
|
export interface AxisPlotLine extends AxisPlot {
|
|
231
246
|
/** The position of the line in axis units. */
|
|
@@ -259,7 +274,7 @@ export interface AxisPlotBand extends AxisPlot {
|
|
|
259
274
|
*/
|
|
260
275
|
to: number | string | null;
|
|
261
276
|
}
|
|
262
|
-
export interface AxisCrosshair extends Omit<AxisPlotLine, 'value' | 'label'> {
|
|
277
|
+
export interface AxisCrosshair extends Omit<AxisPlotLine, 'value' | 'label' | 'custom'> {
|
|
263
278
|
/** Whether the crosshair should snap to the point or follow the pointer independent of points.
|
|
264
279
|
* @default true
|
|
265
280
|
*/
|
|
@@ -269,7 +284,7 @@ export interface AxisCrosshair extends Omit<AxisPlotLine, 'value' | 'label'> {
|
|
|
269
284
|
*/
|
|
270
285
|
enabled?: boolean;
|
|
271
286
|
}
|
|
272
|
-
interface ChartYAxisTitle extends ChartAxisTitle {
|
|
287
|
+
export interface ChartYAxisTitle extends ChartAxisTitle {
|
|
273
288
|
/** Rotation of the title in degrees.
|
|
274
289
|
* Currently, the available values are only for rotation in multiples of 90 degrees.
|
|
275
290
|
*
|
|
@@ -297,4 +312,3 @@ export interface ChartYAxis extends ChartAxis {
|
|
|
297
312
|
plotIndex?: number;
|
|
298
313
|
title?: ChartYAxisTitle;
|
|
299
314
|
}
|
|
300
|
-
export {};
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { TOOLTIP_TOTALS_BUILT_IN_AGGREGATION } from '../../constants';
|
|
2
2
|
import type { MeaningfulAny } from '../misc';
|
|
3
3
|
import type { AreaSeries, AreaSeriesData } from './area';
|
|
4
|
-
import type { ChartXAxis, ChartYAxis } from './axis';
|
|
4
|
+
import type { AxisPlotBand, AxisPlotLine, ChartXAxis, ChartYAxis } from './axis';
|
|
5
5
|
import type { BarXSeries, BarXSeriesData } from './bar-x';
|
|
6
6
|
import type { BarYSeries, BarYSeriesData } from './bar-y';
|
|
7
7
|
import type { CustomFormat, ValueFormat } from './base';
|
|
@@ -91,6 +91,10 @@ export type TooltipDataChunk<T = MeaningfulAny> = (TooltipDataChunkBarX<T> | Too
|
|
|
91
91
|
};
|
|
92
92
|
export interface ChartTooltipRendererArgs<T = MeaningfulAny> {
|
|
93
93
|
hovered: TooltipDataChunk<T>[];
|
|
94
|
+
/** Plot lines that intersect with the current pointer position. */
|
|
95
|
+
hoveredPlotLines?: AxisPlotLine[];
|
|
96
|
+
/** Plot bands that contain the current pointer position. */
|
|
97
|
+
hoveredPlotBands?: AxisPlotBand[];
|
|
94
98
|
xAxis?: ChartXAxis | null;
|
|
95
99
|
yAxis?: ChartYAxis;
|
|
96
100
|
}
|
|
@@ -2,14 +2,12 @@ import type { AxisDomain, AxisScale, ScaleBand, ScaleTime } from 'd3';
|
|
|
2
2
|
import type { ChartScale, PreparedAxis, PreparedAxisPlotBand, PreparedSplit } from '../../../hooks';
|
|
3
3
|
import type { ChartAxis } from '../../../types';
|
|
4
4
|
import type { AxisDirection } from '../types';
|
|
5
|
-
type Ticks = number[] | string[] | Date[];
|
|
6
5
|
export declare function getTicksCountByPixelInterval({ axis, axisWidth, }: {
|
|
7
6
|
axis: PreparedAxis;
|
|
8
7
|
axisWidth: number;
|
|
9
8
|
}): number | undefined;
|
|
10
9
|
export declare function isBandScale(scale?: ChartScale | AxisScale<AxisDomain>): scale is ScaleBand<string>;
|
|
11
10
|
export declare function isTimeScale(scale?: ChartScale | AxisScale<AxisDomain>): scale is ScaleTime<number, number>;
|
|
12
|
-
export declare function getScaleTicks(scale: ChartScale | AxisScale<AxisDomain>, ticksCount?: number): Ticks;
|
|
13
11
|
export declare function getXAxisOffset(): 0 | 0.5;
|
|
14
12
|
export declare function getXTickPosition({ scale, offset }: {
|
|
15
13
|
scale: AxisScale<AxisDomain>;
|
|
@@ -19,7 +17,7 @@ export declare function getAxisItems({ scale, count, maxCount, }: {
|
|
|
19
17
|
scale: ChartScale | AxisScale<AxisDomain>;
|
|
20
18
|
count?: number;
|
|
21
19
|
maxCount?: number;
|
|
22
|
-
}):
|
|
20
|
+
}): number[] | string[] | Date[];
|
|
23
21
|
export declare function getAxisHeight(args: {
|
|
24
22
|
split: PreparedSplit;
|
|
25
23
|
boundsHeight: number;
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { ascending, descending, reverse, sort } from 'd3';
|
|
2
2
|
import clamp from 'lodash/clamp';
|
|
3
|
+
import { getScaleTicks } from '../ticks';
|
|
3
4
|
export function getTicksCountByPixelInterval({ axis, axisWidth, }) {
|
|
4
5
|
let ticksCount;
|
|
5
6
|
if (axis.ticks.pixelInterval) {
|
|
@@ -16,15 +17,6 @@ export function isTimeScale(scale) {
|
|
|
16
17
|
}
|
|
17
18
|
return scale.domain()[0] instanceof Date;
|
|
18
19
|
}
|
|
19
|
-
export function getScaleTicks(scale, ticksCount) {
|
|
20
|
-
if ('ticks' in scale && typeof scale.ticks === 'function') {
|
|
21
|
-
return scale.ticks(ticksCount);
|
|
22
|
-
}
|
|
23
|
-
if (isBandScale(scale)) {
|
|
24
|
-
return scale.domain();
|
|
25
|
-
}
|
|
26
|
-
return [];
|
|
27
|
-
}
|
|
28
20
|
export function getXAxisOffset() {
|
|
29
21
|
return typeof window !== 'undefined' && window.devicePixelRatio > 1 ? 0 : 0.5;
|
|
30
22
|
}
|
|
@@ -42,7 +34,7 @@ export function getXTickPosition({ scale, offset }) {
|
|
|
42
34
|
return isBandScale(scale) ? center(scale, offset) : number(scale);
|
|
43
35
|
}
|
|
44
36
|
export function getAxisItems({ scale, count, maxCount, }) {
|
|
45
|
-
let values = getScaleTicks(scale, count);
|
|
37
|
+
let values = getScaleTicks({ scale, ticksCount: count });
|
|
46
38
|
if (maxCount && values.length > maxCount) {
|
|
47
39
|
const step = Math.ceil(values.length / maxCount);
|
|
48
40
|
values = values.filter((_, i) => i % step === 0);
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { getMinSpaceBetween } from '../array';
|
|
2
2
|
import { isSeriesWithNumericalXValues } from '../series-type-guards';
|
|
3
|
+
import { getScaleTicks } from '../ticks';
|
|
3
4
|
import { getTicksCountByPixelInterval, isBandScale, thinOut } from './common';
|
|
4
5
|
const DEFAULT_TICKS_COUNT = 10;
|
|
5
6
|
function getTicksCount(args) {
|
|
@@ -29,7 +30,7 @@ export function getXAxisTickValues({ axis, labelLineHeight, scale, series, }) {
|
|
|
29
30
|
return [];
|
|
30
31
|
}
|
|
31
32
|
const scaleTicksCount = getTicksCount({ axis, axisWidth, series });
|
|
32
|
-
const scaleTicks = scale
|
|
33
|
+
const scaleTicks = getScaleTicks({ scale, ticksCount: scaleTicksCount });
|
|
33
34
|
const originalTickValues = scaleTicks.map((t) => ({
|
|
34
35
|
x: scale(t),
|
|
35
36
|
value: t,
|
|
@@ -43,7 +44,7 @@ export function getXAxisTickValues({ axis, labelLineHeight, scale, series, }) {
|
|
|
43
44
|
let ticksCount = result.length - 1;
|
|
44
45
|
while (availableSpaceForLabel < labelLineHeight && result.length > 1) {
|
|
45
46
|
ticksCount = ticksCount ? ticksCount - 1 : result.length - 1;
|
|
46
|
-
const newScaleTicks = scale
|
|
47
|
+
const newScaleTicks = getScaleTicks({ scale, ticksCount });
|
|
47
48
|
result = newScaleTicks.map((t) => ({
|
|
48
49
|
x: scale(t),
|
|
49
50
|
value: t,
|
|
@@ -2,7 +2,7 @@ import { dateTimeUtc } from '@gravity-ui/date-utils';
|
|
|
2
2
|
import capitalize from 'lodash/capitalize';
|
|
3
3
|
import { DEFAULT_DATE_FORMAT } from '../../constants';
|
|
4
4
|
import { formatNumber, getDefaultUnit } from '../../libs';
|
|
5
|
-
import { getDefaultDateFormat } from './time';
|
|
5
|
+
import { DATETIME_LABEL_FORMATS, TIME_UNITS, getDefaultDateFormat, getDefaultTimeOnlyFormat, } from './time';
|
|
6
6
|
const LETTER_MOUNTH_AT_START_FORMAT_REGEXP = /^M{3,}/;
|
|
7
7
|
function getFormattedDate(args) {
|
|
8
8
|
const { value, format = DEFAULT_DATE_FORMAT } = args;
|
|
@@ -43,7 +43,22 @@ export function formatAxisTickLabel(args) {
|
|
|
43
43
|
}
|
|
44
44
|
case 'datetime': {
|
|
45
45
|
const date = value;
|
|
46
|
-
|
|
46
|
+
let format;
|
|
47
|
+
if (axis.labels.dateFormat) {
|
|
48
|
+
format = axis.labels.dateFormat;
|
|
49
|
+
}
|
|
50
|
+
else if (step !== undefined && step < TIME_UNITS.day) {
|
|
51
|
+
const d = dateTimeUtc({ input: date });
|
|
52
|
+
const isMidnight = d.isValid() &&
|
|
53
|
+
d.hour() === 0 &&
|
|
54
|
+
d.minute() === 0 &&
|
|
55
|
+
d.second() === 0 &&
|
|
56
|
+
d.millisecond() === 0;
|
|
57
|
+
format = isMidnight ? DATETIME_LABEL_FORMATS.day : getDefaultTimeOnlyFormat(step);
|
|
58
|
+
}
|
|
59
|
+
else {
|
|
60
|
+
format = getDefaultDateFormat(step);
|
|
61
|
+
}
|
|
47
62
|
return getFormattedDate({ value: date, format });
|
|
48
63
|
}
|
|
49
64
|
case 'linear':
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { PreparedXAxis, PreparedYAxis } from '../../hooks';
|
|
2
|
+
import type { ChartScale } from '../../hooks/useAxisScales/types';
|
|
3
|
+
import type { AxisPlotBand, AxisPlotLine } from '../../types';
|
|
4
|
+
export declare function getHoveredPlots(args: {
|
|
5
|
+
pointerX: number;
|
|
6
|
+
pointerY: number;
|
|
7
|
+
xAxis?: PreparedXAxis | null;
|
|
8
|
+
yAxis: PreparedYAxis[];
|
|
9
|
+
xScale?: ChartScale;
|
|
10
|
+
yScale?: (ChartScale | undefined)[];
|
|
11
|
+
}): {
|
|
12
|
+
plotLines: AxisPlotLine[];
|
|
13
|
+
plotBands: AxisPlotBand[];
|
|
14
|
+
};
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { getBandsPosition, isBandScale } from './axis/common';
|
|
2
|
+
const PLOT_LINE_HIT_THRESHOLD_PX = 4;
|
|
3
|
+
function getHoveredAxisPlotBands(args) {
|
|
4
|
+
const { pointerPx, plotBands, scale, axis } = args;
|
|
5
|
+
const axisScale = scale;
|
|
6
|
+
const result = [];
|
|
7
|
+
for (const band of plotBands) {
|
|
8
|
+
const { from, to } = getBandsPosition({ band, axisScale, axis });
|
|
9
|
+
const startPx = Math.min(from, to);
|
|
10
|
+
const endPx = Math.max(from, to);
|
|
11
|
+
if (pointerPx >= startPx && pointerPx <= endPx) {
|
|
12
|
+
result.push(band);
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
return result;
|
|
16
|
+
}
|
|
17
|
+
function getHoveredAxisPlotLines(args) {
|
|
18
|
+
const { pointerPx, plotLines, scale } = args;
|
|
19
|
+
const result = [];
|
|
20
|
+
if (isBandScale(scale)) {
|
|
21
|
+
return result;
|
|
22
|
+
}
|
|
23
|
+
for (const line of plotLines) {
|
|
24
|
+
const linePx = Number(scale(line.value));
|
|
25
|
+
if (Math.abs(pointerPx - linePx) <= PLOT_LINE_HIT_THRESHOLD_PX + line.width / 2) {
|
|
26
|
+
result.push(line);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
return result;
|
|
30
|
+
}
|
|
31
|
+
export function getHoveredPlots(args) {
|
|
32
|
+
const { pointerX, pointerY, xAxis, yAxis, xScale, yScale } = args;
|
|
33
|
+
const plotLines = [];
|
|
34
|
+
const plotBands = [];
|
|
35
|
+
if (xAxis && xScale) {
|
|
36
|
+
plotBands.push(...getHoveredAxisPlotBands({
|
|
37
|
+
pointerPx: pointerX,
|
|
38
|
+
plotBands: xAxis.plotBands,
|
|
39
|
+
scale: xScale,
|
|
40
|
+
axis: 'x',
|
|
41
|
+
}));
|
|
42
|
+
plotLines.push(...getHoveredAxisPlotLines({
|
|
43
|
+
pointerPx: pointerX,
|
|
44
|
+
plotLines: xAxis.plotLines,
|
|
45
|
+
scale: xScale,
|
|
46
|
+
}));
|
|
47
|
+
}
|
|
48
|
+
for (let i = 0; i < yAxis.length; i++) {
|
|
49
|
+
const yAxisItem = yAxis[i];
|
|
50
|
+
const yScaleItem = yScale === null || yScale === void 0 ? void 0 : yScale[i];
|
|
51
|
+
if (!yAxisItem || !yScaleItem) {
|
|
52
|
+
continue;
|
|
53
|
+
}
|
|
54
|
+
plotBands.push(...getHoveredAxisPlotBands({
|
|
55
|
+
pointerPx: pointerY,
|
|
56
|
+
plotBands: yAxisItem.plotBands,
|
|
57
|
+
scale: yScaleItem,
|
|
58
|
+
axis: 'y',
|
|
59
|
+
}));
|
|
60
|
+
plotLines.push(...getHoveredAxisPlotLines({
|
|
61
|
+
pointerPx: pointerY,
|
|
62
|
+
plotLines: yAxisItem.plotLines,
|
|
63
|
+
scale: yScaleItem,
|
|
64
|
+
}));
|
|
65
|
+
}
|
|
66
|
+
return { plotLines, plotBands };
|
|
67
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Generates time ticks for the given interval.
|
|
3
|
+
*
|
|
4
|
+
* Based on d3's utcTicks algorithm (https://github.com/d3/d3-time/blob/main/src/ticks.js) with one modification:
|
|
5
|
+
* weeks are considered to start on Monday (ISO 8601 standard)
|
|
6
|
+
* instead of Sunday (d3 default).
|
|
7
|
+
*/
|
|
8
|
+
export declare function getDateTimeTicks(start: Date, stop: Date, count?: number): Date[];
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { bisector, tickStep, utcDay, utcHour, utcMillisecond, utcMinute, utcMonth, utcSecond, utcMonday as utcWeek, utcYear, } from 'd3';
|
|
2
|
+
import { DAY, HOUR, MINUTE, MONTH, SECOND, WEEK, YEAR } from '../../../constants';
|
|
3
|
+
const tickIntervals = [
|
|
4
|
+
[utcSecond, 1, SECOND],
|
|
5
|
+
[utcSecond, 5, 5 * SECOND],
|
|
6
|
+
[utcSecond, 15, 15 * SECOND],
|
|
7
|
+
[utcSecond, 30, 30 * SECOND],
|
|
8
|
+
[utcMinute, 1, MINUTE],
|
|
9
|
+
[utcMinute, 5, 5 * MINUTE],
|
|
10
|
+
[utcMinute, 15, 15 * MINUTE],
|
|
11
|
+
[utcMinute, 30, 30 * MINUTE],
|
|
12
|
+
[utcHour, 1, HOUR],
|
|
13
|
+
[utcHour, 3, 3 * HOUR],
|
|
14
|
+
[utcHour, 6, 6 * HOUR],
|
|
15
|
+
[utcHour, 12, 12 * HOUR],
|
|
16
|
+
[utcDay, 1, DAY],
|
|
17
|
+
[utcDay, 2, 2 * DAY],
|
|
18
|
+
[utcWeek, 1, WEEK],
|
|
19
|
+
[utcMonth, 1, MONTH],
|
|
20
|
+
[utcMonth, 3, 3 * MONTH],
|
|
21
|
+
[utcYear, 1, YEAR],
|
|
22
|
+
];
|
|
23
|
+
function getDateTimeTickInterval(start, stop, count) {
|
|
24
|
+
const target = Math.abs(stop - start) / count;
|
|
25
|
+
const i = bisector(([, , step]) => step).right(tickIntervals, target);
|
|
26
|
+
if (i === tickIntervals.length) {
|
|
27
|
+
return utcYear.every(tickStep(start / YEAR, stop / YEAR, count));
|
|
28
|
+
}
|
|
29
|
+
if (i === 0) {
|
|
30
|
+
return utcMillisecond.every(Math.max(tickStep(start, stop, count), 1));
|
|
31
|
+
}
|
|
32
|
+
const [t, step] = tickIntervals[target / tickIntervals[i - 1][2] < tickIntervals[i][2] / target ? i - 1 : i];
|
|
33
|
+
return t.every(step);
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Generates time ticks for the given interval.
|
|
37
|
+
*
|
|
38
|
+
* Based on d3's utcTicks algorithm (https://github.com/d3/d3-time/blob/main/src/ticks.js) with one modification:
|
|
39
|
+
* weeks are considered to start on Monday (ISO 8601 standard)
|
|
40
|
+
* instead of Sunday (d3 default).
|
|
41
|
+
*/
|
|
42
|
+
export function getDateTimeTicks(start, stop, count = 10) {
|
|
43
|
+
const reverse = stop < start;
|
|
44
|
+
if (reverse) {
|
|
45
|
+
[start, stop] = [stop, start];
|
|
46
|
+
}
|
|
47
|
+
const interval = getDateTimeTickInterval(start.getTime(), stop.getTime(), count);
|
|
48
|
+
const ticks = interval ? interval.range(start, new Date(Number(stop) + 1)) : [];
|
|
49
|
+
return reverse ? ticks.reverse() : ticks;
|
|
50
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { AxisDomain, AxisScale } from 'd3';
|
|
2
|
+
import type { ChartScale } from '../../../hooks';
|
|
3
|
+
export declare function getScaleTicks({ scale, ticksCount, }: {
|
|
4
|
+
scale: ChartScale | AxisScale<AxisDomain>;
|
|
5
|
+
ticksCount?: number;
|
|
6
|
+
}): string[] | number[] | Date[];
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { getDateTimeTicks } from './datetime';
|
|
2
|
+
export function getScaleTicks({ scale, ticksCount, }) {
|
|
3
|
+
const scaleDomain = scale.domain();
|
|
4
|
+
switch (typeof scaleDomain[0]) {
|
|
5
|
+
case 'number': {
|
|
6
|
+
return scale.ticks(ticksCount);
|
|
7
|
+
}
|
|
8
|
+
// datetime scale
|
|
9
|
+
case 'object': {
|
|
10
|
+
return getDateTimeTicks(scaleDomain[0], scaleDomain[scaleDomain.length - 1], ticksCount);
|
|
11
|
+
}
|
|
12
|
+
case 'string': {
|
|
13
|
+
return scaleDomain;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
return [];
|
|
17
|
+
}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
1
|
export declare const TIME_UNITS: Record<string, number>;
|
|
2
2
|
export declare const DATETIME_LABEL_FORMATS: Record<keyof typeof TIME_UNITS, string>;
|
|
3
3
|
export declare function getDefaultDateFormat(range?: number): string;
|
|
4
|
+
export declare function getDefaultTimeOnlyFormat(step: number): string;
|
|
@@ -32,3 +32,12 @@ export function getDefaultDateFormat(range) {
|
|
|
32
32
|
}
|
|
33
33
|
return DATETIME_LABEL_FORMATS.day;
|
|
34
34
|
}
|
|
35
|
+
export function getDefaultTimeOnlyFormat(step) {
|
|
36
|
+
if (step < TIME_UNITS.second) {
|
|
37
|
+
return 'HH:mm:ss.SSS';
|
|
38
|
+
}
|
|
39
|
+
if (step < TIME_UNITS.minute) {
|
|
40
|
+
return 'HH:mm:ss';
|
|
41
|
+
}
|
|
42
|
+
return 'HH:mm';
|
|
43
|
+
}
|
package/package.json
CHANGED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare const DEFAULT_DATE_FORMAT = "DD.MM.YY";
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export const DEFAULT_DATE_FORMAT = 'DD.MM.YY';
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare const DEFAULT_DATE_FORMAT = "DD.MM.YY";
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export const DEFAULT_DATE_FORMAT = 'DD.MM.YY';
|