2012-02-23 37 views
4

我正在使用Flot創建圖形。 這是這裏jsFiddle code如何獲取flot中的x軸標籤?

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(); 
} 
} 
  1. 我drawseriesHook功能我的代碼需要得到x軸標籤像數據1,數據2,數據3,數據4
  2. 有什麼辦法來唯一地標識最後一個數據項不使用series.label & series.color.Currently我已經在功能

回答

4

1)好吧,我想你想設置labelText每個條修改series.color識別它。在for循環中添加以下行:

labelText = series.xaxis.ticks[i/ps].label; 

另請參閱updated example

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

+0

此標籤var將具有所有刻度標籤。如何單獨獲取標籤? – 2012-02-23 07:11:21

+0

@Coder_sLaY:你不需要全部,你只需要當前的標籤? – scessor 2012-02-23 07:13:59

+0

是的,但是當我把警報(標籤),然後我得到了所有4.我只需要第一個。 – 2012-02-23 07:19:36