Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 2 additions & 15 deletions packages/vtable-gantt/src/scenegraph/gantt-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Group } from '@visactor/vtable/es/vrender';
import { getTextPos } from '../gantt-helper';
import { toBoxArray } from '../tools/util';
import { isValid } from '@visactor/vutils';
import { textMeasure } from '@visactor/vtable';

export class GanttTaskBarNode extends Group {
clipGroupBox: Group;
Expand All @@ -28,9 +29,6 @@ export class GanttTaskBarNode extends Group {
this._lastY = attrs.y;
}

// 用于测量文本的 canvas 上下文
private static measureContext: CanvasRenderingContext2D;

/**
* 更新任务条文本标签的位置和样式
* @description 根据任务条的大小和配置,更新文本标签的位置、对齐方式等属性
Expand All @@ -42,16 +40,6 @@ export class GanttTaskBarNode extends Group {
return;
}

// 懒加载创建用于测量文本的 canvas 上下文
if (!GanttTaskBarNode.measureContext) {
const canvas = document.createElement('canvas');
const ctx = canvas.getContext('2d');
if (!ctx) {
return;
}
GanttTaskBarNode.measureContext = ctx;
}

const labelStyle = this.labelStyle || {};
const {
textAlign = 'left',
Expand All @@ -68,9 +56,8 @@ export class GanttTaskBarNode extends Group {

const fontSize = this.textLabel.attribute.fontSize || 12;
const fontFamily = this.textLabel.attribute.fontFamily || 'Arial';
GanttTaskBarNode.measureContext.font = `${fontSize}px ${fontFamily}`;
const text = String(this.textLabel.attribute.text || '');
const textWidth = GanttTaskBarNode.measureContext.measureText(text).width;
const textWidth = textMeasure.measureTextWidth(text, { fontSize, fontFamily });

const textFitsInBar = textWidth + padding * 2 <= barWidth;
const defaultPosition = getTextPos(toBoxArray(padding), textAlign, textBaseline, barWidth, barHeight);
Expand Down
3 changes: 2 additions & 1 deletion packages/vtable/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ import * as CustomLayout from './render/layout';

import { updateCell } from './scenegraph/group-creater/cell-helper';
import { renderChart } from './scenegraph/graphic/contributions/chart-render-helper';
import { restoreMeasureText, setCustomAlphabetCharSet } from './scenegraph/utils/text-measure';
import { restoreMeasureText, setCustomAlphabetCharSet, textMeasure } from './scenegraph/utils/text-measure';
import type { BaseTableAPI } from './ts-types/base-table';

// import { container, loadCanvasPicker } from '@src/vrender';
Expand Down Expand Up @@ -93,6 +93,7 @@ export {
GroupColumnDefine,
TextAlignType,
TextBaselineType,
textMeasure,
themes,
data,
MousePointerCellEvent,
Expand Down