Tôi chỉ tìm thấy câu hỏi này trong khi tìm kiếm các giải pháp tương tự. Điều này được thực hiện bằng javascript/jquery.
Tôi có thể chia sẻ với bạn mã mà tôi đang sử dụng mà tôi đã phát hiện bằng bản dùng thử, lỗi và nghiên cứu.
function parseSVG(s) {
var div= document.createElementNS('http://www.w3.org/1999/xhtml', 'div');
div.innerHTML= '<svg xmlns="http://www.w3.org/2000/svg">'+s+'</svg>';
var frag= document.createDocumentFragment();
while (div.firstChild.firstChild)
frag.appendChild(div.firstChild.firstChild);
return frag;
}
var theData = [
{mapname: 's1', value: 10, count: 3},
{mapname: 's2', value: 4, count: 4},
{mapname: 's3', value: 12, count: 13}
]
Morris.Bar ({
element: 'bar-example',
data: theData,
xkey: 'mapname',
ykeys: ['value'],
labels: ['No. of days'],
barRatio: 0.4,
xLabelAngle: 35,
hideHover: 'auto',
barColors: function (row, series, type) {
console.log("--> "+row.label, series, type);
if(row.label == "s1") return "#AD1D28";
else if(row.label == "s2") return "#DEBB27";
else if(row.label == "s3") return "#fec04c";
}
});
var items = $("#bar-example").find("svg").find("rect");
$.each(items,function(index,v){
var value = theJson[index].count;
var newY = parseFloat($(this).attr('y') - 20);
var halfWidth = parseFloat($(this).attr('width')/2);
var newX = parseFloat($(this).attr('x')) + halfWidth;
var output = '<text style="text-anchor: middle; font: 12px sans-serif;" x="'+newX+'" y="'+newY+'" text-anchor="middle" font="10px "Arial"" stroke="none" fill="#000000" font-size="12px" font-family="sans-serif" font-weight="normal" transform="matrix(1,0,0,1,0,6.875)"><tspan dy="3.75">'+value+'</tspan></text>';
$("#bar-example").find("svg").append(parseSVG(output));
});
Kết quả trông như thế này.
Nhưng những gì bạn có thể thử, là thay đổi giá trị ở đây
var newY = parseFloat($(this).attr('y') - 20);
một cái gì đó giống như
var halfHeight = parseFloat($(this).attr('height')/2);
var newY = parseFloat($(this).attr('y') - halfHeight);
Sự thay đổi này là chưa được kiểm tra, nhưng sẽ đóng vai trò như là một điểm khởi đầu tốt đẹp .
Kính trọng :)
Bạn có tìm thấy giải pháp cho vấn đề này không? Tự hỏi điều tương tự. – qubit
Bất kỳ may mắn nào với điều này? Tôi gặp vấn đề tương tự! – Pooshonk
voidwalker & @Pooshonk: chưa .. – abi1964