Flot Axes的p2c计算偏移量

时间:2013-02-05 16:19:24

标签: flot

我正在使用flots drawOverlay hook来帮助注释图表。当我使用p2c从一个位置计算我的y轴坐标时,我注意到有一个轻微的偏移。看看如果我在yaxis.box.top中添加回来,事情就会排好,但我不确定这是否是正确的做法。

小提琴here

想象我正在谈论的内容:

enter image description here

代码:

$(function () {

 overlayThresholdHook = function(plot, ctx) {

        var threshold = plot.getOptions().thresholdY;

        if (threshold == -1) return;

        var axes = plot.getAxes();
        var xStart = axes.xaxis.box.left;
        var xStop = axes.xaxis.box.width + xStart;
        var yCor = axes.yaxis.p2c(threshold);// + axes.yaxis.box.top;

        ctx.font = '9pt Arial';
        ctx.strokeStyle = 'blue';
        ctx.fillStyle = 'blue';
        ctx.lineWidth = 2;
        ctx.textAlign = 'left';
        ctx.textBaseline = 'middle';

        ctx.fillText(threshold+'',xStart,yCor);

        ctx.beginPath();
        ctx.moveTo(xStart+ctx.measureText(threshold+'').width+2, yCor);
        ctx.lineTo(xStop, yCor);
        ctx.stroke();
    }    

    var options = { legend: {show: false}, yaxis: {min: -120, max: 150, show: true}, thresholdY: 100, hooks: {drawOverlay: [overlayThresholdHook]}   };
    var plot0 = $.plot($("#plot0"), [ {"data":[[1,-0.998195931084865],[45,2.26027181085055]],"color":"#C0D800","points":{"symbol":"circle"}}], options);
    plot0.triggerRedrawOverlay();
});

1 个答案:

答案 0 :(得分:0)

我做错了计算。我必须考虑到情节偏移。要以正确的方式获得y坐标:

var axes = plot.getAxes();
var plotOffset = plot.getPlotOffset();
var yCor = axes.yaxis.p2c(threshold) + plotOffset.top;
相关问题