import { jsx } from '@antv/f-engine';
import { vec2 } from 'gl-matrix';
import { PolarProps } from '../types';
// 相对圆心偏移量的点
function getOffsetPoint(center, point, offset) {
const vectorX = point.x - center.x;
const vectorY = point.y - center.y;
const vectorLength = vec2.length([vectorX, vectorY]);
const offsetLength = vectorLength + offset;
const x = (vectorX / vectorLength) * offsetLength;
const y = (vectorY / vectorLength) * offsetLength;
return {
x: center.x + x,
y: center.y + y,
};
}
// 获取文本的对齐方式
function getTextAlignInfo(center, point) {
// 文本点向量
const vector = [point.x - center.x, point.y - center.y];
let align;
let baseLine;
// 水平对齐
if (vector[0] > 0) {
align = 'left';
} else if (vector[0] < 0) {
align = 'right';
} else {
align = 'center';
}
// 垂直对齐
if (vector[1] > 0) {
baseLine = 'top';
} else if (vector[1] < 0) {
baseLine = 'bottom';
} else {
baseLine = 'middle';
}
return {
textAlign: align,
textBaseline: baseLine,
};
}
const Line = (props) => {
const { line, gridType, center, radius, ticks } = props;
if (!line) return null;
if (gridType !== 'line') {
return (
);
}
const points = ticks.map((tick) => {
const { points } = tick;
return points[points.length - 1];
});
// 头尾相连
points.push(points[0]);
return (
[d.x, d.y]),
...line,
}}
/>
);
};
export default (props: PolarProps) => {
const { ticks: originTicks, coord, style, grid: gridType } = props;
const { center } = coord;
const { grid, tickLine, line, labelOffset, label } = style;
const ticks = originTicks.filter((d) => !isNaN(d.value));
const firstTicks = ticks[0];
const { points } = firstTicks;
const end = points[points.length - 1];
const radius = vec2.length([end.x - center.x, end.y - center.y]);
return (
{grid
? ticks.map((tick) => {
const { points, gridStyle } = tick;
const end = points[points.length - 1];
return (
);
})
: null}
{tickLine && tickLine.length
? ticks.map((tick) => {
const { points } = tick;
const end = points[points.length - 1];
const offsetPoint = getOffsetPoint(center, end, tickLine.length);
return (
);
})
: null}
{label
? ticks.map((tick) => {
const { points, text, labelStyle } = tick;
const end = points[points.length - 1];
const offsetPoint = getOffsetPoint(center, end, labelOffset);
return (
);
})
: null}
);
};