@mhjuma/traverse 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/package.json +46 -0
- package/src/bootstrap.spec.tsx +290 -0
- package/src/bootstrap.tsx +195 -0
- package/src/colors.ts +23 -0
- package/src/components/ChartPanel/ChartPanel.logic.spec.ts +358 -0
- package/src/components/ChartPanel/ChartPanel.logic.ts +318 -0
- package/src/components/ChartPanel/ChartPanel.spec.tsx +331 -0
- package/src/components/ChartPanel/ChartPanel.styles.ts +14 -0
- package/src/components/ChartPanel/ChartPanel.tsx +151 -0
- package/src/components/ConnectionDialog/ConnectionDialog.spec.tsx +767 -0
- package/src/components/ConnectionDialog/ConnectionDialog.tsx +652 -0
- package/src/components/ConnectionDialog/ConnectionRow.spec.tsx +104 -0
- package/src/components/ConnectionDialog/ConnectionRow.tsx +177 -0
- package/src/components/EditorToolbar/EditorToolbar.spec.tsx +599 -0
- package/src/components/EditorToolbar/EditorToolbar.tsx +259 -0
- package/src/components/Explorer/Explorer.spec.tsx +615 -0
- package/src/components/Explorer/Explorer.tsx +49 -0
- package/src/components/ExplorerWorkspace/ExplorerWorkspace.logic.spec.ts +715 -0
- package/src/components/ExplorerWorkspace/ExplorerWorkspace.logic.ts +324 -0
- package/src/components/ExplorerWorkspace/ExplorerWorkspace.spec.tsx +450 -0
- package/src/components/ExplorerWorkspace/ExplorerWorkspace.styles.ts +478 -0
- package/src/components/ExplorerWorkspace/ExplorerWorkspace.tsx +648 -0
- package/src/components/ExplorerWorkspace/kqlLanguage.spec.ts +321 -0
- package/src/components/ExplorerWorkspace/kqlLanguage.ts +155 -0
- package/src/components/ResultsPanel/ElapsedTimer.spec.tsx +51 -0
- package/src/components/ResultsPanel/ElapsedTimer.tsx +15 -0
- package/src/components/ResultsPanel/HistoryItem.spec.tsx +123 -0
- package/src/components/ResultsPanel/HistoryItem.tsx +56 -0
- package/src/components/ResultsPanel/ResultSetTab.tsx +18 -0
- package/src/components/ResultsPanel/ResultsPanel.spec.tsx +1194 -0
- package/src/components/ResultsPanel/ResultsPanel.tsx +321 -0
- package/src/components/ResultsPanel/TabButton.tsx +11 -0
- package/src/components/ResultsTable/ColumnHeader.tsx +42 -0
- package/src/components/ResultsTable/ColumnPickerItem.tsx +16 -0
- package/src/components/ResultsTable/FilterCell.tsx +24 -0
- package/src/components/ResultsTable/ResultsTable.logic.spec.ts +655 -0
- package/src/components/ResultsTable/ResultsTable.logic.ts +264 -0
- package/src/components/ResultsTable/ResultsTable.spec.tsx +930 -0
- package/src/components/ResultsTable/ResultsTable.styles.ts +108 -0
- package/src/components/ResultsTable/ResultsTable.tsx +501 -0
- package/src/components/ResultsTable/VirtualCell.spec.tsx +55 -0
- package/src/components/ResultsTable/VirtualCell.tsx +32 -0
- package/src/components/ResultsTable/VirtualRow.tsx +55 -0
- package/src/components/SchemaSidebar/FolderRow.tsx +35 -0
- package/src/components/SchemaSidebar/SchemaColumnItem.tsx +41 -0
- package/src/components/SchemaSidebar/SchemaContextMenu.spec.tsx +148 -0
- package/src/components/SchemaSidebar/SchemaContextMenu.tsx +49 -0
- package/src/components/SchemaSidebar/SchemaSidebar.logic.spec.ts +230 -0
- package/src/components/SchemaSidebar/SchemaSidebar.logic.ts +74 -0
- package/src/components/SchemaSidebar/SchemaSidebar.spec.tsx +1111 -0
- package/src/components/SchemaSidebar/SchemaSidebar.tsx +244 -0
- package/src/components/SchemaSidebar/SchemaTableItem.tsx +77 -0
- package/src/components/TabBar/TabBar.logic.spec.ts +127 -0
- package/src/components/TabBar/TabBar.logic.ts +35 -0
- package/src/components/TabBar/TabBar.spec.tsx +929 -0
- package/src/components/TabBar/TabBar.tsx +284 -0
- package/src/components/TabBar/TabItem.tsx +166 -0
- package/src/components/VegaChart/VegaChart.spec.tsx +126 -0
- package/src/components/VegaChart/VegaChart.tsx +224 -0
- package/src/config.spec.ts +44 -0
- package/src/config.ts +47 -0
- package/src/context/ExplorerColorContext.ts +14 -0
- package/src/context/ExplorerStateContext.ts +49 -0
- package/src/context/KustoClientContext.ts +12 -0
- package/src/context/context.spec.tsx +183 -0
- package/src/index.ts +14 -0
- package/src/services/cache.spec.ts +31 -0
- package/src/services/cache.ts +16 -0
- package/src/services/concurrency.spec.ts +139 -0
- package/src/services/concurrency.ts +46 -0
- package/src/services/kusto.spec.ts +919 -0
- package/src/services/kusto.ts +472 -0
- package/src/services/queryHistory.spec.ts +287 -0
- package/src/services/queryHistory.ts +146 -0
- package/src/services/schema.spec.ts +618 -0
- package/src/services/schema.ts +294 -0
- package/src/services/state-service.spec.ts +670 -0
- package/src/services/state-service.ts +602 -0
- package/src/services/useKustoQuery.spec.ts +234 -0
- package/src/services/useKustoQuery.ts +124 -0
- package/src/state/actions.ts +50 -0
- package/src/state/index.ts +3 -0
- package/src/state/reducer.spec.ts +644 -0
- package/src/state/reducer.ts +207 -0
- package/src/state/types.ts +54 -0
- package/tsconfig.json +7 -0
- package/tsconfig.lint.json +4 -0
- package/tsconfig.spec.json +7 -0
- package/vitest.config.ts +34 -0
|
@@ -0,0 +1,331 @@
|
|
|
1
|
+
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
|
|
2
|
+
import React from 'react';
|
|
3
|
+
import { createRoot } from 'react-dom/client';
|
|
4
|
+
import { act } from 'react';
|
|
5
|
+
import { ChartPanel } from './ChartPanel';
|
|
6
|
+
|
|
7
|
+
vi.mock('../VegaChart/VegaChart', () => ({
|
|
8
|
+
VegaChart: () => React.createElement('div', { 'data-testid': 'vega-chart' }),
|
|
9
|
+
}));
|
|
10
|
+
vi.mock('../../context/ExplorerColorContext', () => ({
|
|
11
|
+
useExplorerColors: () => ({
|
|
12
|
+
semantic: {},
|
|
13
|
+
chart: { palette: ['#1', '#2', '#3', '#4', '#5', '#6', '#7', '#8', '#9', '#10'] },
|
|
14
|
+
}),
|
|
15
|
+
}));
|
|
16
|
+
vi.mock('./ChartPanel.styles', () => ({
|
|
17
|
+
useStyles: () => new Proxy({}, { get: (_, p) => String(p) }),
|
|
18
|
+
}));
|
|
19
|
+
vi.mock('@fluentui/react-components', () => ({
|
|
20
|
+
Dropdown: (p: Record<string, unknown>) => {
|
|
21
|
+
const handler = p['onOptionSelect'] as ((e: unknown, d: { optionValue?: string }) => void) | undefined;
|
|
22
|
+
return React.createElement('select', {
|
|
23
|
+
...p,
|
|
24
|
+
'data-testid': `dropdown-${typeof p['value'] === 'string' ? p['value'] : ''}`,
|
|
25
|
+
onChange: (e: React.ChangeEvent<HTMLSelectElement>) => handler?.(e, { optionValue: e.target.value }),
|
|
26
|
+
}, p['children'] as React.ReactNode);
|
|
27
|
+
},
|
|
28
|
+
Option: (p: Record<string, unknown>) => React.createElement('option', p, p['children'] as React.ReactNode),
|
|
29
|
+
Label: (p: Record<string, unknown>) => React.createElement('label', p, p['children'] as React.ReactNode),
|
|
30
|
+
}));
|
|
31
|
+
|
|
32
|
+
describe('ChartPanel', () => {
|
|
33
|
+
let container: HTMLDivElement;
|
|
34
|
+
let root: ReturnType<typeof createRoot>;
|
|
35
|
+
let consoleErrorSpy: ReturnType<typeof vi.spyOn>;
|
|
36
|
+
let consoleWarnSpy: ReturnType<typeof vi.spyOn>;
|
|
37
|
+
|
|
38
|
+
beforeEach(() => {
|
|
39
|
+
consoleErrorSpy = vi.spyOn(console, 'error').mockImplementation(() => {});
|
|
40
|
+
consoleWarnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {});
|
|
41
|
+
container = document.createElement('div');
|
|
42
|
+
document.body.appendChild(container);
|
|
43
|
+
root = createRoot(container);
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
afterEach(() => {
|
|
47
|
+
act(() => {
|
|
48
|
+
root.unmount();
|
|
49
|
+
});
|
|
50
|
+
container.remove();
|
|
51
|
+
consoleErrorSpy.mockRestore();
|
|
52
|
+
consoleWarnSpy.mockRestore();
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
it('renders with empty data (fewer than 2 columns)', () => {
|
|
56
|
+
act(() => {
|
|
57
|
+
root.render(React.createElement(ChartPanel, {
|
|
58
|
+
columns: [],
|
|
59
|
+
rows: [],
|
|
60
|
+
}));
|
|
61
|
+
});
|
|
62
|
+
expect(container.textContent).toContain('Need at least 2 columns');
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
it('renders with sample data', () => {
|
|
66
|
+
act(() => {
|
|
67
|
+
root.render(React.createElement(ChartPanel, {
|
|
68
|
+
columns: ['Category', 'Count'],
|
|
69
|
+
rows: [
|
|
70
|
+
{ Category: 'A', Count: 10 },
|
|
71
|
+
{ Category: 'B', Count: 20 },
|
|
72
|
+
],
|
|
73
|
+
}));
|
|
74
|
+
});
|
|
75
|
+
expect(container.innerHTML).not.toBe('');
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
it('auto-detects line chart for datetime + number + string columns', () => {
|
|
79
|
+
act(() => {
|
|
80
|
+
root.render(React.createElement(ChartPanel, {
|
|
81
|
+
columns: ['Date', 'Value', 'Category'],
|
|
82
|
+
rows: [
|
|
83
|
+
{ Date: '2026-01-01T00:00:00Z', Value: 10, Category: 'A' },
|
|
84
|
+
{ Date: '2026-01-02T00:00:00Z', Value: 20, Category: 'B' },
|
|
85
|
+
],
|
|
86
|
+
}));
|
|
87
|
+
});
|
|
88
|
+
expect(container.querySelector('[data-testid="vega-chart"]')).toBeTruthy();
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
it('auto-detects line chart for datetime + number columns', () => {
|
|
92
|
+
act(() => {
|
|
93
|
+
root.render(React.createElement(ChartPanel, {
|
|
94
|
+
columns: ['Date', 'Value'],
|
|
95
|
+
rows: [
|
|
96
|
+
{ Date: '2026-01-01T00:00:00Z', Value: 10 },
|
|
97
|
+
{ Date: '2026-01-02T00:00:00Z', Value: 20 },
|
|
98
|
+
],
|
|
99
|
+
}));
|
|
100
|
+
});
|
|
101
|
+
expect(container.querySelector('[data-testid="vega-chart"]')).toBeTruthy();
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
it('auto-detects scatter chart for 2 numeric columns', () => {
|
|
105
|
+
act(() => {
|
|
106
|
+
root.render(React.createElement(ChartPanel, {
|
|
107
|
+
columns: ['X', 'Y'],
|
|
108
|
+
rows: [
|
|
109
|
+
{ X: 1, Y: 2 },
|
|
110
|
+
{ X: 3, Y: 4 },
|
|
111
|
+
],
|
|
112
|
+
}));
|
|
113
|
+
});
|
|
114
|
+
expect(container.querySelector('[data-testid="vega-chart"]')).toBeTruthy();
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
it('auto-detects scatter chart for 2 numbers + 1 string (colorField)', () => {
|
|
118
|
+
act(() => {
|
|
119
|
+
root.render(React.createElement(ChartPanel, {
|
|
120
|
+
columns: ['Group', 'X', 'Y'],
|
|
121
|
+
rows: [
|
|
122
|
+
{ Group: 'A', X: 1, Y: 2 },
|
|
123
|
+
{ Group: 'B', X: 3, Y: 4 },
|
|
124
|
+
],
|
|
125
|
+
}));
|
|
126
|
+
});
|
|
127
|
+
expect(container.querySelector('[data-testid="vega-chart"]')).toBeTruthy();
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
it('shows row-limit message for data exceeding 1000 rows', () => {
|
|
131
|
+
const rows = Array.from({ length: 1100 }, (_, i) => ({ Category: `Cat${i}`, Count: i }));
|
|
132
|
+
act(() => {
|
|
133
|
+
root.render(React.createElement(ChartPanel, {
|
|
134
|
+
columns: ['Category', 'Count'],
|
|
135
|
+
rows,
|
|
136
|
+
}));
|
|
137
|
+
});
|
|
138
|
+
expect(container.textContent).toContain('1,000');
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
it('handles column with all null/empty values as string', () => {
|
|
142
|
+
act(() => {
|
|
143
|
+
root.render(React.createElement(ChartPanel, {
|
|
144
|
+
columns: ['Empty', 'Count'],
|
|
145
|
+
rows: [
|
|
146
|
+
{ Empty: '', Count: 10 },
|
|
147
|
+
{ Empty: null, Count: 20 },
|
|
148
|
+
],
|
|
149
|
+
}));
|
|
150
|
+
});
|
|
151
|
+
expect(container.querySelector('[data-testid="vega-chart"]')).toBeTruthy();
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
it('resets chart type when columns change', () => {
|
|
155
|
+
act(() => {
|
|
156
|
+
root.render(React.createElement(ChartPanel, {
|
|
157
|
+
columns: ['Cat', 'Val'],
|
|
158
|
+
rows: [{ Cat: 'A', Val: 1 }],
|
|
159
|
+
}));
|
|
160
|
+
});
|
|
161
|
+
expect(container.querySelector('[data-testid="vega-chart"]')).toBeTruthy();
|
|
162
|
+
|
|
163
|
+
act(() => {
|
|
164
|
+
root.render(React.createElement(ChartPanel, {
|
|
165
|
+
columns: ['Date', 'Amount'],
|
|
166
|
+
rows: [{ Date: '2026-01-01T00:00:00Z', Amount: 100 }],
|
|
167
|
+
}));
|
|
168
|
+
});
|
|
169
|
+
expect(container.querySelector('[data-testid="vega-chart"]')).toBeTruthy();
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
it('handles dropdown handlers with and without optionValue', () => {
|
|
173
|
+
act(() => {
|
|
174
|
+
root.render(React.createElement(ChartPanel, {
|
|
175
|
+
columns: ['Category', 'Count'],
|
|
176
|
+
rows: [{ Category: 'A', Count: 10 }],
|
|
177
|
+
}));
|
|
178
|
+
});
|
|
179
|
+
const selects = container.querySelectorAll('select');
|
|
180
|
+
expect(selects.length).toBeGreaterThan(0);
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
it('auto-detects donut chart for 2 strings + 1 number', () => {
|
|
184
|
+
act(() => {
|
|
185
|
+
root.render(React.createElement(ChartPanel, {
|
|
186
|
+
columns: ['Region', 'SubRegion', 'Sales'],
|
|
187
|
+
rows: [
|
|
188
|
+
{ Region: 'East', SubRegion: 'NE', Sales: 100 },
|
|
189
|
+
{ Region: 'West', SubRegion: 'NW', Sales: 200 },
|
|
190
|
+
],
|
|
191
|
+
}));
|
|
192
|
+
});
|
|
193
|
+
expect(container.querySelector('[data-testid="vega-chart"]')).toBeTruthy();
|
|
194
|
+
});
|
|
195
|
+
|
|
196
|
+
it('falls back to bar when no columns match heuristics', () => {
|
|
197
|
+
act(() => {
|
|
198
|
+
root.render(React.createElement(ChartPanel, {
|
|
199
|
+
columns: ['A', 'B'],
|
|
200
|
+
rows: [{ A: 'x', B: 'y' }],
|
|
201
|
+
}));
|
|
202
|
+
});
|
|
203
|
+
expect(container.querySelector('[data-testid="vega-chart"]')).toBeTruthy();
|
|
204
|
+
});
|
|
205
|
+
|
|
206
|
+
it('changes chart type when dropdown changes', () => {
|
|
207
|
+
act(() => {
|
|
208
|
+
root.render(React.createElement(ChartPanel, {
|
|
209
|
+
columns: ['Category', 'Count'],
|
|
210
|
+
rows: [{ Category: 'A', Count: 10 }],
|
|
211
|
+
}));
|
|
212
|
+
});
|
|
213
|
+
const selects = container.querySelectorAll('select');
|
|
214
|
+
// First select is chart type
|
|
215
|
+
if (selects[0]) {
|
|
216
|
+
act(() => {
|
|
217
|
+
selects[0].value = 'donut';
|
|
218
|
+
selects[0].dispatchEvent(new Event('change', { bubbles: true }));
|
|
219
|
+
});
|
|
220
|
+
}
|
|
221
|
+
expect(container.querySelector('[data-testid="vega-chart"]')).toBeTruthy();
|
|
222
|
+
});
|
|
223
|
+
|
|
224
|
+
it('changes X field when dropdown changes', () => {
|
|
225
|
+
act(() => {
|
|
226
|
+
root.render(React.createElement(ChartPanel, {
|
|
227
|
+
columns: ['Category', 'Count', 'Extra'],
|
|
228
|
+
rows: [{ Category: 'A', Count: 10, Extra: 5 }],
|
|
229
|
+
}));
|
|
230
|
+
});
|
|
231
|
+
const selects = container.querySelectorAll('select');
|
|
232
|
+
// Second select is X field
|
|
233
|
+
if (selects[1]) {
|
|
234
|
+
act(() => {
|
|
235
|
+
selects[1].value = 'Extra';
|
|
236
|
+
selects[1].dispatchEvent(new Event('change', { bubbles: true }));
|
|
237
|
+
});
|
|
238
|
+
}
|
|
239
|
+
expect(container.querySelector('[data-testid="vega-chart"]')).toBeTruthy();
|
|
240
|
+
});
|
|
241
|
+
|
|
242
|
+
it('changes Y field when dropdown changes', () => {
|
|
243
|
+
act(() => {
|
|
244
|
+
root.render(React.createElement(ChartPanel, {
|
|
245
|
+
columns: ['Category', 'Count', 'Extra'],
|
|
246
|
+
rows: [{ Category: 'A', Count: 10, Extra: 20 }],
|
|
247
|
+
}));
|
|
248
|
+
});
|
|
249
|
+
const selects = container.querySelectorAll('select');
|
|
250
|
+
// Third select is Y field
|
|
251
|
+
if (selects[2]) {
|
|
252
|
+
act(() => {
|
|
253
|
+
selects[2].value = 'Extra';
|
|
254
|
+
selects[2].dispatchEvent(new Event('change', { bubbles: true }));
|
|
255
|
+
});
|
|
256
|
+
}
|
|
257
|
+
expect(container.querySelector('[data-testid="vega-chart"]')).toBeTruthy();
|
|
258
|
+
});
|
|
259
|
+
|
|
260
|
+
it('changes color field when dropdown changes', () => {
|
|
261
|
+
act(() => {
|
|
262
|
+
root.render(React.createElement(ChartPanel, {
|
|
263
|
+
columns: ['Category', 'Count', 'Group'],
|
|
264
|
+
rows: [{ Category: 'A', Count: 10, Group: 'G1' }],
|
|
265
|
+
}));
|
|
266
|
+
});
|
|
267
|
+
const selects = container.querySelectorAll('select');
|
|
268
|
+
// Fourth select is color field
|
|
269
|
+
if (selects[3]) {
|
|
270
|
+
act(() => {
|
|
271
|
+
selects[3].value = 'Category';
|
|
272
|
+
selects[3].dispatchEvent(new Event('change', { bubbles: true }));
|
|
273
|
+
});
|
|
274
|
+
}
|
|
275
|
+
expect(container.querySelector('[data-testid="vega-chart"]')).toBeTruthy();
|
|
276
|
+
});
|
|
277
|
+
|
|
278
|
+
it('sets color to null when (none) is selected', () => {
|
|
279
|
+
act(() => {
|
|
280
|
+
root.render(React.createElement(ChartPanel, {
|
|
281
|
+
columns: ['Category', 'Count', 'Group'],
|
|
282
|
+
rows: [{ Category: 'A', Count: 10, Group: 'G1' }],
|
|
283
|
+
}));
|
|
284
|
+
});
|
|
285
|
+
const selects = container.querySelectorAll('select');
|
|
286
|
+
if (selects[3]) {
|
|
287
|
+
act(() => {
|
|
288
|
+
selects[3].value = '(none)';
|
|
289
|
+
selects[3].dispatchEvent(new Event('change', { bubbles: true }));
|
|
290
|
+
});
|
|
291
|
+
}
|
|
292
|
+
expect(container.querySelector('[data-testid="vega-chart"]')).toBeTruthy();
|
|
293
|
+
});
|
|
294
|
+
|
|
295
|
+
it('renders area chart type', () => {
|
|
296
|
+
act(() => {
|
|
297
|
+
root.render(React.createElement(ChartPanel, {
|
|
298
|
+
columns: ['Date', 'Value'],
|
|
299
|
+
rows: [
|
|
300
|
+
{ Date: '2026-01-01T00:00:00Z', Value: 10 },
|
|
301
|
+
{ Date: '2026-01-02T00:00:00Z', Value: 20 },
|
|
302
|
+
],
|
|
303
|
+
}));
|
|
304
|
+
});
|
|
305
|
+
const selects = container.querySelectorAll('select');
|
|
306
|
+
if (selects[0]) {
|
|
307
|
+
act(() => {
|
|
308
|
+
selects[0].value = 'area';
|
|
309
|
+
selects[0].dispatchEvent(new Event('change', { bubbles: true }));
|
|
310
|
+
});
|
|
311
|
+
}
|
|
312
|
+
expect(container.querySelector('[data-testid="vega-chart"]')).toBeTruthy();
|
|
313
|
+
});
|
|
314
|
+
|
|
315
|
+
it('renders grouped-bar chart type', () => {
|
|
316
|
+
act(() => {
|
|
317
|
+
root.render(React.createElement(ChartPanel, {
|
|
318
|
+
columns: ['Category', 'Count'],
|
|
319
|
+
rows: [{ Category: 'A', Count: 10 }],
|
|
320
|
+
}));
|
|
321
|
+
});
|
|
322
|
+
const selects = container.querySelectorAll('select');
|
|
323
|
+
if (selects[0]) {
|
|
324
|
+
act(() => {
|
|
325
|
+
selects[0].value = 'grouped-bar';
|
|
326
|
+
selects[0].dispatchEvent(new Event('change', { bubbles: true }));
|
|
327
|
+
});
|
|
328
|
+
}
|
|
329
|
+
expect(container.querySelector('[data-testid="vega-chart"]')).toBeTruthy();
|
|
330
|
+
});
|
|
331
|
+
});
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { makeStyles, tokens } from '@fluentui/react-components';
|
|
2
|
+
|
|
3
|
+
export const useStyles = makeStyles({
|
|
4
|
+
container: { display: 'flex', flexDirection: 'column', flex: 1, overflow: 'hidden' },
|
|
5
|
+
controls: {
|
|
6
|
+
display: 'flex', alignItems: 'center', gap: '12px', padding: '8px 12px', flexShrink: 0,
|
|
7
|
+
borderBottom: `1px solid ${tokens.colorNeutralStroke2}`, backgroundColor: tokens.colorNeutralBackground2,
|
|
8
|
+
flexWrap: 'wrap' as const,
|
|
9
|
+
},
|
|
10
|
+
field: { display: 'flex', alignItems: 'center', gap: '4px' },
|
|
11
|
+
fieldLabel: { fontSize: '11px', color: tokens.colorNeutralForeground3, whiteSpace: 'nowrap' as const },
|
|
12
|
+
chartArea: { flex: 1, overflow: 'auto', padding: '8px', minHeight: 0 },
|
|
13
|
+
empty: { display: 'flex', alignItems: 'center', justifyContent: 'center', flex: 1, color: tokens.colorNeutralForeground3, fontSize: '13px' },
|
|
14
|
+
});
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
import { useExplorerColors } from '../../context/ExplorerColorContext';
|
|
2
|
+
import { Dropdown, Option, Label } from '@fluentui/react-components';
|
|
3
|
+
import { useState, useMemo, useCallback } from 'react';
|
|
4
|
+
|
|
5
|
+
import { VegaChart } from '../VegaChart/VegaChart';
|
|
6
|
+
import { useStyles } from './ChartPanel.styles';
|
|
7
|
+
import { CHART_TYPES, autoDetect, buildSpec, applyVisualizationHints, buildTreemapVegaSpec } from './ChartPanel.logic';
|
|
8
|
+
import type { ChartType } from './ChartPanel.logic';
|
|
9
|
+
import type { KustoVisualization } from '../../services/kusto';
|
|
10
|
+
|
|
11
|
+
// --- Component ---
|
|
12
|
+
|
|
13
|
+
interface Props {
|
|
14
|
+
columns: string[];
|
|
15
|
+
rows: Record<string, unknown>[];
|
|
16
|
+
isDark?: boolean;
|
|
17
|
+
visualization?: KustoVisualization;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const dropdownStyleNarrow = { minWidth: '110px' } as const;
|
|
21
|
+
const dropdownStyleWide = { minWidth: '120px' } as const;
|
|
22
|
+
const marginLeftAutoStyle = { marginLeft: 'auto' } as const;
|
|
23
|
+
|
|
24
|
+
export function ChartPanel({ columns, rows, isDark = false, visualization }: Props) {
|
|
25
|
+
const { chart } = useExplorerColors();
|
|
26
|
+
const chartColors = useMemo(() => {
|
|
27
|
+
const base = chart.palette.slice(0, 8);
|
|
28
|
+
if (!isDark) {
|
|
29
|
+
return base;
|
|
30
|
+
}
|
|
31
|
+
// Darken palette for dark mode to improve contrast with light text
|
|
32
|
+
return base.map((hex) => {
|
|
33
|
+
const r = parseInt(hex.slice(1, 3), 16);
|
|
34
|
+
const g = parseInt(hex.slice(3, 5), 16);
|
|
35
|
+
const b = parseInt(hex.slice(5, 7), 16);
|
|
36
|
+
const factor = 0.7;
|
|
37
|
+
return `#${Math.round(r * factor).toString(16).padStart(2, '0')}${Math.round(g * factor).toString(16).padStart(2, '0')}${Math.round(b * factor).toString(16).padStart(2, '0')}`;
|
|
38
|
+
});
|
|
39
|
+
}, [chart.palette, isDark]);
|
|
40
|
+
const styles = useStyles();
|
|
41
|
+
|
|
42
|
+
// Limit data to 1000 rows for chart performance
|
|
43
|
+
const chartRows = useMemo(() => rows.length > 1000 ? rows.slice(0, 1000) : rows, [rows]);
|
|
44
|
+
const rowsLimited = rows.length > 1000;
|
|
45
|
+
|
|
46
|
+
const detected = useMemo(() => {
|
|
47
|
+
const fromViz = applyVisualizationHints(visualization, columns, chartRows);
|
|
48
|
+
return fromViz ?? autoDetect(columns, chartRows);
|
|
49
|
+
}, [columns, chartRows, visualization]);
|
|
50
|
+
|
|
51
|
+
const [chartType, setChartType] = useState<ChartType>(detected.chartType);
|
|
52
|
+
const [xField, setXField] = useState(detected.xField);
|
|
53
|
+
const [yField, setYField] = useState(detected.yField);
|
|
54
|
+
const [colorField, setColorField] = useState<string | null>(detected.colorField);
|
|
55
|
+
|
|
56
|
+
// Reset when data or visualization changes
|
|
57
|
+
const resetKey = useMemo(() => `${columns.join(',')}_${visualization?.type ?? ''}`, [columns, visualization]);
|
|
58
|
+
const [lastResetKey, setLastResetKey] = useState(resetKey);
|
|
59
|
+
if (resetKey !== lastResetKey) {
|
|
60
|
+
setLastResetKey(resetKey);
|
|
61
|
+
setChartType(detected.chartType);
|
|
62
|
+
setXField(detected.xField);
|
|
63
|
+
setYField(detected.yField);
|
|
64
|
+
setColorField(detected.colorField);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
const spec = useMemo(() =>
|
|
68
|
+
buildSpec(chartType, xField, yField, colorField, columns, chartRows, chartColors),
|
|
69
|
+
[chartType, xField, yField, colorField, columns, chartRows, chartColors]);
|
|
70
|
+
|
|
71
|
+
const rawVegaSpec = useMemo(() => {
|
|
72
|
+
if (chartType === 'treemap') {
|
|
73
|
+
return buildTreemapVegaSpec(xField, yField, colorField, chartRows, chartColors, isDark);
|
|
74
|
+
}
|
|
75
|
+
return undefined;
|
|
76
|
+
}, [chartType, xField, yField, colorField, chartRows, chartColors, isDark]);
|
|
77
|
+
|
|
78
|
+
const data = useMemo(() => ({ values: chartRows }), [chartRows]);
|
|
79
|
+
|
|
80
|
+
const handleChartType = useCallback((_: unknown, d: { optionValue?: string }) => {
|
|
81
|
+
if (d.optionValue) {
|
|
82
|
+
setChartType(d.optionValue as ChartType);
|
|
83
|
+
}
|
|
84
|
+
}, []);
|
|
85
|
+
const handleX = useCallback((_: unknown, d: { optionValue?: string }) => {
|
|
86
|
+
if (d.optionValue) {
|
|
87
|
+
setXField(d.optionValue);
|
|
88
|
+
}
|
|
89
|
+
}, []);
|
|
90
|
+
const handleY = useCallback((_: unknown, d: { optionValue?: string }) => {
|
|
91
|
+
if (d.optionValue) {
|
|
92
|
+
setYField(d.optionValue);
|
|
93
|
+
}
|
|
94
|
+
}, []);
|
|
95
|
+
const handleColor = useCallback((_: unknown, d: { optionValue?: string }) => {
|
|
96
|
+
if (d.optionValue) {
|
|
97
|
+
setColorField(d.optionValue === '(none)' ? null : d.optionValue);
|
|
98
|
+
}
|
|
99
|
+
}, []);
|
|
100
|
+
|
|
101
|
+
const selectedChartType = useMemo(() => [chartType], [chartType]);
|
|
102
|
+
const selectedXField = useMemo(() => [xField], [xField]);
|
|
103
|
+
const selectedYField = useMemo(() => [yField], [yField]);
|
|
104
|
+
const selectedColorField = useMemo(() => [colorField ?? '(none)'], [colorField]);
|
|
105
|
+
|
|
106
|
+
if (columns.length < 2) {
|
|
107
|
+
return <div className={styles.empty}>Need at least 2 columns to chart</div>;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
return (
|
|
111
|
+
<div className={styles.container}>
|
|
112
|
+
<div className={styles.controls}>
|
|
113
|
+
<div className={styles.field}>
|
|
114
|
+
<Label className={styles.fieldLabel}>Type</Label>
|
|
115
|
+
<Dropdown size="small" value={CHART_TYPES.find((t) => t.value === chartType)?.label ?? ''}
|
|
116
|
+
selectedOptions={selectedChartType} onOptionSelect={handleChartType} style={dropdownStyleNarrow}>
|
|
117
|
+
{CHART_TYPES.map((t) => <Option key={t.value} value={t.value}>{t.label}</Option>)}
|
|
118
|
+
</Dropdown>
|
|
119
|
+
</div>
|
|
120
|
+
<div className={styles.field}>
|
|
121
|
+
<Label className={styles.fieldLabel}>X</Label>
|
|
122
|
+
<Dropdown size="small" value={xField} selectedOptions={selectedXField}
|
|
123
|
+
onOptionSelect={handleX} style={dropdownStyleWide}>
|
|
124
|
+
{columns.map((c) => <Option key={c} value={c}>{c}</Option>)}
|
|
125
|
+
</Dropdown>
|
|
126
|
+
</div>
|
|
127
|
+
<div className={styles.field}>
|
|
128
|
+
<Label className={styles.fieldLabel}>Y</Label>
|
|
129
|
+
<Dropdown size="small" value={yField} selectedOptions={selectedYField}
|
|
130
|
+
onOptionSelect={handleY} style={dropdownStyleWide}>
|
|
131
|
+
{columns.map((c) => <Option key={c} value={c}>{c}</Option>)}
|
|
132
|
+
</Dropdown>
|
|
133
|
+
</div>
|
|
134
|
+
<div className={styles.field}>
|
|
135
|
+
<Label className={styles.fieldLabel}>Color</Label>
|
|
136
|
+
<Dropdown size="small" value={colorField ?? '(none)'} selectedOptions={selectedColorField}
|
|
137
|
+
onOptionSelect={handleColor} style={dropdownStyleWide}>
|
|
138
|
+
<Option value="(none)">(none)</Option>
|
|
139
|
+
{columns.map((c) => <Option key={c} value={c}>{c}</Option>)}
|
|
140
|
+
</Dropdown>
|
|
141
|
+
</div>
|
|
142
|
+
{rowsLimited && (
|
|
143
|
+
<span className={styles.fieldLabel} style={marginLeftAutoStyle}>Showing first 1,000 of {rows.length.toLocaleString()} rows</span>
|
|
144
|
+
)}
|
|
145
|
+
</div>
|
|
146
|
+
<div className={styles.chartArea}>
|
|
147
|
+
<VegaChart spec={spec} data={data} isDark={isDark} rawVegaSpec={rawVegaSpec} />
|
|
148
|
+
</div>
|
|
149
|
+
</div>
|
|
150
|
+
);
|
|
151
|
+
}
|