JPX
JPX

Reputation: 167

Dygraphs, comma as decimal separator

I need to plot CSV file with dygraphs but my CSV files use comma as decimal separator.

Format is:

12,46;35,26;5,19

How can I change decimal separator from . to , in dygraphs?

Input file is given like this.

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

Upvotes: 0

Views: 333

Answers (1)

mpromonet
mpromonet

Reputation: 11962

In order to translate the file content, a possible way is to :

  • get the file using XMLHttpRequest (like Dygraph does)
  • tranform the content replacing "," with "."
    Next the modified CSV could be given to Dygraph.

This could be achieve with :

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();

Upvotes: 1

Related Questions