diff --git a/src/chart/pie/pieLayout.ts b/src/chart/pie/pieLayout.ts index 03a670604e..2af7130431 100644 --- a/src/chart/pie/pieLayout.ts +++ b/src/chart/pie/pieLayout.ts @@ -57,7 +57,21 @@ function pieLayout( !isNaN(value) && validDataCount++; }); - const sum = data.getSum(valueDim); + // [0...max] + const extent = data.getDataExtent(valueDim); + extent[0] = 0; + + let sum = data.getSum(valueDim); + let scale = 1; + if (!isFinite(sum) && sum > 0 && extent[1] > 0) { + scale = extent[1]; + sum = 0; + data.each(valueDim, function (val: number) { + if (!isNaN(val)) { + sum += val / scale; + } + }); + } // Sum may be 0 let unitRadian = Math.PI / (sum || validDataCount) * 2; @@ -66,10 +80,6 @@ function pieLayout( const roseType = seriesModel.get('roseType'); const stillShowZeroSum = seriesModel.get('stillShowZeroSum'); - // [0...max] - const extent = data.getDataExtent(valueDim); - extent[0] = 0; - const dir = clockwise ? 1 : -1; const angles = [startAngle, endAngle]; const halfPadAngle = dir * padAngle / 2; @@ -118,7 +128,7 @@ function pieLayout( // FIXME 兼容 2.0 但是 roseType 是 area 的时候才是这样? if (roseType !== 'area') { angle = (sum === 0 && stillShowZeroSum) - ? unitRadian : (value * unitRadian); + ? unitRadian : ((value / scale) * unitRadian); } else { angle = angleRange / validDataCount; @@ -130,7 +140,7 @@ function pieLayout( restAngle -= minAndPadAngle; } else { - valueSumLargerThanMinAngle += value; + valueSumLargerThanMinAngle += value / scale; } const endAngle = currentAngle + dir * angle; @@ -200,7 +210,7 @@ function pieLayout( if (!isNaN(value)) { const layout = data.getItemLayout(idx); const angle = layout.angle === minAndPadAngle - ? minAndPadAngle : value * unitRadian; + ? minAndPadAngle : (value / scale) * unitRadian; let actualStartAngle = 0; let actualEndAngle = 0; diff --git a/src/util/number.ts b/src/util/number.ts index 662695f7f7..03ea2215ae 100644 --- a/src/util/number.ts +++ b/src/util/number.ts @@ -400,15 +400,37 @@ export function getPercentWithPrecision(valueList: number[], idx: number, precis * @return {Array} */ export function getPercentSeats(valueList: number[], precision: number): number[] { - const sum = zrUtil.reduce(valueList, function (acc, val) { + let sum = zrUtil.reduce(valueList, function (acc, val) { return acc + (isNaN(val) ? 0 : val); }, 0); if (sum === 0) { return []; } + let values = valueList; + if (!isFinite(sum) && sum > 0) { + let max = 0; + for (let i = 0; i < valueList.length; i++) { + const val = valueList[i]; + if (!isNaN(val) && val > max) { + max = val; + } + } + if (max > 0) { + values = zrUtil.map(valueList, function (val) { + return isNaN(val) ? 0 : val / max; + }); + sum = zrUtil.reduce(values, function (acc, val) { + return acc + val; + }, 0); + } + } + if (sum === 0 || !isFinite(sum)) { + return []; + } + const digits = mathPow(10, precision); - const votesPerQuota = zrUtil.map(valueList, function (val) { + const votesPerQuota = zrUtil.map(values, function (val) { return (isNaN(val) ? 0 : val) / sum * digits * 100; }); const targetSeats = digits * 100; diff --git a/test/ut/spec/util/number.test.ts b/test/ut/spec/util/number.test.ts index 2c26932148..bc8ebf555a 100755 --- a/test/ut/spec/util/number.test.ts +++ b/test/ut/spec/util/number.test.ts @@ -23,7 +23,8 @@ import { getPercentWithPrecision, quantityExponent, quantity, nice, isNumeric, numericToNumber, addSafe, getPixelPrecision, - getAcceptableTickPrecision + getAcceptableTickPrecision, + getPercentSeats } from '@/src/util/number'; @@ -1087,4 +1088,12 @@ describe('util/number', function () { }); + describe('getPercentSeats', function () { + it('should handle large numbers without overflow', function () { + expect(getPercentSeats([1e308, 1e308], 2)).toEqual([50, 50]); + expect(getPercentSeats([1e308, 1e308, 2e308], 2)).toEqual([25, 25, 50]); + expect(getPercentSeats([10, 20, 70], 2)).toEqual([10, 20, 70]); + }); + }); + }); \ No newline at end of file