将自定义属性添加到flot数据 [英] Adding custom attributes to flot data
问题描述
当我将数据传递到flot中时,如果我可以传递一些我想要访问的补充数据,这将是非常方便的 plotclick
触发事件。
It would be very convenient when I am passing data into flot if I could pass some supplementary data which I want to access when the plotclick
event is triggered.
这是一些标准数据;
[{label: 'first', data: 5, color: '#123'},
{ label: 'first', data: 10, color: '#456'}]
我想要能够做类似的事情;
I want to be able to do something like;
[{label: 'first', data: 5, color: '#123', my_custom_attribute: 'some data'},
{ label: 'first', data: 10, color: '#456', my_custom_attribute: 'some more data'}]
因此我可以在我的 plotclick
事件中进行;
So that inside of my plotclick
event I could do;
$('chart').bind('plotclick', function(event, pos, item) {
console.log(item.series.my_custom_attribute) //Or something to that effect
});
我尝试了什么
我有尝试在我的 plotclick
事件中插入以上内容并查看 item
的返回内容,它不会出现在任何地方存储 my_custom_attribute
。
What I have tried
I have tried just inserting the above and looking at the returned contents of item
inside of my plotclick
event, it doesn't appear to store my_custom_attribute
anywhere.
我已阅读 https://github.com/flot/flot/blob/master/API.md 并且无法解决任何问题相关信息。
I have read through the documentation at https://github.com/flot/flot/blob/master/API.md and couldn't gleam any relevant information.
我搜索了谷歌和这里的答案,找不到符合我需要的答案。
I have searched google and here for answers and couldn't find one that suited my needs.
感谢Khawer Zeshan提供解决方案,这对我来说仍然不起作用;
Thanks to Khawer Zeshan for providing a solution, this still isn't working for me;
这是我传递的数据;
[{breakdown: "test", color: "#00A4D3", data: 1.5, label: "History"},
{breakdown: "test", color: "#1464F6", data: 0, label: "Geography"}]
但细分
属性未出现在 item
的输出中。
But the breakdown
attribute doesn't appear in the output for item
.
图表上的其他所有内容似乎都有效。
Everything else about the chart appears to work.
推荐答案
尝试这
如果您通过原始 数据
初始化图表时使用的变量。数据仍将存在。由于某些原因,无法通过 item
直接访问数据...它似乎被删除了。
If you look up the custom data via the original data
variable you used when initializing the chart. the data will still be there. For some reason the data cannot be accessed directly through item
... it seems to get deleted.
var data = [
{ label: "Series1", data: [[1,1]], myData: "test 1"},
{ label: "Series2", data: [[1,1]], myData: "test 2"},
{ label: "Series3", data: [[1,1]], myData: "test 3"},
{ label: "Series4", data: [[1,1]], myData: "test 4"},
{ label: "Series5", data: [[1,1]], myData: "test 5"},
{ label: "Series6", data: [[1,5]], myData: "test 6"}
];
$.plot($("#placeholder"), data, {
series: {
pie: {
show: true
}
},
grid: {
hoverable: true,
clickable: true
}
});
$("#placeholder").bind("plotclick", function (event, pos, item) {
if (item) {
console.log(data[item.seriesIndex]);
}
});
希望有所帮助!
这篇关于将自定义属性添加到flot数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!