2012-02-23 6 views
4

Flot을 사용하여 그래프를 만듭니다. 이 내가 데이터 1, 데이터 2, DATA3, DATA4 flot에서 x 축 레이블을 얻는 방법은 무엇입니까?

  • 같은 x 축 레이블을 얻을 필요가 유일하게 마지막 데이터 항목을 식별 할 수있는 방법 여기 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. 내 drawseriesHook 기능에 내 코드가 있습니다 series.label &를 사용하지 않고 series.color.Currently 난 기능
  • 답변

    4

    1) 좋아, 난 당신이 각 막대에 대한 labelText을 설정하려는 생각에 series.color을 수정하여 그것을 확인했다. 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를 참조하십시오.

    +0

    이 라벨은 모든 틱 레이블을 갖습니다. 개별적으로 라벨을 가져 오는 방법은 무엇입니까? –

    +0

    @Coder_sLaY : 모두 원하지 않는다면 현재 레이블 만 원하십니까? – scessor

    +0

    예. 그렇지만 경고 (레이블)를 입력하면 모든 것이 4가됩니다. 첫 번째 만 필요합니다. –

    관련 문제