@gravity-ui/charts 1.35.0 → 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.
Files changed (55) hide show
  1. package/dist/cjs/components/AxisX/AxisX.js +3 -0
  2. package/dist/cjs/components/AxisY/AxisY.js +3 -1
  3. package/dist/cjs/constants/datetime.d.ts +8 -0
  4. package/dist/cjs/constants/datetime.js +8 -0
  5. package/dist/cjs/constants/index.d.ts +1 -1
  6. package/dist/cjs/constants/index.js +1 -1
  7. package/dist/cjs/hooks/useAxis/utils.d.ts +1 -1
  8. package/dist/cjs/hooks/useAxisScales/x-scale.d.ts +1 -1
  9. package/dist/cjs/hooks/useAxisScales/y-scale.d.ts +1 -1
  10. package/dist/cjs/hooks/useShapes/area/index.js +38 -0
  11. package/dist/cjs/hooks/useShapes/area/prepare-data.js +12 -7
  12. package/dist/cjs/hooks/useShapes/bar-x/prepare-data.js +18 -7
  13. package/dist/cjs/hooks/useShapes/line/index.js +38 -0
  14. package/dist/cjs/hooks/useShapes/line/prepare-data.js +12 -7
  15. package/dist/cjs/types/chart/axis.d.ts +1 -2
  16. package/dist/cjs/types/chart/base.d.ts +1 -1
  17. package/dist/cjs/utils/chart/axis/common.d.ts +1 -3
  18. package/dist/cjs/utils/chart/axis/common.js +2 -10
  19. package/dist/cjs/utils/chart/axis/x-axis.js +3 -2
  20. package/dist/cjs/utils/chart/ticks/datetime.d.ts +8 -0
  21. package/dist/cjs/utils/chart/ticks/datetime.js +50 -0
  22. package/dist/cjs/utils/chart/ticks/index.d.ts +6 -0
  23. package/dist/cjs/utils/chart/ticks/index.js +17 -0
  24. package/dist/esm/components/AxisX/AxisX.js +3 -0
  25. package/dist/esm/components/AxisY/AxisY.js +3 -1
  26. package/dist/esm/constants/datetime.d.ts +8 -0
  27. package/dist/esm/constants/datetime.js +8 -0
  28. package/dist/esm/constants/index.d.ts +1 -1
  29. package/dist/esm/constants/index.js +1 -1
  30. package/dist/esm/hooks/useAxis/utils.d.ts +1 -1
  31. package/dist/esm/hooks/useAxisScales/x-scale.d.ts +1 -1
  32. package/dist/esm/hooks/useAxisScales/y-scale.d.ts +1 -1
  33. package/dist/esm/hooks/useShapes/area/index.js +38 -0
  34. package/dist/esm/hooks/useShapes/area/prepare-data.js +12 -7
  35. package/dist/esm/hooks/useShapes/bar-x/prepare-data.js +18 -7
  36. package/dist/esm/hooks/useShapes/line/index.js +38 -0
  37. package/dist/esm/hooks/useShapes/line/prepare-data.js +12 -7
  38. package/dist/esm/types/chart/axis.d.ts +1 -2
  39. package/dist/esm/types/chart/base.d.ts +1 -1
  40. package/dist/esm/utils/chart/axis/common.d.ts +1 -3
  41. package/dist/esm/utils/chart/axis/common.js +2 -10
  42. package/dist/esm/utils/chart/axis/x-axis.js +3 -2
  43. package/dist/esm/utils/chart/ticks/datetime.d.ts +8 -0
  44. package/dist/esm/utils/chart/ticks/datetime.js +50 -0
  45. package/dist/esm/utils/chart/ticks/index.d.ts +6 -0
  46. package/dist/esm/utils/chart/ticks/index.js +17 -0
  47. package/package.json +1 -1
  48. package/dist/cjs/constants/date.d.ts +0 -1
  49. package/dist/cjs/constants/date.js +0 -1
  50. package/dist/cjs/hooks/utils/bar-x.d.ts +0 -17
  51. package/dist/cjs/hooks/utils/bar-x.js +0 -43
  52. package/dist/esm/constants/date.d.ts +0 -1
  53. package/dist/esm/constants/date.js +0 -1
  54. package/dist/esm/hooks/utils/bar-x.d.ts +0 -17
  55. package/dist/esm/hooks/utils/bar-x.js +0 -43
@@ -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;
@@ -1,6 +1,6 @@
1
1
  export * from './axis';
2
2
  export * from './chart-types';
3
- export * from './date';
3
+ export * from './datetime';
4
4
  export * from './defaults';
5
5
  export * from './layout-algorithms';
6
6
  export * from './line-styles';
@@ -1,6 +1,6 @@
1
1
  export * from './axis';
2
2
  export * from './chart-types';
3
- export * from './date';
3
+ export * from './datetime';
4
4
  export * from './defaults';
5
5
  export * from './layout-algorithms';
6
6
  export * from './line-styles';
@@ -3,7 +3,7 @@ export declare function prepareAxisPlotLabel(d: AxisPlot): {
3
3
  text: string;
4
4
  style: {
5
5
  fontSize: string;
6
- fontWeight?: string;
6
+ fontWeight?: string | number;
7
7
  fontColor: string;
8
8
  };
9
9
  padding: number;
@@ -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").ScaleBand<string> | import("d3-scale").ScaleLinear<number, number, never> | import("d3-scale").ScaleTime<number, number, never> | undefined;
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").ScaleBand<string> | import("d3-scale").ScaleLinear<number, number, never> | import("d3-scale").ScaleTime<number, number, never> | undefined;
10
+ }): import("d3-scale").ScaleLinear<number, number, never> | import("d3-scale").ScaleBand<string> | import("d3-scale").ScaleTime<number, number, never> | undefined;
@@ -11,6 +11,7 @@ export const AreaSeriesShapes = (args) => {
11
11
  const hoveredDataRef = React.useRef(null);
12
12
  const plotRef = React.useRef(null);
13
13
  const markersRef = React.useRef(null);
14
+ const hoverMarkersRef = React.useRef(null);
14
15
  const allowOverlapDataLabels = React.useMemo(() => {
15
16
  return preparedData.some((d) => d === null || d === void 0 ? void 0 : d.series.dataLabels.allowOverlap);
16
17
  }, [preparedData]);
@@ -20,6 +21,7 @@ export const AreaSeriesShapes = (args) => {
20
21
  }
21
22
  const plotSvgElement = select(plotRef.current);
22
23
  const markersSvgElement = select(markersRef.current);
24
+ const hoverMarkersSvgElement = select(hoverMarkersRef.current);
23
25
  const hoverOptions = get(seriesOptions, 'area.states.hover');
24
26
  const inactiveOptions = get(seriesOptions, 'area.states.inactive');
25
27
  const line = lineGenerator()
@@ -141,6 +143,41 @@ export const AreaSeriesShapes = (args) => {
141
143
  }
142
144
  return d;
143
145
  });
146
+ hoverMarkersSvgElement.selectAll('*').remove();
147
+ if (hoverEnabled && selected.length > 0) {
148
+ const hoverOnlyMarkers = [];
149
+ for (const chunk of selected) {
150
+ const seriesData = preparedData.find((pd) => pd.id === chunk.series.id);
151
+ if (!seriesData) {
152
+ continue;
153
+ }
154
+ const { series } = seriesData;
155
+ if (series.marker.states.normal.enabled ||
156
+ !series.marker.states.hover.enabled) {
157
+ continue;
158
+ }
159
+ const point = seriesData.points.find((p) => p.data === chunk.data);
160
+ if (!point || point.y === null) {
161
+ continue;
162
+ }
163
+ hoverOnlyMarkers.push({
164
+ point: point,
165
+ active: true,
166
+ hovered: true,
167
+ clipped: false,
168
+ });
169
+ }
170
+ if (hoverOnlyMarkers.length > 0) {
171
+ hoverMarkersSvgElement
172
+ .selectAll('g')
173
+ .data(hoverOnlyMarkers)
174
+ .join('g')
175
+ .call(renderMarker)
176
+ .each((_d, i, nodes) => {
177
+ selectMarkerSymbol(select(nodes[i])).call(setMarker, 'hover');
178
+ });
179
+ }
180
+ }
144
181
  }
145
182
  if (hoveredDataRef.current !== null) {
146
183
  handleShapeHover(hoveredDataRef.current);
@@ -160,5 +197,6 @@ export const AreaSeriesShapes = (args) => {
160
197
  return (React.createElement(React.Fragment, null,
161
198
  React.createElement("g", { ref: plotRef, className: b(), clipPath: `url(#${clipPathId})` }),
162
199
  React.createElement("g", { ref: markersRef }),
200
+ React.createElement("g", { ref: hoverMarkersRef }),
163
201
  React.createElement(HtmlLayer, { preparedData: htmlLayerData, htmlLayout: htmlLayout })));
164
202
  };
@@ -220,17 +220,22 @@ export const prepareAreaData = async (args) => {
220
220
  return pointsAcc;
221
221
  }, []);
222
222
  let markers = [];
223
- if (s.marker.states.normal.enabled || s.marker.states.hover.enabled) {
223
+ const hasPerPointNormalMarkers = s.data.some((d) => { var _a, _b, _c; return (_c = (_b = (_a = d.marker) === null || _a === void 0 ? void 0 : _a.states) === null || _b === void 0 ? void 0 : _b.normal) === null || _c === void 0 ? void 0 : _c.enabled; });
224
+ if (s.marker.states.normal.enabled || hasPerPointNormalMarkers) {
224
225
  markers = points.reduce((markersAcc, p) => {
226
+ var _a, _b, _c, _d;
225
227
  if (p.y === null) {
226
228
  return markersAcc;
227
229
  }
228
- markersAcc.push({
229
- point: p,
230
- active: true,
231
- hovered: false,
232
- clipped: isOutsideBounds(p.x, p.y),
233
- });
230
+ const pointNormalEnabled = (_d = (_c = (_b = (_a = p.data.marker) === null || _a === void 0 ? void 0 : _a.states) === null || _b === void 0 ? void 0 : _b.normal) === null || _c === void 0 ? void 0 : _c.enabled) !== null && _d !== void 0 ? _d : false;
231
+ if (s.marker.states.normal.enabled || pointNormalEnabled) {
232
+ markersAcc.push({
233
+ point: p,
234
+ active: true,
235
+ hovered: false,
236
+ clipped: isOutsideBounds(p.x, p.y),
237
+ });
238
+ }
234
239
  return markersAcc;
235
240
  }, []);
236
241
  }
@@ -1,9 +1,10 @@
1
- import { ascending, descending, reverse, sort } from 'd3';
1
+ import { ascending, descending, max, reverse, sort } from 'd3';
2
2
  import get from 'lodash/get';
3
3
  import { getDataCategoryValue, getLabelsSize } from '../../../utils';
4
4
  import { getFormattedValue } from '../../../utils/chart/format';
5
+ import { MIN_BAR_GAP, MIN_BAR_GROUP_GAP, MIN_BAR_WIDTH } from '../../constants';
5
6
  import { getSeriesStackId } from '../../useSeries/utils';
6
- import { getBarXLayout } from '../../utils/bar-x';
7
+ import { getBandSize } from '../../utils/get-band-size';
7
8
  const isSeriesDataValid = (d) => d.y !== null;
8
9
  async function getLabelData(d, xMax) {
9
10
  var _a;
@@ -54,6 +55,8 @@ export const prepareBarXData = async (args) => {
54
55
  }
55
56
  }
56
57
  })();
58
+ const domain = new Set();
59
+ let maxGroupSize = 1;
57
60
  // series grouped by plotIndex > xValue > data[];
58
61
  const dataByPlots = new Map();
59
62
  series.forEach((s) => {
@@ -81,19 +84,27 @@ export const prepareBarXData = async (args) => {
81
84
  data[key][stackId] = [];
82
85
  }
83
86
  data[key][stackId].push({ data: d, series: s });
87
+ domain.add(key);
84
88
  }
85
89
  });
90
+ maxGroupSize = Math.max(maxGroupSize, max(Object.values(data), (d) => Object.values(d).length) || 1);
86
91
  });
87
92
  const result = [];
93
+ const barMaxWidth = get(seriesOptions, 'bar-x.barMaxWidth');
94
+ const barPadding = get(seriesOptions, 'bar-x.barPadding');
95
+ const groupPadding = get(seriesOptions, 'bar-x.groupPadding');
96
+ const bandSize = getBandSize({
97
+ domain: Array.from(domain),
98
+ scale: xScale,
99
+ });
100
+ const groupGap = Math.max(bandSize * groupPadding, MIN_BAR_GROUP_GAP);
101
+ const groupSize = bandSize - groupGap;
102
+ const rectGap = Math.max(bandSize * barPadding, MIN_BAR_GAP);
103
+ const rectWidth = Math.max(MIN_BAR_WIDTH, Math.min(groupSize / maxGroupSize - rectGap, barMaxWidth));
88
104
  const plotIndexes = Array.from(dataByPlots.keys());
89
105
  for (let plotDataIndex = 0; plotDataIndex < plotIndexes.length; plotDataIndex++) {
90
106
  const data = (_b = dataByPlots.get(plotIndexes[plotDataIndex])) !== null && _b !== void 0 ? _b : {};
91
107
  const groupedData = Object.entries(data);
92
- const { bandSize, barGap: rectGap, barSize: rectWidth, } = getBarXLayout({
93
- groupedData: data,
94
- seriesOptions,
95
- scale: xScale,
96
- });
97
108
  for (let groupedDataIndex = 0; groupedDataIndex < groupedData.length; groupedDataIndex++) {
98
109
  const [xValue, val] = groupedData[groupedDataIndex];
99
110
  const stacks = Object.values(val);
@@ -11,6 +11,7 @@ export const LineSeriesShapes = (args) => {
11
11
  const hoveredDataRef = React.useRef(null);
12
12
  const plotRef = React.useRef(null);
13
13
  const markersRef = React.useRef(null);
14
+ const hoverMarkersRef = React.useRef(null);
14
15
  const allowOverlapDataLabels = React.useMemo(() => {
15
16
  return preparedData.some((d) => d === null || d === void 0 ? void 0 : d.series.dataLabels.allowOverlap);
16
17
  }, [preparedData]);
@@ -20,6 +21,7 @@ export const LineSeriesShapes = (args) => {
20
21
  }
21
22
  const plotSvgElement = select(plotRef.current);
22
23
  const markersSvgElement = select(markersRef.current);
24
+ const hoverMarkersSvgElement = select(hoverMarkersRef.current);
23
25
  const hoverOptions = get(seriesOptions, 'line.states.hover');
24
26
  const inactiveOptions = get(seriesOptions, 'line.states.inactive');
25
27
  const line = lineGenerator()
@@ -127,6 +129,41 @@ export const LineSeriesShapes = (args) => {
127
129
  }
128
130
  return d;
129
131
  });
132
+ hoverMarkersSvgElement.selectAll('*').remove();
133
+ if (hoverEnabled && selected.length > 0) {
134
+ const hoverOnlyMarkers = [];
135
+ for (const chunk of selected) {
136
+ const seriesData = preparedData.find((pd) => pd.id === chunk.series.id);
137
+ if (!seriesData) {
138
+ continue;
139
+ }
140
+ const { series } = seriesData;
141
+ if (series.marker.states.normal.enabled ||
142
+ !series.marker.states.hover.enabled) {
143
+ continue;
144
+ }
145
+ const point = seriesData.points.find((p) => p.data === chunk.data);
146
+ if (!point || point.x === null || point.y === null) {
147
+ continue;
148
+ }
149
+ hoverOnlyMarkers.push({
150
+ point: point,
151
+ active: true,
152
+ hovered: true,
153
+ clipped: false,
154
+ });
155
+ }
156
+ if (hoverOnlyMarkers.length > 0) {
157
+ hoverMarkersSvgElement
158
+ .selectAll('g')
159
+ .data(hoverOnlyMarkers)
160
+ .join('g')
161
+ .call(renderMarker)
162
+ .each((_d, i, nodes) => {
163
+ selectMarkerSymbol(select(nodes[i])).call(setMarker, 'hover');
164
+ });
165
+ }
166
+ }
130
167
  }
131
168
  if (hoveredDataRef.current !== null) {
132
169
  handleShapeHover(hoveredDataRef.current);
@@ -146,5 +183,6 @@ export const LineSeriesShapes = (args) => {
146
183
  return (React.createElement(React.Fragment, null,
147
184
  React.createElement("g", { ref: plotRef, className: b(), clipPath: `url(#${clipPathId})` }),
148
185
  React.createElement("g", { ref: markersRef }),
186
+ React.createElement("g", { ref: hoverMarkersRef }),
149
187
  React.createElement(HtmlLayer, { preparedData: htmlLayerData, htmlLayout: htmlLayout })));
150
188
  };
@@ -86,17 +86,22 @@ export const prepareLineData = async (args) => {
86
86
  }
87
87
  }
88
88
  let markers = [];
89
- if (s.marker.states.normal.enabled || s.marker.states.hover.enabled) {
89
+ const hasPerPointNormalMarkers = s.data.some((d) => { var _a, _b, _c; return (_c = (_b = (_a = d.marker) === null || _a === void 0 ? void 0 : _a.states) === null || _b === void 0 ? void 0 : _b.normal) === null || _c === void 0 ? void 0 : _c.enabled; });
90
+ if (s.marker.states.normal.enabled || hasPerPointNormalMarkers) {
90
91
  markers = points.reduce((result, p) => {
92
+ var _a, _b, _c, _d;
91
93
  if (p.y === null || p.x === null) {
92
94
  return result;
93
95
  }
94
- result.push({
95
- point: p,
96
- active: true,
97
- hovered: false,
98
- clipped: isOutsideBounds(p.x, p.y),
99
- });
96
+ const pointNormalEnabled = (_d = (_c = (_b = (_a = p.data.marker) === null || _a === void 0 ? void 0 : _a.states) === null || _b === void 0 ? void 0 : _b.normal) === null || _c === void 0 ? void 0 : _c.enabled) !== null && _d !== void 0 ? _d : false;
97
+ if (s.marker.states.normal.enabled || pointNormalEnabled) {
98
+ result.push({
99
+ point: p,
100
+ active: true,
101
+ hovered: false,
102
+ clipped: isOutsideBounds(p.x, p.y),
103
+ });
104
+ }
100
105
  return result;
101
106
  }, []);
102
107
  }
@@ -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 {};
@@ -84,7 +84,7 @@ export interface BaseSeriesLegend extends ChartLegendItem {
84
84
  }
85
85
  export interface BaseTextStyle {
86
86
  fontSize: string;
87
- fontWeight?: string;
87
+ fontWeight?: string | number;
88
88
  fontColor?: string;
89
89
  }
90
90
  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
- }): Ticks;
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.ticks(scaleTicksCount);
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.ticks(ticksCount);
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;
@@ -1,6 +1,6 @@
1
1
  export * from './axis';
2
2
  export * from './chart-types';
3
- export * from './date';
3
+ export * from './datetime';
4
4
  export * from './defaults';
5
5
  export * from './layout-algorithms';
6
6
  export * from './line-styles';
@@ -1,6 +1,6 @@
1
1
  export * from './axis';
2
2
  export * from './chart-types';
3
- export * from './date';
3
+ export * from './datetime';
4
4
  export * from './defaults';
5
5
  export * from './layout-algorithms';
6
6
  export * from './line-styles';
@@ -3,7 +3,7 @@ export declare function prepareAxisPlotLabel(d: AxisPlot): {
3
3
  text: string;
4
4
  style: {
5
5
  fontSize: string;
6
- fontWeight?: string;
6
+ fontWeight?: string | number;
7
7
  fontColor: string;
8
8
  };
9
9
  padding: number;
@@ -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").ScaleBand<string> | import("d3-scale").ScaleLinear<number, number, never> | import("d3-scale").ScaleTime<number, number, never> | undefined;
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").ScaleBand<string> | import("d3-scale").ScaleLinear<number, number, never> | import("d3-scale").ScaleTime<number, number, never> | undefined;
10
+ }): import("d3-scale").ScaleLinear<number, number, never> | import("d3-scale").ScaleBand<string> | import("d3-scale").ScaleTime<number, number, never> | undefined;