@scality/core-ui 0.185.0 → 0.187.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 (177) hide show
  1. package/.storybook/preview.js +1 -7
  2. package/dist/components/{linetemporalchart/MetricTimespanProvider.d.ts → charts/MetricsTimeSpanProvider.d.ts} +1 -1
  3. package/dist/components/charts/MetricsTimeSpanProvider.d.ts.map +1 -0
  4. package/dist/components/{barchartv2/Barchart.component.d.ts → charts/barchart/Barchart.d.ts} +2 -33
  5. package/dist/components/charts/barchart/Barchart.d.ts.map +1 -0
  6. package/dist/components/charts/barchart/Barchart.js +56 -0
  7. package/dist/components/{barchartv2/utils.d.ts → charts/barchart/Barchart.utils.d.ts} +8 -35
  8. package/dist/components/charts/barchart/Barchart.utils.d.ts.map +1 -0
  9. package/dist/components/{barchartv2/utils.js → charts/barchart/Barchart.utils.js} +7 -88
  10. package/dist/components/{barchartv2 → charts/barchart}/BarchartTooltip.d.ts +2 -1
  11. package/dist/components/charts/barchart/BarchartTooltip.d.ts.map +1 -0
  12. package/dist/components/{barchartv2 → charts/barchart}/BarchartTooltip.js +3 -3
  13. package/dist/components/{charttooltip → charts/common}/ChartTooltip.d.ts +9 -2
  14. package/dist/components/charts/common/ChartTooltip.d.ts.map +1 -0
  15. package/dist/components/{charttooltip → charts/common}/ChartTooltip.js +11 -15
  16. package/dist/components/charts/common/SharedComponents.d.ts +47 -0
  17. package/dist/components/charts/common/SharedComponents.d.ts.map +1 -0
  18. package/dist/components/charts/common/SharedComponents.js +73 -0
  19. package/dist/components/charts/common/chartUtils.d.ts +91 -0
  20. package/dist/components/charts/common/chartUtils.d.ts.map +1 -0
  21. package/dist/components/charts/common/chartUtils.js +243 -0
  22. package/dist/components/{globalhealthbar/GlobalHealthBarRecharts.component.d.ts → charts/globalhealthbar/GlobalHealthBar.d.ts} +2 -2
  23. package/dist/components/charts/globalhealthbar/GlobalHealthBar.d.ts.map +1 -0
  24. package/dist/components/{globalhealthbar/useHealthBarData.d.ts → charts/globalhealthbar/GlobalHealthBar.hooks.d.ts} +1 -1
  25. package/dist/components/charts/globalhealthbar/GlobalHealthBar.hooks.d.ts.map +1 -0
  26. package/dist/components/{globalhealthbar/GlobalHealthBarRecharts.component.js → charts/globalhealthbar/GlobalHealthBar.js} +4 -4
  27. package/dist/components/{globalhealthbar/healthBarUtils.d.ts → charts/globalhealthbar/GlobalHealthBar.utils.d.ts} +1 -1
  28. package/dist/components/charts/globalhealthbar/GlobalHealthBar.utils.d.ts.map +1 -0
  29. package/dist/components/{globalhealthbar/healthBarUtils.js → charts/globalhealthbar/GlobalHealthBar.utils.js} +1 -1
  30. package/dist/components/{globalhealthbar/components → charts/globalhealthbar}/GlobalHealthBarTooltip.d.ts +1 -1
  31. package/dist/components/charts/globalhealthbar/GlobalHealthBarTooltip.d.ts.map +1 -0
  32. package/dist/components/{globalhealthbar/components → charts/globalhealthbar}/GlobalHealthBarTooltip.js +7 -3
  33. package/dist/components/charts/globalhealthbar/HealthBarXAxis.d.ts.map +1 -0
  34. package/dist/components/{globalhealthbar/components → charts/globalhealthbar}/HealthBarXAxis.js +1 -1
  35. package/dist/components/charts/index.d.ts +16 -0
  36. package/dist/components/charts/index.d.ts.map +1 -0
  37. package/dist/components/charts/index.js +15 -0
  38. package/dist/components/{chartlegend → charts/legend}/ChartLegend.d.ts +1 -1
  39. package/dist/components/charts/legend/ChartLegend.d.ts.map +1 -0
  40. package/dist/components/{chartlegend → charts/legend}/ChartLegend.js +2 -2
  41. package/dist/components/{chartlegend → charts/legend}/ChartLegendWrapper.d.ts +1 -1
  42. package/dist/components/charts/legend/ChartLegendWrapper.d.ts.map +1 -0
  43. package/dist/components/{linetimeseriechart/linetimeseriechart.component.d.ts → charts/linetimeseries/LineTimeSerieChart.d.ts} +12 -2
  44. package/dist/components/charts/linetimeseries/LineTimeSerieChart.d.ts.map +1 -0
  45. package/dist/components/{linetimeseriechart/linetimeseriechart.component.js → charts/linetimeseries/LineTimeSerieChart.js} +34 -35
  46. package/dist/components/charts/linetimeseries/LineTimeSerieChart.utils.d.ts +14 -0
  47. package/dist/components/charts/linetimeseries/LineTimeSerieChart.utils.d.ts.map +1 -0
  48. package/dist/components/{linetimeseriechart/utils.js → charts/linetimeseries/LineTimeSerieChart.utils.js} +4 -6
  49. package/dist/components/charts/sparkline/Sparkline.d.ts +23 -0
  50. package/dist/components/charts/sparkline/Sparkline.d.ts.map +1 -0
  51. package/dist/components/{sparkline/sparkline.component.js → charts/sparkline/Sparkline.js} +12 -6
  52. package/dist/components/charts/types.d.ts +34 -0
  53. package/dist/components/charts/types.d.ts.map +1 -0
  54. package/dist/components/charts/types.js +4 -0
  55. package/dist/components/icon/Icon.component.d.ts +1 -0
  56. package/dist/components/icon/Icon.component.d.ts.map +1 -1
  57. package/dist/components/icon/Icon.component.js +2 -2
  58. package/dist/components/textbadge/TextBadge.component.d.ts +1 -1
  59. package/dist/components/textbadge/TextBadge.component.d.ts.map +1 -1
  60. package/dist/components/tooltip/Tooltip.component.d.ts.map +1 -1
  61. package/dist/components/tooltip/Tooltip.component.js +16 -29
  62. package/dist/index.d.ts +0 -2
  63. package/dist/index.d.ts.map +1 -1
  64. package/dist/index.js +4 -2
  65. package/dist/next.d.ts +3 -11
  66. package/dist/next.d.ts.map +1 -1
  67. package/dist/next.js +4 -11
  68. package/package.json +2 -6
  69. package/src/lib/components/{barchartv2/Barchart.component.test.tsx → charts/barchart/Barchart.test.tsx} +35 -10
  70. package/src/lib/components/{barchartv2/Barchart.component.tsx → charts/barchart/Barchart.tsx} +42 -207
  71. package/src/lib/components/{barchartv2/utils.test.ts → charts/barchart/Barchart.utils.test.ts} +2 -141
  72. package/src/lib/components/{barchartv2/utils.ts → charts/barchart/Barchart.utils.ts} +17 -143
  73. package/src/lib/components/{barchartv2 → charts/barchart}/BarchartTooltip.tsx +5 -9
  74. package/src/lib/components/{charttooltip → charts/common}/ChartTooltip.tsx +13 -20
  75. package/src/lib/components/charts/common/SharedComponents.tsx +180 -0
  76. package/src/lib/components/charts/common/chartUtils.test.ts +402 -0
  77. package/src/lib/components/charts/common/chartUtils.ts +334 -0
  78. package/src/lib/components/{globalhealthbar/useHealthBarData.spec.tsx → charts/globalhealthbar/GlobalHealthBar.hooks.test.tsx} +1 -1
  79. package/src/lib/components/{globalhealthbar/GlobalHealthBarRecharts.component.tsx → charts/globalhealthbar/GlobalHealthBar.tsx} +4 -4
  80. package/src/lib/components/{globalhealthbar/healthBarUtils.spec.ts → charts/globalhealthbar/GlobalHealthBar.utils.test.ts} +1 -1
  81. package/src/lib/components/{globalhealthbar/healthBarUtils.ts → charts/globalhealthbar/GlobalHealthBar.utils.ts} +1 -1
  82. package/src/lib/components/{globalhealthbar/components → charts/globalhealthbar}/GlobalHealthBarTooltip.tsx +8 -4
  83. package/src/lib/components/{globalhealthbar/components → charts/globalhealthbar}/HealthBarXAxis.tsx +1 -1
  84. package/src/lib/components/charts/index.ts +59 -0
  85. package/src/lib/components/{chartlegend → charts/legend}/ChartLegend.tsx +2 -2
  86. package/src/lib/components/{chartlegend → charts/legend}/ChartLegendWrapper.tsx +1 -1
  87. package/src/lib/components/{linetimeseriechart/linetimeseriechart.test.tsx → charts/linetimeseries/LineTimeSerieChart.test.tsx} +3 -6
  88. package/src/lib/components/{linetimeseriechart/linetimeseriechart.component.tsx → charts/linetimeseries/LineTimeSerieChart.tsx} +48 -44
  89. package/src/lib/components/{linetimeseriechart/utils.test.ts → charts/linetimeseries/LineTimeSerieChart.utils.test.ts} +1 -1
  90. package/src/lib/components/{linetimeseriechart/utils.ts → charts/linetimeseries/LineTimeSerieChart.utils.ts} +4 -6
  91. package/src/lib/components/charts/sparkline/Sparkline.tsx +80 -0
  92. package/src/lib/components/charts/types.ts +36 -0
  93. package/src/lib/components/icon/Icon.component.tsx +12 -8
  94. package/src/lib/components/textbadge/TextBadge.component.tsx +1 -1
  95. package/src/lib/components/tooltip/Tooltip.component.tsx +37 -47
  96. package/src/lib/index.ts +4 -2
  97. package/src/lib/next.ts +26 -13
  98. package/stories/BarChart/barchart.stories.tsx +10 -9
  99. package/stories/GlobalHealthBar/{globalhealthbarRecharts.stories.tsx → globalhealthbar.stories.tsx} +3 -21
  100. package/stories/GlobalHealthBar/globalheathbarrecharts.guideline.mdx +2 -2
  101. package/stories/common.tsx +1 -1
  102. package/stories/guideline/chart-guideline.mdx +1 -38
  103. package/stories/linetimeseriechart.stories.tsx +4 -6
  104. package/stories/sparkline.stories.tsx +13 -11
  105. package/stories/tablev2.stories.tsx +1 -1
  106. package/stories/textbadge.stories.tsx +15 -0
  107. package/dist/components/barchart/BarChart.component.d.ts +0 -17
  108. package/dist/components/barchart/BarChart.component.d.ts.map +0 -1
  109. package/dist/components/barchart/BarChart.component.js +0 -27
  110. package/dist/components/barchartv2/Barchart.component.d.ts.map +0 -1
  111. package/dist/components/barchartv2/Barchart.component.js +0 -122
  112. package/dist/components/barchartv2/BarchartTooltip.d.ts.map +0 -1
  113. package/dist/components/barchartv2/utils.d.ts.map +0 -1
  114. package/dist/components/chartlegend/ChartLegend.d.ts.map +0 -1
  115. package/dist/components/chartlegend/ChartLegendWrapper.d.ts.map +0 -1
  116. package/dist/components/charttooltip/ChartTooltip.d.ts.map +0 -1
  117. package/dist/components/globalhealthbar/GlobalHealthBar.component.d.ts +0 -23
  118. package/dist/components/globalhealthbar/GlobalHealthBar.component.d.ts.map +0 -1
  119. package/dist/components/globalhealthbar/GlobalHealthBar.component.js +0 -173
  120. package/dist/components/globalhealthbar/GlobalHealthBarRecharts.component.d.ts.map +0 -1
  121. package/dist/components/globalhealthbar/components/GlobalHealthBarTooltip.d.ts.map +0 -1
  122. package/dist/components/globalhealthbar/components/HealthBarXAxis.d.ts.map +0 -1
  123. package/dist/components/globalhealthbar/healthBarUtils.d.ts.map +0 -1
  124. package/dist/components/globalhealthbar/healthBarUtils.spec.d.ts +0 -2
  125. package/dist/components/globalhealthbar/healthBarUtils.spec.d.ts.map +0 -1
  126. package/dist/components/globalhealthbar/healthBarUtils.spec.js +0 -391
  127. package/dist/components/globalhealthbar/tooltip/index.d.ts +0 -8
  128. package/dist/components/globalhealthbar/tooltip/index.d.ts.map +0 -1
  129. package/dist/components/globalhealthbar/tooltip/index.js +0 -55
  130. package/dist/components/globalhealthbar/useHealthBarData.d.ts.map +0 -1
  131. package/dist/components/globalhealthbar/useHealthBarData.spec.d.ts +0 -2
  132. package/dist/components/globalhealthbar/useHealthBarData.spec.d.ts.map +0 -1
  133. package/dist/components/globalhealthbar/useHealthBarData.spec.js +0 -209
  134. package/dist/components/linetemporalchart/ChartUtil.d.ts +0 -41
  135. package/dist/components/linetemporalchart/ChartUtil.d.ts.map +0 -1
  136. package/dist/components/linetemporalchart/ChartUtil.js +0 -148
  137. package/dist/components/linetemporalchart/LineTemporalChart.component.d.ts +0 -46
  138. package/dist/components/linetemporalchart/LineTemporalChart.component.d.ts.map +0 -1
  139. package/dist/components/linetemporalchart/LineTemporalChart.component.js +0 -579
  140. package/dist/components/linetemporalchart/MetricTimespanProvider.d.ts.map +0 -1
  141. package/dist/components/linetemporalchart/tooltip/index.d.ts +0 -30
  142. package/dist/components/linetemporalchart/tooltip/index.d.ts.map +0 -1
  143. package/dist/components/linetemporalchart/tooltip/index.js +0 -104
  144. package/dist/components/linetimeseriechart/linetimeseriechart.component.d.ts.map +0 -1
  145. package/dist/components/linetimeseriechart/utils.d.ts +0 -16
  146. package/dist/components/linetimeseriechart/utils.d.ts.map +0 -1
  147. package/dist/components/sparkline/sparkline.component.d.ts +0 -17
  148. package/dist/components/sparkline/sparkline.component.d.ts.map +0 -1
  149. package/dist/components/vegachartv2/SyncedCursorCharts.d.ts +0 -12
  150. package/dist/components/vegachartv2/SyncedCursorCharts.d.ts.map +0 -1
  151. package/dist/components/vegachartv2/SyncedCursorCharts.js +0 -15
  152. package/dist/components/vegachartv2/VegaChartV2.component.d.ts +0 -15
  153. package/dist/components/vegachartv2/VegaChartV2.component.d.ts.map +0 -1
  154. package/dist/components/vegachartv2/VegaChartV2.component.js +0 -218
  155. package/src/lib/components/barchart/BarChart.component.tsx +0 -51
  156. package/src/lib/components/globalhealthbar/GlobalHealthBar.component.tsx +0 -204
  157. package/src/lib/components/globalhealthbar/tooltip/index.ts +0 -72
  158. package/src/lib/components/linetemporalchart/ChartUtil.test.ts +0 -250
  159. package/src/lib/components/linetemporalchart/ChartUtil.ts +0 -225
  160. package/src/lib/components/linetemporalchart/LineTemporalChart.component.tsx +0 -800
  161. package/src/lib/components/linetemporalchart/tooltip/index.ts +0 -178
  162. package/src/lib/components/sparkline/sparkline.component.tsx +0 -56
  163. package/src/lib/components/vegachartv2/SyncedCursorCharts.tsx +0 -28
  164. package/src/lib/components/vegachartv2/VegaChartV2.component.tsx +0 -276
  165. package/stories/barchart.stories.tsx +0 -142
  166. package/stories/globalhealthbar.stories.tsx +0 -191
  167. package/stories/guideline/mdxExampleComponents.tsx +0 -57
  168. package/stories/linecharttemporal.stories.tsx +0 -88
  169. /package/dist/components/{linetemporalchart/MetricTimespanProvider.js → charts/MetricsTimeSpanProvider.js} +0 -0
  170. /package/dist/components/{globalhealthbar/useHealthBarData.js → charts/globalhealthbar/GlobalHealthBar.hooks.js} +0 -0
  171. /package/dist/components/{globalhealthbar/components → charts/globalhealthbar}/HealthBarXAxis.d.ts +0 -0
  172. /package/dist/components/{chartlegend → charts/legend}/ChartLegendWrapper.js +0 -0
  173. /package/src/lib/components/{linetemporalchart/MetricTimespanProvider.tsx → charts/MetricsTimeSpanProvider.tsx} +0 -0
  174. /package/src/lib/components/{barchartv2 → charts/barchart}/BarchartTooltip.test.tsx +0 -0
  175. /package/src/lib/components/{globalhealthbar/useHealthBarData.ts → charts/globalhealthbar/GlobalHealthBar.hooks.ts} +0 -0
  176. /package/src/lib/components/{chartlegend → charts/legend}/ChartLegend.test.tsx +0 -0
  177. /package/src/lib/components/{chartlegend → charts/legend}/ChartLegendWrapper.test.tsx +0 -0
@@ -1,178 +0,0 @@
1
- // @ts-nocheck
2
- import { stringify } from 'vega-lite';
3
- import { Options } from 'vega-tooltip';
4
- import { Handler } from 'vega-tooltip';
5
- import { isArray, isObject, isString } from 'vega-util';
6
- import { spacing } from '../../../style/theme';
7
- import { Serie } from '../LineTemporalChart.component';
8
- export function defaultRenderTooltipSerie({
9
- color,
10
- isLineDashed,
11
- name,
12
- value,
13
- key,
14
- }: {
15
- color?: string;
16
- isLineDashed?: boolean;
17
- name: string;
18
- value: string;
19
- key: string;
20
- }) {
21
- return `<tr>
22
- <td class="color">
23
- ${
24
- color !== undefined
25
- ? `<span style='background: ${
26
- isLineDashed
27
- ? `repeating-linear-gradient(to right,${color} 0,${color} ${spacing.sp1},transparent ${spacing.sp1},transparent ${spacing.sp2})`
28
- : color
29
- };width: ${spacing.sp8};height:${
30
- spacing.sp2
31
- };display: inline-block;vertical-align: middle;'></span>`
32
- : ''
33
- }
34
- </td>
35
- <td class="key" style="text-align: left;">
36
- ${name}
37
- </td>
38
- <td class="value" style="text-align: right;">
39
- ${value}
40
- </td>
41
- </tr>`;
42
- }
43
- export class TooltipHandlerWithPaint extends Handler {
44
- constructor(options?: Options, onHover?: (datum: any) => void) {
45
- super(options);
46
- this.prevCall = this.call;
47
- this.onHover = onHover;
48
- this.call = this.newCall.bind(this);
49
- }
50
-
51
- newCall(handler: any, event: MouseEvent, item: any, value: any) {
52
- if (
53
- item &&
54
- this.onHover &&
55
- JSON.stringify(value) !== JSON.stringify(this.value)
56
- ) {
57
- this.onHover(item.datum.datum);
58
- }
59
-
60
- this.handler = handler;
61
- this.event = event;
62
- this.item = item;
63
- this.value = value;
64
- this.prevCall(handler, event, item, value);
65
- }
66
-
67
- paint() {
68
- this.prevCall(this.handler, this.event, this.item, this.value);
69
- }
70
- }
71
-
72
- /**
73
- * Format the value to be shown in the tooltip.
74
- *
75
- * @param value The value to show in the tooltip.
76
- * @param valueToHtml Function to convert a single cell value to an HTML string
77
- */
78
- export function formatValue(
79
- series: Serie[],
80
- customizedColorRange: string[],
81
- colorRange: string[],
82
- unitLabel: string,
83
- yAxisType?: 'default' | 'percentage' | 'symmetrical',
84
- renderTooltipSerie: (
85
- arg0: {
86
- color?: string;
87
- isLineDashed?: boolean;
88
- name: string;
89
- value: string;
90
- key: string;
91
- unitLabel: string;
92
- },
93
- tooltipData: any,
94
- ) => string = defaultRenderTooltipSerie,
95
- ) {
96
- return (
97
- value: any,
98
- valueToHtml: (value: any) => string,
99
- maxDepth: number,
100
- ): string => {
101
- if (isArray(value)) {
102
- return `[${value
103
- .map((v) => valueToHtml(isString(v) ? v : stringify(v, maxDepth)))
104
- .join(', ')}]`;
105
- }
106
-
107
- if (isObject(value)) {
108
- let content = '';
109
- const { title, image, ...rest } = value;
110
-
111
- if (title) {
112
- content += `<h2 style="text-align: center;">${valueToHtml(title)}</h2>`;
113
- }
114
-
115
- if (image) {
116
- content += `<img src="${valueToHtml(image)}">`;
117
- }
118
-
119
- const keys = Object.keys(rest);
120
-
121
- if (keys.length > 0) {
122
- content += '<table>';
123
-
124
- for (const key of keys) {
125
- let val = rest[key];
126
-
127
- // ignore undefined properties
128
- if (val === undefined) {
129
- continue;
130
- }
131
-
132
- if (isObject(val)) {
133
- val = stringify(val, maxDepth);
134
- }
135
-
136
- const currentSerie = series.find(
137
- (serie) =>
138
- serie.getTooltipLabel(serie.metricPrefix, serie.resource) === key,
139
- );
140
- const currentSerieIndex = series.findIndex(
141
- (serie) =>
142
- serie.getTooltipLabel(serie.metricPrefix, serie.resource) === key,
143
- );
144
- const serieIndex =
145
- yAxisType === 'symmetrical' && !customizedColorRange.length // $FlowFixMe
146
- ? [...new Set(series.map((serie) => serie.resource))].findIndex(
147
- (serieResource) =>
148
- serieResource ===
149
- (currentSerie ? currentSerie.resource : null),
150
- )
151
- : currentSerieIndex;
152
- const serieColorRange = customizedColorRange.length
153
- ? customizedColorRange
154
- : colorRange;
155
- content += renderTooltipSerie(
156
- {
157
- key,
158
- color:
159
- serieIndex !== -1 ? serieColorRange[serieIndex] : undefined,
160
- isLineDashed:
161
- serieIndex !== -1 ? series[serieIndex].isLineDashed : undefined,
162
- name: valueToHtml(key),
163
- value: val !== 'NaN' ? `${valueToHtml(val)} ${unitLabel}` : '-',
164
- unitLabel,
165
- },
166
- value,
167
- );
168
- }
169
-
170
- content += `</table>`;
171
- }
172
-
173
- return content || '{}'; // show empty object if there are no properties
174
- }
175
-
176
- return valueToHtml(value);
177
- };
178
- }
@@ -1,56 +0,0 @@
1
- import { useMemo } from "react";
2
- import { Area, AreaChart, CartesianGrid, ResponsiveContainer, YAxis } from "recharts";
3
- import { useTheme } from "styled-components";
4
- import { chartColors } from "../../style/theme";
5
- import { addMissingDataPoint } from "../linetemporalchart/ChartUtil";
6
-
7
- type SparklineProps = {
8
- serie: {
9
- data: [number, number|null][],
10
- color?: string, // exa color code like '#ff0000'
11
- },
12
- startingTimeStamp: number,
13
- sampleDuration: number,
14
- sampleInterval: number,
15
- yAxisType?: 'default' | 'percentage',
16
- };
17
-
18
- /**
19
- * Sparkline is a simple dynamically sized area chart.
20
- * Used to show trends in data over time.
21
- */
22
- export function Sparkline({ serie, startingTimeStamp, sampleDuration, sampleInterval, yAxisType }: SparklineProps) {
23
- const data = useMemo(
24
- () => {
25
- const dataMdp = addMissingDataPoint(serie.data, startingTimeStamp, sampleDuration, sampleInterval);
26
- return dataMdp.map(([x, y]) => ({ x, y }));
27
- },
28
- [serie.data]
29
- );
30
- const color = serie.color ?? chartColors.lineColor1;
31
- const strokeGridColor = useTheme().border;
32
-
33
- return (
34
- <ResponsiveContainer>
35
- <AreaChart data={data}>
36
- <defs>
37
- <linearGradient id={`gradient-${color}`} x1="0" y1="0" x2="0" y2="1">
38
- <stop offset="0%" stopColor={color} stopOpacity={0.7} />
39
- <stop offset="100%" stopColor={color} stopOpacity={0.1} />
40
- </linearGradient>
41
- </defs>
42
- <CartesianGrid horizontal={false} stroke={strokeGridColor} strokeOpacity={0.5} />
43
- <Area
44
- type="linear"
45
- dataKey="y"
46
- stroke={color}
47
- fill={`url(#gradient-${color})`}
48
- dot={false}
49
- activeDot={false}
50
- isAnimationActive={false}
51
- />
52
- {yAxisType === 'percentage' && <YAxis domain={[0, 100]} hide />}
53
- </AreaChart>
54
- </ResponsiveContainer>
55
- );
56
- }
@@ -1,28 +0,0 @@
1
- import { useState, createContext, useContext, useMemo, useCallback } from 'react';
2
- export const SyncedCursorChartsContext = createContext<{
3
- cursorX: number;
4
- setCursorX: (cursorX: number) => void;
5
- } | null>(null);
6
- export const useCursorX = (): {
7
- cursorX: number;
8
- setCursorX: (cursorX: number) => void;
9
- } | null => {
10
- const contextValue = useContext(SyncedCursorChartsContext);
11
-
12
- if (contextValue === null) {
13
- console.error("Can't use useCursorX() outside SyncedCursorCharts");
14
- }
15
-
16
- return contextValue;
17
- };
18
- export function SyncedCursorCharts({ children }: { children: JSX.Element }) {
19
- const [cursorX, setCursorX] = useState(0);
20
-
21
- const contextValue = useMemo(() => ({cursorX, setCursorX}), [cursorX]);
22
-
23
- return (
24
- <SyncedCursorChartsContext.Provider value={contextValue}>
25
- {children}
26
- </SyncedCursorChartsContext.Provider>
27
- );
28
- }
@@ -1,276 +0,0 @@
1
- // @ts-nocheck
2
- import { useEffect, useRef, useLayoutEffect, useMemo, forwardRef } from 'react';
3
- import * as vega from 'vega';
4
- import vegaEmbed, { Result } from 'vega-embed';
5
- import { createGlobalStyle, css, useTheme } from 'styled-components';
6
- import { useCursorX, SyncedCursorChartsContext } from './SyncedCursorCharts';
7
- import { TooltipHandlerWithPaint } from '../linetemporalchart/tooltip';
8
- export const TOP = 'top';
9
- export const BOTTOM = 'bottom';
10
- type Position = typeof TOP | typeof BOTTOM;
11
- type Props = {
12
- spec: Record<string, any>;
13
- tooltipPosition?: Position;
14
- theme?: 'light' | 'dark' | 'custom';
15
- id?: string;
16
- onHover?: (dataPoint: any) => void;
17
- formatTooltip?: (
18
- value: any,
19
- valueToHtml: (value: any) => string,
20
- maxDepth: number,
21
- ) => string;
22
- };
23
-
24
- /* How to theme tooltip:
25
- https://github.com/vega/vega-tooltip/blob/master/docs/customizing_your_tooltip.md
26
- */
27
- const VegaTooltipTheme = createGlobalStyle`
28
- #vg-tooltip-element.vg-tooltip.custom-theme {
29
- ${(props) => {
30
- const { theme } = props;
31
- return css`
32
- padding: 8px;
33
- position: fixed;
34
- z-index: 1000;
35
- width: calc(100vw / 6);
36
- font-family: 'Lato';
37
- font-size: 12px;
38
- border-radius: 3px;
39
- box-shadow: 2px 2px 4px rgba(0, 0, 0, 0.1);
40
- color: ${theme.textPrimary};
41
- background-color: ${theme.backgroundLevel1};
42
- border: 1px solid ${theme.border};
43
- // customize the title
44
- h2 {
45
- color: ${theme.textPrimary};
46
- margin-bottom: 10px;
47
- font-size: 12px;
48
- }
49
- table {
50
- width: 100%;
51
- }
52
- table tr td.key {
53
- color: ${theme.textSecondary};
54
- }
55
- `;
56
- }}
57
-
58
- }
59
- `;
60
-
61
- function VegaChartInternal(
62
- {
63
- spec,
64
- tooltipPosition = BOTTOM,
65
- theme = 'custom',
66
- formatTooltip,
67
- onHover,
68
- }: Props,
69
- ref?: {
70
- current: typeof vega.View | null;
71
- },
72
- ) {
73
- // $FlowFixMe
74
- const { cursorX, setCursorX } = useCursorX();
75
- const currentTheme = useTheme();
76
- const themeConfig = {
77
- config: {
78
- background: 'transparent',
79
- axis: {
80
- labelColor: currentTheme.textSecondary,
81
- titleColor: currentTheme.textSecondary,
82
- grid: false,
83
- domainColor: 'transparent',
84
- },
85
- title: {
86
- color: currentTheme.textPrimary,
87
- font: 'Lato',
88
- },
89
- view: {
90
- stroke: currentTheme.border,
91
- strokeWidth: 0.5,
92
- fill: currentTheme.backgroundLevel1,
93
- },
94
- // the headers provide a title and labels for faceted plots.
95
- header: {
96
- labelColor: currentTheme.textPrimary,
97
- },
98
- // the label of max/min
99
- text: {
100
- color: currentTheme.textPrimary,
101
- font: 'Lato',
102
- },
103
- legend: {
104
- labelColor: currentTheme.textSecondary,
105
- },
106
- },
107
- };
108
- const themedSpec = { ...spec, ...themeConfig };
109
- const vegaInstance = useRef<Result>();
110
- const vegaDOMInstance = useRef<HTMLDivElement | null>(null);
111
- let tooltipOptions = {
112
- theme: theme,
113
- formatTooltip: formatTooltip,
114
- };
115
-
116
- if (tooltipPosition === TOP) {
117
- tooltipOptions = {
118
- theme: theme,
119
- offsetX: -85,
120
- offsetY: -140,
121
- formatTooltip: formatTooltip,
122
- };
123
- }
124
-
125
- const tooltipHandler = useMemo(
126
- () => new TooltipHandlerWithPaint(tooltipOptions, onHover),
127
- [theme],
128
- );
129
-
130
- /*
131
- useEffect() and useEffectLayout():
132
- The first effect will only render once, to initalize the chart and add the event lisener.
133
- The second useEffectLayout is in charge of updating the chart when the `themedSpec` or `tooltipOptions` get updated.
134
- Note it's important to useEffectLayout for the performance.
135
- */
136
- useEffect(() => {
137
- let isMounted = true;
138
- // embed(el, spec[, opt]) the el can be a DOM element or CSS selector. https://github.com/vega/vega-embed
139
- vegaDOMInstance &&
140
- vegaDOMInstance.current &&
141
- vegaEmbed(vegaDOMInstance.current, themedSpec, {
142
- renderer: 'svg',
143
- // Override the DEFAULT_OPTIONS https://github.com/vega/vega-tooltip/blob/master/src/defaults.ts
144
- tooltip: tooltipHandler.call,
145
-
146
- /* Determines if action links
147
- ("Export as PNG/SVG", "View Source", "View Vega" (only for Vega-Lite), "Open in Vega Editor") are included with the embedded view.
148
- If the value is true, all action links will be shown and none if the value is false. */
149
- actions: false,
150
- })
151
- .then((result) => {
152
- vegaInstance.current = result;
153
- // result.view contains the Vega view
154
- // get the current state of view: result.view.getState()
155
- const view = result.view;
156
-
157
- if (ref) {
158
- ref.current = view;
159
- }
160
-
161
- if (SyncedCursorChartsContext && view) {
162
- view.addEventListener('mouseover', function (event, item) {
163
- const currentTime =
164
- item &&
165
- item.datum &&
166
- item.datum.datum &&
167
- item.datum.datum.timestamp;
168
-
169
- if (currentTime) {
170
- setCursorX(currentTime);
171
- }
172
- });
173
-
174
- /*When the mouse leaves the chart area, set the cursorX to null*/
175
- view.addEventListener('mouseleave', function (event, item) {
176
- setCursorX(0);
177
- });
178
- }
179
- })
180
- .catch((...args) => {
181
- if (isMounted) {
182
- console.error(...args); // TODO: we should handle this with a retry or an error state of the component
183
- }
184
- });
185
- return () => {
186
- isMounted = false;
187
-
188
- if (vegaInstance.current) {
189
- vegaInstance.current.view.finalize();
190
- }
191
- }; // eslint-disable-next-line react-hooks/exhaustive-deps
192
- }, [vegaDOMInstance, currentTheme]);
193
-
194
- useLayoutEffect(() => {
195
- if (vegaInstance.current) {
196
- const view = vegaInstance.current.view;
197
- tooltipHandler.options.formatTooltip = formatTooltip;
198
- tooltipHandler.onHover = onHover;
199
- view.tooltip(tooltipHandler.call).run();
200
- tooltipHandler.paint(); // to repaint the tooltip
201
- }
202
- }, [formatTooltip, vegaInstance, onHover]);
203
-
204
- useLayoutEffect(() => {
205
- if (vegaInstance.current) {
206
- const view = vegaInstance.current.view;
207
- let changeset = vega
208
- .changeset()
209
- .remove(() => true)
210
- .insert(themedSpec.data.values);
211
- //only the data.values changes trigger the graph's repaint
212
- // For some reason source_0 is the default dataset name
213
- view
214
- .change('source_0', changeset)
215
- .runAsync()
216
- .then(() => {
217
- // call resize() after the data is loaded to make sure the width is set correctly.
218
- view.resize().runAsync();
219
- });
220
- } // eslint-disable-next-line react-hooks/exhaustive-deps
221
- }, [
222
- // eslint-disable-next-line
223
- JSON.stringify(themedSpec.data.values),
224
- vegaInstance,
225
- ]);
226
-
227
- useLayoutEffect(() => {
228
- if (vegaInstance.current && themedSpec.params) {
229
- const view = vegaInstance.current.view;
230
-
231
- // when the mouse go out, we trigger the event to set cursorX to null
232
- if (
233
- !themedSpec.params.find((param) => param.name === 'cursorX').value ||
234
- !cursorX
235
- ) {
236
- view
237
- .signal(
238
- 'cursorX',
239
- (themedSpec &&
240
- themedSpec.data &&
241
- themedSpec.data.values &&
242
- themedSpec.data.values[0] &&
243
- themedSpec.data.values[0].timestamp) ||
244
- Date.now(),
245
- )
246
- .run();
247
- view.signal('isCursorDisplayed', false).run();
248
- } else {
249
- view
250
- .signal(
251
- 'cursorX',
252
- themedSpec.params.find((param) => param.name === 'cursorX').value,
253
- )
254
- .run();
255
- view.signal('isCursorDisplayed', true).run();
256
- }
257
- } // eslint-disable-next-line react-hooks/exhaustive-deps
258
- }, [
259
- // eslint-disable-next-line
260
- JSON.stringify(themedSpec),
261
- vegaInstance,
262
- ]);
263
- return (
264
- <div
265
- className="sc-vegachart"
266
- ref={vegaDOMInstance}
267
- style={{
268
- width: '100%',
269
- }}
270
- >
271
- <VegaTooltipTheme />
272
- </div>
273
- );
274
- } // @ts-expect-error
275
-
276
- export const VegaChart = forwardRef(VegaChartInternal);
@@ -1,142 +0,0 @@
1
- import React from 'react';
2
- import { BarChart } from '../src/lib/components/barchart/BarChart.component';
3
- import { Wrapper } from './common';
4
- import {
5
- verticalStackedData,
6
- horizontalStackedData,
7
- barchartData,
8
- } from './data/barchart';
9
- import { SyncedCursorCharts } from '../src/lib/components/vegachartv2/SyncedCursorCharts';
10
-
11
- import { Component } from '@storybook/blocks';
12
-
13
- const width = 800;
14
- // the size control the size of each small item of the bar
15
-
16
- const barConfig = {
17
- cornerRadius: 8,
18
- size: 12,
19
- };
20
- // props for vertical stacked bar chart
21
- const idVerticalStacked = 'vis_vertical_stacked';
22
-
23
- // props for vertical stacked bar chart
24
-
25
- const verticalStackedBarChartArgs = {
26
- id: 'vis_vertical_stacked',
27
- xAxis: {
28
- field: 'xlabel',
29
- type: 'ordinal',
30
- title: null,
31
- axis: {
32
- labelAngle: 0,
33
- },
34
- sort: {
35
- order: 'ascending',
36
- },
37
- },
38
- yAxis: {
39
- aggregate: 'count',
40
- field: '*',
41
- title: null,
42
- type: 'quantitative',
43
- scale: {
44
- padding: 1,
45
- },
46
- },
47
- color: {
48
- field: 'status',
49
- type: 'nominal',
50
- legend: {
51
- direction: 'horizontal',
52
- orient: 'top',
53
- title: null,
54
- symbolType: 'circle',
55
- columnPadding: 50,
56
- },
57
- scale: {
58
- domain: ['2XX', '401', '404', '4XX', '503', '5XX'],
59
- range: ['#4BE4E2', '#E45834', '#FEFA52', '#968BFF', '#BE2543', '#DC90F1'],
60
- },
61
- },
62
- data: verticalStackedData,
63
- barConfig,
64
- };
65
-
66
- // props for horizontal stacked bar chart
67
- const horizontalStackedChartArgs = {
68
- id: 'vis_horizontal_stacked',
69
- xAxis: {
70
- aggregate: 'sum',
71
- field: 'yield',
72
- type: 'quantitative',
73
- },
74
- yAxis: {
75
- field: 'variety',
76
- type: 'nominal',
77
- },
78
- color: {
79
- field: 'site',
80
- type: 'nominal',
81
- },
82
- data: horizontalStackedData,
83
- };
84
-
85
- // props for vertical bar chart props
86
- const verticalBarChartArgs = {
87
- id: 'vis_vertical',
88
- xAxis: {
89
- field: 'a',
90
- type: 'ordinal',
91
- },
92
- yAxis: {
93
- field: 'b',
94
- type: 'quantitative',
95
- },
96
- data: barchartData,
97
- };
98
-
99
- //props for horizontal bar chart
100
- const horizontalBarChartArgs = {
101
- id: 'vis_horizontal',
102
- xAxis: {
103
- field: 'b',
104
- type: 'quantitative',
105
- },
106
- yAxis: {
107
- field: 'a',
108
- type: 'ordinal',
109
- },
110
- data: barchartData,
111
- };
112
-
113
- export default {
114
- title: 'Components/Deprecated/Charts/BarChart',
115
- component: BarChart,
116
- decorators: [
117
- (story: Component) => <SyncedCursorCharts>{story()}</SyncedCursorCharts>,
118
- ],
119
- argTypes: {
120
- data: {
121
- table: {
122
- disable: true,
123
- },
124
- },
125
- },
126
- };
127
-
128
- export const verticalStackedChart = {
129
- args: { ...verticalStackedBarChartArgs },
130
- };
131
-
132
- export const horizontalStackedchart = {
133
- args: { ...horizontalStackedChartArgs },
134
- };
135
-
136
- export const verticalBarChart = {
137
- args: { ...verticalBarChartArgs },
138
- };
139
-
140
- export const horizontalBarChart = {
141
- args: { ...horizontalBarChartArgs },
142
- };