【问题标题】:How does one use Flot's selection graph with time on the xaxis?如何在 x 轴上使用 Flot 的选择图和时间?
【发布时间】:2013-09-03 15:01:16
【问题描述】:

更新:这是一个演示: http://jsfiddle.net/MsybN/1/

我正在使用 Flot 的选择图: http://www.flotcharts.org/flot/examples/selection/index.html

我使用时间作为 x 轴,但是当我拖动以放大到更小的时间间隔时,图表变为空白。在工作示例中,x 轴是基于时间的,但以年为单位,因此在数字上很容易绘制(即 1994.5 是 1994 年的一半)。

我的 xaxis 上有月份年份,例如:“2012 年 7 月”、“2013 年 1 月”、“2013 年 7 月”等,以 6 个月为间隔。我将它与十字准线插件和时间插件结合使用。

缩放不起作用。它正确地获得了毫秒值,但无法将图表设置为它们。代码如下,谁能帮忙?

导入的脚本: 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>

【问题讨论】:

  • 是不是图形的数据还是只是 x 轴标签?
  • 数据绘制得很好,xaxis也很好,但是当我选择放大时,它试图在xaxis上绘制毫秒但我认为因为它转换为字符串日期它不能做吧
  • 请注意,如果您的标签重复 flot 只会在它们在绘制周期结束时唯一的点开始使用它们,但听起来您根本没有得到任何标签?
  • 在我放大(通过图形选择)之前,标签和数据点不会按预期显示。

标签: javascript jquery html flot


【解决方案1】:

(根据解决Fiddle中的问题更新)

存在三个问题:

  1. 主要问题是您没有使用与最初绘制相同的代码在 plotselected 回调中重新绘制。 plotAccordingToChoices 函数构建一个新数组,其中包含来自 datasets 对象的数据系列。 plotselected 回调只是传入“数据集”本身,这是无效的。

    这在您的原始代码 sn-p 中很难看到,因为您省略了这些行。

  2. 如前所述,您错误地将 'from' 和 'to' 除以 1000;您应该按原样使用范围值。

  3. 十字准线不起作用,因为在您的 plothover 回调中未定义 updateLegend。当您将鼠标移过绘图时,此操作反复失败,挡住了十字准线。

我已经更新了 Fiddle 来演示。

【讨论】:

  • 不幸的是,当我不除以 1000 时,我会得到像 1752883820067 这样的时间戳值,即日期为 57516 年 9 月
  • 我为此添加了一个 jsfiddle 和一个赏金
  • 这很好,以同样的方式传递数据是我的问题,你对除以 1000 是正确的。未定义的属性主要是因为我没有包含大部分代码,对不起关于这一点,但感谢您抽出时间享受您的赏金!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-13
  • 1970-01-01
相关资源
最近更新 更多