我这里有 stackbiltz -https://stackblitz.com/edit/lable-line-break-vaszab?file=src/app/app.component.ts https://stackblitz.com/edit/lable-line-break-vaszab?file=src/app/app.component.ts
我在 Angular 应用程序中有一个 d3 条形图。
使用在创建 z 轴刻度时调用的函数将 x 轴标签分成两行
private insertLinebreak(d) {
let labels = d3.select(this);
let words = d;
console.log("Label:", labels.html());
labels.text('');
let index = words.indexOf(' ', words.indexOf(' ') + 1)
let title = words.substr(0, index)
let subtitle = words.substr(index + 1)
let tspantitle = labels.append('tspan').text(title)
let tspansubtitle = labels.append('tspan').text(subtitle)
tspantitle
.attr('x', 0)
.attr('dy', '15')
.attr('class', 'x-axis-title');
tspansubtitle
.attr('x', 0)
.attr('dy', '16')
.attr('class', 'x-axis-subtitle');
};
该函数使用“this”来选择调用该函数的“g”(但在 d3 中我认为它选择了整个 svg)
这段代码在 stackblitz 中有效,但在我的实际代码中我收到错误
Argument of type 'this' is not assignable to parameter of type 'BaseType'.
我知道这不是很有帮助
我认为这与 Typescript 及其处理“this”的方式有关
有谁知道为什么会发生这种情况以及我该如何解决这个问题。