[英]How to access an array of objects with tooltip.format() from anychart.js
I am having trouble trying to present an array of objects on the tooltip of an Anychart.js map.我在尝试在 Anychart.js 地图的工具提示上显示一组对象时遇到问题。 I understand that we can access the dataset by doing something like: %[name of property in data set].我知道我们可以通过执行以下操作来访问数据集:%[数据集中的属性名称]。 My data set has the following form:我的数据集具有以下形式:
{
"country": "Austria",
"id": "AT",
"continent": "Europe",
"songs": [
{
"rank": 33,
"title": "Stuck with U (with Justin Bieber)",
"artists": "Ariana Grande, Justin Bieber",
"album": "Stuck with U",
"explicit": 0,
"duration": "3:48"},
{
"rank": 34,
"title": "Late Night",
"artists": "Luciano",
"album": "Late Night",
"explicit": 0,
"duration": "3:20"
},
... more objects
]
}
}
If I wanted to access the Country property I would simply add it to the tooltip by doing:如果我想访问 Country 属性,我只需通过执行以下操作将其添加到工具提示中:
tooltip.format("Country: " + {%country});
The issue is when trying to access an array of objects, I have tried different variations and none of them worked.问题是在尝试访问一组对象时,我尝试了不同的变体,但都没有奏效。 Trying to show the title of every song:试图显示每首歌的标题:
tooltip.format({%songs}.{%title});
tooltip.format({%songs.%title});
tooltip.format({%songs}[{%title}]);
I also saw in the documentation that we can send a function as argument so I tried the following where I would concatenate every title of the collection but did not succeed either:我还在文档中看到我们可以发送一个函数作为参数,所以我尝试了以下方法,我将连接集合的每个标题,但也没有成功:
tooltip.format(function() {
let concatenated = '';
this.songs.forEach(song => {
concatenated += song + ' ';
});
return concatenated;
});
I would really appreciate your help guys.我真的很感激你们的帮助。
String tokens do not support nested objects/properties.字符串标记不支持嵌套对象/属性。 But you can use the callback function of the formatted to get access to songs
.但是您可以使用格式化的回调函数来访问songs
。 The context prototype includes getData()
method provides that.上下文原型包括getData()
方法提供的。 Like this:像这样:
series.tooltip().format(function() {
console.log(this.getData('songs'));
return 'tooltip';
});
For details, check the live sample we prepared.有关详细信息,请查看我们准备的实时样本。
In case any one else is looking for a solution to this answer.万一其他人正在寻找此答案的解决方案。 I figured out how to loop through an embed array, and call on specific information.我想出了如何遍历嵌入数组,并调用特定信息。
chart.edges().tooltip().format(function () {
var format = ''
var songs = this.getData('songs');
songs.forEach(function (data, builtin, dom) {
format = '<p>'+data['title']+' by '+data['artists']+' </span></p>' + format
});
console.log(format)
return format
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.