summaryrefslogtreecommitdiff
path: root/www/lib/vis/examples/graph3d/playground/datasource.html
blob: 7a59360411a71e2a7edc693a2f8ce5c53f41c884 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
<html>

<head>
    <title>Graph3d documentation</title>
    <link rel='stylesheet' href='default.css' type='text/css'>

    <link href="prettify/prettify.css" type="text/css" rel="stylesheet" />
    <script type="text/javascript" src="prettify/prettify.js"></script>
<script>(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)})(window,document,'script','//www.google-analytics.com/analytics.js','ga');ga('create', 'UA-61231638-1', 'auto');ga('send', 'pageview');</script></head>

<body onload="prettyPrint();">

<pre class="prettyprint lang-php">
&lt;?php

/*
This datasource returns a response in the form of a google query response

USAGE
All parameters are optional
datasource.php?xmin=0&xmax=314&xstepnum=25&ymin=0&ymax=314&ystepnum=25

DOCUMENTATION
http://code.google.com/apis/visualization/documentation/dev/implementing_data_source.html


EXAMPLE OF A RESPONSE FILE

Note that the reqId in the response must correspond with the reqId from the
request.
________________________________________________________________________________

google.visualization.Query.setResponse({
  version:'0.6',
  reqId:'0',
  status:'ok',
  table:{
    cols:[
      {id:'x',
       label:'x',
       type:'number'},
      {id:'y',
       label:'y',
       type:'number'},
      {id:'value',
       label:'value',
       type:'number'}
    ],
    rows:[
      {c:[{v:0}, {v:0}, {v:10.0}]},
      {c:[{v:1}, {v:0}, {v:12.0}]},
      {c:[{v:2}, {v:0}, {v:13.0}]},
      {c:[{v:0}, {v:1}, {v:11.0}]},
      {c:[{v:1}, {v:1}, {v:14.0}]},
      {c:[{v:2}, {v:1}, {v:11.0}]}
    ]
  }
});
________________________________________________________________________________

*/


/**
 * A custom function
 */
function custom($x, $y) {
  $d = sqrt(pow($x/100, 2) + pow($y/100, 2));

  return 50 * exp(-5 * $d / 10) * sin($d*5)
}




// retrieve parameters
$default_stepnum = 25;

$xmin     = isset($_REQUEST['xmin'])     ? (float)$_REQUEST['xmin']   : -100;
$xmax     = isset($_REQUEST['xmax'])     ? (float)$_REQUEST['xmax']   : 100;
$xstepnum = isset($_REQUEST['xstepnum']) ? (int)$_REQUEST['xstepnum'] : $default_stepnum;

$ymin     = isset($_REQUEST['ymin'])     ? (float)$_REQUEST['ymin']   : -100;
$ymax     = isset($_REQUEST['ymax'])     ? (float)$_REQUEST['ymax']   : 100;
$ystepnum = isset($_REQUEST['ystepnum']) ? (int)$_REQUEST['ystepnum'] : $default_stepnum;

// in the reply we must fill in the request id that came with the request
$reqId = getReqId();

// check for a maximum number of datapoints (for safety)
if ($xstepnum * $ystepnum &gt; 10000) {
  echo "google.visualization.Query.setResponse({
    version:'0.6',
    reqId:'$reqId',
    status:'error',
    errors:[{reason:'not_supported', message:'Maximum number of datapoints exceeded'}]
  });";

  exit;
}


// output the header part of the response
echo "google.visualization.Query.setResponse({
  version:'0.6',
  reqId:'$reqId',
  status:'ok',
  table:{
    cols:[
      {id:'x',
       label:'x',
       type:'number'},
      {id:'y',
       label:'y',
       type:'number'},
      {id:'value',
       label:'',
       type:'number'}
    ],
    rows:[";

// output the actual values
$first = true;
$xstep = ($xmax - $xmin) / $xstepnum;
$ystep = ($ymax - $ymin) / $ystepnum;
for ($x = $xmin; $x &lt; $xmax; $x+=$xstep) {
  for ($y = $ymin; $y &lt; $ymax; $y+=$ystep) {
    $value = custom($x,$y);

    if (!$first) {
      echo ",\n";
    }
    else {
      echo "\n";
    }
    echo "      {c:[{v:$x}, {v:$y}, {v:$value}]}";

    $first = false;
  }
}


// output the end part of the response
echo "
    ]
  }
});
";


/**
 * Retrieve the request id from the get/post data
 * @return {number} $reqId       The request id, or 0 if not found
 */
function getReqId() {
  $reqId = 0;

  foreach ($_REQUEST as $req) {
    if (substr($req, 0,6) == "reqId:") {
      $reqId = substr($req, 6);
    }
  }

  return $reqId;
}


?&gt;

</pre>

</body>
</html>