在flots點繪製例程中,它調用自定義符號函數(在您的情況下寫入文本),然後(在頂部)它用藍色填充符號。要做你想做的事情,你必須先禁用flots填寫並填寫你的drawSymbol
,然後再編寫文本。
在你的選擇:
points: {
show: true,
symbol: drawSymbol,
fill: false // disable flots fill
},
並修改drawSymbol到:
var drawSymbol = function(ctx, x, y, radius, shadow) {
ctx.arc(x, y, 15, 0, 2 * Math.PI, false);
ctx.fillStyle = "blue";
ctx.fill(); // fill it yourself
ctx.font = "12px 'Segoe UI'";
ctx.fillStyle = "white";
ctx.textAlign = 'center';
ctx.fillText('65kg', x, y + 4); //now the text
}
這裏有一個小提琴demonstration。
![enter image description here](https://i.stack.imgur.com/q9rdr.png)
要drawSymbol
訪問您的數據:
var drawSymbol = function(ctx, x, y, radius, shadow) {
// keep track of how many times you've called this
if (this.numCalls == undefined){
this.numCalls = -1;
}
this.numCalls++;
// flot "over" draws
// so always keep the index correct...
if (this.numCalls >= someData[0].data.length){
this.numCalls = this.numCalls - someData[0].data.length;
}
//console.log(this.numCalls);
ctx.arc(x, y, 15, 0, 2 * Math.PI, false);
ctx.fillStyle = "blue";
ctx.fill();
ctx.font = "12px 'Segoe UI'";
ctx.fillStyle = "white";
ctx.textAlign = 'center';
// access your data point
ctx.fillText(someData[0].data[this.numCalls][3]+'kg', x, y + 4);
}
更新fiddle here。
非常感謝你! –
如果我想通過我的數據來繪製符號方法,該怎麼辦?我希望點上的文本與我傳遞給yaxis的數據相同,但是我檢查drawSymbol沒有我的數據參數,我應該怎麼做? –
@JackZhang,這是一個艱難的。我會將數據存儲到全局,然後根據我的時間稱爲drawSymbol例程來訪問,請參閱上面的更新答案。 – Mark