@ant-design/agentic-ui 2.0.3 → 2.0.4
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.
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { ChartOptions } from 'chart.js';
|
|
1
2
|
import React from 'react';
|
|
2
3
|
import { ChartContainerProps } from '../components';
|
|
3
4
|
import { StatisticConfigType } from '../hooks/useChartStatistic';
|
|
@@ -80,6 +81,8 @@ export interface BarChartProps extends ChartContainerProps {
|
|
|
80
81
|
dataIndex: number;
|
|
81
82
|
datasetIndex: number;
|
|
82
83
|
}) => string;
|
|
84
|
+
/** 外部传入的 Chart.js 选项,会与默认选项合并 */
|
|
85
|
+
chartOptions?: Partial<ChartOptions<'bar'>>;
|
|
83
86
|
}
|
|
84
87
|
declare const BarChart: React.FC<BarChartProps>;
|
|
85
88
|
export default BarChart;
|
|
@@ -91,7 +91,8 @@ var BarChart = ({
|
|
|
91
91
|
statistic: statisticConfig,
|
|
92
92
|
variant,
|
|
93
93
|
showDataLabels = false,
|
|
94
|
-
dataLabelFormatter
|
|
94
|
+
dataLabelFormatter,
|
|
95
|
+
chartOptions
|
|
95
96
|
}) => {
|
|
96
97
|
const safeData = Array.isArray(data) ? data : [];
|
|
97
98
|
const [windowWidth, setWindowWidth] = useState(
|
|
@@ -351,10 +352,93 @@ var BarChart = ({
|
|
|
351
352
|
const isLight = theme === "light";
|
|
352
353
|
const axisTextColor = isLight ? "rgba(0, 25, 61, 0.3255)" : "rgba(255, 255, 255, 0.8)";
|
|
353
354
|
const gridColor = isLight ? "rgba(0,0,0,0.08)" : "rgba(255,255,255,0.2)";
|
|
354
|
-
const
|
|
355
|
+
const calculateLabelWidth = (text, fontSize = 11) => {
|
|
356
|
+
const canvas = document.createElement("canvas");
|
|
357
|
+
const context2 = canvas.getContext("2d");
|
|
358
|
+
if (!context2)
|
|
359
|
+
return text.length * fontSize * 0.6;
|
|
360
|
+
context2.font = `${fontSize}px Arial, sans-serif`;
|
|
361
|
+
const metrics = context2.measureText(text);
|
|
362
|
+
return metrics.width;
|
|
363
|
+
};
|
|
364
|
+
const calculateMaxLabelWidth = useMemo(() => {
|
|
365
|
+
if (!showDataLabels || !filteredData.length)
|
|
366
|
+
return 0;
|
|
367
|
+
const fontSize = isMobile ? 10 : 11;
|
|
368
|
+
let maxWidth = 0;
|
|
369
|
+
filteredData.forEach((item) => {
|
|
370
|
+
const value = typeof item.y === "number" ? item.y : Number(item.y);
|
|
371
|
+
if (Number.isFinite(value)) {
|
|
372
|
+
let labelText = "";
|
|
373
|
+
if (dataLabelFormatter) {
|
|
374
|
+
labelText = dataLabelFormatter({
|
|
375
|
+
value,
|
|
376
|
+
label: String(item.x),
|
|
377
|
+
datasetLabel: String(item.type || "默认"),
|
|
378
|
+
dataIndex: 0,
|
|
379
|
+
datasetIndex: 0
|
|
380
|
+
});
|
|
381
|
+
} else {
|
|
382
|
+
labelText = value.toLocaleString();
|
|
383
|
+
}
|
|
384
|
+
const width2 = calculateLabelWidth(labelText, fontSize);
|
|
385
|
+
maxWidth = Math.max(maxWidth, width2);
|
|
386
|
+
}
|
|
387
|
+
});
|
|
388
|
+
return maxWidth;
|
|
389
|
+
}, [filteredData, showDataLabels, dataLabelFormatter, isMobile]);
|
|
390
|
+
const calculateDynamicPadding = useMemo(() => {
|
|
391
|
+
if (!showDataLabels || calculateMaxLabelWidth === 0)
|
|
392
|
+
return { top: 0, right: 0, bottom: 0, left: 0 };
|
|
393
|
+
const basePadding = 8;
|
|
394
|
+
const labelPadding = Math.ceil(calculateMaxLabelWidth);
|
|
395
|
+
if (indexAxis === "y") {
|
|
396
|
+
return {
|
|
397
|
+
top: basePadding,
|
|
398
|
+
right: Math.max(basePadding, labelPadding),
|
|
399
|
+
bottom: basePadding,
|
|
400
|
+
left: basePadding
|
|
401
|
+
};
|
|
402
|
+
} else {
|
|
403
|
+
return {
|
|
404
|
+
top: Math.max(basePadding, labelPadding),
|
|
405
|
+
right: basePadding,
|
|
406
|
+
bottom: basePadding,
|
|
407
|
+
left: basePadding
|
|
408
|
+
};
|
|
409
|
+
}
|
|
410
|
+
}, [showDataLabels, calculateMaxLabelWidth, indexAxis]);
|
|
411
|
+
const deepMerge = (target, source) => {
|
|
412
|
+
var _a;
|
|
413
|
+
if (!source || typeof source !== "object")
|
|
414
|
+
return source;
|
|
415
|
+
if (!target || typeof target !== "object")
|
|
416
|
+
return source;
|
|
417
|
+
const result = __spreadValues({}, target);
|
|
418
|
+
for (const key in source) {
|
|
419
|
+
if (source.hasOwnProperty(key)) {
|
|
420
|
+
if (typeof source[key] === "object" && source[key] !== null && !Array.isArray(source[key])) {
|
|
421
|
+
if (key === "layout" && source[key].padding && ((_a = target.layout) == null ? void 0 : _a.padding)) {
|
|
422
|
+
result[key] = __spreadProps(__spreadValues(__spreadValues({}, target[key]), source[key]), {
|
|
423
|
+
padding: __spreadValues(__spreadValues({}, target[key].padding), source[key].padding)
|
|
424
|
+
});
|
|
425
|
+
} else {
|
|
426
|
+
result[key] = deepMerge(target[key] || {}, source[key]);
|
|
427
|
+
}
|
|
428
|
+
} else {
|
|
429
|
+
result[key] = source[key];
|
|
430
|
+
}
|
|
431
|
+
}
|
|
432
|
+
}
|
|
433
|
+
return result;
|
|
434
|
+
};
|
|
435
|
+
const defaultOptions = {
|
|
355
436
|
responsive: true,
|
|
356
437
|
maintainAspectRatio: false,
|
|
357
438
|
indexAxis,
|
|
439
|
+
layout: {
|
|
440
|
+
padding: calculateDynamicPadding
|
|
441
|
+
},
|
|
358
442
|
plugins: __spreadValues({
|
|
359
443
|
legend: {
|
|
360
444
|
display: showLegend,
|
|
@@ -526,6 +610,7 @@ var BarChart = ({
|
|
|
526
610
|
}
|
|
527
611
|
}
|
|
528
612
|
};
|
|
613
|
+
const options = chartOptions ? deepMerge(defaultOptions, chartOptions) : defaultOptions;
|
|
529
614
|
const handleDownload = () => {
|
|
530
615
|
downloadChart(chartRef.current, "bar-chart");
|
|
531
616
|
};
|