@gravity-ui/charts 1.35.1 → 1.35.2
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 +3 -0
- package/dist/cjs/components/AxisY/AxisY.js +3 -1
- package/dist/cjs/constants/datetime.d.ts +8 -0
- package/dist/cjs/constants/datetime.js +8 -0
- package/dist/cjs/constants/index.d.ts +1 -1
- package/dist/cjs/constants/index.js +1 -1
- package/dist/cjs/hooks/useAxis/utils.d.ts +1 -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/types/chart/axis.d.ts +1 -2
- package/dist/cjs/types/chart/base.d.ts +1 -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/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/esm/components/AxisX/AxisX.js +3 -0
- package/dist/esm/components/AxisY/AxisY.js +3 -1
- package/dist/esm/constants/datetime.d.ts +8 -0
- package/dist/esm/constants/datetime.js +8 -0
- package/dist/esm/constants/index.d.ts +1 -1
- package/dist/esm/constants/index.js +1 -1
- package/dist/esm/hooks/useAxis/utils.d.ts +1 -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/types/chart/axis.d.ts +1 -2
- package/dist/esm/types/chart/base.d.ts +1 -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/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/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
|
@@ -10,6 +10,7 @@ export const AxisX = (props) => {
|
|
|
10
10
|
const lineGenerator = line();
|
|
11
11
|
const htmlLabels = preparedAxisData.ticks.map((d) => d.htmlLabel).filter(Boolean);
|
|
12
12
|
React.useEffect(() => {
|
|
13
|
+
var _a, _b;
|
|
13
14
|
if (!ref.current) {
|
|
14
15
|
return () => { };
|
|
15
16
|
}
|
|
@@ -34,6 +35,8 @@ export const AxisX = (props) => {
|
|
|
34
35
|
.attr('text-anchor', 'start')
|
|
35
36
|
.style('transform', `translate(${preparedAxisData.title.x}px, ${preparedAxisData.title.y}px) rotate(${preparedAxisData.title.rotate}deg) translate(0px, ${preparedAxisData.title.offset}px)`)
|
|
36
37
|
.attr('font-size', preparedAxisData.title.style.fontSize)
|
|
38
|
+
.attr('font-weight', (_a = preparedAxisData.title.style.fontWeight) !== null && _a !== void 0 ? _a : null)
|
|
39
|
+
.attr('fill', (_b = preparedAxisData.title.style.fontColor) !== null && _b !== void 0 ? _b : null)
|
|
37
40
|
.selectAll('tspan')
|
|
38
41
|
.data(preparedAxisData.title.content)
|
|
39
42
|
.join('tspan')
|
|
@@ -15,7 +15,7 @@ export const AxisY = (props) => {
|
|
|
15
15
|
htmlElements.push(preparedAxisData.title);
|
|
16
16
|
}
|
|
17
17
|
React.useEffect(() => {
|
|
18
|
-
var _a;
|
|
18
|
+
var _a, _b, _c;
|
|
19
19
|
if (!ref.current) {
|
|
20
20
|
return () => { };
|
|
21
21
|
}
|
|
@@ -41,6 +41,8 @@ export const AxisY = (props) => {
|
|
|
41
41
|
.style('dominant-baseline', 'text-after-edge')
|
|
42
42
|
.style('transform', `translate(${preparedAxisData.title.x}px, ${preparedAxisData.title.y}px) rotate(${preparedAxisData.title.rotate}deg) translate(0px, ${preparedAxisData.title.offset}px)`)
|
|
43
43
|
.attr('font-size', preparedAxisData.title.style.fontSize)
|
|
44
|
+
.attr('font-weight', (_b = preparedAxisData.title.style.fontWeight) !== null && _b !== void 0 ? _b : null)
|
|
45
|
+
.attr('fill', (_c = preparedAxisData.title.style.fontColor) !== null && _c !== void 0 ? _c : null)
|
|
44
46
|
.selectAll('tspan')
|
|
45
47
|
.data(preparedAxisData.title.content)
|
|
46
48
|
.join('tspan')
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export declare const DEFAULT_DATE_FORMAT = "DD.MM.YY";
|
|
2
|
+
export declare const SECOND = 1000;
|
|
3
|
+
export declare const MINUTE: number;
|
|
4
|
+
export declare const HOUR: number;
|
|
5
|
+
export declare const DAY: number;
|
|
6
|
+
export declare const WEEK: number;
|
|
7
|
+
export declare const MONTH: number;
|
|
8
|
+
export declare const YEAR: number;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export const DEFAULT_DATE_FORMAT = 'DD.MM.YY';
|
|
2
|
+
export const SECOND = 1000;
|
|
3
|
+
export const MINUTE = SECOND * 60;
|
|
4
|
+
export const HOUR = MINUTE * 60;
|
|
5
|
+
export const DAY = HOUR * 24;
|
|
6
|
+
export const WEEK = DAY * 7;
|
|
7
|
+
export const MONTH = DAY * 30;
|
|
8
|
+
export const YEAR = DAY * 365;
|
|
@@ -6,4 +6,4 @@ export declare function createXScale(args: {
|
|
|
6
6
|
series: (PreparedSeries | ChartSeries)[];
|
|
7
7
|
rangeSliderState?: RangeSliderState;
|
|
8
8
|
zoomStateX?: [number, number];
|
|
9
|
-
}): import("d3-scale").
|
|
9
|
+
}): import("d3-scale").ScaleLinear<number, number, never> | import("d3-scale").ScaleBand<string> | import("d3-scale").ScaleTime<number, number, never> | undefined;
|
|
@@ -7,4 +7,4 @@ export declare function createYScale(args: {
|
|
|
7
7
|
primaryAxis?: PreparedAxis;
|
|
8
8
|
primaryTicksCount?: number;
|
|
9
9
|
zoomStateY?: [number, number];
|
|
10
|
-
}): import("d3-scale").
|
|
10
|
+
}): import("d3-scale").ScaleLinear<number, number, never> | import("d3-scale").ScaleBand<string> | import("d3-scale").ScaleTime<number, number, never> | undefined;
|
|
@@ -269,7 +269,7 @@ export interface AxisCrosshair extends Omit<AxisPlotLine, 'value' | 'label'> {
|
|
|
269
269
|
*/
|
|
270
270
|
enabled?: boolean;
|
|
271
271
|
}
|
|
272
|
-
interface ChartYAxisTitle extends ChartAxisTitle {
|
|
272
|
+
export interface ChartYAxisTitle extends ChartAxisTitle {
|
|
273
273
|
/** Rotation of the title in degrees.
|
|
274
274
|
* Currently, the available values are only for rotation in multiples of 90 degrees.
|
|
275
275
|
*
|
|
@@ -297,4 +297,3 @@ export interface ChartYAxis extends ChartAxis {
|
|
|
297
297
|
plotIndex?: number;
|
|
298
298
|
title?: ChartYAxisTitle;
|
|
299
299
|
}
|
|
300
|
-
export {};
|
|
@@ -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,
|
|
@@ -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
|
+
}
|
|
@@ -10,6 +10,7 @@ export const AxisX = (props) => {
|
|
|
10
10
|
const lineGenerator = line();
|
|
11
11
|
const htmlLabels = preparedAxisData.ticks.map((d) => d.htmlLabel).filter(Boolean);
|
|
12
12
|
React.useEffect(() => {
|
|
13
|
+
var _a, _b;
|
|
13
14
|
if (!ref.current) {
|
|
14
15
|
return () => { };
|
|
15
16
|
}
|
|
@@ -34,6 +35,8 @@ export const AxisX = (props) => {
|
|
|
34
35
|
.attr('text-anchor', 'start')
|
|
35
36
|
.style('transform', `translate(${preparedAxisData.title.x}px, ${preparedAxisData.title.y}px) rotate(${preparedAxisData.title.rotate}deg) translate(0px, ${preparedAxisData.title.offset}px)`)
|
|
36
37
|
.attr('font-size', preparedAxisData.title.style.fontSize)
|
|
38
|
+
.attr('font-weight', (_a = preparedAxisData.title.style.fontWeight) !== null && _a !== void 0 ? _a : null)
|
|
39
|
+
.attr('fill', (_b = preparedAxisData.title.style.fontColor) !== null && _b !== void 0 ? _b : null)
|
|
37
40
|
.selectAll('tspan')
|
|
38
41
|
.data(preparedAxisData.title.content)
|
|
39
42
|
.join('tspan')
|
|
@@ -15,7 +15,7 @@ export const AxisY = (props) => {
|
|
|
15
15
|
htmlElements.push(preparedAxisData.title);
|
|
16
16
|
}
|
|
17
17
|
React.useEffect(() => {
|
|
18
|
-
var _a;
|
|
18
|
+
var _a, _b, _c;
|
|
19
19
|
if (!ref.current) {
|
|
20
20
|
return () => { };
|
|
21
21
|
}
|
|
@@ -41,6 +41,8 @@ export const AxisY = (props) => {
|
|
|
41
41
|
.style('dominant-baseline', 'text-after-edge')
|
|
42
42
|
.style('transform', `translate(${preparedAxisData.title.x}px, ${preparedAxisData.title.y}px) rotate(${preparedAxisData.title.rotate}deg) translate(0px, ${preparedAxisData.title.offset}px)`)
|
|
43
43
|
.attr('font-size', preparedAxisData.title.style.fontSize)
|
|
44
|
+
.attr('font-weight', (_b = preparedAxisData.title.style.fontWeight) !== null && _b !== void 0 ? _b : null)
|
|
45
|
+
.attr('fill', (_c = preparedAxisData.title.style.fontColor) !== null && _c !== void 0 ? _c : null)
|
|
44
46
|
.selectAll('tspan')
|
|
45
47
|
.data(preparedAxisData.title.content)
|
|
46
48
|
.join('tspan')
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export declare const DEFAULT_DATE_FORMAT = "DD.MM.YY";
|
|
2
|
+
export declare const SECOND = 1000;
|
|
3
|
+
export declare const MINUTE: number;
|
|
4
|
+
export declare const HOUR: number;
|
|
5
|
+
export declare const DAY: number;
|
|
6
|
+
export declare const WEEK: number;
|
|
7
|
+
export declare const MONTH: number;
|
|
8
|
+
export declare const YEAR: number;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export const DEFAULT_DATE_FORMAT = 'DD.MM.YY';
|
|
2
|
+
export const SECOND = 1000;
|
|
3
|
+
export const MINUTE = SECOND * 60;
|
|
4
|
+
export const HOUR = MINUTE * 60;
|
|
5
|
+
export const DAY = HOUR * 24;
|
|
6
|
+
export const WEEK = DAY * 7;
|
|
7
|
+
export const MONTH = DAY * 30;
|
|
8
|
+
export const YEAR = DAY * 365;
|
|
@@ -6,4 +6,4 @@ export declare function createXScale(args: {
|
|
|
6
6
|
series: (PreparedSeries | ChartSeries)[];
|
|
7
7
|
rangeSliderState?: RangeSliderState;
|
|
8
8
|
zoomStateX?: [number, number];
|
|
9
|
-
}): import("d3-scale").
|
|
9
|
+
}): import("d3-scale").ScaleLinear<number, number, never> | import("d3-scale").ScaleBand<string> | import("d3-scale").ScaleTime<number, number, never> | undefined;
|
|
@@ -7,4 +7,4 @@ export declare function createYScale(args: {
|
|
|
7
7
|
primaryAxis?: PreparedAxis;
|
|
8
8
|
primaryTicksCount?: number;
|
|
9
9
|
zoomStateY?: [number, number];
|
|
10
|
-
}): import("d3-scale").
|
|
10
|
+
}): import("d3-scale").ScaleLinear<number, number, never> | import("d3-scale").ScaleBand<string> | import("d3-scale").ScaleTime<number, number, never> | undefined;
|
|
@@ -269,7 +269,7 @@ export interface AxisCrosshair extends Omit<AxisPlotLine, 'value' | 'label'> {
|
|
|
269
269
|
*/
|
|
270
270
|
enabled?: boolean;
|
|
271
271
|
}
|
|
272
|
-
interface ChartYAxisTitle extends ChartAxisTitle {
|
|
272
|
+
export interface ChartYAxisTitle extends ChartAxisTitle {
|
|
273
273
|
/** Rotation of the title in degrees.
|
|
274
274
|
* Currently, the available values are only for rotation in multiples of 90 degrees.
|
|
275
275
|
*
|
|
@@ -297,4 +297,3 @@ export interface ChartYAxis extends ChartAxis {
|
|
|
297
297
|
plotIndex?: number;
|
|
298
298
|
title?: ChartYAxisTitle;
|
|
299
299
|
}
|
|
300
|
-
export {};
|
|
@@ -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,
|
|
@@ -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
|
+
}
|
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';
|