我使用Flot创建图表。 这是我的代码jsFiddle code
function drawSeriesHook(plot, canvascontext, series) {
var ctx = canvascontext,
plotOffset = plot.offset(),
labelText = 'VALUE', // customise this text, maybe to series.label
points = series.datapoints.points,
ps = series.datapoints.pointsize,
xaxis = series.xaxis,
yaxis = series.yaxis,
textWidth, textHeight, textX, textY;
// only draw label for top yellow series
if (series.color === '#F8C095') {
ctx.save();
ctx.translate(plotOffset.left, plotOffset.top);
ctx.lineWidth = series.bars.lineWidth;
ctx.fillStyle = '#000'; // customise the colour here
for (var i = 0; i < points.length; i += ps) {
if (points[i] == null) continue;
textWidth = ctx.measureText(labelText).width; // measure how wide the label will be
textHeight = parseInt(ctx.font); // extract the font size from the context.font string
textX = xaxis.p2c(points[i] + series.bars.barWidth / 2) - textWidth / 2;
textY = yaxis.p2c(points[i + 1]) - textHeight / 2;
ctx.fillText(labelText, textX, textY); // draw the label
}
ctx.restore();
}
}
答案 0 :(得分:4)
1。)好的,我想你想为每个栏设置labelText
。在for循环中添加以下行:
labelText = series.xaxis.ticks[i / ps].label;
2。)您可以将自己的值添加到data
,例如标记最后一个:
var data = [
{
label : 'Used',
color : '#EF7816',
data : [ [ 1, 85], [ 2, 50 ], [ 3, 18], [ 4, 8 ] ],
last : false
},
...
{
color : '#F8C095',
data : [ [ 1, 12], [ 2, 10], [ 3, 3], [ 4, 2] ],
last : true
}
];
在你的钩子里你可以检查旗帜:
if (series.last) { ...
另请参阅下一个updated example。