顯然人力車不支持這個本地。我可能通過將rickshar擴展到它所基於的d3庫(根據其網站上的示例,似乎能夠做到我打算實現的目標)來完成此任務。但是,我最終得到了一個簡單的解決方案 - 根據每個數據點距離圖元素左上角的距離,手動將數據標籤添加爲div。下面的代碼使用數據點的顏色來搜索圖的數據屬性以顯示標籤中的數據,因爲它是將數據點與數據屬性中的信息進行匹配的唯一項。
$(".pointMarker").each(function(index) {
var percentage = 0;
var currentMarkerColor = self.rgb2hex($(this).css("border-top-color"));
self.graph.series.forEach(function(series) {
if(currentMarkerColor === series.color) {
if (!/undef/i.test(typeof series.data[index])) {
percentage = parseFloat(series.data[index].y).toFixed(2);
}
//end loop
return false;
}
});
if (percentage > 0) {
var totalHeight = $(this).parent().height();
var distanceTop = $(this).css("top").replace(/[^-\d\.]/g, '') ;
//display data
$(this).parent().append("<div class='dataLabel' style='top:"+(parseInt($(this).css('top'), 10)-5)+"px;left:"+(parseInt($(this).css('left'), 10)-9)+"px;height:100px;width:100px;'>"+percentage+"</div>");
}
});
和
this.rgb2hex = function (rgb){
rgb = rgb.match(/^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/);
return "#" +
("0" + parseInt(rgb[1],10).toString(16)).slice(-2) +
("0" + parseInt(rgb[2],10).toString(16)).slice(-2) +
("0" + parseInt(rgb[3],10).toString(16)).slice(-2);
}