in src/label/LabelManager.ts [315:425]
updateLayoutConfig(api: ExtensionAPI) {
const width = api.getWidth();
const height = api.getHeight();
function createDragHandler(el: Element, labelLineModel: Model) {
return function () {
updateLabelLinePoints(el, labelLineModel);
};
}
for (let i = 0; i < this._labelList.length; i++) {
const labelItem = this._labelList[i];
const label = labelItem.label;
const hostEl = label.__hostTarget;
const defaultLabelAttr = labelItem.defaultAttr;
let layoutOption;
// TODO A global layout option?
if (isFunction(labelItem.layoutOption)) {
layoutOption = labelItem.layoutOption(
prepareLayoutCallbackParams(labelItem, hostEl)
);
}
else {
layoutOption = labelItem.layoutOption;
}
layoutOption = layoutOption || {};
labelItem.computedLayoutOption = layoutOption;
const degreeToRadian = Math.PI / 180;
// TODO hostEl should always exists.
// Or label should not have parent because the x, y is all in global space.
if (hostEl) {
hostEl.setTextConfig({
// Force to set local false.
local: false,
// Ignore position and rotation config on the host el if x or y is changed.
position: (layoutOption.x != null || layoutOption.y != null)
? null : defaultLabelAttr.attachedPos,
// Ignore rotation config on the host el if rotation is changed.
rotation: layoutOption.rotate != null
? layoutOption.rotate * degreeToRadian : defaultLabelAttr.attachedRot,
offset: [layoutOption.dx || 0, layoutOption.dy || 0]
});
}
let needsUpdateLabelLine = false;
if (layoutOption.x != null) {
// TODO width of chart view.
label.x = parsePercent(layoutOption.x, width);
label.setStyle('x', 0); // Ignore movement in style. TODO: origin.
needsUpdateLabelLine = true;
}
else {
label.x = defaultLabelAttr.x;
label.setStyle('x', defaultLabelAttr.style.x);
}
if (layoutOption.y != null) {
// TODO height of chart view.
label.y = parsePercent(layoutOption.y, height);
label.setStyle('y', 0); // Ignore movement in style.
needsUpdateLabelLine = true;
}
else {
label.y = defaultLabelAttr.y;
label.setStyle('y', defaultLabelAttr.style.y);
}
if (layoutOption.labelLinePoints) {
const guideLine = hostEl.getTextGuideLine();
if (guideLine) {
guideLine.setShape({ points: layoutOption.labelLinePoints });
// Not update
needsUpdateLabelLine = false;
}
}
const labelLayoutStore = labelLayoutInnerStore(label);
labelLayoutStore.needsUpdateLabelLine = needsUpdateLabelLine;
label.rotation = layoutOption.rotate != null
? layoutOption.rotate * degreeToRadian : defaultLabelAttr.rotation;
label.scaleX = defaultLabelAttr.scaleX;
label.scaleY = defaultLabelAttr.scaleY;
for (let k = 0; k < LABEL_OPTION_TO_STYLE_KEYS.length; k++) {
const key = LABEL_OPTION_TO_STYLE_KEYS[k];
label.setStyle(key, layoutOption[key] != null ? layoutOption[key] : defaultLabelAttr.style[key]);
}
if (layoutOption.draggable) {
label.draggable = true;
label.cursor = 'move';
if (hostEl) {
let hostModel: Model<LabelLineOptionMixin> =
labelItem.seriesModel as SeriesModel<LabelLineOptionMixin>;
if (labelItem.dataIndex != null) {
const data = labelItem.seriesModel.getData(labelItem.dataType);
hostModel = data.getItemModel<LabelLineOptionMixin>(labelItem.dataIndex);
}
label.on('drag', createDragHandler(hostEl, hostModel.getModel('labelLine')));
}
}
else {
// TODO Other drag functions?
label.off('drag');
label.cursor = defaultLabelAttr.cursor;
}
}
}