javascript - flot chart plothover 事件触发但条形图中的项目始终为空

标签 javascript jquery charts flot

我正在使用 float 图表在 shoppimon.com 上显示随时间变化的问题发生数据。 使用折线图时,我的 flothover 功能完美运行,但一旦我切换到条形图,工具提示就会停止显示。 调试时,我在 flothover 回调内部中断,这意味着事件正在触发,但“item”参数始终为 null。

非常感谢任何建议。

这是我的代码:

renderChart : function(containerId) {
    setTimeout(function(){
        var data = chartServer.getData(containerId);
        var container = $('#' + containerId);
        if (window.localStorage && window.localStorage['cbMode'] == 'true') {
            data.options.colors = chartClient.cbColors;
        } else {
            data.options.colors = chartClient.chartColors;
        }
        container.before('<div id="' + containerId + '-controls" class="chart-controls group"> \
            <div class="chart-legend" /> ');

        //Plot the chart
        var chart = $.plot(container, data.dataSet, data.options);

        container.bind('plothover', chartClient.hoverFunction);
        container.bind('plotclick', chartClient.clickFunction);

        container.bind('plothover', function(event, pos, item) {
            if (item) {
                $(event.target).css({cursor:"pointer"});
            } else {
                $(event.target).css({cursor:"auto"});
            }
        });
    }, 100);
},

hoverFunction : function(event, pos, item) {
    if (item) {
        if (chartClient.previousPoint != item.dataIndex) {
            chartClient.previousPoint = item.dataIndex;
            $("#chart-tooltip").remove();
            var x = item.datapoint[0].toFixed(2),
                y = item.datapoint[1].toFixed(0);

            var date = new Date(parseInt(x, 10));
            date.add({minutes: Date.today().getTimezoneOffset()}); // compensate for local TZ
            var contents = date.toString("MMM d ") + date.toString("t") + "<br />" + y + " times";

            $('<div id="chart-tooltip">' + contents + '</div>').css({
                top: item.pageY + 5,
                left: item.pageX + 5
            }).appendTo("body").fadeIn(200);
        }
    } else {
        $("#chart-tooltip").remove();
        chartClient.previousPoint = null;
    }
},

这是我的选项对象:

Object {xaxis: Object, grid: Object, legend: Object, series: Object, colors: Array[5]}
colors: Array[5]0: "#0073B5"1: "#6A4A3C"2: "#CC333F"3: "#EB6841"4: "#EDC951"
length: 5

grid: Object
borderColor: "#EDEDED"
borderWidth: 1
clickable: true
hoverable: true

legend: Object
container: "#occurrence-chart-39631-controls .chart-legend"
noColumns: 1

series: Object

bars: Object
fill: true
show: true

xaxis: Object
mode: "time"

最佳答案

因此,为了在 flotHover 事件处理程序中获取项目对象,您必须在图表中包含数据点,使用以下“points”选项:

    $options['grid']['clickable'] = true;
    $options['grid']['hoverable'] = true;
    $options['series'] = array(
        'bars' => array('show' => true),
        'points' => array('show' => true, 'radius' => 0, 'symbol' => 'square')
    );

如果你想避免显示这些点,你可以像我一样使用 0 的半径。悬停功能仍然有效,项目对象存在于回调中,但图表栏没有明显变化。

关于javascript - flot chart plothover 事件触发但条形图中的项目始终为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25834644/

相关文章:

jquery - 大尺寸的 svg 无法正常工作如何在 React-pan-zoon-svg 中设置大 svg?

javascript - 第二次点击 jQuery

javascript - 操作 DOM 时的数据绑定(bind)不适用于 Angular、Knockout

javascript - 根据数据点强制面积图的 X 轴刻度线

charts - JFree 图表中的注释/注释

javascript - 如何用js获取字符串中的整数值

javascript - 全局 CKEDITOR_VERSION 常量已在 vue 上设置

javascript检查DOM元素是否存在最佳实践

javascript - 在 Firebase 和 Javascript 中构建私有(private)聊天

javascript - D3 可重复使用的圆形热图无法成功更新