FlotJS工具提示干扰了响应性

FlotJS tooltip messing with the responsiveness?

本文关键字:响应 干扰 工具提示 FlotJS      更新时间:2023-09-26

我正在开发一个应用程序,在这个应用程序中,我使用FlotJS图表库来绘制图形,并且我已经打开了工具提示,以便用户可以将鼠标悬停在点上,并可以在特定日期告诉每个点的内容。

就像在这个交互式图形示例中一样,我们可以将鼠标悬停在这些点上以查看sin和cos的值。

我在我的项目中使用了Smart Admin Responsive Theme,如果你导航到他们提供的图形演示,还有一个关于flotchart演示的部分

如果你仔细观察排名第二的Sin图,它和我正在做的很相似。将鼠标悬停在这些点上,将显示工具提示以查看sin的值。

我面临的问题是,如果你把鼠标悬停在最后几个点上,工具提示出现了,但它扰乱了页面的响应性,而且每次都不可见。

如何解决这个问题?当鼠标指针位于页面末端时,是否有某种方法可以使工具提示显示在鼠标指针的左侧?

编辑

<

选项对象/strong>

var layerOptions = {
    xaxis : {
        mode : "time"
    },
    legend : {
        show : true,
        noColumns : 1, // number of colums in legend table
        labelFormatter : null, // fn: string -> string
        labelBoxBorderColor : "#000", // border color for the little label boxes
        container : null, // container (as jQuery object) to put legend in, null means default on top of graph
        position : "ne", // position of default legend container within plot
        margin : [0, 5], // distance from grid edge to default legend container within plot
        backgroundColor : "#efefef", // null means auto-detect
        backgroundOpacity : 0.4 // set to 0 to avoid background
    },
    tooltip : true,
    tooltipOpts : {
        content : "<b>%s</b> content on <b>%x</b> was <b>%y</b>",
        dateFormat : "%y-%m-%d",
        defaultTheme : false
    },
    grid : {
        hoverable : true,
        clickable : true
    },
    series: {
        /*bars: {
            show: true, 
            barWidth: 1000 * 60 * 60 * 24 * 30,
            order: 1
        }*/
    }
};

是的,您可以根据窗口大小动态设置工具提示的位置。我在我的一个fllot页面中使用了这个方法,当工具提示靠近右边框时将其向左翻转:

var prevPoint = null;
$("#placeholder").bind("plothover", function (event, pos, item) {
    if (item) {
        if (prevPoint != item.dataIndex) {
            prevPoint = item.dataIndex;
            $("#tooltip").remove();
            var x = item.datapoint[0].toFixed(2),
                y = item.datapoint[1].toFixed(2),
                text = item.series.label + ' content on ' + x + ' was ' + y;
            showTooltip(item.pageX, item.pageY, text);
        }
    } else {
        $("#tooltip").remove();
        prevPoint = null;
    }
});
function showTooltip(x, y, content) {
    var cssParams = {
        position: 'absolute',
        display: 'none',
        border: '1px solid #888888',
        padding: '2px',
        'background-color': '#eeeeee',
        opacity: 0.8
    };
    if (x < 0.8 * windowWidth) {
        cssParams.left = x + 3;
    }
    else {
        cssParams.right = windowWidth - x + 6;
    }
    if (y < 0.8 * windowHeight) {
        cssParams.top = y + 3;
    }
    else {
        cssParams.bottom = windowHeight - y + 6;
    }
    $('<div id="tooltip">' + content + '</div>').css(cssParams).appendTo('body').fadeIn(100);
}

windowHeightwindowWidth是在resize事件(使用$(window).height())中更新的全局变量。如果愿意,也可以使用容器元素的尺寸。

当你使用给定的事件/函数时,你可以删除工具提示插件和选项。