首页 文章

FullCalendar和Flot Resize Conflict

提问于
浏览
0

我已经成功地将Flot线图和FullCalendar实例集成到我的站点中 . 它们都在不同的页面上(尽管页面通过AJAX加载到div中) .

我已经添加了Flot Resize插件,它可以很好地工作,按预期重新调整线图的大小 . 但是,在调整日历大小时似乎会导致错误 .

即使我首先加载日历页面,当我调整窗口大小时,我在控制台中出现此错误(同样,日历也没有正确调整大小):

TypeError: 'undefined' is not an object (evaluating 'r.w=o!==c?o:q.width()')

我正在努力找出错误来自哪里,所以我删除了Flot Resize JS的链接并再次尝试 . 当然,折线图不会调整大小,但在调整日历大小时,它可以正常工作 .

两个元素的div容器具有不同的名称,并且从函数内调用resize函数以绘制折线图(根据需要) .

我已经尝试将Flot Resize插件的链接移动到不同的位置(即fullCalendar JS的上方/下方,进入保存图形的模板),但都无济于事 .

有谁知道冲突可能在哪里以及如何解决?

非常感谢!

编辑:似乎错误也是在加载完整日历页面后的线图(flot)页面时触发,即使没有调整窗口大小....现在我很困惑!

编辑2:绘制折线图的代码 . 该函数在pageload上调用,并从服务器上取下JSON中的数据 . 加载图形时,我仍然得到关于shutdown()未定义的错误 .

function plotLineGraph(theData){
var myData = theData['data'];
var myEvents = theData['events'];
var myDates = theData['dates'];

var events = new Array();
for (var i=0; i<myEvents.length; i++) {
    events.push(
        {
            min: myEvents[i][0],
            max: myEvents[i][1],
            eventType: "Calendar Entry",
            title: myEvents[i][2],
            description:  myEvents[i][3]
        }

    );
}

  function showTooltip(x, y, contents) {
    $('<div id="tooltip">' + contents + '</div>').css( {
        position: 'absolute',
        display: 'none',
        top: y + 5,
        left: x + 5,
        border: '1px solid #fdd',
        padding: '2px',
        'background-color': 'black',
        opacity: 0.80
    }).appendTo("body").fadeIn(200);
}

 var previousPoint = null;
$("#placeholder").bind("plothover", function (event, pos, item) {
    $("#x").text(pos.x.toFixed(2));
    $("#y").text(pos.y.toFixed(2));

    if ($("#enableTooltip:checked").length == 0) {
        if (item) {
            if (previousPoint != item.dataIndex) {
                previousPoint = item.dataIndex;

                $("#tooltip").remove();
                var x = item.datapoint[0].toFixed(2),
                    y = item.datapoint[1].toFixed(2);

                if(item.series.label != null){
                showTooltip(item.pageX, item.pageY,
                            item.series.label + " of " + y);
                }
            }
        }
        else {
            $("#tooltip").remove();
            previousPoint = null;            
        }
    }
});

var d1 = [
          myData[0],  myData[1],  myData[2],  myData[3],  myData[4],
          myData[5],  myData[6],  myData[7],  myData[8],  myData[9],
          myData[10], myData[11], myData[12], myData[13], myData[14],
          myData[15], myData[16], myData[17], myData[18], myData[19],
          myData[20], myData[21], myData[22], myData[23], myData[24],
          myData[25], myData[26], myData[27], myData[28], myData[29]
          ];
var markings = [
    { color: '#FFBDC1', yaxis: { from: 0, to: 2 } },
    { color: '#F2E2C7', yaxis: { from: 2, to: 3.5 } },
    { color: '#B6F2B7', yaxis: { from: 3.5, to: 5 } }
];

$.plot($("#placeholder"), [
    {label: "Average Daily Rating", data: d1, color: "black"}
    ], {
        events: {
            data: events,
        },
        series: {
            lines: { show: true },
            points: { show: true }
        },
        legend: { show: true, container: '#legend-holder' },
        xaxis: {
            ticks:[
            myDates[0],  myDates[1],  myDates[2],  myDates[3],  myDates[4],
            myDates[5],  myDates[6],  myDates[7],  myDates[8],  myDates[9],
            myDates[10], myDates[11], myDates[12], myDates[13], myDates[14],
            myDates[15], myDates[16], myDates[17], myDates[18], myDates[19],
            myDates[20], myDates[21], myDates[22], myDates[23], myDates[24],
            myDates[25], myDates[26], myDates[27], myDates[28], myDates[29]
            ],
        },
        yaxis: {
            ticks: 5,
            min: 0,
            max: 5
        },
        grid: {
            backgroundColor: { colors: ["#fff", "#eee"] },
            hoverable: true,
            clickable: true,
            markings: markings
        },
        selection: {
            color: 'white',
            mode: 'x'
        },
});
$('#placeholder').resize();
$('#placeholder').shutdown();
}

编辑3:

日历的调用方式如下:

function showCalendar() {
  var date = new Date();
var d = date.getDate();
var m = date.getMonth();
var y = date.getFullYear();

$('#fullcalendar').fullCalendar({
    header: {
        left: 'prev',
        center: 'title',
        right: 'next'
    },
    clickable: true,
    firstDay: 1,
    eventSources: [
        {
            url: '/populate-calendar/{{theProductUuid}}/',
            color: 'black',
            data: {
                text: 'text'
            }
        }
    ],
    eventClick: function(calEvent, jsEvent, view) {
        var startDate = $.fullCalendar.formatDate(calEvent.start, 'yyyy-MM-dd');
        var endDate = $.fullCalendar.formatDate(calEvent.end, 'yyyy-MM-dd');
        var eventId = calEvent.uuid;
        $('#modal-event-title').text(calEvent.title);
        $('#edit-event-name').val(calEvent.title);
        $('#edit-start-date').val(startDate);
        $('#edit-end-date').val(endDate);
        $('#edit-event-text').val(calEvent.text);
        $('#edit-event-btn').attr('data-uuid', eventId);
        $('#modal-edit-event').on('click', '#delete-btn', function(){
            deleteCalendarEvent(eventId);
        });
        $('#modal-edit-event').modal();
        },
});

}

用于加载包含flot图表的页面的AJAX:

function loadDetailedReports(uuid){
$('#product-content').fadeOut('slow', function(){
    $('#product-content').empty();
    $('#whole-product-sub-nav .active').removeClass('active');
    $('#detailed-reports-content').load('/detailed-reports/' + uuid + '/', function(){
        $('#detailed-reports-btn').addClass('active');
        $('#detailed-reports-content').fadeIn('slow', function(){
            if (authorized){
                setLocationHash('loadDetailedReports&' + uuid);
                getChartData(uuid);
            } else {
            setLocationHash('');
            }
        });
    });
});
}

并且AJAX加载包含日历的页面:

function loadCalendar(uuid){
$('#detailed-reports-content').empty().hide();
$('#product-content').fadeOut('slow', function(){
    $('#whole-product-sub-nav .active').removeClass('active');
    $('#product-content').load('/calendar/' + uuid + '/', function(){
        $('#calendar-btn').addClass('active');
        $('#product-content').fadeIn('slow', function(){
            if (authorized){
                setLocationHash('loadCalendar&' + uuid);
            } else {
                setLocationHash('');
            }
            showCalendar();
        });
    });
});
}

对.resize和.shutdown的调用是存在的,因为我的印象是他们有必要实现调整大小功能并回应你之前关于关闭的评论......他们很可能是n00b错误...... .....?!?!

1 回答

  • 0

    看起来这是在jquery-resize的第198行触发:

    data.w = w !== undefined ? w : elem.width();
    

    这听起来像是一种竞争条件,源于你将不同内容加载到同一个div中的方式 . Flot将resize事件绑定到图表div,并且只有在图表被彻底销毁时才解除绑定 .

    编辑:看看你的代码,我的第一个建议是在plotLineGraph结束时摆脱调整大小和关闭调用 . resize插件不需要任何设置;它挂钩到Flot以自动附加到任何新的情节 . 因此,调用resize实际上是对jQuery的resize事件触发器,这可能是导致错误的原因 .

    编辑#2:我保持对它的引用,你可以这样做: $("#placeholder").data("plot").shutdown(); 但是必须考虑到在创建你的第一个绘图之前它未定义的事实 .

    如果仍然无效,我需要看一个实时(简化)示例来进一步提出建议 .

相关问题