在dygraphs中仅加载csv文件中的最后10行

时间:2014-01-23 23:36:31

标签: javascript csv dygraphs

使用tutorial中的示例代码:

<html>
<head>
<script type="text/javascript"
  src="dygraph-combined.js"></script>
</head>
<body>
<div id="graphdiv2"
  style="width:500px; height:300px;"></div>
<script type="text/javascript">
  g2 = new Dygraph(
    document.getElementById("graphdiv2"),
    "temperatures.csv", // path to CSV file
    {}          // options
  );
</script>
</body>
</html>

我只想加载temperatures.csv的最后10行而不是整个文件。我可以将指令传递给dygraphs的csv解析器吗?

1 个答案:

答案 0 :(得分:1)

根据源代码https://github.com/danvk/dygraphs/blob/master/dygraph.js#L2942

,没有设置限制或/和偏移的选项

但您可以在代码中覆盖函数Dygraph.prototype.parseCSV_来实现此目的。

例如this

// Save old function
var parseCSV_ = Dygraph.prototype.parseCSV_;

Dygraph.prototype.parseCSV_ = function(data) {
    // Get all data
    var ret = parseCSV_(data);

    // Return only last 10 items
    return ret.slice(ret.length - 10);
};

g2 = new Dygraph(
    document.getElementById("graphdiv2"),
    "temperatures.csv", // path to CSV file
    {}          // options
);

P.S。我建议您使用新的CSV文件,只包含最后10项,以加快加载速度,如果可能,请在后端生成。