private handleDoubleClick = (event: MouseEvent): void => {
if (this.hidden || this.mode === 'idle' || this.mode === 'drawing') {
return;
}
const rect = this.container.getBoundingClientRect();
const point = {
x: event.clientX - rect.left,
y: event.clientY - rect.top,
};
const pointTarget = this.getPointTarget(point);
const isClickedOnLine = this.isPointNearLine(point);
if (!pointTarget && !isClickedOnLine) {
return;
}
event.preventDefault();
event.stopPropagation();
this.openSettings?.();
};
public getRenderData(): TrendLineRenderData | null {
if (this.hidden) {
return null;
}
const geometry = this.getGeometry();
if (!geometry) {
return null;
}
return {
...geometry,
showHandles: this.isActive,
lineColor: this.settings.lineColor,
};
}
import { CanvasRenderingTarget2D } from 'fancy-canvas';
import { IPrimitivePaneRenderer } from 'lightweight-charts';
import { getThemeStore } from '@src/theme';
import { TrendLine } from './trendLine';
const UI = {
lineWidth: 2,
handleRadius: 5,
handleBorderWidth: 2,
};
export class TrendLinePaneRenderer implements IPrimitivePaneRenderer {
private readonly trendLine: TrendLine;
constructor(trendLine: TrendLine) {
this.trendLine = trendLine;
}
public draw(target: CanvasRenderingTarget2D): void {
const data = this.trendLine.getRenderData();
if (!data) {
return;
}
const { colors } = getThemeStore();
target.useBitmapCoordinateSpace(({ context, horizontalPixelRatio, verticalPixelRatio }) => {
const pixelRatio = Math.max(horizontalPixelRatio, verticalPixelRatio);
const startX = data.startPoint.x * horizontalPixelRatio;
const startY = data.startPoint.y * verticalPixelRatio;
const endX = data.endPoint.x * horizontalPixelRatio;
const endY = data.endPoint.y * verticalPixelRatio;
context.save();
context.lineWidth = UI.lineWidth * pixelRatio;
context.strokeStyle = data.lineColor;
context.beginPath();
context.moveTo(startX, startY);
context.lineTo(endX, endY);
context.stroke();
if (data.showHandles) {
context.fillStyle = colors.chartBackground;
context.strokeStyle = data.lineColor;
context.lineWidth = UI.handleBorderWidth * pixelRatio;
drawHandle(context, startX, startY, UI.handleRadius * pixelRatio);
drawHandle(context, endX, endY, UI.handleRadius * pixelRatio);
}
context.restore();
});
}
}
function drawHandle(context: CanvasRenderingContext2D, x: number, y: number, radius: number): void {
context.beginPath();
context.arc(x, y, radius, 0, Math.PI * 2);
context.fill();
context.stroke();
}