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.

113 lines
2.1 KiB

10 years ago
  1. <?php
  2. /**
  3. This file can read data from an external csv source and output the same
  4. data in Google DataTable JSON format
  5. Note that it supposes that each data column contains numbers
  6. */
  7. header('Content-type: text/plain');
  8. // datasource url. This can be an external source
  9. //$datasourceUrl = "http://demo.almende.com/links/graph3d/js/examples/datasource_csv.php";
  10. $path = dirname("http://" . $_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"]) . '/';
  11. $dataSourceUrl = $path . "datasource_csv.php";
  12. $reqId = getReqId();
  13. $data = file_get_contents($dataSourceUrl);
  14. $rows = split("\n", $data);
  15. // output the header part of the response
  16. echo "google.visualization.Query.setResponse({
  17. version:'0.6',
  18. reqId:'$reqId',
  19. status:'ok',
  20. table:{
  21. cols:[
  22. ";
  23. // output the column names
  24. $cols = split(",", $rows[0]);
  25. $colCount = count($cols);
  26. $colFirst = true;
  27. foreach($cols as $col) {
  28. if ($colFirst == true)
  29. $colFirst = false;
  30. else
  31. echo ",\n"; // end of previous label
  32. $colStr = trim(str_replace('"', '', $col)); // TODO: bad way to remove enclosing quotes
  33. echo " {id:'$colStr', label:'$colStr', type:'number'}";
  34. }
  35. unset($rows[0]); // remove the first row with headers from the array
  36. // output the part between cols and rows
  37. echo "
  38. ],
  39. rows:[
  40. ";
  41. // output the data
  42. $firstRow = true;
  43. foreach ($rows as $row) {
  44. $cols = split(",", $row);
  45. if (count($cols) == $colCount) {
  46. if ($firstRow == true)
  47. $firstRow = false;
  48. else
  49. echo ",\n"; // end of previous line
  50. echo " {c:["; // start of the row
  51. $firstCol = true;
  52. foreach ($cols as $col) {
  53. if ($firstCol == true)
  54. $firstCol = false;
  55. else
  56. echo ", "; // end of previous value
  57. echo "{v:" . $col . "}";
  58. }
  59. echo "]}"; // end of the row
  60. }
  61. }
  62. // output the end part of the response
  63. echo "
  64. ]
  65. }
  66. });
  67. ";
  68. /**
  69. * Retrieve the request id from the get/post data
  70. * @return {number} $reqId The request id, or 0 if not found
  71. */
  72. function getReqId() {
  73. $reqId = 0;
  74. foreach ($_REQUEST as $req) {
  75. if (substr($req, 0,6) == "reqId:") {
  76. $reqId = substr($req, 6);
  77. }
  78. }
  79. return $reqId;
  80. }
  81. ?>