简体   繁体   English

如何在xaxis上使用Flot的选择图随时间变化?

[英]How does one use Flot's selection graph with time on the xaxis?

UPDATE: here is a fiddle to demonstrate: http://jsfiddle.net/MsybN/1/ 更新:这是一个小提琴演示: http//jsfiddle.net/MsybN/1/

I am using Flot's Selection graph: http://www.flotcharts.org/flot/examples/selection/index.html 我正在使用Flot的选择图: http ://www.flotcharts.org/flot/examples/selection/index.html

I use time as the xaxis, but when I drag to zoom in to a smaller time interval the graph goes blank. 我使用时间作为x轴,但是当我拖动以放大到较小的时间间隔时,图形变为空白。 In the working examples the xaxis is time based but in years, so numerically it is easy to plot (ie 1994.5 is half way through 1994). 在工作示例中,x轴是基于时间的,但是在数年内,因此在数字上很容易绘图(即1994年是1994年的一半)。

I have month year on my xaxis such as: 'Jul 2012', 'Jan 2013', 'Jul 2013' and so on in 6 month intervals. 我在我的xaxis上有一个月的年份,例如:'2012年7月','2013年1月','2013年7月'等,每隔6个月一次。 I am using it in conjuntion with the crosshair plugin and the time plugin. 我正在使用它与十字线插件和时间插件结合使用。

The zooming wont work. 缩放不会起作用。 It get's the milli sec values correctly but cant set the graph to them. 它正确得到毫秒值,但无法将图形设置为它们。 Code below, can anyone help? 以下代码,有人可以帮忙吗?

imported scripts: jquery.flot.js jquery.flot.time.js jquery.flot.crosshair.js jquery.flot.selection.js 导入的脚本:jquery.flot.js jquery.flot.time.js jquery.flot.crosshair.js jquery.flot.selection.js

$(function() {



    //define datasets
    var datasets = {
            "blah": {
                label: "blah",
                key: "blah",
                data: []
            }, //etc
    }; 

    $.each(datasets, function(i, item) {    
            item.data.push([(time*1000), datapoints]);
    });

    // hard-code color indices to prevent them from shifting as
    // countries are turned on/off
    var i = 0;
    $.each(datasets, function(key, val) {
        val.color = i;
        ++i;
    });

    var plot;

    var options = {
            xaxis: {
                timezone: "browser",
                mode: "time"
            }, series: {
                lines: {
                    show: true
                }
            }, crosshair: {
                mode: "x"
            }, grid: {
                hoverable: true,
                autoHighlight: false
            }, selection: {
                mode: "x"
            }
        };

    function plotAccordingToChoices() {

        var data = [];
                    //inputs not shown
        choiceContainer.find("input:checked").each(function () {
            var key = $(this).attr("name");
            if (key && datasets[key]) {
                data.push(datasets[key]);
            }
        });

        if (data.length > 0) {
            plot = $.plot("#placeholder", data, options);
        }
    }

    plotAccordingToChoices();


    var placeholder = $("#placeholder");

    placeholder.bind("plotselected", function (event, ranges) {

        var from = Math.ceil(ranges.xaxis.from / 1000);
        var to = Math.ceil(ranges.xaxis.to / 1000);

//THIS IS WHERE IT BREAKS


        plot = $.plot(placeholder, datasets, $.extend(true, {}, options, {
            xaxis: {
                min: from,
                max: to
            }
        }));
    });
});

</script>

(Updated based on solving the problems in the Fiddle) (基于解决小提琴中的问题而更新)

There are three problems: 有三个问题:

  1. The main problem is that you're not using the same code to re-plot in the plotselected callback as you do to plot originally. 主要问题是你没有使用相同的代码在plotselected回调中重新绘制,就像你最初绘制的那样。 The plotAccordingToChoices function builds a new array containing the data series from the datasets object. plotAcordingToChoices函数构建一个包含数据集对象的数据系列的新数组。 The plotselected callback simply passes in 'datasets' itself, which is invalid. plotselected回调只是传递'数据集'本身,这是无效的。

    This was difficult to see in your original code snippet, since you omitted those lines. 这在您的原始代码段中很难看到,因为您省略了这些行。

  2. As mentioned before, you're incorrectly dividing 'from' and 'to' by 1000; 如前所述,你错误地将'from'和'to'除以1000; you should use the range values as-is. 你应该按原样使用范围值。

  3. The crosshair doesn't work because updateLegend is undefined in your plothover callback. 十字准线不起作用,因为在您的plothover回调中未定义updateLegend。 That fails repeatedly as you move the mouse across the plot, blocking the crosshair. 当您将鼠标移动到绘图上时,会反复失败,从而阻挡十字准线。

I've updated the Fiddle to demonstrate. 我已经更新了小提琴来演示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM