javascript - float 图表悬停标记

标签 javascript jquery html charts flot

我做了一些图表,可以在里面放一个标记,但我不明白如何让标记可以悬停在 y 轴值上。

$.plot($("#flot-chart-@ControlID"), series_@ControlID, {
        series: {
            lines: {
                lineWidth: 2,
                fill: @((Model.Series.Count() == 1).ToString().ToLower()),
            },
            bars: {
                barWidth: 0.6,
                align: "center"
            },
            points: {
                fill: @((Model.Series.Count() == 1).ToString().ToLower()),
            }
        },
        xaxis: {
            ticks: xlabels_@ControlID,
            tickDecimals: 0
        },
        colors: @Html.Raw(Newtonsoft.Json.JsonConvert.SerializeObject(Model.Series.Select(o => o.Color).ToArray())),
        grid: {
            color: "#999999",
            hoverable: true,
            clickable: true,
            borderWidth: 0,
            @if (Model.LimitLine != null)
            {
                <text>
                    markings: [
                        { color: '#000', lineWidth: 1, yaxis: { from: @Model.LimitLine, to: @Model.LimitLine }},
                    ]
                </text>
            }
            },
        legend: {
            show: true
        },
        tooltip: true,
        tooltipOpts: {
            content: function(label, xval, yval) {
                var content = getLabel_@(ControlID)(xval) + ": " + yval;
                return content;
            },
        }
    });

如何显示带有值的工具提示?
图形示例:

enter image description here

最佳答案

您不能使用工具提示插件来做到这一点,但您可以自己制作工具提示。像这样:

$("#placeholder").bind("plothover", function (event, pos, item) {
    if (item) { // hovering over a datapoint
        var x = item.datapoint[0].toFixed(2),
            y = item.datapoint[1].toFixed(2);

        $("#tooltip").html(item.series.label + " of " + x + " = " + y)
            .css({top: item.pageY+5, left: item.pageX+5}).fadeIn(200);
    } else {
        var hideTooltip = true;

        // you need to save the Flot object for this (here as "plot")
        $.each(plot.getOptions().grid.markings, function(idx, marking) {
            if (Math.abs(pos.y - marking.yaxis.to) < 10) {
                $("#tooltip").html("Limit: " + marking.yaxis.to)
                    .css({top: pos.pageY + 5, left: pos.pageX + 5}).fadeIn(200);
                hideTooltip = false;
                return false;
            }
        });

        if (hideTooltip) {
            $("#tooltip").hide();
        }
    }       
});

基于此example .

关于javascript - float 图表悬停标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41238890/

相关文章:

javascript - 如何在 React 中实现 Cloudinary Upload Widget?

javascript - 在 <p> 中动态创建一个 <div>

javascript - 可选择的表行无法通过 jQuery AJAX 将 id 值发送到 PHP

javascript - 如何将 Google Drive 中的视频嵌入网页?

javascript - 工具提示不出现

javascript - 顶级函数中的 "this"

javascript - 如何使用 Google Analytics 跟踪 IFrame 中的点击次数?

jquery - 根据选择隐藏或显示 DIV 的条件 JQuery 表单?

php - 如何制作简单的动态幻灯片/游戏中时光倒流

javascript - 为什么当我将 xmlns 添加到我的文档类型时,我的文本框的宽度会改变?