FlotJS工具提示弄乱了响应性?

时间:2015-06-09 08:24:57

标签: javascript html css responsive-design flot

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

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

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

如果你仔细观察罪孽图表,它是第二顺序,它与我正在做的相似。将鼠标悬停在这些点上会显示工具提示,以查看罪的价值。

我面临的问题是,如果你将鼠标悬停在最后几个点上,工具提示会出现,但它会因页面的响应性而混乱,并且每个人都不会看到。

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

修改

选项对象

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

1 个答案:

答案 0 :(得分:1)

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

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())。如果您愿意,也可以使用容器元素的尺寸。

使用给定的事件/功能时,可以删除工具提示插件和选项。