@centreon/ui 24.10.12 → 24.10.13
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/package.json +3 -2
- package/public/mockServiceWorker.js +1 -1
- package/src/Button/Icon/index.tsx +3 -1
- package/src/Dashboard/Dashboard.styles.ts +3 -4
- package/src/Dashboard/DashboardLayout.stories.tsx +1 -1
- package/src/Dashboard/Grid.tsx +11 -17
- package/src/Dashboard/Layout.tsx +27 -56
- package/src/Dialog/UnsavedChanges/index.tsx +15 -13
- package/src/Dialog/UnsavedChanges/translatedLabels.ts +15 -13
- package/src/Form/Form.tsx +0 -1
- package/src/Form/Inputs/Autocomplete.tsx +1 -1
- package/src/Form/Inputs/ConnectedAutocomplete.tsx +5 -2
- package/src/Form/Inputs/Grid.tsx +7 -1
- package/src/Form/Inputs/Radio.tsx +1 -1
- package/src/Form/Inputs/Switch.tsx +1 -1
- package/src/Form/Inputs/Text.tsx +1 -1
- package/src/Form/Inputs/index.tsx +25 -24
- package/src/Form/Inputs/models.ts +2 -0
- package/src/Graph/BarChart/BarChart.cypress.spec.tsx +3 -3
- package/src/Graph/BarChart/BarChart.tsx +24 -31
- package/src/Graph/BarChart/BarGroup.tsx +32 -59
- package/src/Graph/BarChart/BarStack.tsx +64 -13
- package/src/Graph/BarChart/MemoizedGroup.tsx +123 -0
- package/src/Graph/BarChart/ResponsiveBarChart.tsx +21 -7
- package/src/Graph/BarStack/BarStack.cypress.spec.tsx +87 -9
- package/src/Graph/BarStack/BarStack.stories.tsx +13 -4
- package/src/Graph/BarStack/BarStack.styles.ts +57 -33
- package/src/Graph/BarStack/Graph.tsx +173 -0
- package/src/Graph/BarStack/GraphAndLegend.tsx +117 -0
- package/src/Graph/BarStack/ResponsiveBarStack.tsx +61 -168
- package/src/Graph/BarStack/constants.ts +5 -0
- package/src/Graph/BarStack/models.ts +0 -1
- package/src/Graph/BarStack/useGraphAndLegend.ts +84 -0
- package/src/Graph/BarStack/useResponsiveBarStack.ts +73 -97
- package/src/Graph/Chart/Chart.cypress.spec.tsx +14 -26
- package/src/Graph/Chart/Chart.stories.tsx +1 -1
- package/src/Graph/Chart/Chart.tsx +53 -37
- package/src/Graph/Chart/InteractiveComponents/AnchorPoint/GuidingLines.tsx +3 -3
- package/src/Graph/Chart/InteractiveComponents/AnchorPoint/useTickGraph.ts +19 -6
- package/src/Graph/Chart/Legend/Legend.styles.ts +25 -11
- package/src/Graph/Chart/Legend/index.tsx +6 -24
- package/src/Graph/Chart/index.tsx +34 -43
- package/src/Graph/Chart/models.ts +0 -1
- package/src/Graph/Chart/useChartData.ts +19 -1
- package/src/Graph/HeatMap/ResponsiveHeatMap.tsx +20 -2
- package/src/Graph/HeatMap/model.ts +6 -2
- package/src/Graph/Legend/Legend.styles.ts +10 -0
- package/src/Graph/Legend/Legend.tsx +6 -1
- package/src/Graph/SingleBar/ResponsiveSingleBar.tsx +9 -10
- package/src/Graph/SingleBar/ThresholdLine.tsx +6 -6
- package/src/Graph/Text/Text.styles.ts +2 -2
- package/src/Graph/Text/Text.tsx +23 -10
- package/src/Graph/Timeline/ResponsiveTimeline.tsx +152 -0
- package/src/Graph/Timeline/Timeline.cypress.spec.tsx +148 -0
- package/src/Graph/Timeline/Timeline.stories.tsx +91 -0
- package/src/Graph/Timeline/Timeline.tsx +28 -0
- package/src/Graph/Timeline/index.ts +1 -0
- package/src/Graph/Timeline/models.ts +20 -0
- package/src/Graph/Timeline/timeline.styles.ts +11 -0
- package/src/Graph/Timeline/translatedLabel.ts +6 -0
- package/src/Graph/Timeline/useTimeline.ts +90 -0
- package/src/Graph/Tree/Links.tsx +2 -2
- package/src/Graph/Tree/Tree.tsx +2 -2
- package/src/Graph/Tree/constants.ts +1 -1
- package/src/Graph/common/Axes/index.tsx +1 -1
- package/src/Graph/common/Axes/useAxisY.ts +8 -4
- package/src/Graph/common/BaseChart/BaseChart.tsx +3 -12
- package/src/Graph/common/BaseChart/ChartSvgWrapper.tsx +12 -4
- package/src/Graph/common/BaseChart/Header/index.tsx +3 -1
- package/src/Graph/common/BaseChart/useComputeBaseChartDimensions.ts +23 -11
- package/src/Graph/common/BaseChart/useComputeYAxisMaxCharacters.ts +92 -0
- package/src/Graph/common/models.ts +7 -8
- package/src/Graph/common/timeSeries/index.test.ts +1 -1
- package/src/Graph/common/timeSeries/index.ts +56 -29
- package/src/Graph/common/timeSeries/models.ts +2 -0
- package/src/Graph/common/utils.ts +51 -3
- package/src/Graph/index.ts +4 -1
- package/src/Graph/mockedData/lastDayWithNullValues.json +6 -6
- package/src/Graph/mockedData/pingServiceLinesBars.json +47 -47
- package/src/Icon/DowntimeIcon.tsx +8 -1
- package/src/Icon/FlappingIcon.tsx +22 -0
- package/src/Icon/index.ts +1 -0
- package/src/InputField/Select/Autocomplete/Connected/Multi/index.test.tsx +21 -1
- package/src/InputField/Select/Autocomplete/Connected/index.test.tsx +2 -2
- package/src/InputField/Select/Autocomplete/Connected/index.tsx +52 -15
- package/src/InputField/Select/Autocomplete/Multi/index.stories.tsx +19 -0
- package/src/InputField/Select/Autocomplete/Multi/index.tsx +8 -5
- package/src/InputField/Select/Autocomplete/index.tsx +79 -54
- package/src/InputField/Text/index.tsx +6 -4
- package/src/InputField/translatedLabels.ts +2 -0
- package/src/Listing/ActionBar/index.tsx +1 -1
- package/src/Listing/Listing.styles.ts +3 -3
- package/src/Listing/index.tsx +40 -37
- package/src/Listing/models.ts +0 -8
- package/src/Listing/useStyleTable.ts +58 -32
- package/src/MultiSelectEntries/index.tsx +2 -0
- package/src/PopoverMenu/index.tsx +2 -9
- package/src/SortableItems/index.tsx +0 -1
- package/src/ThemeProvider/index.tsx +1 -1
- package/src/ThemeProvider/palettes.ts +6 -0
- package/src/TimePeriods/CustomTimePeriod/PopoverCustomTimePeriod/PickersStartEndDate.tsx +2 -3
- package/src/TimePeriods/DateTimePickerInput.tsx +3 -1
- package/src/api/buildListingEndpoint/getSearchQueryParameterValue.ts +7 -1
- package/src/api/buildListingEndpoint/models.ts +1 -0
- package/src/api/customFetch.ts +4 -1
- package/src/api/models.ts +9 -0
- package/src/api/useGraphQuery/index.ts +117 -20
- package/src/api/useGraphQuery/models.ts +1 -0
- package/src/api/useMutationQuery/index.ts +1 -1
- package/src/components/DataTable/DataTable.styles.ts +1 -1
- package/src/components/DataTable/EmptyState/DataTableEmptyState.styles.ts +2 -1
- package/src/components/DataTable/EmptyState/DataTableEmptyState.tsx +4 -1
- package/src/components/DataTable/Item/DataTableItem.styles.ts +28 -2
- package/src/components/DataTable/Item/DataTableItem.tsx +19 -4
- package/src/components/Form/FormActions.tsx +21 -12
- package/src/components/Layout/AreaIndicator.tsx +1 -1
- package/src/components/Layout/PageLayout/PageLayout.styles.ts +2 -7
- package/src/components/Layout/PageLayout/PageLayoutBody.tsx +0 -1
- package/src/components/Zoom/Zoom.tsx +9 -2
- package/src/components/Zoom/ZoomContent.tsx +143 -136
- package/src/components/Zoom/models.ts +18 -15
- package/src/components/Zoom/useMinimap.ts +5 -8
- package/src/components/Zoom/useZoom.ts +3 -3
- package/src/index.ts +2 -0
- package/src/utils/index.ts +1 -0
- package/src/utils/useLocale/index.ts +9 -0
- package/src/utils/useLocale/useLocale.cypress.spec.tsx +38 -0
- package/src/utils/useLocaleDateTimeFormat/index.ts +4 -2
- package/src/utils/usePluralizedTranslation.ts +2 -3
- package/src/Graph/common/timeSeries/index.test.ts-E +0 -622
- package/src/components/CrudPage/Actions/Actions.styles.ts +0 -16
- package/src/components/CrudPage/Actions/Actions.tsx +0 -24
- package/src/components/CrudPage/Actions/AddButton.tsx +0 -23
- package/src/components/CrudPage/Actions/Filters.tsx +0 -25
- package/src/components/CrudPage/Actions/Search.tsx +0 -31
- package/src/components/CrudPage/Actions/useSearch.tsx +0 -24
- package/src/components/CrudPage/Columns/Actions.tsx +0 -88
- package/src/components/CrudPage/CrudPage.cypress.spec.tsx +0 -559
- package/src/components/CrudPage/CrudPage.stories.tsx +0 -278
- package/src/components/CrudPage/CrudPageRoot.tsx +0 -142
- package/src/components/CrudPage/DeleteModal.tsx +0 -77
- package/src/components/CrudPage/Form/AddModal.tsx +0 -35
- package/src/components/CrudPage/Form/Buttons.tsx +0 -98
- package/src/components/CrudPage/Form/UpdateModal.tsx +0 -60
- package/src/components/CrudPage/Listing.tsx +0 -63
- package/src/components/CrudPage/atoms.ts +0 -30
- package/src/components/CrudPage/hooks/useDeleteItem.ts +0 -53
- package/src/components/CrudPage/hooks/useGetItem.ts +0 -36
- package/src/components/CrudPage/hooks/useGetItems.ts +0 -67
- package/src/components/CrudPage/hooks/useListingQueryKey.ts +0 -31
- package/src/components/CrudPage/index.tsx +0 -7
- package/src/components/CrudPage/models.ts +0 -118
- package/src/components/CrudPage/utils.ts +0 -4
|
@@ -1,18 +1,14 @@
|
|
|
1
|
-
import { useRef } from 'react';
|
|
2
|
-
|
|
3
|
-
import { Group } from '@visx/group';
|
|
4
|
-
import { BarStackHorizontal, BarStack as BarStackVertical } from '@visx/shape';
|
|
5
|
-
import { Text } from '@visx/text';
|
|
6
|
-
import numeral from 'numeral';
|
|
7
|
-
import { equals, lt } from 'ramda';
|
|
8
|
-
import { useTranslation } from 'react-i18next';
|
|
9
|
-
|
|
10
|
-
import { Tooltip } from '../../components';
|
|
11
1
|
import { Legend as LegendComponent } from '../Legend';
|
|
12
2
|
import { LegendProps } from '../Legend/models';
|
|
13
|
-
import {
|
|
3
|
+
import { useStyles } from './BarStack.styles';
|
|
14
4
|
|
|
15
|
-
import {
|
|
5
|
+
import { Typography } from '@mui/material';
|
|
6
|
+
import numeral from 'numeral';
|
|
7
|
+
import { equals } from 'ramda';
|
|
8
|
+
import { useMemo } from 'react';
|
|
9
|
+
import { useTranslation } from 'react-i18next';
|
|
10
|
+
import GraphAndLegend from './GraphAndLegend';
|
|
11
|
+
import { gap, smallTitleHeight, titleHeight } from './constants';
|
|
16
12
|
import { BarStackProps } from './models';
|
|
17
13
|
import useResponsiveBarStack from './useResponsiveBarStack';
|
|
18
14
|
|
|
@@ -23,9 +19,8 @@ const DefaultLengd = ({ scale, direction }: LegendProps): JSX.Element => (
|
|
|
23
19
|
const ResponsiveBarStack = ({
|
|
24
20
|
title,
|
|
25
21
|
data,
|
|
26
|
-
width,
|
|
27
22
|
height,
|
|
28
|
-
|
|
23
|
+
width,
|
|
29
24
|
onSingleBarClick,
|
|
30
25
|
displayLegend = true,
|
|
31
26
|
TooltipContent,
|
|
@@ -33,184 +28,82 @@ const ResponsiveBarStack = ({
|
|
|
33
28
|
unit = 'number',
|
|
34
29
|
displayValues,
|
|
35
30
|
variant = 'vertical',
|
|
36
|
-
legendDirection
|
|
31
|
+
legendDirection,
|
|
37
32
|
tooltipProps = {}
|
|
38
33
|
}: BarStackProps & { height: number; width: number }): JSX.Element => {
|
|
39
34
|
const { t } = useTranslation();
|
|
40
|
-
const { classes, cx } =
|
|
41
|
-
|
|
42
|
-
const titleRef = useRef(null);
|
|
43
|
-
const legendRef = useRef(null);
|
|
35
|
+
const { classes, cx } = useStyles();
|
|
44
36
|
|
|
45
37
|
const {
|
|
46
|
-
barSize,
|
|
47
|
-
colorScale,
|
|
48
|
-
input,
|
|
49
|
-
keys,
|
|
50
|
-
legendScale,
|
|
51
38
|
total,
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
39
|
+
titleVariant,
|
|
40
|
+
legendScale,
|
|
41
|
+
isVerticalBar,
|
|
42
|
+
colorScale,
|
|
43
|
+
formattedLegendDirection
|
|
56
44
|
} = useResponsiveBarStack({
|
|
57
45
|
data,
|
|
58
46
|
height,
|
|
59
|
-
|
|
60
|
-
size,
|
|
61
|
-
titleRef,
|
|
47
|
+
width,
|
|
62
48
|
unit,
|
|
63
49
|
variant,
|
|
64
|
-
|
|
50
|
+
legendDirection
|
|
65
51
|
});
|
|
66
52
|
|
|
67
|
-
const
|
|
68
|
-
|
|
69
|
-
|
|
53
|
+
const graphAndLegendHeight = useMemo(() => {
|
|
54
|
+
if (equals(titleVariant, 'xs')) {
|
|
55
|
+
return height - 2 * smallTitleHeight - gap;
|
|
56
|
+
}
|
|
70
57
|
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
58
|
+
if (equals(titleVariant, 'sm')) {
|
|
59
|
+
return height - smallTitleHeight - gap;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
return height - titleHeight - gap;
|
|
63
|
+
}, [titleVariant, height]);
|
|
74
64
|
|
|
75
65
|
return (
|
|
76
|
-
<div
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
className={classes.
|
|
89
|
-
data-testid="barStack"
|
|
90
|
-
style={{
|
|
91
|
-
width: svgContainerSize.width
|
|
92
|
-
}}
|
|
66
|
+
<div
|
|
67
|
+
className={classes.container}
|
|
68
|
+
data-has-title={!!title}
|
|
69
|
+
data-title-variant={titleVariant}
|
|
70
|
+
data-variant={variant}
|
|
71
|
+
>
|
|
72
|
+
{title && (
|
|
73
|
+
<Typography
|
|
74
|
+
data-testid="Title"
|
|
75
|
+
variant={equals(titleVariant, 'md') ? 'h6' : 'body1'}
|
|
76
|
+
textAlign="center"
|
|
77
|
+
fontWeight="bold"
|
|
78
|
+
className={cx(equals(titleVariant, 'md') && classes.clippedTitle)}
|
|
93
79
|
>
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
barStacks.map((barStack) =>
|
|
112
|
-
barStack.bars.map((bar) => {
|
|
113
|
-
const shouldDisplayValues = (): boolean => {
|
|
114
|
-
if (bar.height < 10) {
|
|
115
|
-
return false;
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
return (
|
|
119
|
-
(equals(unit, 'number') && bar.width > 10) ||
|
|
120
|
-
(equals(unit, 'percentage') && bar.width > 25)
|
|
121
|
-
);
|
|
122
|
-
};
|
|
123
|
-
|
|
124
|
-
const TextX = bar.x + bar.width / 2;
|
|
125
|
-
const TextY = bar.y + bar.height / 2;
|
|
126
|
-
|
|
127
|
-
const onClick = (): void => {
|
|
128
|
-
onSingleBarClick?.(bar);
|
|
129
|
-
};
|
|
130
|
-
|
|
131
|
-
return (
|
|
132
|
-
<Tooltip
|
|
133
|
-
hasCaret
|
|
134
|
-
classes={{
|
|
135
|
-
tooltip: classes.barStackTooltip
|
|
136
|
-
}}
|
|
137
|
-
followCursor={false}
|
|
138
|
-
key={`bar-stack-${barStack.index}-${bar.index}`}
|
|
139
|
-
label={
|
|
140
|
-
TooltipContent && (
|
|
141
|
-
<TooltipContent
|
|
142
|
-
color={bar.color}
|
|
143
|
-
label={bar.key}
|
|
144
|
-
title={title}
|
|
145
|
-
total={total}
|
|
146
|
-
value={barStack.bars[0].bar.data[barStack.key]}
|
|
147
|
-
{...tooltipProps}
|
|
148
|
-
/>
|
|
149
|
-
)
|
|
150
|
-
}
|
|
151
|
-
position={
|
|
152
|
-
isVerticalBar ? 'right-start' : 'bottom-start'
|
|
153
|
-
}
|
|
154
|
-
>
|
|
155
|
-
<g
|
|
156
|
-
data-testid={bar.key}
|
|
157
|
-
onClick={onClick}
|
|
158
|
-
onKeyUp={() => undefined}
|
|
159
|
-
>
|
|
160
|
-
<rect
|
|
161
|
-
cursor="pointer"
|
|
162
|
-
fill={bar.color}
|
|
163
|
-
height={
|
|
164
|
-
isVerticalBar ? bar.height - 1 : bar.height
|
|
165
|
-
}
|
|
166
|
-
key={`bar-stack-${barStack.index}-${bar.index}`}
|
|
167
|
-
ry={4}
|
|
168
|
-
width={isVerticalBar ? bar.width : bar.width - 1}
|
|
169
|
-
x={bar.x}
|
|
170
|
-
y={bar.y}
|
|
171
|
-
/>
|
|
172
|
-
{displayValues && shouldDisplayValues() && (
|
|
173
|
-
<Text
|
|
174
|
-
cursor="pointer"
|
|
175
|
-
data-testid="value"
|
|
176
|
-
fill="#000"
|
|
177
|
-
fontSize={12}
|
|
178
|
-
fontWeight={600}
|
|
179
|
-
textAnchor="middle"
|
|
180
|
-
verticalAnchor="middle"
|
|
181
|
-
x={TextX}
|
|
182
|
-
y={TextY}
|
|
183
|
-
>
|
|
184
|
-
{getValueByUnit({
|
|
185
|
-
total,
|
|
186
|
-
unit,
|
|
187
|
-
value: barStack.bars[0].bar.data[barStack.key]
|
|
188
|
-
})}
|
|
189
|
-
</Text>
|
|
190
|
-
)}
|
|
191
|
-
</g>
|
|
192
|
-
</Tooltip>
|
|
193
|
-
);
|
|
194
|
-
})
|
|
195
|
-
)
|
|
196
|
-
}
|
|
197
|
-
</BarStackComponent>
|
|
198
|
-
</Group>
|
|
199
|
-
</svg>
|
|
200
|
-
</div>
|
|
201
|
-
</div>
|
|
202
|
-
{mustDisplayLegend && (
|
|
203
|
-
<div data-testid="Legend" ref={legendRef}>
|
|
80
|
+
{`${numeral(total).format('0a')}`} {t(title)}
|
|
81
|
+
</Typography>
|
|
82
|
+
)}
|
|
83
|
+
<GraphAndLegend
|
|
84
|
+
height={graphAndLegendHeight}
|
|
85
|
+
width={width}
|
|
86
|
+
isVerticalBar={isVerticalBar}
|
|
87
|
+
displayLegend={displayLegend}
|
|
88
|
+
colorScale={colorScale}
|
|
89
|
+
total={total}
|
|
90
|
+
data={data}
|
|
91
|
+
unit={unit}
|
|
92
|
+
displayValues={displayValues}
|
|
93
|
+
onSingleBarClick={onSingleBarClick}
|
|
94
|
+
tooltipProps={tooltipProps}
|
|
95
|
+
TooltipContent={TooltipContent}
|
|
96
|
+
legend={
|
|
204
97
|
<Legend
|
|
205
98
|
data={data}
|
|
206
|
-
direction={
|
|
99
|
+
direction={formattedLegendDirection}
|
|
207
100
|
scale={legendScale}
|
|
208
101
|
title={title}
|
|
209
102
|
total={total}
|
|
210
103
|
unit={unit}
|
|
211
104
|
/>
|
|
212
|
-
|
|
213
|
-
|
|
105
|
+
}
|
|
106
|
+
/>
|
|
214
107
|
</div>
|
|
215
108
|
);
|
|
216
109
|
};
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import { scaleBand, scaleLinear } from '@visx/scale';
|
|
2
|
+
import { pluck } from 'ramda';
|
|
3
|
+
import { useMemo } from 'react';
|
|
4
|
+
import { BarType } from './models';
|
|
5
|
+
|
|
6
|
+
interface UseGraphandLegendProps {
|
|
7
|
+
data: Array<BarType>;
|
|
8
|
+
isVerticalBar: boolean;
|
|
9
|
+
total: number;
|
|
10
|
+
width: number;
|
|
11
|
+
height: number;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
interface UseGraphAndLegendState {
|
|
15
|
+
barStackData: Record<string, number>;
|
|
16
|
+
xScale;
|
|
17
|
+
yScale;
|
|
18
|
+
keys: Array<string>;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export const useGraphAndLegend = ({
|
|
22
|
+
data,
|
|
23
|
+
isVerticalBar,
|
|
24
|
+
total,
|
|
25
|
+
width,
|
|
26
|
+
height
|
|
27
|
+
}: UseGraphandLegendProps): UseGraphAndLegendState => {
|
|
28
|
+
const dataWithNonEmptyValue = useMemo(
|
|
29
|
+
() => data.filter(({ value }) => value),
|
|
30
|
+
[data]
|
|
31
|
+
);
|
|
32
|
+
|
|
33
|
+
const keys = useMemo(
|
|
34
|
+
() => pluck('label', dataWithNonEmptyValue),
|
|
35
|
+
[dataWithNonEmptyValue]
|
|
36
|
+
);
|
|
37
|
+
|
|
38
|
+
const barStackData = useMemo(
|
|
39
|
+
() =>
|
|
40
|
+
dataWithNonEmptyValue.reduce((acc, { label, value }) => {
|
|
41
|
+
acc[label] = value;
|
|
42
|
+
|
|
43
|
+
return acc;
|
|
44
|
+
}, {}),
|
|
45
|
+
[dataWithNonEmptyValue]
|
|
46
|
+
);
|
|
47
|
+
|
|
48
|
+
const yScale = useMemo(
|
|
49
|
+
() =>
|
|
50
|
+
isVerticalBar
|
|
51
|
+
? scaleLinear({
|
|
52
|
+
domain: [0, total],
|
|
53
|
+
range: [height, 0]
|
|
54
|
+
})
|
|
55
|
+
: scaleBand({
|
|
56
|
+
domain: [0, 0],
|
|
57
|
+
padding: 0,
|
|
58
|
+
range: [height, 0]
|
|
59
|
+
}),
|
|
60
|
+
[isVerticalBar, total, height]
|
|
61
|
+
);
|
|
62
|
+
|
|
63
|
+
const xScale = useMemo(
|
|
64
|
+
() =>
|
|
65
|
+
isVerticalBar
|
|
66
|
+
? scaleBand({
|
|
67
|
+
domain: [0, 0],
|
|
68
|
+
padding: 0,
|
|
69
|
+
range: [0, width]
|
|
70
|
+
})
|
|
71
|
+
: scaleLinear({
|
|
72
|
+
domain: [0, total],
|
|
73
|
+
range: [0, width]
|
|
74
|
+
}),
|
|
75
|
+
[total, width, isVerticalBar]
|
|
76
|
+
);
|
|
77
|
+
|
|
78
|
+
return {
|
|
79
|
+
barStackData,
|
|
80
|
+
xScale,
|
|
81
|
+
yScale,
|
|
82
|
+
keys
|
|
83
|
+
};
|
|
84
|
+
};
|
|
@@ -1,37 +1,27 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { equals,
|
|
3
|
-
|
|
1
|
+
import { scaleOrdinal } from '@visx/scale';
|
|
2
|
+
import { equals, isNil, pluck } from 'ramda';
|
|
3
|
+
import { useMemo } from 'react';
|
|
4
4
|
import { LegendScale } from '../Legend/models';
|
|
5
5
|
import { getValueByUnit } from '../common/utils';
|
|
6
|
-
|
|
7
6
|
import { BarType } from './models';
|
|
8
7
|
|
|
9
|
-
interface Size {
|
|
10
|
-
height: number;
|
|
11
|
-
width: number;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
8
|
interface UseBarStackProps {
|
|
15
9
|
data: Array<BarType>;
|
|
16
10
|
height: number;
|
|
17
|
-
|
|
18
|
-
size: number;
|
|
19
|
-
titleRef;
|
|
11
|
+
width: number;
|
|
20
12
|
unit?: 'percentage' | 'number';
|
|
21
13
|
variant?: 'vertical' | 'horizontal';
|
|
22
|
-
|
|
14
|
+
legendDirection?: 'column' | 'row';
|
|
23
15
|
}
|
|
16
|
+
|
|
24
17
|
interface UseBarStackState {
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
18
|
+
total: number;
|
|
19
|
+
isSmall: boolean;
|
|
20
|
+
titleVariant: 'xs' | 'sm' | 'md';
|
|
28
21
|
isVerticalBar: boolean;
|
|
29
|
-
keys: Array<string>;
|
|
30
22
|
legendScale: LegendScale;
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
xScale;
|
|
34
|
-
yScale;
|
|
23
|
+
colorScale;
|
|
24
|
+
formattedLegendDirection: 'column' | 'row';
|
|
35
25
|
}
|
|
36
26
|
|
|
37
27
|
const useResponsiveBarStack = ({
|
|
@@ -40,87 +30,73 @@ const useResponsiveBarStack = ({
|
|
|
40
30
|
height,
|
|
41
31
|
width,
|
|
42
32
|
unit = 'number',
|
|
43
|
-
|
|
44
|
-
legendRef,
|
|
45
|
-
size
|
|
33
|
+
legendDirection
|
|
46
34
|
}: UseBarStackProps): UseBarStackState => {
|
|
47
|
-
const
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
const
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
xScale.rangeRound([0, xMax]);
|
|
105
|
-
yScale.range([yMax, 0]);
|
|
106
|
-
|
|
107
|
-
const input = data.reduce((acc, { label, value }) => {
|
|
108
|
-
acc[label] = value;
|
|
109
|
-
|
|
110
|
-
return acc;
|
|
111
|
-
}, {});
|
|
35
|
+
const total = useMemo(
|
|
36
|
+
() => Math.floor(data.reduce((acc, { value }) => acc + value, 0)),
|
|
37
|
+
[data]
|
|
38
|
+
);
|
|
39
|
+
|
|
40
|
+
const isVerticalBar = useMemo(() => equals(variant, 'vertical'), [variant]);
|
|
41
|
+
|
|
42
|
+
const isSmall = useMemo(
|
|
43
|
+
() => Math.floor(height) < 90,
|
|
44
|
+
[isVerticalBar, height]
|
|
45
|
+
);
|
|
46
|
+
|
|
47
|
+
const titleVariant = useMemo(() => {
|
|
48
|
+
if (width <= 105) {
|
|
49
|
+
return 'xs';
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
if (width <= 150 || isSmall) {
|
|
53
|
+
return 'sm';
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
return 'md';
|
|
57
|
+
}, [isSmall, width]);
|
|
58
|
+
|
|
59
|
+
const keys = useMemo(() => pluck('label', data), [data]);
|
|
60
|
+
|
|
61
|
+
const colorsRange = useMemo(() => pluck('color', data), [data]);
|
|
62
|
+
|
|
63
|
+
const colorScale = useMemo(
|
|
64
|
+
() =>
|
|
65
|
+
scaleOrdinal({
|
|
66
|
+
domain: keys,
|
|
67
|
+
range: colorsRange
|
|
68
|
+
}),
|
|
69
|
+
[keys, colorsRange]
|
|
70
|
+
);
|
|
71
|
+
|
|
72
|
+
const legendScale = useMemo(
|
|
73
|
+
() => ({
|
|
74
|
+
domain: data.map(({ value }) => getValueByUnit({ total, unit, value })),
|
|
75
|
+
range: colorsRange
|
|
76
|
+
}),
|
|
77
|
+
[data, colorsRange]
|
|
78
|
+
);
|
|
79
|
+
|
|
80
|
+
const formattedLegendDirection = useMemo(() => {
|
|
81
|
+
if (!isNil(legendDirection)) {
|
|
82
|
+
return legendDirection;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
if (equals(variant, 'horizontal')) {
|
|
86
|
+
return 'row';
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
return 'column';
|
|
90
|
+
}, [legendDirection, variant]);
|
|
112
91
|
|
|
113
92
|
return {
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
input,
|
|
93
|
+
total,
|
|
94
|
+
isSmall,
|
|
117
95
|
isVerticalBar,
|
|
118
|
-
|
|
96
|
+
titleVariant,
|
|
119
97
|
legendScale,
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
xScale,
|
|
123
|
-
yScale
|
|
98
|
+
colorScale,
|
|
99
|
+
formattedLegendDirection
|
|
124
100
|
};
|
|
125
101
|
};
|
|
126
102
|
|