angular - 如何在使用 ng2-chart 构建的散点图中标记点
问题描述
我使用 ng2-chart 构建了这个散点图: 我想要的是:在图中的每个黑点旁边显示一个“名称”。
Chart.component.html:
<!-- chart.js -->
<div style="display: block;">
<canvas
baseChart
[datasets]="scatterChartData"
[options]="scatterChartOptions"
[chartType]="scatterChartType"
>
</canvas>
</div>
<div class="jumbotron jumbotron-fluid">
<div class="container">
<div class="typo-line">
<h4><p class="category"><b>Légende</b></p></h4>
</div>
<p class="text-primary">
R-1 Tremblement de terre
</p>
<p class="text-primary">
R-2 Température et humidité
</p>
</div>
</div>
Chart.component.ts:创建黑点的代码:
public scatterChartType: ChartType = "scatter";
constructor(private data: AnalyseRisqueService) {}
probaGraviteValuesIndexedByRow:number[][]=[]
ngOnInit() {
this.data.currentProbaGraviteValuesIndexedByRow.subscribe(receivedProbaGraviteValuesIndexedByRow=>
(this.probaGraviteValuesIndexedByRow=receivedProbaGraviteValuesIndexedByRow)
)
setTimeout(() => {
this.chart.chart.data.datasets[0].data = [
{ x: this.probaGraviteValuesIndexedByRow[0][0], y: this.probaGraviteValuesIndexedByRow[0][1] },
{ x: this.probaGraviteValuesIndexedByRow[1][0], y: this.probaGraviteValuesIndexedByRow[1][1] },
{ x: this.probaGraviteValuesIndexedByRow[2][0], y: this.probaGraviteValuesIndexedByRow[2][1] },
{ x: this.probaGraviteValuesIndexedByRow[3][0], y: this.probaGraviteValuesIndexedByRow[3][1] },
{ x: this.probaGraviteValuesIndexedByRow[4][0], y: this.probaGraviteValuesIndexedByRow[4][1] },
{ x: this.probaGraviteValuesIndexedByRow[5][0], y: this.probaGraviteValuesIndexedByRow[5][1] },
]
this.chart.chart.update();
}, 1000);
}
解决方案
点的工具提示是使用options
画布的绑定提供的。
将工具提示作为回调添加到您的scatterChartOptions
对象,就像在您的 TS 文件中一样:
public scatterChartOptions: ChartOptions = {
responsive: true,
tooltips: {
callbacks: {
label: (item, data) =>
{
console.log(item);
return 'Label: ' + item.xLabel + ' ' + item.yLabel
}
}
}
};
这应该显示一个带有 x 和 y 值的简单标签。
看看这个有效的 StackBlitz。
推荐阅读
- node.js - opendir 返回 fs.Dir 可通过 promise 进行迭代,但不可通过 Sync 进行迭代。为什么?
- c - 英特尔上的 gcc / clang / msvc:为什么 ucomiss 为 QNAN == QNAN(预期),但为 QNAN <= QNAN(意外)?
- css - 使用 bootstrap4 排列复选框
- c - 我想把这个顺序代码变成一个循环
- python-3.x - 比较 time1(来自 API 请求)和 time2(SQL 请求)
- python - 当使用 numpy 数组手动复制不会失败时,为什么 Hypothesis 给出了一个伪造的例子?
- java - 如何仅在一个实例上在集群中运行火石英触发器
- azure - 通过 Azure 门户使用 AD 组限制对 AD 应用程序的访问
- python - 计算结果未添加到现有 csv
- android - AndroidStuidio:未启用 EditText 上的触摸