2012-02-23 10 views
4

私はFlotを使ってグラフを作成しています。 これは私がデータ1、データ2、データ3、DATA4 flotでx軸ラベルを取得するには?

  • のようなx軸のラベルを取得する必要があり、ここで私のdrawseriesHook機能で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(); 
    } 
    } 
    
    1. 私のコードで一意に最後のデータ項目を特定する方法はありますseries.color.Currently series.label &を使用せずに、私は機能
  • 答えて

    4

    1にseries.colorを変更することによって、それを特定した)[OK]を、私はあなたがそれぞれのバーのためlabelTextを設定したいと思います。 forループで次の行を追加します。

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

    updated exampleを参照してください。

    2)dataに独自の値を追加することができます。最後の1をマークする:あなたのフックで

    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はすべてのティックラベルを持ちます。個々にラベルを取得する方法は? –

    +0

    @Coder_sLaY:あなたはすべてを望むわけではありませんが、現在のラベルだけが必要ですか? – scessor

    +0

    はい、まさに私がアラート(ラベル)を書いたとき、私はすべて4を得ます。私は最初のものだけが必要です。 –

    関連する問題