@gravity-ui/charts 0.4.0 → 0.5.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/Legend/index.js +23 -4
- package/dist/cjs/hooks/useSeries/prepare-legend.js +1 -0
- package/dist/cjs/hooks/useSeries/types.d.ts +1 -0
- package/dist/cjs/libs/chart-error/index.d.ts +2 -2
- package/dist/cjs/libs/chart-error/index.js +6 -6
- package/dist/cjs/types/chart/legend.d.ts +2 -0
- package/dist/esm/components/Legend/index.js +23 -4
- package/dist/esm/hooks/useSeries/prepare-legend.js +1 -0
- package/dist/esm/hooks/useSeries/types.d.ts +1 -0
- package/dist/esm/libs/chart-error/index.d.ts +2 -2
- package/dist/esm/libs/chart-error/index.js +6 -6
- package/dist/esm/types/chart/legend.d.ts +2 -0
- package/package.json +1 -1
|
@@ -2,12 +2,13 @@ import React from 'react';
|
|
|
2
2
|
import { line as lineGenerator, scaleLinear, select, symbol } from 'd3';
|
|
3
3
|
import { CONTINUOUS_LEGEND_SIZE } from '../../constants';
|
|
4
4
|
import { getLineDashArray } from '../../hooks/useShapes/utils';
|
|
5
|
+
import { formatNumber } from '../../libs';
|
|
5
6
|
import { block, createGradientRect, getContinuesColorFn, getLabelsSize, getSymbol, } from '../../utils';
|
|
6
7
|
import { axisBottom } from '../../utils/chart/axis-generators';
|
|
7
8
|
import './styles.css';
|
|
8
9
|
const b = block('d3-legend');
|
|
9
10
|
const getLegendPosition = (args) => {
|
|
10
|
-
const { align, offsetWidth, width, contentWidth } = args;
|
|
11
|
+
const { align, offsetWidth = 0, width, contentWidth } = args;
|
|
11
12
|
const top = 0;
|
|
12
13
|
if (align === 'left') {
|
|
13
14
|
return { top, left: offsetWidth };
|
|
@@ -229,14 +230,16 @@ export const Legend = (props) => {
|
|
|
229
230
|
}),
|
|
230
231
|
});
|
|
231
232
|
// ticks
|
|
233
|
+
const scale = scaleLinear(domain, [0, legend.width]);
|
|
232
234
|
const xAxisGenerator = axisBottom({
|
|
233
|
-
scale
|
|
235
|
+
scale,
|
|
234
236
|
ticks: {
|
|
235
237
|
items: [[0, -rectHeight]],
|
|
236
238
|
labelsMargin: legend.ticks.labelsMargin,
|
|
237
239
|
labelsLineHeight: legend.ticks.labelsLineHeight,
|
|
238
240
|
maxTickCount: 4,
|
|
239
241
|
tickColor: '#fff',
|
|
242
|
+
labelFormat: (value) => formatNumber(value, { unit: 'auto' }),
|
|
240
243
|
},
|
|
241
244
|
domain: {
|
|
242
245
|
size: legend.width,
|
|
@@ -251,15 +254,31 @@ export const Legend = (props) => {
|
|
|
251
254
|
legendWidth = legend.width;
|
|
252
255
|
}
|
|
253
256
|
if (legend.title.enable) {
|
|
254
|
-
const { maxWidth:
|
|
257
|
+
const { maxWidth: titleWidth } = getLabelsSize({
|
|
255
258
|
labels: [legend.title.text],
|
|
256
259
|
style: legend.title.style,
|
|
257
260
|
});
|
|
261
|
+
let dx = 0;
|
|
262
|
+
switch (legend.title.align) {
|
|
263
|
+
case 'center': {
|
|
264
|
+
dx = legend.width / 2 - titleWidth / 2;
|
|
265
|
+
break;
|
|
266
|
+
}
|
|
267
|
+
case 'right': {
|
|
268
|
+
dx = legend.width - titleWidth;
|
|
269
|
+
break;
|
|
270
|
+
}
|
|
271
|
+
case 'left':
|
|
272
|
+
default: {
|
|
273
|
+
dx = 0;
|
|
274
|
+
break;
|
|
275
|
+
}
|
|
276
|
+
}
|
|
258
277
|
svgElement
|
|
259
278
|
.append('g')
|
|
260
279
|
.attr('class', b('title'))
|
|
261
280
|
.append('text')
|
|
262
|
-
.attr('dx',
|
|
281
|
+
.attr('dx', dx)
|
|
263
282
|
.attr('font-weight', (_c = legend.title.style.fontWeight) !== null && _c !== void 0 ? _c : null)
|
|
264
283
|
.attr('font-size', (_d = legend.title.style.fontSize) !== null && _d !== void 0 ? _d : null)
|
|
265
284
|
.attr('fill', (_e = legend.title.style.fontColor) !== null && _e !== void 0 ? _e : null)
|
|
@@ -10,7 +10,7 @@ export declare const CHART_ERROR_CODE: {
|
|
|
10
10
|
};
|
|
11
11
|
export declare class ChartError extends Error {
|
|
12
12
|
readonly code: number | string;
|
|
13
|
-
readonly
|
|
13
|
+
readonly isCustomError = true;
|
|
14
14
|
constructor({ originalError, message, code }?: ChartErrorArgs);
|
|
15
15
|
}
|
|
16
|
-
export declare const
|
|
16
|
+
export declare const isCustomError: (error: unknown) => error is ChartError;
|
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
export const CHART_ERROR_CODE = {
|
|
2
|
-
NO_DATA: 'NO_DATA',
|
|
3
|
-
INVALID_DATA: 'INVALID_DATA',
|
|
4
|
-
UNKNOWN: 'UNKNOWN_ERROR',
|
|
2
|
+
NO_DATA: 'ERR.CK.NO_DATA',
|
|
3
|
+
INVALID_DATA: 'ERR.CK.INVALID_DATA',
|
|
4
|
+
UNKNOWN: 'ERR.CK.UNKNOWN_ERROR',
|
|
5
5
|
};
|
|
6
6
|
export class ChartError extends Error {
|
|
7
7
|
constructor({ originalError, message, code = CHART_ERROR_CODE.UNKNOWN } = {}) {
|
|
8
8
|
super(message);
|
|
9
|
-
this.
|
|
9
|
+
this.isCustomError = true;
|
|
10
10
|
this.code = code;
|
|
11
11
|
if (originalError) {
|
|
12
12
|
this.name = originalError.name;
|
|
@@ -14,6 +14,6 @@ export class ChartError extends Error {
|
|
|
14
14
|
}
|
|
15
15
|
}
|
|
16
16
|
}
|
|
17
|
-
export const
|
|
18
|
-
return error instanceof Error && '
|
|
17
|
+
export const isCustomError = (error) => {
|
|
18
|
+
return error instanceof Error && 'isCustomError' in error;
|
|
19
19
|
};
|
|
@@ -2,12 +2,13 @@ import React from 'react';
|
|
|
2
2
|
import { line as lineGenerator, scaleLinear, select, symbol } from 'd3';
|
|
3
3
|
import { CONTINUOUS_LEGEND_SIZE } from '../../constants';
|
|
4
4
|
import { getLineDashArray } from '../../hooks/useShapes/utils';
|
|
5
|
+
import { formatNumber } from '../../libs';
|
|
5
6
|
import { block, createGradientRect, getContinuesColorFn, getLabelsSize, getSymbol, } from '../../utils';
|
|
6
7
|
import { axisBottom } from '../../utils/chart/axis-generators';
|
|
7
8
|
import './styles.css';
|
|
8
9
|
const b = block('d3-legend');
|
|
9
10
|
const getLegendPosition = (args) => {
|
|
10
|
-
const { align, offsetWidth, width, contentWidth } = args;
|
|
11
|
+
const { align, offsetWidth = 0, width, contentWidth } = args;
|
|
11
12
|
const top = 0;
|
|
12
13
|
if (align === 'left') {
|
|
13
14
|
return { top, left: offsetWidth };
|
|
@@ -229,14 +230,16 @@ export const Legend = (props) => {
|
|
|
229
230
|
}),
|
|
230
231
|
});
|
|
231
232
|
// ticks
|
|
233
|
+
const scale = scaleLinear(domain, [0, legend.width]);
|
|
232
234
|
const xAxisGenerator = axisBottom({
|
|
233
|
-
scale
|
|
235
|
+
scale,
|
|
234
236
|
ticks: {
|
|
235
237
|
items: [[0, -rectHeight]],
|
|
236
238
|
labelsMargin: legend.ticks.labelsMargin,
|
|
237
239
|
labelsLineHeight: legend.ticks.labelsLineHeight,
|
|
238
240
|
maxTickCount: 4,
|
|
239
241
|
tickColor: '#fff',
|
|
242
|
+
labelFormat: (value) => formatNumber(value, { unit: 'auto' }),
|
|
240
243
|
},
|
|
241
244
|
domain: {
|
|
242
245
|
size: legend.width,
|
|
@@ -251,15 +254,31 @@ export const Legend = (props) => {
|
|
|
251
254
|
legendWidth = legend.width;
|
|
252
255
|
}
|
|
253
256
|
if (legend.title.enable) {
|
|
254
|
-
const { maxWidth:
|
|
257
|
+
const { maxWidth: titleWidth } = getLabelsSize({
|
|
255
258
|
labels: [legend.title.text],
|
|
256
259
|
style: legend.title.style,
|
|
257
260
|
});
|
|
261
|
+
let dx = 0;
|
|
262
|
+
switch (legend.title.align) {
|
|
263
|
+
case 'center': {
|
|
264
|
+
dx = legend.width / 2 - titleWidth / 2;
|
|
265
|
+
break;
|
|
266
|
+
}
|
|
267
|
+
case 'right': {
|
|
268
|
+
dx = legend.width - titleWidth;
|
|
269
|
+
break;
|
|
270
|
+
}
|
|
271
|
+
case 'left':
|
|
272
|
+
default: {
|
|
273
|
+
dx = 0;
|
|
274
|
+
break;
|
|
275
|
+
}
|
|
276
|
+
}
|
|
258
277
|
svgElement
|
|
259
278
|
.append('g')
|
|
260
279
|
.attr('class', b('title'))
|
|
261
280
|
.append('text')
|
|
262
|
-
.attr('dx',
|
|
281
|
+
.attr('dx', dx)
|
|
263
282
|
.attr('font-weight', (_c = legend.title.style.fontWeight) !== null && _c !== void 0 ? _c : null)
|
|
264
283
|
.attr('font-size', (_d = legend.title.style.fontSize) !== null && _d !== void 0 ? _d : null)
|
|
265
284
|
.attr('fill', (_e = legend.title.style.fontColor) !== null && _e !== void 0 ? _e : null)
|
|
@@ -10,7 +10,7 @@ export declare const CHART_ERROR_CODE: {
|
|
|
10
10
|
};
|
|
11
11
|
export declare class ChartError extends Error {
|
|
12
12
|
readonly code: number | string;
|
|
13
|
-
readonly
|
|
13
|
+
readonly isCustomError = true;
|
|
14
14
|
constructor({ originalError, message, code }?: ChartErrorArgs);
|
|
15
15
|
}
|
|
16
|
-
export declare const
|
|
16
|
+
export declare const isCustomError: (error: unknown) => error is ChartError;
|
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
export const CHART_ERROR_CODE = {
|
|
2
|
-
NO_DATA: 'NO_DATA',
|
|
3
|
-
INVALID_DATA: 'INVALID_DATA',
|
|
4
|
-
UNKNOWN: 'UNKNOWN_ERROR',
|
|
2
|
+
NO_DATA: 'ERR.CK.NO_DATA',
|
|
3
|
+
INVALID_DATA: 'ERR.CK.INVALID_DATA',
|
|
4
|
+
UNKNOWN: 'ERR.CK.UNKNOWN_ERROR',
|
|
5
5
|
};
|
|
6
6
|
export class ChartError extends Error {
|
|
7
7
|
constructor({ originalError, message, code = CHART_ERROR_CODE.UNKNOWN } = {}) {
|
|
8
8
|
super(message);
|
|
9
|
-
this.
|
|
9
|
+
this.isCustomError = true;
|
|
10
10
|
this.code = code;
|
|
11
11
|
if (originalError) {
|
|
12
12
|
this.name = originalError.name;
|
|
@@ -14,6 +14,6 @@ export class ChartError extends Error {
|
|
|
14
14
|
}
|
|
15
15
|
}
|
|
16
16
|
}
|
|
17
|
-
export const
|
|
18
|
-
return error instanceof Error && '
|
|
17
|
+
export const isCustomError = (error) => {
|
|
18
|
+
return error instanceof Error && 'isCustomError' in error;
|
|
19
19
|
};
|