【问题标题】:FlotJS tooltip messing with the responsiveness?FlotJS 工具提示弄乱了响应能力?
【发布时间】:2015-06-09 08:24:57
【问题描述】:

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

就像在this interactive graph example by flot 中一样,我们可以将鼠标悬停在这些点上以查看 sin 和 cos 的值。

我在我的项目中使用Smart Admin Responsive Theme,如果您导航到他们提供的图表演示,还有一个关于Flot Chart Demos的部分

如果您仔细查看排名第二的罪恶图,它与我正在做的类似。将鼠标悬停在这些点上会显示工具提示以查看 sin 的值。

我面临的问题是,如果您将鼠标悬停在最后几个点上,则会出现工具提示,但它会影响页面的响应能力,并且按说不可见。

如何解决这个问题?有什么方法可以让工具提示在页面末尾出现在鼠标指针的左侧?

编辑

选项对象

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
        }*/
    }
};

【问题讨论】:

    标签: javascript html css responsive-design flot


    【解决方案1】:

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

    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 是在调整大小事件中更新的全局变量(使用$(window).height())。如果您愿意,也可以使用容器元素的尺寸。

    您可以在使用给定事件/函数时删除工具提示插件和选项。

    【讨论】:

    • 感谢他的快速回复。该代码对我来说很有意义,但我是 Flot 的新手,我不确定该方法的放置位置以及如何为其提供 x、y 和 content 变量。我应该使用div#tooltip 还是div#flotTip?因为flot正在使用第二个。我很困惑。
    • 啊,你用的是工具提示插件。我以为你是手动完成的,就像你链接的example 一样。有关所需代码,请参阅已编辑的问题。 (该插件使用类似但稍微复杂的代码。)
    • 我试过这个但没有用。我设置了windowWidth = $(window).width();windowHeight = $(window).height();。我取出了工具提示插件的选项。我也删除了工具提示插件的加载方法,但仍然由于某种原因它对我不起作用。 :(
    • 我尝试从bind 方法和showTooltip 中使用console.log('foo')console.log('bar'),它们按应有的方式启动。当我将鼠标悬停在图表上时,它会记录“foo”,当我超过一个点时,它会记录“bar”。我不确定我做错了什么。
    • 好的,现在我将其更改为windowWidth = $(window).width();windowHeight = $(window).height();,它现在可以正常工作,但仍有一个小问题。如果这些点太近,它们中的一些会同时出现,然后最后一个淡入,但另一个不会,除非我将指针移到图表上某个调用 plothover 事件的位置,然后将它们一一移除每次通话。我说得有道理吗?如果您知道如何解决此问题,请告诉我。无论如何,谢谢你的帮助。 :)
    猜你喜欢
    • 2018-11-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-28
    • 1970-01-01
    • 2014-08-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多