vis.js is a dynamic, browser-based visualization library
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

80 lines
2.1 KiB

10 years ago
10 years ago
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2. <html>
  3. <head>
  4. <title>Convert CSV to Google Datatable</title>
  5. <style>
  6. body {font: 10pt arial;}
  7. </style>
  8. <script type="text/javascript" src="http://www.google.com/jsapi"></script>
  9. <script type="text/javascript" src="csv2array.js"></script>
  10. <script type="text/javascript">
  11. var data = null;
  12. var graph = null;
  13. function loaded() {
  14. }
  15. google.load("visualization", "1");
  16. // Set callback to run when API is loaded
  17. google.setOnLoadCallback(loaded);
  18. // Called when the Visualization API is loaded.
  19. function convert() {
  20. var csv = document.getElementById("csv").value;
  21. var datatable = "";
  22. // parse the csv content
  23. var csvArray = csv2array(csv);
  24. // Create and populate a data table.
  25. datatable += "data = new google.visualization.DataTable();\n";
  26. // read the header row
  27. for (var col = 0; col < csvArray[0].length; col++) {
  28. datatable += "data.addColumn('number', '" + csvArray[0][col] + "');\n";
  29. }
  30. // read all data
  31. for (var row = 1; row < csvArray.length; row++) {
  32. datatable += "data.addRow([";
  33. for (var col = 0; col < csvArray[row].length; col++) {
  34. if (col != 0)
  35. datatable += ", ";
  36. datatable += csvArray[row][col];
  37. }
  38. datatable += "]);\n";
  39. }
  40. document.getElementById("datatable").value = datatable;
  41. alert(csvArray.length + " rows converted");
  42. }
  43. </script>
  44. </head>
  45. <body>
  46. <div id="graph"></div>
  47. <div id="info"></div>
  48. <b>CSV</b><br>
  49. <textarea id="csv" style="width: 400px; height: 300px;"></textarea>
  50. <br>
  51. <br>
  52. <input type="button" value="Convert" onclick="convert();">
  53. <br>
  54. <br>
  55. <b>Google DataTable</b><br>
  56. <textarea id="datatable" style="width: 400px; height: 300px;"></textarea>
  57. </body>
  58. </html>