RGraph.common.csv.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /**
  2. * o------------------------------------------------------------------------------o
  3. * | This file is part of the RGraph package - you can learn more at: |
  4. * | |
  5. * | http://www.rgraph.net |
  6. * | |
  7. * | This package is licensed under the RGraph license. For all kinds of business |
  8. * | purposes there is a small one-time licensing fee to pay and for non |
  9. * | commercial purposes it is free to use. You can read the full license here: |
  10. * | |
  11. * | http://www.rgraph.net/license |
  12. * o------------------------------------------------------------------------------o
  13. */
  14. /**
  15. * Initialise the various objects
  16. */
  17. if (typeof(RGraph) == 'undefined') RGraph = {isRGraph:true,type:'common'};
  18. RGraph.CSV = function (url, func)
  19. {
  20. /**
  21. * Some default values
  22. */
  23. this.url = url;
  24. this.ready = func;
  25. this.data = null;
  26. this.numrows = null;
  27. this.numcols = null;
  28. this.seperator = arguments[2] || ',';
  29. this.endofline = arguments[3] || /\r?\n/;
  30. /**
  31. * This function splits the CSV data into an array so that it can be useful.
  32. */
  33. this.fetch = function ()
  34. {
  35. var sep = this.seperator;
  36. var eol = this.endofline;
  37. var obj = this;
  38. if (this.url.substring(0,3) == 'id:') {
  39. // Get rid of any trailing slash
  40. var data = document.getElementById(this.url.substring(3)).innerHTML.replace(/(\r?\n)+$/, '');
  41. // Store the CSV data on the CSV object (ie - this object)
  42. obj.data = data.split(eol);
  43. // Store the number of rows
  44. obj.numrows = obj.data.length;
  45. for (var i=0,len=obj.data.length; i<len; i+=1) {
  46. var row = obj.data[i].split(sep);
  47. if (!obj.numcols) {
  48. obj.numcols = row.length;
  49. }
  50. /**
  51. * If the cell is purely made up of numbers - convert it
  52. */
  53. for (var j=0; j<row.length; j+=1) {
  54. if ((/^[0-9.]+$/).test(row[j])) {
  55. row[j] = parseFloat(row[j]);
  56. }
  57. // Assign the split-up-row back to the data array
  58. obj.data[i] = row;
  59. }
  60. }
  61. // Call the ready function straight away
  62. obj.ready(obj);
  63. } else {
  64. RGraph.AJAX.getString(this.url, function (data)
  65. {
  66. data = data.replace(/(\r?\n)+$/, '');
  67. obj.data = data.split(eol);
  68. // Store the number of rows
  69. obj.numrows = obj.data.length;
  70. for (var i=0,len=obj.data.length; i<len; i+=1) {
  71. var row = obj.data[i].split(sep);
  72. if (!obj.numcols) {
  73. obj.numcols = row.length;
  74. }
  75. /**
  76. * If the cell is purely made up of numbers - convert it
  77. */
  78. for (var j=0; j<row.length; j+=1) {
  79. if ((/^[0-9.]+$/).test(row[j])) {
  80. row[j] = parseFloat(row[j]);
  81. }
  82. // Assign the split-up-row back to the data array
  83. obj.data[i] = row;
  84. }
  85. }
  86. // Call the ready function straight away
  87. obj.ready(obj);
  88. });
  89. }
  90. }
  91. /**
  92. * Returns a row of the CSV file
  93. *
  94. * @param number index The index of the row to fetch
  95. * @param start OPTIONAL If desired you can specify a column to start at (which starts at 0 by default)
  96. */
  97. this.getRow = function (index)
  98. {
  99. var row = [];
  100. var start = arguments[1] || 0;
  101. for (var i=start; i<this.numcols; i+=1) {
  102. row.push(this.data[index][i]);
  103. }
  104. return row;
  105. }
  106. /**
  107. * Returns a column of the CSV file
  108. *
  109. * @param number index The index of the column to fetch
  110. * @param start OPTIONAL If desired you can specify a row to start at (which starts at 0 by default)
  111. */
  112. this.getCol =
  113. this.getColumn = function (index)
  114. {
  115. var col = [];
  116. var start = arguments[1] || 0;
  117. for (var i=start; i<this.numrows; i+=1) {
  118. col.push(this.data[i][index]);
  119. }
  120. return col;
  121. }
  122. // Fetch the CSV file
  123. this.fetch();
  124. }