onesight-charts 1.4.0 → 1.4.2
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,9 @@
|
|
|
1
|
+
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
|
|
2
|
+
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
|
|
3
|
+
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
|
|
4
|
+
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
|
|
5
|
+
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
|
|
6
|
+
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
|
|
1
7
|
import React from 'react';
|
|
2
8
|
import { computeFloatNull, dealSort, numberFormatNull, transformedSeriesData } from "../../../utils/chartUtils";
|
|
3
9
|
import Pagination from "../../pagination";
|
|
@@ -17,6 +23,25 @@ function Legend(props) {
|
|
|
17
23
|
paginationCb = props.paginationCb,
|
|
18
24
|
width = props.width,
|
|
19
25
|
platformIconMap = props.platformIconMap;
|
|
26
|
+
|
|
27
|
+
// 辅助函数:判断两个 item 是否为同一个项
|
|
28
|
+
var isSameItem = function isSameItem(item1, item2) {
|
|
29
|
+
// 如果都有 alias,使用 alias 精确匹配
|
|
30
|
+
if (item1.alias && item2.alias) {
|
|
31
|
+
return item1.alias === item2.alias;
|
|
32
|
+
}
|
|
33
|
+
// 如果只有一方有 alias,说明不是同一个项
|
|
34
|
+
if (item1.alias && !item2.alias || !item1.alias && item2.alias) {
|
|
35
|
+
return false;
|
|
36
|
+
}
|
|
37
|
+
// 都没有 alias 时,使用 name + value + color 组合来匹配,提高匹配准确性
|
|
38
|
+
return item1.name === item2.name && item1.value === item2.value && item1.color === item2.color;
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
// 辅助函数:生成唯一 id(与渲染时保持一致)
|
|
42
|
+
var getUniqueId = function getUniqueId(item, index) {
|
|
43
|
+
return item.alias || "".concat(item.name, "_").concat(index);
|
|
44
|
+
};
|
|
20
45
|
var handleHover = function handleHover(item) {
|
|
21
46
|
if (item) {
|
|
22
47
|
chart.current.dispatchAction({
|
|
@@ -30,26 +55,35 @@ function Legend(props) {
|
|
|
30
55
|
}
|
|
31
56
|
};
|
|
32
57
|
var handleLegendClick = function handleLegendClick(item) {
|
|
33
|
-
|
|
34
|
-
|
|
58
|
+
// 创建新的数组副本,避免直接修改 props
|
|
59
|
+
var newSeriesData = JSON.parse(JSON.stringify(seriesData));
|
|
60
|
+
|
|
61
|
+
// 使用 isSameItem 来精确匹配项目
|
|
62
|
+
var index = newSeriesData.findIndex(function (val) {
|
|
63
|
+
return isSameItem(val, item);
|
|
35
64
|
});
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
65
|
+
|
|
66
|
+
// 在 legendData 中找到对应的索引,用于生成唯一的 element id
|
|
67
|
+
var legendItemIndex = legendData.findIndex(function (val) {
|
|
68
|
+
return isSameItem(val, item);
|
|
69
|
+
});
|
|
70
|
+
var uniqueId = getUniqueId(item, legendItemIndex !== -1 ? legendItemIndex : 0);
|
|
71
|
+
var element = document.getElementById('legend-content' + uniqueId);
|
|
72
|
+
if (index !== -1) {
|
|
73
|
+
// 如果找到了,就从数组中移除它
|
|
74
|
+
newSeriesData.splice(index, 1);
|
|
75
|
+
if (element) {
|
|
45
76
|
element.classList.add('legend-item-cancel');
|
|
46
77
|
}
|
|
47
78
|
} else {
|
|
48
|
-
|
|
49
|
-
|
|
79
|
+
// 如果没找到,就添加它
|
|
80
|
+
newSeriesData.push(item);
|
|
81
|
+
if (element) {
|
|
82
|
+
element.classList.remove('legend-item-cancel');
|
|
83
|
+
}
|
|
50
84
|
}
|
|
51
|
-
dealSort(
|
|
52
|
-
var dealData = JSON.parse(JSON.stringify(
|
|
85
|
+
dealSort(newSeriesData, order);
|
|
86
|
+
var dealData = JSON.parse(JSON.stringify(newSeriesData));
|
|
53
87
|
dealData.forEach(function (item) {
|
|
54
88
|
if (item.value === 0) {
|
|
55
89
|
item.value = -1;
|
|
@@ -60,6 +94,11 @@ function Legend(props) {
|
|
|
60
94
|
data: transformedSeriesData(dealData)
|
|
61
95
|
}]
|
|
62
96
|
});
|
|
97
|
+
|
|
98
|
+
// 注意:这里直接修改了传入的 seriesData,虽然不推荐,但保持原有行为
|
|
99
|
+
// 如果父组件使用受控组件模式,应该通过 props 传递更新函数
|
|
100
|
+
seriesData.length = 0;
|
|
101
|
+
seriesData.push.apply(seriesData, _toConsumableArray(newSeriesData));
|
|
63
102
|
};
|
|
64
103
|
var pageData = JSON.parse(JSON.stringify(seriesData));
|
|
65
104
|
return /*#__PURE__*/React.createElement("ul", {
|
|
@@ -67,13 +106,15 @@ function Legend(props) {
|
|
|
67
106
|
}, /*#__PURE__*/React.createElement("div", {
|
|
68
107
|
className: "li-container"
|
|
69
108
|
}, legendData.map(function (item, i) {
|
|
109
|
+
// 使用 alias 或 name+索引来生成唯一的 id(与 handleLegendClick 中的逻辑保持一致)
|
|
110
|
+
var uniqueId = getUniqueId(item, i);
|
|
70
111
|
return /*#__PURE__*/React.createElement("li", {
|
|
71
|
-
id: 'legend-content' +
|
|
112
|
+
id: 'legend-content' + uniqueId,
|
|
72
113
|
className: "legend-item",
|
|
73
114
|
style: {
|
|
74
115
|
marginBottom: legendData.length > 9 ? '4px' : '12px'
|
|
75
116
|
},
|
|
76
|
-
key:
|
|
117
|
+
key: uniqueId
|
|
77
118
|
}, /*#__PURE__*/React.createElement("div", {
|
|
78
119
|
className: "parent-wrap"
|
|
79
120
|
}, /*#__PURE__*/React.createElement("div", {
|
|
@@ -46,7 +46,9 @@ function StackedBar(props) {
|
|
|
46
46
|
_props$yAxisLabelWidt = props.yAxisLabelWidth,
|
|
47
47
|
yAxisLabelWidth = _props$yAxisLabelWidt === void 0 ? true : _props$yAxisLabelWidt,
|
|
48
48
|
_props$showAllLegends = props.showAllLegends,
|
|
49
|
-
showAllLegends = _props$showAllLegends === void 0 ? false : _props$showAllLegends
|
|
49
|
+
showAllLegends = _props$showAllLegends === void 0 ? false : _props$showAllLegends,
|
|
50
|
+
_props$xAxisMaxMultip = props.xAxisMaxMultiplier,
|
|
51
|
+
xAxisMaxMultiplier = _props$xAxisMaxMultip === void 0 ? 1.1 : _props$xAxisMaxMultip;
|
|
50
52
|
var chartRef = useRef();
|
|
51
53
|
var legendMoreBtnRef = useRef(null);
|
|
52
54
|
var dropdownRef = useRef(null);
|
|
@@ -332,8 +334,8 @@ function StackedBar(props) {
|
|
|
332
334
|
finalMax = maxTotal + overflowPx * unitPerPx;
|
|
333
335
|
}
|
|
334
336
|
|
|
335
|
-
// 确保最终值不小于 maxTotal * 1.3
|
|
336
|
-
finalMax = Math.max(finalMax, maxTotal * 1.3) *
|
|
337
|
+
// 确保最终值不小于 maxTotal * 1.3,并应用额外的倍率以确保标签完全显示
|
|
338
|
+
finalMax = Math.max(finalMax, maxTotal * 1.3) * xAxisMaxMultiplier;
|
|
337
339
|
}
|
|
338
340
|
}
|
|
339
341
|
|
|
@@ -53,6 +53,18 @@ function Legend(props) {
|
|
|
53
53
|
width,
|
|
54
54
|
platformIconMap
|
|
55
55
|
} = props;
|
|
56
|
+
const isSameItem = (item1, item2) => {
|
|
57
|
+
if (item1.alias && item2.alias) {
|
|
58
|
+
return item1.alias === item2.alias;
|
|
59
|
+
}
|
|
60
|
+
if (item1.alias && !item2.alias || !item1.alias && item2.alias) {
|
|
61
|
+
return false;
|
|
62
|
+
}
|
|
63
|
+
return item1.name === item2.name && item1.value === item2.value && item1.color === item2.color;
|
|
64
|
+
};
|
|
65
|
+
const getUniqueId = (item, index) => {
|
|
66
|
+
return item.alias || `${item.name}_${index}`;
|
|
67
|
+
};
|
|
56
68
|
const handleHover = (item) => {
|
|
57
69
|
if (item) {
|
|
58
70
|
chart.current.dispatchAction({
|
|
@@ -66,22 +78,29 @@ function Legend(props) {
|
|
|
66
78
|
}
|
|
67
79
|
};
|
|
68
80
|
const handleLegendClick = (item) => {
|
|
69
|
-
const
|
|
70
|
-
|
|
81
|
+
const newSeriesData = JSON.parse(JSON.stringify(seriesData));
|
|
82
|
+
const index = newSeriesData.findIndex((val) => isSameItem(val, item));
|
|
83
|
+
const legendItemIndex = legendData.findIndex(
|
|
84
|
+
(val) => isSameItem(val, item)
|
|
85
|
+
);
|
|
86
|
+
const uniqueId = getUniqueId(
|
|
87
|
+
item,
|
|
88
|
+
legendItemIndex !== -1 ? legendItemIndex : 0
|
|
71
89
|
);
|
|
72
|
-
const element = document.getElementById("legend-content" +
|
|
73
|
-
if (
|
|
74
|
-
|
|
75
|
-
if (
|
|
76
|
-
seriesData.splice(index, 1);
|
|
90
|
+
const element = document.getElementById("legend-content" + uniqueId);
|
|
91
|
+
if (index !== -1) {
|
|
92
|
+
newSeriesData.splice(index, 1);
|
|
93
|
+
if (element) {
|
|
77
94
|
element.classList.add("legend-item-cancel");
|
|
78
95
|
}
|
|
79
96
|
} else {
|
|
80
|
-
|
|
81
|
-
element
|
|
97
|
+
newSeriesData.push(item);
|
|
98
|
+
if (element) {
|
|
99
|
+
element.classList.remove("legend-item-cancel");
|
|
100
|
+
}
|
|
82
101
|
}
|
|
83
|
-
(0, import_chartUtils.dealSort)(
|
|
84
|
-
const dealData = JSON.parse(JSON.stringify(
|
|
102
|
+
(0, import_chartUtils.dealSort)(newSeriesData, order);
|
|
103
|
+
const dealData = JSON.parse(JSON.stringify(newSeriesData));
|
|
85
104
|
dealData.forEach((item2) => {
|
|
86
105
|
if (item2.value === 0) {
|
|
87
106
|
item2.value = -1;
|
|
@@ -94,18 +113,21 @@ function Legend(props) {
|
|
|
94
113
|
}
|
|
95
114
|
]
|
|
96
115
|
});
|
|
116
|
+
seriesData.length = 0;
|
|
117
|
+
seriesData.push(...newSeriesData);
|
|
97
118
|
};
|
|
98
119
|
const pageData = JSON.parse(JSON.stringify(seriesData));
|
|
99
120
|
return /* @__PURE__ */ import_react.default.createElement("ul", { className: "onesight-pie-legend" }, /* @__PURE__ */ import_react.default.createElement("div", { className: "li-container" }, legendData.map((item, i) => {
|
|
121
|
+
const uniqueId = getUniqueId(item, i);
|
|
100
122
|
return /* @__PURE__ */ import_react.default.createElement(
|
|
101
123
|
"li",
|
|
102
124
|
{
|
|
103
|
-
id: "legend-content" +
|
|
125
|
+
id: "legend-content" + uniqueId,
|
|
104
126
|
className: "legend-item",
|
|
105
127
|
style: {
|
|
106
128
|
marginBottom: legendData.length > 9 ? "4px" : "12px"
|
|
107
129
|
},
|
|
108
|
-
key:
|
|
130
|
+
key: uniqueId
|
|
109
131
|
},
|
|
110
132
|
/* @__PURE__ */ import_react.default.createElement("div", { className: "parent-wrap" }, /* @__PURE__ */ import_react.default.createElement(
|
|
111
133
|
"div",
|
|
@@ -56,8 +56,10 @@ function StackedBar(props) {
|
|
|
56
56
|
legendNum = 4,
|
|
57
57
|
legendDropdownRight = -98,
|
|
58
58
|
yAxisLabelWidth = true,
|
|
59
|
-
showAllLegends = false
|
|
59
|
+
showAllLegends = false,
|
|
60
60
|
// 是否显示所有图例
|
|
61
|
+
xAxisMaxMultiplier = 1.1
|
|
62
|
+
// X 轴最大值的额外倍率,用于确保标签完全显示
|
|
61
63
|
} = props;
|
|
62
64
|
const chartRef = (0, import_react.useRef)();
|
|
63
65
|
const legendMoreBtnRef = (0, import_react.useRef)(null);
|
|
@@ -298,7 +300,7 @@ function StackedBar(props) {
|
|
|
298
300
|
const unitPerPx = maxTotal / barAreaWidth;
|
|
299
301
|
finalMax = maxTotal + overflowPx * unitPerPx;
|
|
300
302
|
}
|
|
301
|
-
finalMax = Math.max(finalMax, maxTotal * 1.3) *
|
|
303
|
+
finalMax = Math.max(finalMax, maxTotal * 1.3) * xAxisMaxMultiplier;
|
|
302
304
|
}
|
|
303
305
|
}
|
|
304
306
|
const lastNonZeroSeriesIdxPerRow = yAxisData.map((_, rowIdx) => {
|