使用jquery-tooltip和flot chart

时间:2013-10-22 22:03:37

标签: javascript jquery jquery-ui flot jquery-tooltip

我正在关注example以使用jquery工具提示。我在画布上显示了标题。我不知道如何摆脱它。甚至当我在画布上盘旋时我也看到了。请参阅我附上的图片

$(function() {

    var $chart = $( "#placeholder" ),
        ttPos = $.ui.tooltip.prototype.options.position;

    $.plot(
        $chart, 
        [{
            label: "Number of users",
            data:[
                [ 1, 2233 ],
                [ 2, 1294 ],
                [ 3, 1658 ],
                [ 4, 1603 ],
                [ 5, 1790 ],
                [ 6, 2103 ]
            ]
        }],
        {
            series: {
                lines: { show: true },
                points: { show: true }
            },
            grid: {
                hoverable: true
            },
            legend: {
                show: false
            }
        }
    );

    $chart.bind( "plothover", function( e, pos, item ) {

        var isTooltip = $chart.is( ":ui-tooltip" );

        if ( item !== null && isTooltip === false ) {

            var label = item.series.label,
                data = item.datapoint[1],
                content = label + "<br/><hr>" + data,
                evtPos;

            evtPos = $.extend( ttPos, {
                of: {
                    pageX: item.pageX,
                    pageY: item.pageY,
                    preventDefault: $.noop
                }
            });

            $chart.attr( "title", content  )
                  .tooltip({position: evtPos,
                            content: content})
                  .tooltip( "open" );

        }
        else if ( item === null && isTooltip === true ) {

            $chart.tooltip( "destroy" );

        }

    });

});

1 个答案:

答案 0 :(得分:3)

默认情况下,jquery ui工具提示要求弹出它的元素具有title attribute(这就是jquery ui知道将事件绑定到哪个元素的方式)。这段代码的第一行:

$chart.attr( "title", content  )
      .tooltip({position: evtPos,
                content: content})
      .tooltip( "open" );

正在设置该属性,并且是您在屏幕截图中看到的内容。

如果您将其更改为:

$chart.tooltip({position: evtPos,
                content: content,
                items: '*'})
      .tooltip( "open" );

这应该仍然允许在没有title文本的情况下弹出工具提示。小提琴here

老实说,我发现代码示例非常钝。在flot文档中example之后,您可以获得更好的服务。