cione-comp-lib
Version:
`$ yarn add cione-comp-lib` 或者 `$ npm i cione-comp-lib -S`
143 lines (142 loc) • 5.25 kB
JavaScript
import { each, isString, isFunction } from "../../../../zrender/lib/core/util.mjs";
import { applyTransform } from "../../util/graphic.mjs";
import { getBoundingRect } from "../../../../zrender/lib/contain/text.mjs";
import { normalizeCssArray } from "../../util/format.mjs";
import { rotate, translate, create } from "../../../../zrender/lib/core/matrix.mjs";
import { getAxisRawValue } from "../../coord/axisHelper.mjs";
import AxisBuilder from "../axis/AxisBuilder.mjs";
import { createTextStyle } from "../../label/labelStyle.mjs";
function buildElStyle(axisPointerModel) {
var axisPointerType = axisPointerModel.get("type");
var styleModel = axisPointerModel.getModel(axisPointerType + "Style");
var style;
if (axisPointerType === "line") {
style = styleModel.getLineStyle();
style.fill = null;
} else if (axisPointerType === "shadow") {
style = styleModel.getAreaStyle();
style.stroke = null;
}
return style;
}
function buildLabelElOption(elOption, axisModel, axisPointerModel, api, labelPos) {
var value = axisPointerModel.get("value");
var text = getValueLabel(value, axisModel.axis, axisModel.ecModel, axisPointerModel.get("seriesDataIndices"), {
precision: axisPointerModel.get(["label", "precision"]),
formatter: axisPointerModel.get(["label", "formatter"])
});
var labelModel = axisPointerModel.getModel("label");
var paddings = normalizeCssArray(labelModel.get("padding") || 0);
var font = labelModel.getFont();
var textRect = getBoundingRect(text, font);
var position = labelPos.position;
var width = textRect.width + paddings[1] + paddings[3];
var height = textRect.height + paddings[0] + paddings[2];
var align = labelPos.align;
align === "right" && (position[0] -= width);
align === "center" && (position[0] -= width / 2);
var verticalAlign = labelPos.verticalAlign;
verticalAlign === "bottom" && (position[1] -= height);
verticalAlign === "middle" && (position[1] -= height / 2);
confineInContainer(position, width, height, api);
var bgColor = labelModel.get("backgroundColor");
if (!bgColor || bgColor === "auto") {
bgColor = axisModel.get(["axisLine", "lineStyle", "color"]);
}
elOption.label = {
x: position[0],
y: position[1],
style: createTextStyle(labelModel, {
text,
font,
fill: labelModel.getTextColor(),
padding: paddings,
backgroundColor: bgColor
}),
z2: 10
};
}
function confineInContainer(position, width, height, api) {
var viewWidth = api.getWidth();
var viewHeight = api.getHeight();
position[0] = Math.min(position[0] + width, viewWidth) - width;
position[1] = Math.min(position[1] + height, viewHeight) - height;
position[0] = Math.max(position[0], 0);
position[1] = Math.max(position[1], 0);
}
function getValueLabel(value, axis, ecModel, seriesDataIndices, opt) {
value = axis.scale.parse(value);
var text = axis.scale.getLabel({
value
}, {
precision: opt.precision
});
var formatter = opt.formatter;
if (formatter) {
var params_1 = {
value: getAxisRawValue(axis, {
value
}),
axisDimension: axis.dim,
axisIndex: axis.index,
seriesData: []
};
each(seriesDataIndices, function(idxItem) {
var series = ecModel.getSeriesByIndex(idxItem.seriesIndex);
var dataIndex = idxItem.dataIndexInside;
var dataParams = series && series.getDataParams(dataIndex);
dataParams && params_1.seriesData.push(dataParams);
});
if (isString(formatter)) {
text = formatter.replace("{value}", text);
} else if (isFunction(formatter)) {
text = formatter(params_1);
}
}
return text;
}
function getTransformedPosition(axis, value, layoutInfo) {
var transform = create();
rotate(transform, transform, layoutInfo.rotation);
translate(transform, transform, layoutInfo.position);
return applyTransform([axis.dataToCoord(value), (layoutInfo.labelOffset || 0) + (layoutInfo.labelDirection || 1) * (layoutInfo.labelMargin || 0)], transform);
}
function buildCartesianSingleLabelElOption(value, elOption, layoutInfo, axisModel, axisPointerModel, api) {
var textLayout = AxisBuilder.innerTextLayout(layoutInfo.rotation, 0, layoutInfo.labelDirection);
layoutInfo.labelMargin = axisPointerModel.get(["label", "margin"]);
buildLabelElOption(elOption, axisModel, axisPointerModel, api, {
position: getTransformedPosition(axisModel.axis, value, layoutInfo),
align: textLayout.textAlign,
verticalAlign: textLayout.textVerticalAlign
});
}
function makeLineShape(p1, p2, xDimIndex) {
xDimIndex = xDimIndex || 0;
return {
x1: p1[xDimIndex],
y1: p1[1 - xDimIndex],
x2: p2[xDimIndex],
y2: p2[1 - xDimIndex]
};
}
function makeRectShape(xy, wh, xDimIndex) {
xDimIndex = xDimIndex || 0;
return {
x: xy[xDimIndex],
y: xy[1 - xDimIndex],
width: wh[xDimIndex],
height: wh[1 - xDimIndex]
};
}
function makeSectorShape(cx, cy, r0, r, startAngle, endAngle) {
return {
cx,
cy,
r0,
r,
startAngle,
endAngle,
clockwise: true
};
}
export { buildCartesianSingleLabelElOption, buildElStyle, buildLabelElOption, getTransformedPosition, getValueLabel, makeLineShape, makeRectShape, makeSectorShape };