1

我正在使用 flotsdrawOverlay钩子来帮助注释图表。当我使用 计算从某个位置的 y 轴坐标时p2c,我注意到有轻微的偏移。似乎如果我重新添加yaxis.box.top,事情就会排成一行,但我不确定这是否是正确的方法。

在这里拉小提琴。

画出我在说什么:

在此处输入图像描述

代码:

$(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();
});
4

1 回答 1

0

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

var axes = plot.getAxes();
var plotOffset = plot.getPlotOffset();
var yCor = axes.yaxis.p2c(threshold) + plotOffset.top;
于 2013-10-03T16:34:37.657 回答