@scality/core-ui 0.186.0 → 0.188.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.
Files changed (171) hide show
  1. package/dist/components/{linetemporalchart/MetricTimespanProvider.d.ts → charts/MetricsTimeSpanProvider.d.ts} +1 -1
  2. package/dist/components/charts/MetricsTimeSpanProvider.d.ts.map +1 -0
  3. package/dist/components/{barchartv2/Barchart.component.d.ts → charts/barchart/Barchart.d.ts} +2 -33
  4. package/dist/components/charts/barchart/Barchart.d.ts.map +1 -0
  5. package/dist/components/charts/barchart/Barchart.js +56 -0
  6. package/dist/components/{barchartv2/utils.d.ts → charts/barchart/Barchart.utils.d.ts} +8 -35
  7. package/dist/components/charts/barchart/Barchart.utils.d.ts.map +1 -0
  8. package/dist/components/{barchartv2/utils.js → charts/barchart/Barchart.utils.js} +7 -88
  9. package/dist/components/{barchartv2 → charts/barchart}/BarchartTooltip.d.ts +2 -1
  10. package/dist/components/charts/barchart/BarchartTooltip.d.ts.map +1 -0
  11. package/dist/components/{barchartv2 → charts/barchart}/BarchartTooltip.js +3 -3
  12. package/dist/components/{charttooltip → charts/common}/ChartTooltip.d.ts +9 -2
  13. package/dist/components/charts/common/ChartTooltip.d.ts.map +1 -0
  14. package/dist/components/{charttooltip → charts/common}/ChartTooltip.js +11 -15
  15. package/dist/components/charts/common/SharedComponents.d.ts +47 -0
  16. package/dist/components/charts/common/SharedComponents.d.ts.map +1 -0
  17. package/dist/components/charts/common/SharedComponents.js +83 -0
  18. package/dist/components/charts/common/chartUtils.d.ts +91 -0
  19. package/dist/components/charts/common/chartUtils.d.ts.map +1 -0
  20. package/dist/components/charts/common/chartUtils.js +243 -0
  21. package/dist/components/{globalhealthbar/GlobalHealthBarRecharts.component.d.ts → charts/globalhealthbar/GlobalHealthBar.d.ts} +2 -2
  22. package/dist/components/charts/globalhealthbar/GlobalHealthBar.d.ts.map +1 -0
  23. package/dist/components/{globalhealthbar/useHealthBarData.d.ts → charts/globalhealthbar/GlobalHealthBar.hooks.d.ts} +1 -1
  24. package/dist/components/charts/globalhealthbar/GlobalHealthBar.hooks.d.ts.map +1 -0
  25. package/dist/components/{globalhealthbar/GlobalHealthBarRecharts.component.js → charts/globalhealthbar/GlobalHealthBar.js} +4 -4
  26. package/dist/components/{globalhealthbar/healthBarUtils.d.ts → charts/globalhealthbar/GlobalHealthBar.utils.d.ts} +1 -1
  27. package/dist/components/charts/globalhealthbar/GlobalHealthBar.utils.d.ts.map +1 -0
  28. package/dist/components/{globalhealthbar/healthBarUtils.js → charts/globalhealthbar/GlobalHealthBar.utils.js} +1 -1
  29. package/dist/components/{globalhealthbar/components → charts/globalhealthbar}/GlobalHealthBarTooltip.d.ts +1 -1
  30. package/dist/components/charts/globalhealthbar/GlobalHealthBarTooltip.d.ts.map +1 -0
  31. package/dist/components/{globalhealthbar/components → charts/globalhealthbar}/GlobalHealthBarTooltip.js +7 -3
  32. package/dist/components/charts/globalhealthbar/HealthBarXAxis.d.ts.map +1 -0
  33. package/dist/components/{globalhealthbar/components → charts/globalhealthbar}/HealthBarXAxis.js +1 -1
  34. package/dist/components/charts/index.d.ts +16 -0
  35. package/dist/components/charts/index.d.ts.map +1 -0
  36. package/dist/components/charts/index.js +15 -0
  37. package/dist/components/{chartlegend → charts/legend}/ChartLegend.d.ts +1 -1
  38. package/dist/components/charts/legend/ChartLegend.d.ts.map +1 -0
  39. package/dist/components/{chartlegend → charts/legend}/ChartLegend.js +2 -2
  40. package/dist/components/{chartlegend → charts/legend}/ChartLegendWrapper.d.ts +1 -1
  41. package/dist/components/charts/legend/ChartLegendWrapper.d.ts.map +1 -0
  42. package/dist/components/{linetimeseriechart/linetimeseriechart.component.d.ts → charts/linetimeseries/LineTimeSerieChart.d.ts} +12 -2
  43. package/dist/components/charts/linetimeseries/LineTimeSerieChart.d.ts.map +1 -0
  44. package/dist/components/{linetimeseriechart/linetimeseriechart.component.js → charts/linetimeseries/LineTimeSerieChart.js} +34 -35
  45. package/dist/components/charts/linetimeseries/LineTimeSerieChart.utils.d.ts +14 -0
  46. package/dist/components/charts/linetimeseries/LineTimeSerieChart.utils.d.ts.map +1 -0
  47. package/dist/components/{linetimeseriechart/utils.js → charts/linetimeseries/LineTimeSerieChart.utils.js} +4 -6
  48. package/dist/components/charts/sparkline/Sparkline.d.ts +23 -0
  49. package/dist/components/charts/sparkline/Sparkline.d.ts.map +1 -0
  50. package/dist/components/{sparkline/sparkline.component.js → charts/sparkline/Sparkline.js} +12 -6
  51. package/dist/components/charts/types.d.ts +34 -0
  52. package/dist/components/charts/types.d.ts.map +1 -0
  53. package/dist/components/charts/types.js +4 -0
  54. package/dist/components/icon/Icon.component.d.ts +1 -0
  55. package/dist/components/icon/Icon.component.d.ts.map +1 -1
  56. package/dist/components/icon/Icon.component.js +2 -2
  57. package/dist/components/textbadge/TextBadge.component.d.ts +1 -1
  58. package/dist/components/textbadge/TextBadge.component.d.ts.map +1 -1
  59. package/dist/index.d.ts +0 -2
  60. package/dist/index.d.ts.map +1 -1
  61. package/dist/index.js +4 -2
  62. package/dist/next.d.ts +3 -11
  63. package/dist/next.d.ts.map +1 -1
  64. package/dist/next.js +4 -11
  65. package/package.json +2 -6
  66. package/src/lib/components/{barchartv2/Barchart.component.test.tsx → charts/barchart/Barchart.test.tsx} +35 -10
  67. package/src/lib/components/{barchartv2/Barchart.component.tsx → charts/barchart/Barchart.tsx} +42 -207
  68. package/src/lib/components/{barchartv2/utils.test.ts → charts/barchart/Barchart.utils.test.ts} +2 -141
  69. package/src/lib/components/{barchartv2/utils.ts → charts/barchart/Barchart.utils.ts} +17 -143
  70. package/src/lib/components/{barchartv2 → charts/barchart}/BarchartTooltip.tsx +5 -9
  71. package/src/lib/components/{charttooltip → charts/common}/ChartTooltip.tsx +13 -20
  72. package/src/lib/components/charts/common/SharedComponents.tsx +193 -0
  73. package/src/lib/components/charts/common/chartUtils.test.ts +402 -0
  74. package/src/lib/components/charts/common/chartUtils.ts +334 -0
  75. package/src/lib/components/{globalhealthbar/useHealthBarData.spec.tsx → charts/globalhealthbar/GlobalHealthBar.hooks.test.tsx} +1 -1
  76. package/src/lib/components/{globalhealthbar/GlobalHealthBarRecharts.component.tsx → charts/globalhealthbar/GlobalHealthBar.tsx} +4 -4
  77. package/src/lib/components/{globalhealthbar/healthBarUtils.spec.ts → charts/globalhealthbar/GlobalHealthBar.utils.test.ts} +1 -1
  78. package/src/lib/components/{globalhealthbar/healthBarUtils.ts → charts/globalhealthbar/GlobalHealthBar.utils.ts} +1 -1
  79. package/src/lib/components/{globalhealthbar/components → charts/globalhealthbar}/GlobalHealthBarTooltip.tsx +8 -4
  80. package/src/lib/components/{globalhealthbar/components → charts/globalhealthbar}/HealthBarXAxis.tsx +1 -1
  81. package/src/lib/components/charts/index.ts +59 -0
  82. package/src/lib/components/{chartlegend → charts/legend}/ChartLegend.tsx +2 -2
  83. package/src/lib/components/{chartlegend → charts/legend}/ChartLegendWrapper.tsx +1 -1
  84. package/src/lib/components/{linetimeseriechart/linetimeseriechart.test.tsx → charts/linetimeseries/LineTimeSerieChart.test.tsx} +3 -6
  85. package/src/lib/components/{linetimeseriechart/linetimeseriechart.component.tsx → charts/linetimeseries/LineTimeSerieChart.tsx} +48 -44
  86. package/src/lib/components/{linetimeseriechart/utils.test.ts → charts/linetimeseries/LineTimeSerieChart.utils.test.ts} +1 -1
  87. package/src/lib/components/{linetimeseriechart/utils.ts → charts/linetimeseries/LineTimeSerieChart.utils.ts} +4 -6
  88. package/src/lib/components/charts/sparkline/Sparkline.tsx +80 -0
  89. package/src/lib/components/charts/types.ts +36 -0
  90. package/src/lib/components/icon/Icon.component.tsx +12 -8
  91. package/src/lib/components/textbadge/TextBadge.component.tsx +1 -1
  92. package/src/lib/index.ts +4 -2
  93. package/src/lib/next.ts +26 -13
  94. package/stories/BarChart/barchart.stories.tsx +10 -9
  95. package/stories/GlobalHealthBar/{globalhealthbarRecharts.stories.tsx → globalhealthbar.stories.tsx} +3 -21
  96. package/stories/GlobalHealthBar/globalheathbarrecharts.guideline.mdx +2 -2
  97. package/stories/guideline/chart-guideline.mdx +1 -38
  98. package/stories/linetimeseriechart.stories.tsx +4 -6
  99. package/stories/sparkline.stories.tsx +13 -11
  100. package/stories/textbadge.stories.tsx +15 -0
  101. package/dist/components/barchart/BarChart.component.d.ts +0 -17
  102. package/dist/components/barchart/BarChart.component.d.ts.map +0 -1
  103. package/dist/components/barchart/BarChart.component.js +0 -27
  104. package/dist/components/barchartv2/Barchart.component.d.ts.map +0 -1
  105. package/dist/components/barchartv2/Barchart.component.js +0 -122
  106. package/dist/components/barchartv2/BarchartTooltip.d.ts.map +0 -1
  107. package/dist/components/barchartv2/utils.d.ts.map +0 -1
  108. package/dist/components/chartlegend/ChartLegend.d.ts.map +0 -1
  109. package/dist/components/chartlegend/ChartLegendWrapper.d.ts.map +0 -1
  110. package/dist/components/charttooltip/ChartTooltip.d.ts.map +0 -1
  111. package/dist/components/globalhealthbar/GlobalHealthBar.component.d.ts +0 -23
  112. package/dist/components/globalhealthbar/GlobalHealthBar.component.d.ts.map +0 -1
  113. package/dist/components/globalhealthbar/GlobalHealthBar.component.js +0 -173
  114. package/dist/components/globalhealthbar/GlobalHealthBarRecharts.component.d.ts.map +0 -1
  115. package/dist/components/globalhealthbar/components/GlobalHealthBarTooltip.d.ts.map +0 -1
  116. package/dist/components/globalhealthbar/components/HealthBarXAxis.d.ts.map +0 -1
  117. package/dist/components/globalhealthbar/healthBarUtils.d.ts.map +0 -1
  118. package/dist/components/globalhealthbar/healthBarUtils.spec.d.ts +0 -2
  119. package/dist/components/globalhealthbar/healthBarUtils.spec.d.ts.map +0 -1
  120. package/dist/components/globalhealthbar/healthBarUtils.spec.js +0 -391
  121. package/dist/components/globalhealthbar/tooltip/index.d.ts +0 -8
  122. package/dist/components/globalhealthbar/tooltip/index.d.ts.map +0 -1
  123. package/dist/components/globalhealthbar/tooltip/index.js +0 -55
  124. package/dist/components/globalhealthbar/useHealthBarData.d.ts.map +0 -1
  125. package/dist/components/globalhealthbar/useHealthBarData.spec.d.ts +0 -2
  126. package/dist/components/globalhealthbar/useHealthBarData.spec.d.ts.map +0 -1
  127. package/dist/components/globalhealthbar/useHealthBarData.spec.js +0 -209
  128. package/dist/components/linetemporalchart/ChartUtil.d.ts +0 -41
  129. package/dist/components/linetemporalchart/ChartUtil.d.ts.map +0 -1
  130. package/dist/components/linetemporalchart/ChartUtil.js +0 -148
  131. package/dist/components/linetemporalchart/LineTemporalChart.component.d.ts +0 -46
  132. package/dist/components/linetemporalchart/LineTemporalChart.component.d.ts.map +0 -1
  133. package/dist/components/linetemporalchart/LineTemporalChart.component.js +0 -579
  134. package/dist/components/linetemporalchart/MetricTimespanProvider.d.ts.map +0 -1
  135. package/dist/components/linetemporalchart/tooltip/index.d.ts +0 -30
  136. package/dist/components/linetemporalchart/tooltip/index.d.ts.map +0 -1
  137. package/dist/components/linetemporalchart/tooltip/index.js +0 -104
  138. package/dist/components/linetimeseriechart/linetimeseriechart.component.d.ts.map +0 -1
  139. package/dist/components/linetimeseriechart/utils.d.ts +0 -16
  140. package/dist/components/linetimeseriechart/utils.d.ts.map +0 -1
  141. package/dist/components/sparkline/sparkline.component.d.ts +0 -17
  142. package/dist/components/sparkline/sparkline.component.d.ts.map +0 -1
  143. package/dist/components/vegachartv2/SyncedCursorCharts.d.ts +0 -12
  144. package/dist/components/vegachartv2/SyncedCursorCharts.d.ts.map +0 -1
  145. package/dist/components/vegachartv2/SyncedCursorCharts.js +0 -15
  146. package/dist/components/vegachartv2/VegaChartV2.component.d.ts +0 -15
  147. package/dist/components/vegachartv2/VegaChartV2.component.d.ts.map +0 -1
  148. package/dist/components/vegachartv2/VegaChartV2.component.js +0 -218
  149. package/src/lib/components/barchart/BarChart.component.tsx +0 -51
  150. package/src/lib/components/globalhealthbar/GlobalHealthBar.component.tsx +0 -204
  151. package/src/lib/components/globalhealthbar/tooltip/index.ts +0 -72
  152. package/src/lib/components/linetemporalchart/ChartUtil.test.ts +0 -250
  153. package/src/lib/components/linetemporalchart/ChartUtil.ts +0 -225
  154. package/src/lib/components/linetemporalchart/LineTemporalChart.component.tsx +0 -800
  155. package/src/lib/components/linetemporalchart/tooltip/index.ts +0 -178
  156. package/src/lib/components/sparkline/sparkline.component.tsx +0 -56
  157. package/src/lib/components/vegachartv2/SyncedCursorCharts.tsx +0 -28
  158. package/src/lib/components/vegachartv2/VegaChartV2.component.tsx +0 -276
  159. package/stories/barchart.stories.tsx +0 -142
  160. package/stories/globalhealthbar.stories.tsx +0 -191
  161. package/stories/guideline/mdxExampleComponents.tsx +0 -57
  162. package/stories/linecharttemporal.stories.tsx +0 -88
  163. /package/dist/components/{linetemporalchart/MetricTimespanProvider.js → charts/MetricsTimeSpanProvider.js} +0 -0
  164. /package/dist/components/{globalhealthbar/useHealthBarData.js → charts/globalhealthbar/GlobalHealthBar.hooks.js} +0 -0
  165. /package/dist/components/{globalhealthbar/components → charts/globalhealthbar}/HealthBarXAxis.d.ts +0 -0
  166. /package/dist/components/{chartlegend → charts/legend}/ChartLegendWrapper.js +0 -0
  167. /package/src/lib/components/{linetemporalchart/MetricTimespanProvider.tsx → charts/MetricsTimeSpanProvider.tsx} +0 -0
  168. /package/src/lib/components/{barchartv2 → charts/barchart}/BarchartTooltip.test.tsx +0 -0
  169. /package/src/lib/components/{globalhealthbar/useHealthBarData.ts → charts/globalhealthbar/GlobalHealthBar.hooks.ts} +0 -0
  170. /package/src/lib/components/{chartlegend → charts/legend}/ChartLegend.test.tsx +0 -0
  171. /package/src/lib/components/{chartlegend → charts/legend}/ChartLegendWrapper.test.tsx +0 -0
@@ -1,800 +0,0 @@
1
- // @ts-nocheck
2
- import { useMemo, useRef, useLayoutEffect, Fragment } from 'react';
3
- import styled, { useTheme } from 'styled-components';
4
- import { lighten, darken } from 'polished';
5
- import { expressionFunction } from 'vega';
6
- import {
7
- lineColor1,
8
- lineColor2,
9
- lineColor3,
10
- lineColor4,
11
- lineColor5,
12
- lineColor6,
13
- lineColor7,
14
- lineColor8,
15
- } from '../../style/theme';
16
- import { VegaChart } from '../vegachartv2/VegaChartV2.component';
17
- import { useCursorX } from '../vegachartv2/SyncedCursorCharts';
18
- import { ChartTitleText } from '../text/Text.component';
19
- import {
20
- convert2VegaData,
21
- getUnitLabel,
22
- convertDataBaseValue,
23
- addMissingDataPoint,
24
- getRelativeValue,
25
- getColorDomains,
26
- relativeDatumToOriginalDatum,
27
- normlizeVegaFieldName,
28
- } from './ChartUtil';
29
- import { useMetricsTimeSpan } from './MetricTimespanProvider';
30
- import { spacing } from '../../spacing';
31
- import { SmallerText } from '../text/Text.component';
32
- import { Loader } from '../loader/Loader.component';
33
- import { formatValue } from './tooltip/index';
34
- import { Tooltip as TooltipComponent } from '../tooltip/Tooltip.component';
35
- import { Icon } from '../icon/Icon.component';
36
- // some predefined values
37
- export const YAXIS_TITLE_READ_WRITE = 'write(+) / read(-)';
38
- export const YAXIS_TITLE_IN_OUT = 'in(+) / out(-)';
39
- export const UNIT_RANGE_BS = [
40
- {
41
- threshold: 0,
42
- label: 'B/s',
43
- },
44
- {
45
- threshold: 1024,
46
- label: 'KiB/s',
47
- },
48
- {
49
- threshold: 1024 * 1024,
50
- label: 'MiB/s',
51
- },
52
- {
53
- threshold: 1024 * 1024 * 1024,
54
- label: 'GiB/s',
55
- },
56
- {
57
- threshold: 1024 * 1024 * 1024 * 1024,
58
- label: 'TiB/s',
59
- },
60
- ];
61
- const LineTemporalChartWrapper = styled.div`
62
- display: flex;
63
- flex-direction: column;
64
- justify-content: flex-start; // to make sure the header, the graph itself and legend are aligned
65
- `;
66
- const Legends = styled.div`
67
- display: flex;
68
- align-items: center;
69
- `;
70
- const LegendStroke = styled.div`
71
- margin: 0 ${spacing.r8} 0 ${spacing.r16};
72
- height: ${spacing.r2};
73
- background: ${(props) =>
74
- props.isLineDashed
75
- ? `repeating-linear-gradient(to right,${props.lineColor} 0,${props.lineColor} ${spacing.r1},transparent ${spacing.r1},transparent ${spacing.r2})`
76
- : props.lineColor};
77
- width: ${spacing.r8};
78
- `;
79
- const ChartHeader = styled.div`
80
- display: flex;
81
- align-items: center;
82
- `;
83
-
84
- /*
85
- We need to convert the data from raw prometheus data in the following steps:
86
- prometheusData => series => addMissingDataPoint(series.data, startTimeStamp, sampleDuration, sampleFrequency) => VegaData
87
-
88
- The data structure of multi-series chart using Vega-lite library
89
- https://vega.github.io/vega-lite/examples/interactive_multi_line_pivot_tooltip.html
90
- */
91
- export type Serie = {
92
- resource: string;
93
- // the name of the resource
94
- data: [number, string | null][];
95
- // the original data format from prometheus
96
- getTooltipLabel: (metricPrefix?: string, resource?: string) => string;
97
- // it's mandatory to display tooltip label in the tooltip
98
- getLegendLabel?: (metricPrefix?: string, resource?: string) => string;
99
- // get the legend label for each of the series
100
- color?: string;
101
- // optional color field to specify the color of the line
102
- metricPrefix?: string;
103
- // the name of the metric prefix with read, write, in, out
104
- isLineDashed?: boolean; // to specify if the line is dash
105
- };
106
- export type LineChartProps = {
107
- series: Serie[];
108
- title: string;
109
- height: number;
110
- startingTimeStamp: number;
111
- // pass to addMissingDataPoint()
112
- unitRange?: {
113
- threshold: number;
114
- label: string;
115
- }[];
116
- isLoading?: boolean;
117
- // if to display a loader next to the title
118
- isLegendHidden?: boolean;
119
- yAxisType?: 'default' | 'percentage' | 'symmetrical';
120
- yAxisTitle?: string;
121
- helpText?: string | JSX.Element;
122
- onHover?: (dataPoint: any) => void;
123
- renderTooltipSerie?: (
124
- arg0: {
125
- color?: string;
126
- isLineDashed?: boolean;
127
- name: string;
128
- value: string;
129
- key: string;
130
- unitLabel: string;
131
- },
132
- tooltipData: any,
133
- ) => string;
134
- };
135
- // Custom formatter to display negative value as an absolute value in read/write, in/out chart
136
- expressionFunction('negativeValueFormatter', function (datum) {
137
- return Math.abs(datum).toFixed(2);
138
- });
139
- // We use 8 main color from the palette and decline them (lighter/ darker) when we have more than 8 datasets
140
- const colorRange = [
141
- lineColor1,
142
- lineColor2,
143
- lineColor3,
144
- lineColor4,
145
- lineColor5,
146
- lineColor6,
147
- lineColor7,
148
- lineColor8,
149
- lighten(0.3, lineColor1),
150
- lighten(0.3, lineColor2),
151
- lighten(0.3, lineColor3),
152
- lighten(0.3, lineColor4),
153
- lighten(0.3, lineColor5),
154
- lighten(0.3, lineColor6),
155
- lighten(0.3, lineColor7),
156
- lighten(0.3, lineColor8),
157
- darken(0.2, lineColor1),
158
- darken(0.2, lineColor2),
159
- darken(0.2, lineColor3),
160
- darken(0.2, lineColor4),
161
- darken(0.3, lineColor5),
162
- darken(0.3, lineColor6),
163
- darken(0.3, lineColor7),
164
- darken(0.3, lineColor8),
165
- ];
166
-
167
- // Note: we need to make sure the start time and end timefor the prometheus query between the series are the same.
168
- /**
169
- * @deprecated Use LineTimeSerieChart instead
170
- * @example import { LineTimeSerieChart } from '@scality/core-ui/dist/next';
171
- */
172
- function LineTemporalChart({
173
- series,
174
- title,
175
- height,
176
- startingTimeStamp,
177
- unitRange,
178
- isLoading = false,
179
- isLegendHidden = false,
180
- yAxisType = 'default',
181
- yAxisTitle,
182
- helpText,
183
- renderTooltipSerie,
184
- onHover,
185
- ...rest
186
- }: LineChartProps) {
187
- // property validation
188
- if (!['default', 'percentage', 'symmetrical'].includes(yAxisType)) {
189
- console.error(
190
- `Invalid yAxisType props, expected default, percentage or symmetrical but received ${yAxisType}`,
191
- );
192
- }
193
-
194
- if (!height) {
195
- console.error('Please specify the height of the chart.');
196
- }
197
-
198
- if (!title) {
199
- console.error('Please specify the title of the chart.');
200
- }
201
-
202
- // we have to make sure that when prop unitRange exists, there is at least one item defined.
203
- if (unitRange) {
204
- if (unitRange.length === 0) {
205
- console.error('Please provide at least one entry in unitRange.');
206
- }
207
- }
208
-
209
- const vegaViewRef = useRef();
210
- const theme = useTheme();
211
- const { frequency, duration } = useMetricsTimeSpan();
212
- //##### Data Transformation Start
213
-
214
- /**
215
- * 1. Add missing data points
216
- * During the downtime of the platform, there is no data returned by Prometheus API.
217
- * Hence, we can see a line link the last data point before the downtime and the first data point once the platform restarts.
218
- * However, this is not what we expect to see.
219
- * So we need to manually add the missing data points with the value `null` to make sure there is nothing displayed on the graph during the downtime.
220
- */
221
- const addedMissingDataPointSeries = useMemo(() => {
222
- return series.map((line) => {
223
- return {
224
- ...line,
225
- data: addMissingDataPoint(
226
- line.data,
227
- startingTimeStamp,
228
- duration,
229
- frequency,
230
- ),
231
- };
232
- });
233
- }, [series, startingTimeStamp, duration, frequency]);
234
- // 2. Change the data structure to a flat array which is required by Vega-lite
235
- const vegaData = useMemo(() => {
236
- return convert2VegaData(addedMissingDataPointSeries);
237
- }, [addedMissingDataPointSeries]);
238
- // 3. Search for the biggest value in order to define the unit for the chart, if the unit is needed.
239
- const maxValue = useMemo(() => {
240
- return Math.max.apply(
241
- Math,
242
- vegaData.map(function (datum: {
243
- timestamp: number;
244
- label: string;
245
- value: number | 'NAN';
246
- }): number {
247
- if (datum.value && typeof datum.value === 'number') {
248
- return datum.value;
249
- }
250
-
251
- return 0;
252
- }),
253
- );
254
- }, [vegaData]);
255
- // 4. Recompute the value base on the unit
256
- const valueBase = useMemo(() => {
257
- return unitRange ? getUnitLabel(unitRange, maxValue).valueBase : 1;
258
- }, [maxValue, unitRange]);
259
- const vegaDataWithUnit = unitRange
260
- ? convertDataBaseValue(vegaData, valueBase)
261
- : vegaData;
262
- // 5. Convert the values of metric prefix `read` and `out` to negative.
263
- const vegaSpecValues = vegaDataWithUnit.map(
264
- (data: {
265
- timestamp: number;
266
- label: string;
267
- // same as the tooltip label
268
- value: number | 'NAN';
269
- // manually set it to a string. It's for the tooltip to display a hyphen for the data that don't exist
270
- isNegativeValue: boolean;
271
- }) => {
272
- if (
273
- data.isNegativeValue &&
274
- data.value &&
275
- typeof data.value === 'number'
276
- ) {
277
- return { ...data, value: 0 - data.value };
278
- } else return { ...data };
279
- },
280
- );
281
- //##### Data Transformation End
282
- const customizedColorRange = useMemo(() => {
283
- const customizedColors = [];
284
- series.map((line) => {
285
- if (line.color) {
286
- return customizedColors.push(line.color);
287
- }
288
- });
289
- return customizedColors;
290
- }, [series]);
291
- // $FlowFixMe
292
- const seriesResources = [...new Set(series.map((serie) => serie.resource))];
293
- // for the series with the same resource, the color of legend and tooltip should be the same.
294
- const legendLabels = useMemo(() => {
295
- const uniqueLabel = [];
296
- series.forEach((serie, index) => {
297
- if (serie.getLegendLabel) {
298
- const legend = serie.getLegendLabel(serie.metricPrefix, serie.resource);
299
-
300
- if (!uniqueLabel.find((uLabel) => uLabel === legend)) {
301
- const serieIndex =
302
- yAxisType === 'symmetrical' && !customizedColorRange.length
303
- ? seriesResources.findIndex(
304
- (serieResource) => serieResource === serie.resource,
305
- )
306
- : index;
307
- uniqueLabel.push({
308
- legend,
309
- serie,
310
- serieIndex,
311
- });
312
- }
313
- }
314
- });
315
- return uniqueLabel;
316
- }, [series]);
317
- const tooltipLabels = useMemo(
318
- () =>
319
- series.map((line) => {
320
- return line.getTooltipLabel(line.metricPrefix, line.resource);
321
- }),
322
- [series],
323
- );
324
- const syncedVerticalRuler = {
325
- mark: 'rule',
326
- encoding: {
327
- x: {
328
- datum: {
329
- expr: 'toDate(cursorX)',
330
- }, // convert the timestamp in milliseconds to Date object
331
- //https://vega.github.io/vega-lite/docs/datetime.html
332
- },
333
- color: {
334
- value: theme.selectedActive,
335
- },
336
-
337
- /*
338
- According to the design, the vertical ruler should be hided when the mouse points out of the graph area.
339
- We can use param `isCursorDisplayed` to control the size of the vertical line.
340
- */
341
- size: {
342
- value: 0,
343
- condition: {
344
- test: 'isCursorDisplayed',
345
- value: 1,
346
- },
347
- },
348
- },
349
- };
350
- const syncedVerticalRulerPercentage = {
351
- mark: 'rule',
352
- encoding: {
353
- x: {
354
- datum: {
355
- expr: 'toDate(cursorX)',
356
- }, // convert the timestamp to Date object
357
- },
358
- // We draw the line manually for the percentage chart to solve the issue that the synced vertical line can
359
- // only reach the max value one the line.
360
- y: {
361
- datum: 0,
362
- },
363
- y2: {
364
- datum: 100,
365
- },
366
- color: {
367
- value: theme.highlight,
368
- opacity: 0.3,
369
- },
370
-
371
- /*
372
- According to the design, the vertical ruler should be hided when the mouse points out of the graph area.
373
- We can use param `isCursorDisplayed` to control the size of the vertical line.
374
- */
375
- size: {
376
- value: 0,
377
- condition: {
378
- test: 'isCursorDisplayed',
379
- value: 1,
380
- },
381
- },
382
- },
383
- };
384
- const syncedVerticalRulerSymmetrical = {
385
- mark: 'rule',
386
- encoding: {
387
- x: {
388
- datum: {
389
- expr: 'toDate(cursorX)',
390
- }, // convert the timestamp to Date object
391
- },
392
- y: {
393
- expr: '-yAxisMaxValue',
394
- },
395
- y2: {
396
- expr: 'yAxisMaxValue',
397
- },
398
- color: {
399
- value: theme.highlight,
400
- opacity: 0.3,
401
- },
402
-
403
- /*
404
- According to the design, the vertical ruler should be hided when the mouse points out of the graph area.
405
- We can use param `isCursorDisplayed` to control the size of the vertical line.
406
- */
407
- size: {
408
- value: 0,
409
- condition: {
410
- test: 'isCursorDisplayed',
411
- value: 1,
412
- },
413
- },
414
- },
415
- };
416
- const xAxis = {
417
- field: 'timestamp',
418
- type: 'temporal',
419
- axis: {
420
- // Refer to all the available time format: https://github.com/d3/d3-time-format#locale_format
421
- format: '%d %b %H:%M',
422
- ticks: true,
423
- tickCount: 5,
424
- labelColor: theme.textSecondary,
425
- // TODO: labelFontSize is not responsiveness
426
- labelSeparation: 12, // The minimum separation that must be between label bounding boxes for them to be considered non-overlapping (default 0). This property is ignored if labelOverlap resolution is not enabled.
427
- },
428
- title: null,
429
- };
430
- const yAxis = useMemo(() => {
431
- return {
432
- field: 'value',
433
- type: 'quantitative',
434
- axis: {
435
- title: yAxisTitle ? yAxisTitle : ' ',
436
- orient: 'right',
437
- translate: -5,
438
- // translate both the x and y coordinates by 5 pixel
439
- tickOffset: 5,
440
- // shift back the y translate to make sure the tick align with the 0 seperation line
441
- labelBaseline: 'middle',
442
- labelPadding: 6,
443
- labelFlush: true,
444
- },
445
- scale:
446
- yAxisType === 'symmetrical'
447
- ? {
448
- domain: [
449
- {
450
- expr: '-yAxisMaxValue',
451
- },
452
- {
453
- expr: 'yAxisMaxValue',
454
- },
455
- ],
456
- }
457
- : yAxisType === 'percentage'
458
- ? {
459
- domain: [0, 100],
460
- }
461
- : undefined,
462
- };
463
- }, [yAxisTitle, yAxisType]);
464
- const symmetricalColorRange =
465
- yAxisType !== 'symmetrical'
466
- ? colorRange
467
- : series.map(
468
- (serie) => colorRange[seriesResources.indexOf(serie.resource)],
469
- );
470
- const color = {
471
- field: 'label',
472
- type: 'nominal',
473
- scale: {
474
- domain: getColorDomains(series),
475
- // the order of the domain should be the same as the order of colorRange, otherwise the colors will be assigned to the line base on the alphabetical order: ;
476
- range: customizedColorRange.length
477
- ? customizedColorRange
478
- : symmetricalColorRange, //if there is no customized color range, we will use the default the line color
479
- },
480
- legend: null,
481
- };
482
-
483
- // In order to add the tooltip we refered this example
484
- // https://vega.github.io/vega-lite/examples/interactive_multi_line_pivot_tooltip.html
485
- const getTooltipConfig = (
486
- fields: {
487
- field: string;
488
- type: string;
489
- title: string;
490
- format: string;
491
- }[],
492
- ) => {
493
- const tooltipConfigBase = {
494
- // The pivot transform maps unique values from a field to new aggregated fields (columns) in the output stream.
495
- // https://vega.github.io/vega-lite/docs/pivot.html
496
- transform: [
497
- {
498
- pivot: 'label',
499
- value: 'value',
500
- groupby: ['timestamp'],
501
- },
502
- ],
503
- mark: 'rule',
504
- encoding: {
505
- x: xAxis,
506
- opacity: {
507
- // to be check if we can remove this channel, since we don't need to have a rule next to the tooltip
508
- condition: {
509
- value: 0,
510
- selection: 'hover',
511
- },
512
- value: 0,
513
- },
514
- tooltip: [
515
- {
516
- field: 'timestamp',
517
- type: 'temporal',
518
- axis: {
519
- format: '%d %b %H:%M:%S',
520
- ticks: true,
521
- tickCount: 4,
522
- labelAngle: -50,
523
- labelColor: '#B5B5B5',
524
- },
525
- title: 'title',
526
- },
527
- ],
528
- },
529
- selection: {
530
- hover: {
531
- type: 'single',
532
- fields: ['timestamp'],
533
- nearest: true,
534
- on: 'mouseover',
535
- empty: 'none',
536
- clear: 'mouseout',
537
- },
538
- },
539
- };
540
-
541
- if (fields.length) {
542
- const newFields = [...tooltipConfigBase.encoding.tooltip, ...fields];
543
- const newConfig = Object.assign({}, tooltipConfigBase);
544
- newConfig.encoding.tooltip = newFields;
545
- return newConfig;
546
- }
547
-
548
- return tooltipConfigBase;
549
- };
550
-
551
- const tooltipConfig = useMemo(
552
- () =>
553
- getTooltipConfig(
554
- (() => {
555
- const res = [];
556
- tooltipLabels.forEach((label) => {
557
- res.push({
558
- field: `${normlizeVegaFieldName(label)}`,
559
- type: 'quantitative',
560
- title: `${label}`,
561
- format: '.2f',
562
- formatType: 'negativeValueFormatter',
563
- });
564
- });
565
- return res;
566
- })(),
567
- ),
568
- [tooltipLabels],
569
- );
570
- // we need to retrieve the vega view in order to update the signal
571
- useLayoutEffect(() => {
572
- if (vegaViewRef.current && yAxisType === 'symmetrical') {
573
- vegaViewRef.current
574
- .signal(
575
- 'yAxisMaxValue',
576
- Math.ceil(getRelativeValue(maxValue, valueBase)),
577
- )
578
- .run();
579
- }
580
- }, [maxValue, valueBase, vegaViewRef, yAxisType]);
581
- // $FlowFixMe
582
- const cursorX = useCursorX().cursorX;
583
- // the specification of the Vega-lite chart
584
- const spec = {
585
- data: {
586
- values: vegaSpecValues,
587
- },
588
- height,
589
- width: 'container',
590
- // set responsive width
591
- mark: {
592
- type: 'line',
593
- tooltip: true,
594
- },
595
- // Add two params to control the display of the vertical ruler.
596
- params: [
597
- {
598
- name: 'cursorX',
599
- value: cursorX || Date.now(), // the value of signal can't be null
600
- },
601
- {
602
- name: 'isCursorDisplayed',
603
- value: false,
604
- },
605
- ],
606
- layer: [
607
- {
608
- encoding: {
609
- x: xAxis,
610
- y: yAxis,
611
- strokeDash: {
612
- field: 'isDashed',
613
- type: 'nominal',
614
- legend: null,
615
- condition: {
616
- test: 'datum.isDashed === true',
617
- value: [4, 2], // Change the value here if the dash is not visible. https://vega.github.io/vega-lite/docs/mark.html#stroke
618
- },
619
- },
620
- color: color,
621
- opacity: {
622
- condition: {
623
- test: 'datum.isDashed === true',
624
- // for the dashed line, set the opacity to 0.5
625
- value: 0.6,
626
- },
627
- value: 1,
628
- },
629
- },
630
- layer: [
631
- {
632
- mark: {
633
- type: 'line',
634
- strokeWidth: 1,
635
- },
636
- }, // the width of the line should be 1px
637
- {
638
- mark: 'point',
639
- encoding: {
640
- size: {
641
- value: 0,
642
- condition: {
643
- selection: 'hover',
644
- value: 10,
645
- },
646
- },
647
- },
648
- },
649
- yAxisType === 'percentage'
650
- ? {
651
- // for percentage chart we need to manually draw the line from 0-100
652
- ...syncedVerticalRuler,
653
- encoding: {
654
- ...syncedVerticalRuler.encoding,
655
- ...syncedVerticalRulerPercentage.encoding,
656
- },
657
- }
658
- : yAxisType === 'symmetrical'
659
- ? {
660
- // for symmetrical chart we manually draw the line from minValue to maxValue
661
- ...syncedVerticalRuler,
662
- encoding: {
663
- ...syncedVerticalRuler.encoding,
664
- ...syncedVerticalRulerSymmetrical.encoding,
665
- },
666
- }
667
- : syncedVerticalRuler,
668
- ],
669
- },
670
- tooltipConfig,
671
- ],
672
- ...rest,
673
- };
674
- // the seperation line for symmetrical charts
675
- const seperationLine = {
676
- mark: 'rule',
677
- encoding: {
678
- y: {
679
- datum: 0,
680
- },
681
- color: {
682
- value: theme.border,
683
- opacity: 1,
684
- },
685
- },
686
- };
687
-
688
- if (yAxisType === 'symmetrical') {
689
- spec.layer.unshift(seperationLine);
690
- spec.params.push({
691
- name: 'yAxisMaxValue',
692
- value: Math.ceil(getRelativeValue(maxValue, valueBase)),
693
- });
694
- }
695
-
696
- const seriesNames = series
697
- .map(
698
- (serie) =>
699
- title + serie.resource + (serie.metricPrefix ? serie.metricPrefix : ''),
700
- )
701
- .join(',');
702
- const unitLabel = unitRange
703
- ? getUnitLabel(unitRange, maxValue).unitLabel
704
- : yAxisType === 'percentage'
705
- ? '%'
706
- : '';
707
- return (
708
- <LineTemporalChartWrapper>
709
- <ChartHeader>
710
- {unitLabel ? (
711
- <ChartTitleText>
712
- {title} ({unitLabel})
713
- </ChartTitleText> // for the chart doesn't have title
714
- ) : (
715
- <ChartTitleText>{title}</ChartTitleText>
716
- )}
717
- {helpText && (
718
- <TooltipComponent
719
- placement="bottom-right"
720
- overlay={
721
- <SmallerText
722
- style={{
723
- minWidth: '15rem',
724
- display: 'block',
725
- }}
726
- >
727
- {helpText}
728
- </SmallerText>
729
- }
730
- >
731
- <Icon name="Info" color={theme.buttonSecondary} />
732
- </TooltipComponent>
733
- )}
734
- {isLoading && (
735
- <Loader
736
- style={{
737
- paddingLeft: `${spacing.r4}`,
738
- }}
739
- />
740
- )}
741
- </ChartHeader>
742
- {/* When the chart is in loading status, we display the chart skeleton */}
743
- <VegaChart
744
- key={seriesNames}
745
- spec={spec}
746
- onHover={(datum) => {
747
- if (onHover) {
748
- onHover({
749
- ...datum,
750
- metadata: {
751
- unitLabel,
752
- valueBase,
753
- },
754
- originalData: {
755
- ...relativeDatumToOriginalDatum(datum, valueBase),
756
- timestamp: datum.timestamp,
757
- },
758
- });
759
- }
760
- }}
761
- theme={'custom'}
762
- ref={vegaViewRef}
763
- formatTooltip={useMemo(
764
- () =>
765
- formatValue(
766
- series,
767
- customizedColorRange,
768
- colorRange,
769
- unitLabel,
770
- yAxisType,
771
- renderTooltipSerie,
772
- ),
773
- [unitLabel, seriesNames, renderTooltipSerie],
774
- )}
775
- ></VegaChart>
776
- {/* if it's for read/write and in/out graph, we only display the legends for the instances. */}
777
- {!isLegendHidden && (
778
- <Legends>
779
- {legendLabels.map(({ legend, serie, serieIndex }, index) => {
780
- return (
781
- <Fragment key={`${title}-${legend}-${index}`}>
782
- <LegendStroke
783
- lineColor={
784
- customizedColorRange.length
785
- ? customizedColorRange[serieIndex]
786
- : colorRange[serieIndex]
787
- }
788
- isLineDashed={serie.isLineDashed}
789
- ></LegendStroke>
790
- <SmallerText>{legend}</SmallerText>
791
- </Fragment>
792
- );
793
- })}
794
- </Legends>
795
- )}
796
- </LineTemporalChartWrapper>
797
- );
798
- }
799
-
800
- export { LineTemporalChart };