import * as d3 from 'd3'; import { Link } from '../../renderables/Link'; import { LabelStyle } from '../../styles/LabelStyle'; import { Renderer } from '../Renderer'; class LinkLabelRenderer extends Renderer { static defaultColor = '#333333'; static defaultFont = '14px sans-serif'; // Nodes to render protected _links: Link[]; // Styling protected color: string | null; protected font: string; constructor (style?: LabelStyle) { super(); this._links = []; this.color = LinkLabelRenderer.defaultColor; this.font = LinkLabelRenderer.defaultFont; if (style) { if (style.color) this.color = style.color; if (style.font) this.font = style.font; } } links (): Link[]; links (links: Link[]): this; links (links?: Link[]): Link[] | this { return arguments.length ? (this._links = links!, this) : this._links; } render (context: CanvasRenderingContext2D, x: d3.ScaleLinear, y: d3.ScaleLinear): void { if (!this._links.length) return; context.save(); context.beginPath(); if (this.color) context.fillStyle = this.color; context.font = this.font; context.textAlign = 'center'; context.textBaseline = 'middle'; this._links.forEach(link => { const path = link.path; if (path) { link.labels.forEach(label => { const p = path.pointAlong(label.t, x, y); context.fillText( label.text, p.x, p.y ); }); } }); } } export { LinkLabelRenderer }