1

我有一个情节,我正在创建一切工作正常,除了当我单击按钮时,它会为我的数据生成线条。如果我再次点击该按钮,它只会覆盖第一个 ajax 请求,而不是从头开始,这正是我想要的。请帮助这是我的ajax调用。

 $('#process').click(function(e) {

    var market = '&market=' + $('#market').val();
    var productgroup = '&productgroup=' + $('#curve').val();
    var from = '&from=' + $('#date_from').val();
    var to = '&to=' + $('#date_to').val();
    var lookback = '&lookback=' + $('.timeframe').attr('id');
    var fullUrl = url + market + productgroup + from + to + lookback;
    e.preventDefault();

        $.ajax({

            url: fullUrl,
            type: "GET",
            dataType: "json",
            success: onDataReceived,
            error: onError//,
            // data: data
        });

});

这是我的情节的选项

  var options = {
    series: {
        lines: {
            show: true,
            lineWidth: .1
        },
        points: {
            show: false
        }
    },
    xaxis: {
        mode: "time",
        minTickSize: [1, "day"],
        timeformat: "%m/%d/%Y",
        panRange: timespan
    },
    yaxis: {
        mode: "money",
        tickDecimals: 2,
        panRange: [0, 100],
        tickFormatter: dollarFormatter
    },
    zoom: {
        interactive: true
    },
    pan: {
        interactive: true
    }
};

这是创建情节的功能

var data = [];

$.plot("#placeholder", data, options);

这是我的 onDataReceived 函数

 function onDataReceived(series) {

    // Push the new data onto our existing data array
    var count = 0;
    for(var prop in series) {
        if(series.hasOwnProperty(prop))
            ++count;
    }
    for(i=0;i<count;i++)
    {
        if (!alreadyFetched[series[i].label]) {

            data.push(series[i]);
        }
    }

    $.plot("#placeholder", data, options);
}
4

1 回答 1

2

https://github.com/flot/flot/blob/master/API.md#plot-methods

根据 API,您可能希望使用 setData() 和 draw() 重绘

var data = [];
var plot = $.plot("#placeholder", data, options);

然后在 onDataRecieved

plot.setData(data);
plot.draw();

而不是在占位符上重新创建浮动

于 2013-08-26T03:35:35.817 回答