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.

155 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. /**
  42. * A custom function
  43. */
  44. function custom($x, $y) {
  45. $d = sqrt(pow($x/100, 2) + pow($y/100, 2));
  46. return 50 * exp(-5 * $d / 10) * sin($d*5);
  47. }
  48. // retrieve parameters
  49. $default_stepnum = 25;
  50. $xmin = isset($_REQUEST['xmin']) ? (float)$_REQUEST['xmin'] : -100;
  51. $xmax = isset($_REQUEST['xmax']) ? (float)$_REQUEST['xmax'] : 100;
  52. $xstepnum = isset($_REQUEST['xstepnum']) ? (int)$_REQUEST['xstepnum'] : $default_stepnum;
  53. $ymin = isset($_REQUEST['ymin']) ? (float)$_REQUEST['ymin'] : -100;
  54. $ymax = isset($_REQUEST['ymax']) ? (float)$_REQUEST['ymax'] : 100;
  55. $ystepnum = isset($_REQUEST['ystepnum']) ? (int)$_REQUEST['ystepnum'] : $default_stepnum;
  56. // in the reply we must fill in the request id that came with the request
  57. $reqId = getReqId();
  58. // check for a maximum number of datapoints (for safety)
  59. if ($xstepnum * $ystepnum > 10000) {
  60. echo "google.visualization.Query.setResponse({
  61. version:'0.6',
  62. reqId:'$reqId',
  63. status:'error',
  64. errors:[{reason:'not_supported', message:'Maximum number of datapoints exceeded'}]
  65. });";
  66. exit;
  67. }
  68. // output the header part of the response
  69. echo "google.visualization.Query.setResponse({
  70. version:'0.6',
  71. reqId:'$reqId',
  72. status:'ok',
  73. table:{
  74. cols:[
  75. {id:'x',
  76. label:'x',
  77. type:'number'},
  78. {id:'y',
  79. label:'y',
  80. type:'number'},
  81. {id:'value',
  82. label:'',
  83. type:'number'}
  84. ],
  85. rows:[";
  86. // output the actual values
  87. $first = true;
  88. $xstep = ($xmax - $xmin) / $xstepnum;
  89. $ystep = ($ymax - $ymin) / $ystepnum;
  90. for ($x = $xmin; $x < $xmax; $x+=$xstep) {
  91. for ($y = $ymin; $y < $ymax; $y+=$ystep) {
  92. $value = custom($x,$y);
  93. if (!$first) {
  94. echo ",\n";
  95. }
  96. else {
  97. echo "\n";
  98. }
  99. echo " {c:[{v:$x}, {v:$y}, {v:$value}]}";
  100. $first = false;
  101. }
  102. }
  103. // output the end part of the response
  104. echo "
  105. ]
  106. }
  107. });
  108. ";
  109. /**
  110. * Retrieve the request id from the get/post data
  111. * @return {number} $reqId The request id, or 0 if not found
  112. */
  113. function getReqId() {
  114. $reqId = 0;
  115. foreach ($_REQUEST as $req) {
  116. if (substr($req, 0,6) == "reqId:") {
  117. $reqId = substr($req, 6);
  118. }
  119. }
  120. return $reqId;
  121. }
  122. ?>