plugin-elastic-report 0.1.1 → 0.7.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 (60) hide show
  1. package/ai/skills/elastic-report/SKILLS.md +5 -3
  2. package/dist/client/357.0dd385384cab5dbf.js +10 -0
  3. package/dist/client/index.js +1 -1
  4. package/dist/client-v2/851.142c93fc6d666b28.js +10 -0
  5. package/dist/client-v2/857.a5bf9637d5fd4ef5.js +10 -0
  6. package/dist/client-v2/877.f4095a3dbcadf98f.js +10 -0
  7. package/dist/client-v2/index.js +1 -1
  8. package/dist/externalVersion.js +3 -2
  9. package/dist/locale/en-US.json +100 -1
  10. package/dist/locale/vi-VN.json +100 -1
  11. package/dist/locale/zh-CN.json +100 -1
  12. package/dist/node_modules/@elastic/elasticsearch/package.json +1 -1
  13. package/dist/server/acl-guard.js +97 -25
  14. package/dist/server/collections/es-report-data-views.js +6 -0
  15. package/dist/server/collections/es-reports.js +6 -0
  16. package/dist/server/mcp/ElasticReportToolCatalog.js +232 -10
  17. package/dist/server/plugin.js +153 -19
  18. package/dist/server/services/ElasticReportService.js +125 -11
  19. package/dist/server/services/ElasticsearchConnectionService.js +274 -34
  20. package/dist/server/services/ReportExportService.js +243 -0
  21. package/dist/server/services/ReportQueryCompiler.js +532 -91
  22. package/dist/server/workflow/ElasticReportQueryInstruction.js +123 -0
  23. package/dist/shared/constants.js +17 -2
  24. package/package.json +3 -2
  25. package/src/client/index.tsx +16 -1
  26. package/src/client/workflow-instructions.tsx +44 -0
  27. package/src/client-v2/__tests__/elastic-report.test.tsx +519 -39
  28. package/src/client-v2/__tests__/kql-autocomplete.test.ts +178 -0
  29. package/src/client-v2/api.ts +96 -0
  30. package/src/client-v2/bus/DashboardFilterBus.ts +212 -0
  31. package/src/client-v2/components/ElasticBasicCharts.tsx +345 -92
  32. package/src/client-v2/components/ElasticFilterBar.tsx +372 -0
  33. package/src/client-v2/components/ElasticKqlInput.tsx +421 -0
  34. package/src/client-v2/components/ElasticReportBlock.tsx +157 -11
  35. package/src/client-v2/components/ElasticReportRenderer.tsx +621 -71
  36. package/src/client-v2/components/ReportSelect.tsx +39 -26
  37. package/src/client-v2/models/ElasticFilterBarBlockModel.tsx +115 -0
  38. package/src/client-v2/models/ElasticReportBlockModel.tsx +36 -3
  39. package/src/client-v2/pages/ElasticReportManager.tsx +432 -26
  40. package/src/client-v2/plugin.tsx +20 -0
  41. package/src/client-v2/utils/kqlAutocomplete.ts +861 -0
  42. package/src/client-v2/workflow/instructions.tsx +149 -0
  43. package/src/locale/en-US.json +100 -1
  44. package/src/locale/vi-VN.json +100 -1
  45. package/src/locale/zh-CN.json +100 -1
  46. package/src/server/__tests__/query-compiler.test.ts +390 -1
  47. package/src/server/__tests__/report-mcp-acl.integration.test.ts +271 -3
  48. package/src/server/acl-guard.ts +119 -28
  49. package/src/server/collections/es-report-data-views.ts +6 -0
  50. package/src/server/collections/es-reports.ts +6 -0
  51. package/src/server/mcp/ElasticReportToolCatalog.ts +255 -9
  52. package/src/server/plugin.ts +189 -21
  53. package/src/server/services/ElasticReportService.ts +165 -5
  54. package/src/server/services/ElasticsearchConnectionService.ts +375 -48
  55. package/src/server/services/ReportExportService.ts +282 -0
  56. package/src/server/services/ReportQueryCompiler.ts +629 -94
  57. package/src/server/workflow/ElasticReportQueryInstruction.ts +124 -0
  58. package/src/shared/constants.ts +7 -0
  59. package/src/shared/types.ts +98 -1
  60. package/dist/client-v2/851.488fdbf5bc5b6a36.js +0 -10
@@ -7,9 +7,16 @@
7
7
  * For more information, please refer to: https://www.nocobase.com/agreement.
8
8
  */
9
9
 
10
- import { Button, Card, Empty, Space, Statistic, Table, Tooltip, Typography, theme } from 'antd';
11
- import React, { useId, useState } from 'react';
12
- import type { ReportHistogramBucket, ReportSeriesPoint, ReportVisualizationType } from '../../shared/types';
10
+ import { Button, Card, Empty, Space, Statistic, Table, Tag, Tooltip, Typography, theme } from 'antd';
11
+ import type { TableColumnsType } from 'antd';
12
+ import React, { useId, useMemo, useState } from 'react';
13
+ import type {
14
+ ChartStackMode,
15
+ MetricAggType,
16
+ ReportHistogramBucket,
17
+ ReportSeriesPoint,
18
+ ReportVisualizationType,
19
+ } from '../../shared/types';
13
20
  import { useT } from '../locale';
14
21
 
15
22
  const { Text } = Typography;
@@ -86,9 +93,15 @@ export interface ElasticBasicChartProps {
86
93
  title: string;
87
94
  visualization: Exclude<ReportVisualizationType, 'table'>;
88
95
  series: ReportSeriesPoint[];
96
+ seriesKeys?: string[];
97
+ stackMode?: ChartStackMode;
89
98
  metricValue: number | null;
99
+ previousMetricValue?: number | null;
100
+ deltaPercent?: number | null;
90
101
  metricLabel: string;
102
+ metricType?: MetricAggType;
91
103
  totalHits: number;
104
+ totalRelation?: 'eq' | 'gte';
92
105
  onSelectBucket?: (bucketKey: string) => void;
93
106
  }
94
107
 
@@ -96,9 +109,15 @@ export const ElasticBasicChart: React.FC<ElasticBasicChartProps> = ({
96
109
  title,
97
110
  visualization,
98
111
  series,
112
+ seriesKeys,
113
+ stackMode = 'none',
99
114
  metricValue,
115
+ previousMetricValue,
116
+ deltaPercent,
100
117
  metricLabel,
118
+ metricType,
101
119
  totalHits,
120
+ totalRelation = 'eq',
102
121
  onSelectBucket,
103
122
  }) => {
104
123
  const t = useT();
@@ -106,17 +125,58 @@ export const ElasticBasicChart: React.FC<ElasticBasicChartProps> = ({
106
125
  const svgTitleId = useId();
107
126
  const [showTableAlt, setShowTableAlt] = useState(false);
108
127
 
128
+ const activeSeriesKeys = useMemo(() => (seriesKeys && seriesKeys.length > 0 ? seriesKeys : []), [seriesKeys]);
129
+ const hasMultiSeries = activeSeriesKeys.length > 0 && visualization !== 'pie' && visualization !== 'metric';
130
+
131
+ const altTableColumns: TableColumnsType<ReportSeriesPoint> = useMemo(() => {
132
+ const base: TableColumnsType<ReportSeriesPoint> = [{ title: t('Bucket Field'), dataIndex: 'label', key: 'label' }];
133
+ if (hasMultiSeries) {
134
+ for (const sKey of activeSeriesKeys) {
135
+ base.push({
136
+ title: sKey,
137
+ key: `series-${sKey}`,
138
+ render: (_: unknown, row: ReportSeriesPoint) => {
139
+ const sub = row.breakdown?.find((b) => b.seriesKey === sKey);
140
+ return sub ? sub.value.toLocaleString() : '0';
141
+ },
142
+ });
143
+ }
144
+ } else {
145
+ base.push({ title: metricLabel || t('Value'), dataIndex: 'value', key: 'value' });
146
+ }
147
+ base.push({ title: t('Hits'), dataIndex: 'count', key: 'count' });
148
+ return base;
149
+ }, [activeSeriesKeys, hasMultiSeries, metricLabel, t]);
150
+
109
151
  if (visualization === 'metric') {
152
+ const isTruncatedCount =
153
+ totalRelation === 'gte' && (metricType === 'count' || metricType === undefined || metricValue === totalHits);
110
154
  return (
111
- <Card bordered={false} style={{ textAlign: 'center', padding: '16px 8px' }}>
155
+ <Card variant="borderless" style={{ textAlign: 'center', padding: '16px 8px' }}>
112
156
  <Statistic
113
157
  title={metricLabel || title}
114
158
  value={metricValue ?? 0}
159
+ prefix={isTruncatedCount ? '≥ ' : undefined}
115
160
  valueStyle={{ fontSize: 36, fontWeight: 600, color: token.colorPrimary }}
116
161
  />
117
- <Text type="secondary" style={{ fontSize: 12 }}>
118
- {t('Hits')}: {totalHits.toLocaleString()}
119
- </Text>
162
+ {deltaPercent !== undefined && deltaPercent !== null && (
163
+ <div style={{ marginTop: 6 }}>
164
+ <Tag color={deltaPercent > 0 ? 'geekblue' : deltaPercent < 0 ? 'cyan' : 'default'}>
165
+ {deltaPercent > 0 ? `▲ +${deltaPercent}%` : deltaPercent < 0 ? `▼ ${deltaPercent}%` : '0%'}
166
+ </Tag>
167
+ {previousMetricValue !== undefined && previousMetricValue !== null && (
168
+ <Text type="secondary" style={{ fontSize: 12 }}>
169
+ {t('vs previous period')}: {previousMetricValue.toLocaleString()}
170
+ </Text>
171
+ )}
172
+ </div>
173
+ )}
174
+ <div style={{ marginTop: 4 }}>
175
+ <Text type="secondary" style={{ fontSize: 12 }}>
176
+ {t('Hits')}: {totalHits.toLocaleString()}
177
+ {totalRelation === 'gte' ? '+' : ''}
178
+ </Text>
179
+ </div>
120
180
  </Card>
121
181
  );
122
182
  }
@@ -125,7 +185,6 @@ export const ElasticBasicChart: React.FC<ElasticBasicChartProps> = ({
125
185
  return <Empty description={t('No matching documents')} />;
126
186
  }
127
187
 
128
- const maxVal = Math.max(1, ...series.map((p) => Math.abs(p.value)));
129
188
  const svgWidth = 760;
130
189
  const svgHeight = 260;
131
190
  const padLeft = 56;
@@ -135,21 +194,41 @@ export const ElasticBasicChart: React.FC<ElasticBasicChartProps> = ({
135
194
  const plotW = svgWidth - padLeft - padRight;
136
195
  const plotH = svgHeight - padTop - padBottom;
137
196
 
197
+ const isPercentStack = hasMultiSeries && stackMode === 'percent';
198
+ const isStacked = hasMultiSeries && (stackMode === 'stacked' || stackMode === 'percent');
199
+
200
+ const computedMaxVal = (() => {
201
+ if (isPercentStack) return 100;
202
+ if (isStacked) {
203
+ return Math.max(
204
+ 1,
205
+ ...series.map((pt) => (pt.breakdown ?? []).reduce((acc, sub) => acc + Math.max(0, sub.value), 0)),
206
+ );
207
+ }
208
+ if (hasMultiSeries) {
209
+ return Math.max(1, ...series.flatMap((pt) => (pt.breakdown ?? []).map((sub) => Math.abs(sub.value))));
210
+ }
211
+ return Math.max(1, ...series.map((p) => Math.abs(p.value)));
212
+ })();
213
+
138
214
  const renderCartesian = () => {
139
215
  const ticks = [0, 0.25, 0.5, 0.75, 1];
140
216
  if (visualization === 'bar') {
141
- const gap = Math.min(16, Math.max(4, Math.floor(plotW / (series.length * 4))));
217
+ const gap = Math.max(6, Math.min(20, Math.floor(plotW / (series.length * 4))));
142
218
  const barW = Math.max(8, (plotW - gap * (series.length + 1)) / series.length);
143
219
  return (
144
220
  <svg
145
221
  role="img"
146
222
  aria-labelledby={svgTitleId}
147
223
  viewBox={`0 0 ${svgWidth} ${svgHeight}`}
148
- style={{ width: '100%', height: 260, display: 'block' }}
224
+ style={{ width: '100%', height: svgHeight, display: 'block' }}
149
225
  >
150
- <title id={svgTitleId}>{`${title} - ${metricLabel}`}</title>
226
+ <title id={svgTitleId}>{`${title} — ${metricLabel}`}</title>
151
227
  {ticks.map((ratio) => {
152
- const y = padTop + plotH - ratio * plotH;
228
+ const y = padTop + plotH * (1 - ratio);
229
+ const tickLabel = isPercentStack
230
+ ? `${Math.round(ratio * 100)}%`
231
+ : formatCompactNumber(computedMaxVal * ratio);
153
232
  return (
154
233
  <g key={`grid-${ratio}`}>
155
234
  <line
@@ -161,39 +240,128 @@ export const ElasticBasicChart: React.FC<ElasticBasicChartProps> = ({
161
240
  strokeDasharray="3 3"
162
241
  />
163
242
  <text x={padLeft - 8} y={y + 4} textAnchor="end" fontSize={11} fill={token.colorTextSecondary}>
164
- {formatCompactNumber(ratio * maxVal)}
243
+ {tickLabel}
165
244
  </text>
166
245
  </g>
167
246
  );
168
247
  })}
169
- {series.map((item, idx) => {
170
- const h = Math.max(2, (Math.max(0, item.value) / maxVal) * plotH);
171
- const x = padLeft + gap + idx * (barW + gap);
172
- const y = padTop + plotH - h;
248
+ {series.map((pt, idx) => {
249
+ const groupX = padLeft + gap + idx * (barW + gap);
173
250
  const showLabel = series.length <= 12 || idx % Math.ceil(series.length / 8) === 0;
251
+
252
+ if (hasMultiSeries) {
253
+ const bucketSum = Math.max(
254
+ 0.0001,
255
+ (pt.breakdown ?? []).reduce((sum, b) => sum + Math.max(0, b.value), 0),
256
+ );
257
+ if (isStacked) {
258
+ let cumulativeH = 0;
259
+ return (
260
+ <g key={`${pt.key}-${idx}`}>
261
+ {activeSeriesKeys.map((sKey, sIdx) => {
262
+ const subVal = Math.max(0, pt.breakdown?.find((b) => b.seriesKey === sKey)?.value ?? 0);
263
+ const normRatio = isPercentStack ? subVal / bucketSum : subVal / computedMaxVal;
264
+ const segH = Math.max(0, normRatio * plotH);
265
+ const segY = padTop + plotH - cumulativeH - segH;
266
+ cumulativeH += segH;
267
+ if (segH <= 0) return null;
268
+ const tooltipVal = isPercentStack
269
+ ? `${sKey}: ${subVal.toLocaleString()} (${((subVal / bucketSum) * 100).toFixed(1)}%)`
270
+ : `${sKey}: ${subVal.toLocaleString()}`;
271
+ return (
272
+ <Tooltip key={sKey} title={`${pt.label} · ${tooltipVal}`}>
273
+ <rect
274
+ x={groupX}
275
+ y={segY}
276
+ width={barW}
277
+ height={segH}
278
+ fill={PALETTE[sIdx % PALETTE.length]}
279
+ style={{ cursor: onSelectBucket ? 'pointer' : 'default' }}
280
+ onClick={() => onSelectBucket?.(pt.key)}
281
+ />
282
+ </Tooltip>
283
+ );
284
+ })}
285
+ {showLabel && (
286
+ <text
287
+ x={groupX + barW / 2}
288
+ y={svgHeight - 16}
289
+ textAnchor="middle"
290
+ fontSize={11}
291
+ fill={token.colorTextSecondary}
292
+ >
293
+ {truncateLabel(pt.label, 14)}
294
+ </text>
295
+ )}
296
+ </g>
297
+ );
298
+ }
299
+
300
+ // Grouped side-by-side bars
301
+ const subBarW = Math.max(3, (barW - (activeSeriesKeys.length - 1) * 2) / activeSeriesKeys.length);
302
+ return (
303
+ <g key={`${pt.key}-${idx}`}>
304
+ {activeSeriesKeys.map((sKey, sIdx) => {
305
+ const subVal = Math.max(0, pt.breakdown?.find((b) => b.seriesKey === sKey)?.value ?? 0);
306
+ const segH = Math.max(subVal > 0 ? 2 : 0, (subVal / computedMaxVal) * plotH);
307
+ const subX = groupX + sIdx * (subBarW + 2);
308
+ const subY = padTop + plotH - segH;
309
+ return (
310
+ <Tooltip key={sKey} title={`${pt.label} · ${sKey}: ${subVal.toLocaleString()}`}>
311
+ <rect
312
+ x={subX}
313
+ y={subY}
314
+ width={subBarW}
315
+ height={segH}
316
+ rx={2}
317
+ fill={PALETTE[sIdx % PALETTE.length]}
318
+ style={{ cursor: onSelectBucket ? 'pointer' : 'default' }}
319
+ onClick={() => onSelectBucket?.(pt.key)}
320
+ />
321
+ </Tooltip>
322
+ );
323
+ })}
324
+ {showLabel && (
325
+ <text
326
+ x={groupX + barW / 2}
327
+ y={svgHeight - 16}
328
+ textAnchor="middle"
329
+ fontSize={11}
330
+ fill={token.colorTextSecondary}
331
+ >
332
+ {truncateLabel(pt.label, 14)}
333
+ </text>
334
+ )}
335
+ </g>
336
+ );
337
+ }
338
+
339
+ const h = Math.max(2, (Math.max(0, pt.value) / computedMaxVal) * plotH);
340
+ const y = padTop + plotH - h;
341
+ const color = PALETTE[idx % PALETTE.length];
174
342
  return (
175
- <g key={`${item.key}-${idx}`}>
176
- <Tooltip title={`${item.label}: ${item.value.toLocaleString()} (${t('Hits')}: ${item.count})`}>
343
+ <g key={`${pt.key}-${idx}`}>
344
+ <Tooltip title={`${pt.label}: ${pt.value.toLocaleString()} (${pt.count} docs)`}>
177
345
  <rect
178
- x={x}
346
+ x={groupX}
179
347
  y={y}
180
348
  width={barW}
181
349
  height={h}
182
350
  rx={3}
183
- fill={PALETTE[idx % PALETTE.length]}
351
+ fill={color}
184
352
  style={{ cursor: onSelectBucket ? 'pointer' : 'default' }}
185
- onClick={() => onSelectBucket?.(item.key)}
353
+ onClick={() => onSelectBucket?.(pt.key)}
186
354
  />
187
355
  </Tooltip>
188
356
  {showLabel && (
189
357
  <text
190
- x={x + barW / 2}
191
- y={padTop + plotH + 18}
358
+ x={groupX + barW / 2}
359
+ y={svgHeight - 16}
192
360
  textAnchor="middle"
193
361
  fontSize={11}
194
362
  fill={token.colorTextSecondary}
195
363
  >
196
- {truncateLabel(item.label, 12)}
364
+ {truncateLabel(pt.label, 14)}
197
365
  </text>
198
366
  )}
199
367
  </g>
@@ -203,18 +371,95 @@ export const ElasticBasicChart: React.FC<ElasticBasicChartProps> = ({
203
371
  );
204
372
  }
205
373
 
206
- const stepX = series.length > 1 ? plotW / (series.length - 1) : plotW / 2;
207
- const points = series.map((item, idx) => {
374
+ // Line or Area chart (single-series or multi-series)
375
+ const stepX = series.length > 1 ? plotW / (series.length - 1) : 0;
376
+
377
+ if (hasMultiSeries) {
378
+ return (
379
+ <svg
380
+ role="img"
381
+ aria-labelledby={svgTitleId}
382
+ viewBox={`0 0 ${svgWidth} ${svgHeight}`}
383
+ style={{ width: '100%', height: svgHeight, display: 'block' }}
384
+ >
385
+ <title id={svgTitleId}>{`${title} — ${metricLabel}`}</title>
386
+ {ticks.map((ratio) => {
387
+ const y = padTop + plotH * (1 - ratio);
388
+ return (
389
+ <g key={`grid-${ratio}`}>
390
+ <line
391
+ x1={padLeft}
392
+ y1={y}
393
+ x2={svgWidth - padRight}
394
+ y2={y}
395
+ stroke={token.colorSplit}
396
+ strokeDasharray="3 3"
397
+ />
398
+ <text x={padLeft - 8} y={y + 4} textAnchor="end" fontSize={11} fill={token.colorTextSecondary}>
399
+ {formatCompactNumber(computedMaxVal * ratio)}
400
+ </text>
401
+ </g>
402
+ );
403
+ })}
404
+ {activeSeriesKeys.map((sKey, sIdx) => {
405
+ const color = PALETTE[sIdx % PALETTE.length];
406
+ const pts = series.map((pt, idx) => {
407
+ const x = series.length > 1 ? padLeft + idx * stepX : padLeft + plotW / 2;
408
+ const val = Math.max(0, pt.breakdown?.find((b) => b.seriesKey === sKey)?.value ?? 0);
409
+ const y = padTop + plotH - (val / computedMaxVal) * plotH;
410
+ return { x, y, val, pt };
411
+ });
412
+ const polylineStr = pts.map((c) => `${c.x},${c.y}`).join(' ');
413
+ const areaStr =
414
+ pts.length > 0
415
+ ? `M ${pts[0].x},${padTop + plotH} ` +
416
+ pts.map((c) => `L ${c.x},${c.y}`).join(' ') +
417
+ ` L ${pts[pts.length - 1].x},${padTop + plotH} Z`
418
+ : '';
419
+ return (
420
+ <g key={sKey}>
421
+ {visualization === 'area' && areaStr && <path d={areaStr} fill={color} opacity={0.16} />}
422
+ <polyline fill="none" stroke={color} strokeWidth={2.2} points={polylineStr} />
423
+ {pts.map((c, idx) => (
424
+ <Tooltip key={`${sKey}-${idx}`} title={`${c.pt.label} · ${sKey}: ${c.val.toLocaleString()}`}>
425
+ <circle cx={c.x} cy={c.y} r={3.5} fill={color} />
426
+ </Tooltip>
427
+ ))}
428
+ </g>
429
+ );
430
+ })}
431
+ {series.map((pt, idx) => {
432
+ const showLabel = series.length <= 8 || idx % Math.ceil(series.length / 6) === 0;
433
+ if (!showLabel) return null;
434
+ const x = series.length > 1 ? padLeft + idx * stepX : padLeft + plotW / 2;
435
+ return (
436
+ <text
437
+ key={`lbl-${idx}`}
438
+ x={x}
439
+ y={svgHeight - 16}
440
+ textAnchor="middle"
441
+ fontSize={11}
442
+ fill={token.colorTextSecondary}
443
+ >
444
+ {truncateLabel(pt.label, 16)}
445
+ </text>
446
+ );
447
+ })}
448
+ </svg>
449
+ );
450
+ }
451
+
452
+ const coords = series.map((pt, idx) => {
208
453
  const x = series.length > 1 ? padLeft + idx * stepX : padLeft + plotW / 2;
209
- const y = padTop + plotH - (Math.max(0, item.value) / maxVal) * plotH;
210
- return { x, y, item, idx };
454
+ const y = padTop + plotH - (Math.max(0, pt.value) / computedMaxVal) * plotH;
455
+ return { x, y, pt };
211
456
  });
212
- const linePath = points.map((pt, i) => `${i === 0 ? 'M' : 'L'} ${pt.x.toFixed(1)} ${pt.y.toFixed(1)}`).join(' ');
457
+ const polylinePoints = coords.map((c) => `${c.x},${c.y}`).join(' ');
213
458
  const areaPath =
214
- points.length > 0
215
- ? `${linePath} L ${points[points.length - 1].x.toFixed(1)} ${(padTop + plotH).toFixed(
216
- 1,
217
- )} L ${points[0].x.toFixed(1)} ${(padTop + plotH).toFixed(1)} Z`
459
+ coords.length > 0
460
+ ? `M ${coords[0].x},${padTop + plotH} ` +
461
+ coords.map((c) => `L ${c.x},${c.y}`).join(' ') +
462
+ ` L ${coords[coords.length - 1].x},${padTop + plotH} Z`
218
463
  : '';
219
464
 
220
465
  return (
@@ -222,11 +467,11 @@ export const ElasticBasicChart: React.FC<ElasticBasicChartProps> = ({
222
467
  role="img"
223
468
  aria-labelledby={svgTitleId}
224
469
  viewBox={`0 0 ${svgWidth} ${svgHeight}`}
225
- style={{ width: '100%', height: 260, display: 'block' }}
470
+ style={{ width: '100%', height: svgHeight, display: 'block' }}
226
471
  >
227
- <title id={svgTitleId}>{`${title} - ${metricLabel}`}</title>
472
+ <title id={svgTitleId}>{`${title} — ${metricLabel}`}</title>
228
473
  {ticks.map((ratio) => {
229
- const y = padTop + plotH - ratio * plotH;
474
+ const y = padTop + plotH * (1 - ratio);
230
475
  return (
231
476
  <g key={`grid-${ratio}`}>
232
477
  <line
@@ -238,32 +483,30 @@ export const ElasticBasicChart: React.FC<ElasticBasicChartProps> = ({
238
483
  strokeDasharray="3 3"
239
484
  />
240
485
  <text x={padLeft - 8} y={y + 4} textAnchor="end" fontSize={11} fill={token.colorTextSecondary}>
241
- {formatCompactNumber(ratio * maxVal)}
486
+ {formatCompactNumber(computedMaxVal * ratio)}
242
487
  </text>
243
488
  </g>
244
489
  );
245
490
  })}
246
- {visualization === 'area' && areaPath && <path d={areaPath} fill={token.colorPrimary} fillOpacity={0.18} />}
247
- {linePath && <path d={linePath} fill="none" stroke={token.colorPrimary} strokeWidth={2.5} />}
248
- {points.map(({ x, y, item, idx }) => {
249
- const showLabel = series.length <= 10 || idx % Math.ceil(series.length / 7) === 0;
491
+ {visualization === 'area' && areaPath && <path d={areaPath} fill={token.colorPrimary} opacity={0.18} />}
492
+ <polyline fill="none" stroke={token.colorPrimary} strokeWidth={2.5} points={polylinePoints} />
493
+ {coords.map((c, idx) => {
494
+ const showLabel = series.length <= 8 || idx % Math.ceil(series.length / 6) === 0;
250
495
  return (
251
- <g key={`${item.key}-${idx}`}>
252
- <Tooltip title={`${item.label}: ${item.value.toLocaleString()}`}>
496
+ <g key={`${c.pt.key}-${idx}`}>
497
+ <Tooltip title={`${c.pt.label}: ${c.pt.value.toLocaleString()}`}>
253
498
  <circle
254
- cx={x}
255
- cy={y}
499
+ cx={c.x}
500
+ cy={c.y}
256
501
  r={4}
257
- fill={token.colorBgContainer}
258
- stroke={token.colorPrimary}
259
- strokeWidth={2}
502
+ fill={token.colorPrimary}
260
503
  style={{ cursor: onSelectBucket ? 'pointer' : 'default' }}
261
- onClick={() => onSelectBucket?.(item.key)}
504
+ onClick={() => onSelectBucket?.(c.pt.key)}
262
505
  />
263
506
  </Tooltip>
264
507
  {showLabel && (
265
- <text x={x} y={padTop + plotH + 18} textAnchor="middle" fontSize={11} fill={token.colorTextSecondary}>
266
- {truncateLabel(item.label, 12)}
508
+ <text x={c.x} y={svgHeight - 16} textAnchor="middle" fontSize={11} fill={token.colorTextSecondary}>
509
+ {truncateLabel(c.pt.label, 16)}
267
510
  </text>
268
511
  )}
269
512
  </g>
@@ -274,47 +517,32 @@ export const ElasticBasicChart: React.FC<ElasticBasicChartProps> = ({
274
517
  };
275
518
 
276
519
  const renderPie = () => {
277
- const totalValue = Math.max(
278
- 1,
279
- series.reduce((acc, item) => acc + Math.max(0, item.value), 0),
280
- );
520
+ const totalValue = series.reduce((sum, item) => sum + Math.max(0, item.value), 0) || 1;
281
521
  const cx = 130;
282
522
  const cy = 120;
283
- const radius = 92;
523
+ const r = 95;
284
524
  let cumulativeAngle = -Math.PI / 2;
285
525
 
286
526
  return (
287
527
  <div style={{ display: 'flex', flexWrap: 'wrap', alignItems: 'center', gap: 24 }}>
288
- <svg
289
- role="img"
290
- aria-labelledby={svgTitleId}
291
- viewBox="0 0 260 240"
292
- style={{ width: 240, height: 220, flexShrink: 0 }}
293
- >
294
- <title id={svgTitleId}>{`${title} - ${metricLabel}`}</title>
528
+ <svg role="img" aria-labelledby={svgTitleId} viewBox="0 0 260 240" style={{ width: 260, height: 240 }}>
529
+ <title id={svgTitleId}>{`${title} — ${metricLabel}`}</title>
295
530
  {series.map((item, idx) => {
296
531
  const val = Math.max(0, item.value);
297
- const angle = (val / totalValue) * Math.PI * 2;
532
+ const sliceAngle = (val / totalValue) * Math.PI * 2;
298
533
  const startAngle = cumulativeAngle;
299
- const endAngle = cumulativeAngle + angle;
534
+ const endAngle = cumulativeAngle + sliceAngle;
300
535
  cumulativeAngle = endAngle;
301
536
 
302
- if (series.length === 1 || angle >= Math.PI * 2 - 0.001) {
303
- return (
304
- <Tooltip key={item.key} title={`${item.label}: ${item.value.toLocaleString()} (100%)`}>
305
- <circle cx={cx} cy={cy} r={radius} fill={PALETTE[0]} />
306
- </Tooltip>
307
- );
308
- }
309
-
310
- const x1 = cx + radius * Math.cos(startAngle);
311
- const y1 = cy + radius * Math.sin(startAngle);
312
- const x2 = cx + radius * Math.cos(endAngle);
313
- const y2 = cy + radius * Math.sin(endAngle);
314
- const largeArc = angle > Math.PI ? 1 : 0;
315
- const pathData = `M ${cx} ${cy} L ${x1.toFixed(2)} ${y1.toFixed(
316
- 2,
317
- )} A ${radius} ${radius} 0 ${largeArc} 1 ${x2.toFixed(2)} ${y2.toFixed(2)} Z`;
537
+ const x1 = cx + r * Math.cos(startAngle);
538
+ const y1 = cy + r * Math.sin(startAngle);
539
+ const x2 = cx + r * Math.cos(endAngle);
540
+ const y2 = cy + r * Math.sin(endAngle);
541
+ const largeArc = sliceAngle > Math.PI ? 1 : 0;
542
+ const pathData =
543
+ series.length === 1
544
+ ? `M ${cx} ${cy - r} A ${r} ${r} 0 1 1 ${cx - 0.01} ${cy - r} Z`
545
+ : `M ${cx} ${cy} L ${x1} ${y1} A ${r} ${r} 0 ${largeArc} 1 ${x2} ${y2} Z`;
318
546
  const pct = ((val / totalValue) * 100).toFixed(1);
319
547
 
320
548
  return (
@@ -368,26 +596,51 @@ export const ElasticBasicChart: React.FC<ElasticBasicChartProps> = ({
368
596
  };
369
597
 
370
598
  return (
371
- <figure style={{ margin: 0 }}>
372
- <div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center', marginBottom: 8 }}>
599
+ <figure style={{ margin: 0, width: '100%', maxWidth: '100%', minWidth: 0 }}>
600
+ <div
601
+ style={{
602
+ display: 'flex',
603
+ flexWrap: 'wrap',
604
+ justifyContent: 'space-between',
605
+ alignItems: 'center',
606
+ gap: 8,
607
+ marginBottom: 8,
608
+ }}
609
+ >
373
610
  <figcaption style={{ fontSize: 12, color: token.colorTextSecondary }}>
374
611
  {metricLabel} · {t('Hits')}: {totalHits.toLocaleString()}
612
+ {totalRelation === 'gte' ? '+' : ''}
375
613
  </figcaption>
376
- <Button size="small" type="link" onClick={() => setShowTableAlt((prev) => !prev)}>
377
- {showTableAlt ? t('Show chart') : t('Show data table')}
378
- </Button>
614
+ <Space wrap size={12}>
615
+ {hasMultiSeries &&
616
+ activeSeriesKeys.map((sKey, sIdx) => (
617
+ <span key={sKey} style={{ display: 'inline-flex', alignItems: 'center', gap: 5, fontSize: 12 }}>
618
+ <span
619
+ aria-hidden="true"
620
+ style={{
621
+ display: 'inline-block',
622
+ width: 10,
623
+ height: 10,
624
+ borderRadius: 2,
625
+ backgroundColor: PALETTE[sIdx % PALETTE.length],
626
+ }}
627
+ />
628
+ <span>{sKey}</span>
629
+ </span>
630
+ ))}
631
+ <Button size="small" type="link" onClick={() => setShowTableAlt((prev) => !prev)}>
632
+ {showTableAlt ? t('Show chart') : t('Show data table')}
633
+ </Button>
634
+ </Space>
379
635
  </div>
380
636
  {showTableAlt ? (
381
637
  <Table
382
638
  size="small"
383
639
  rowKey="key"
384
640
  pagination={false}
641
+ scroll={{ x: 'max-content' }}
385
642
  dataSource={series}
386
- columns={[
387
- { title: t('Bucket Field'), dataIndex: 'label', key: 'label' },
388
- { title: metricLabel || t('Value'), dataIndex: 'value', key: 'value' },
389
- { title: t('Hits'), dataIndex: 'count', key: 'count' },
390
- ]}
643
+ columns={altTableColumns}
391
644
  />
392
645
  ) : visualization === 'pie' ? (
393
646
  renderPie()