【问题标题】:Dygraphs, comma as decimal separatorDygraphs,逗号作为小数分隔符
【发布时间】:2016-08-10 11:20:36
【问题描述】:

我需要用 dygraphs 绘制 CSV 文件,但我的 CSV 文件使用逗号作为小数分隔符。

格式为:

12,46;35,26;5,19

如何更改小数点分隔符。到 , 在 dygraphs 中?

输入文件是这样给出的。

<script type="text/javascript">
  g2 = new Dygraph(
    document.getElementById("graphdiv2"),
    "values.csv", // path to CSV file
    {}          // options
  );

【问题讨论】:

  • 在解析 CSV 之前您是否尝试过使用 replace(/,/g, ".")
  • 怎么做?我添加了一个如何将文件 values.csv 提供给 dygraphs 的示例。

标签: decimal dygraphs separator


【解决方案1】:

为了翻译文件内容,一种可能的方法是:

  • 使用 XMLHttpRequest 获取文件(就像 Dygraph 一样)
  • 将替换“,”的内容转换为“。”
    接下来,可以将修改后的 CSV 提供给 Dygraph。

这可以通过以下方式实现:

var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
  if(xmlhttp.status == 200 && xmlhttp.readyState == 4){
    // got the file
    var data = xmlhttp.responseText;
    // modify content
    var data = data.replace(/,/g, ".").replace(/;/g, "\n");
    // create the graph with modified data
    new Dygraph(document.getElementById("graphdiv2"),data);
  }
};
xmlhttp.open("GET","values.csv",true);
xmlhttp.send();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-02-10
    • 1970-01-01
    • 2017-09-30
    • 2014-07-16
    • 1970-01-01
    • 2011-04-11
    • 2014-01-04
    • 1970-01-01
    相关资源
    最近更新 更多