UNPKG

@visactor/vchart

Version:

charts lib based @visactor/VGrammar

51 lines (46 loc) 4.9 kB
import { isNil, isNumber, degreeToRadian, isArray, isFunction, field as getFieldAccessor, extent, toPercent } from "@visactor/vutils"; import { CORRELATION_X, CORRELATION_Y, CORRELATION_SIZE } from "../../constant/correlation"; const resolveOptionValue = option => isFunction(option) ? option() : option; export const correlation = (data, options) => { var _a, _b, _c, _d, _e, _f, _g, _h; if (!data || !(null == options ? void 0 : options.view) || !isArray(data)) return data; const dataList = data, viewBox = options.view(); if (viewBox.x1 - viewBox.x0 == 0 || viewBox.y1 - viewBox.y0 == 0 || viewBox.x1 - viewBox.x0 == -1 / 0 || viewBox.x1 - viewBox.x0 == 1 / 0 || viewBox.y1 - viewBox.y0 == -1 / 0 || viewBox.y1 - viewBox.y0 == 1 / 0) return data; const startAngle = degreeToRadian(null !== (_a = resolveOptionValue(options.startAngle)) && void 0 !== _a ? _a : -90), endAngle = degreeToRadian(null !== (_b = resolveOptionValue(options.endAngle)) && void 0 !== _b ? _b : 270), maxRadius = Math.max((viewBox.x1 - viewBox.x0) / 2, (viewBox.y1 - viewBox.y0) / 2), innerRadius = toPercent(null !== (_c = resolveOptionValue(options.innerRadius)) && void 0 !== _c ? _c : 0, maxRadius), outerRadius = toPercent(resolveOptionValue(options.outerRadius), maxRadius), centerOption = resolveOptionValue(options.center), center = [ isNumber(null == centerOption ? void 0 : centerOption[0]) ? centerOption[0] : viewBox.x0 + toPercent(null !== (_d = null == centerOption ? void 0 : centerOption[0]) && void 0 !== _d ? _d : "50%", viewBox.x1 - viewBox.x0), isNumber(null == centerOption ? void 0 : centerOption[1]) ? centerOption[1] : viewBox.y0 + toPercent(null !== (_e = null == centerOption ? void 0 : centerOption[1]) && void 0 !== _e ? _e : "50%", viewBox.y1 - viewBox.y0) ], fieldAccessor = getFieldAccessor(resolveOptionValue(options.field)), values = dataList.map(fieldAccessor), [min, max] = extent(values), radiusScale = min === max ? val => (innerRadius + outerRadius) / 2 : val => innerRadius + (outerRadius - innerRadius) * (val - min) / (max - min), radiusField = resolveOptionValue(options.radiusField), radiusRange = resolveOptionValue(options.radiusRange), sizeAccessor = isNil(radiusField) ? fieldAccessor : getFieldAccessor(radiusField), defaultSize = null !== (_f = null == radiusRange ? void 0 : radiusRange[1]) && void 0 !== _f ? _f : 5; let sizeScale = datum => defaultSize; if (sizeAccessor) { const [minSize, maxSize] = sizeAccessor !== fieldAccessor ? extent(dataList.map(sizeAccessor)) : [ min, max ], minR = null !== (_g = null == radiusRange ? void 0 : radiusRange[0]) && void 0 !== _g ? _g : 5, maxR = null !== (_h = null == radiusRange ? void 0 : radiusRange[1]) && void 0 !== _h ? _h : 5; minSize !== maxSize && (sizeScale = datum => minR + (maxR - minR) * (sizeAccessor(datum) - minSize) / (maxSize - minSize)); } const minAngle = Math.min(startAngle, endAngle), maxAngle = Math.max(startAngle, endAngle), angles = getPartialAngles(minAngle, maxAngle, dataList.length), res = [], searchAngle = (maxAngle - minAngle) / 60; return dataList.forEach(((datum, index) => { const radius = radiusScale(values[index]), size = sizeScale(datum); let x, y, angle = angles[index]; for (let i = 0; i < 60 && (x = center[0] + radius * Math.cos(angle), y = center[1] + radius * Math.sin(angle), hasOverlap({ x: x, y: y, size: size }, res) || x - size < viewBox.x0 || x + size > viewBox.x1 || y - size < viewBox.y0 || y + size > viewBox.y1); i++) i < 59 && (angle += searchAngle, angle > maxAngle ? angle = minAngle : angle < minAngle && (angle = maxAngle)); res.push(Object.assign({ [CORRELATION_X]: x, [CORRELATION_Y]: y, [CORRELATION_SIZE]: size }, datum)); })), res; }; const getPartialAngles = (minAngle, maxAngle, count) => { let offsetAngle = 0, stepCount = Math.max(Math.ceil(2 * (maxAngle - minAngle) / Math.PI), 2), stepAngle = (maxAngle - minAngle) / stepCount, stepIndex = 0, stepSign = 1, i = 0, j = 0; const res = []; let startAngle = minAngle; for (;i < count; ) j < stepCount && (res.push(startAngle + (j % 2 ? Math.floor(j / 2) + Math.floor(stepCount / 2) : j / 2) * stepAngle * stepSign), j++), i++, j === stepCount && (j = 0, stepIndex += 1, stepSign *= -1, 0 === offsetAngle ? offsetAngle = stepAngle / 2 : offsetAngle /= 2, startAngle = -1 === stepSign ? maxAngle - offsetAngle : minAngle + offsetAngle, stepIndex >= 2 && (stepAngle /= 2, stepCount *= 2)); return res; }, hasOverlap = (item, arr) => !(!arr || !arr.length) && arr.some((entry => { const x = entry.x, y = entry.y, size = entry.size; return Math.pow(item.x - x, 2) + Math.pow(item.y - y, 2) < Math.pow(item.size + size, 2); })); //# sourceMappingURL=correlation.js.map