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.

156 lines
3.3 KiB

10 years ago
  1. <?php
  2. /*
  3. This datasource returns a response in the form of a google query response
  4. USAGE
  5. All parameters are optional
  6. datasource.php?xmin=0&xmax=314&xstepnum=25&ymin=0&ymax=314&ystepnum=25
  7. DOCUMENTATION
  8. http://code.google.com/apis/visualization/documentation/dev/implementing_data_source.html
  9. EXAMPLE OF A RESPONSE FILE
  10. Note that the reqId in the response must correspond with the reqId from the
  11. request.
  12. ________________________________________________________________________________
  13. google.visualization.Query.setResponse({
  14. version:'0.6',
  15. reqId:'0',
  16. status:'ok',
  17. table:{
  18. cols:[
  19. {id:'x',
  20. label:'x',
  21. type:'number'},
  22. {id:'y',
  23. label:'y',
  24. type:'number'},
  25. {id:'value',
  26. label:'value',
  27. type:'number'}
  28. ],
  29. rows:[
  30. {c:[{v:0}, {v:0}, {v:10.0}]},
  31. {c:[{v:1}, {v:0}, {v:12.0}]},
  32. {c:[{v:2}, {v:0}, {v:13.0}]},
  33. {c:[{v:0}, {v:1}, {v:11.0}]},
  34. {c:[{v:1}, {v:1}, {v:14.0}]},
  35. {c:[{v:2}, {v:1}, {v:11.0}]}
  36. ]
  37. }
  38. });
  39. ________________________________________________________________________________
  40. */
  41. header('Content-type: text/plain');
  42. /**
  43. * A custom function
  44. */
  45. function custom($x, $y) {
  46. $d = sqrt(pow($x/100, 2) + pow($y/100, 2));
  47. return 50 * exp(-5 * $d / 10) * sin($d*5);
  48. }
  49. // retrieve parameters
  50. $default_stepnum = 25;
  51. $xmin = isset($_REQUEST['xmin']) ? (float)$_REQUEST['xmin'] : -100;
  52. $xmax = isset($_REQUEST['xmax']) ? (float)$_REQUEST['xmax'] : 100;
  53. $xstepnum = isset($_REQUEST['xstepnum']) ? (int)$_REQUEST['xstepnum'] : $default_stepnum;
  54. $ymin = isset($_REQUEST['ymin']) ? (float)$_REQUEST['ymin'] : -100;
  55. $ymax = isset($_REQUEST['ymax']) ? (float)$_REQUEST['ymax'] : 100;
  56. $ystepnum = isset($_REQUEST['ystepnum']) ? (int)$_REQUEST['ystepnum'] : $default_stepnum;
  57. // in the reply we must fill in the request id that came with the request
  58. $reqId = getReqId();
  59. // check for a maximum number of datapoints (for safety)
  60. if ($xstepnum * $ystepnum > 10000) {
  61. echo "google.visualization.Query.setResponse({
  62. version:'0.6',
  63. reqId:'$reqId',
  64. status:'error',
  65. errors:[{reason:'not_supported', message:'Maximum number of datapoints exceeded'}]
  66. });";
  67. exit;
  68. }
  69. // output the header part of the response
  70. echo "google.visualization.Query.setResponse({
  71. version:'0.6',
  72. reqId:'$reqId',
  73. status:'ok',
  74. table:{
  75. cols:[
  76. {id:'x',
  77. label:'x',
  78. type:'number'},
  79. {id:'y',
  80. label:'y',
  81. type:'number'},
  82. {id:'value',
  83. label:'',
  84. type:'number'}
  85. ],
  86. rows:[";
  87. // output the actual values
  88. $first = true;
  89. $xstep = ($xmax - $xmin) / $xstepnum;
  90. $ystep = ($ymax - $ymin) / $ystepnum;
  91. for ($x = $xmin; $x < $xmax; $x+=$xstep) {
  92. for ($y = $ymin; $y < $ymax; $y+=$ystep) {
  93. $value = custom($x,$y);
  94. if (!$first) {
  95. echo ",\n";
  96. }
  97. else {
  98. echo "\n";
  99. }
  100. echo " {c:[{v:$x}, {v:$y}, {v:$value}]}";
  101. $first = false;
  102. }
  103. }
  104. // output the end part of the response
  105. echo "
  106. ]
  107. }
  108. });
  109. ";
  110. /**
  111. * Retrieve the request id from the get/post data
  112. * @return {number} $reqId The request id, or 0 if not found
  113. */
  114. function getReqId() {
  115. $reqId = 0;
  116. foreach ($_REQUEST as $req) {
  117. if (substr($req, 0,6) == "reqId:") {
  118. $reqId = substr($req, 6);
  119. }
  120. }
  121. return $reqId;
  122. }
  123. ?>