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.

548 lines
16 KiB

  1. var query = null;
  2. function load() {
  3. selectDataType();
  4. loadCsvExample();
  5. loadJsonExample();
  6. loadJavascriptExample();
  7. loadGooglespreadsheetExample();
  8. loadDatasourceExample();
  9. draw();
  10. }
  11. /**
  12. * Upate the UI based on the currently selected datatype
  13. */
  14. function selectDataType() {
  15. }
  16. function round(value, decimals) {
  17. return parseFloat(value.toFixed(decimals));
  18. }
  19. function loadCsvExample() {
  20. var csv = "";
  21. // headers
  22. csv += '"x", "y", "value"\n';
  23. // create some nice looking data with sin/cos
  24. var steps = 30;
  25. var axisMax = 314;
  26. var axisStep = axisMax / steps;
  27. for (var x = 0; x < axisMax; x+=axisStep) {
  28. for (var y = 0; y < axisMax; y+=axisStep) {
  29. var value = Math.sin(x/50) * Math.cos(y/50) * 50 + 50;
  30. csv += round(x, 2) + ', ' + round(y, 2) + ', ' + round(value, 2) + '\n';
  31. }
  32. }
  33. document.getElementById("csvTextarea").innerHTML = csv;
  34. // also adjust some settings
  35. document.getElementById("style").value = "surface";
  36. document.getElementById("verticalRatio").value = "0.5";
  37. document.getElementById("xLabel").value = "x";
  38. document.getElementById("yLabel").value = "y";
  39. document.getElementById("zLabel").value = "value";
  40. document.getElementById("filterLabel").value = "";
  41. document.getElementById("legendLabel").value = "";
  42. drawCsv();
  43. }
  44. function loadCsvAnimationExample() {
  45. var csv = "";
  46. // headers
  47. csv += '"x", "y", "value", "time"\n';
  48. // create some nice looking data with sin/cos
  49. var steps = 20;
  50. var axisMax = 314;
  51. var tMax = 31;
  52. var axisStep = axisMax / steps;
  53. for (var t = 0; t < tMax; t++) {
  54. for (var x = 0; x < axisMax; x+=axisStep) {
  55. for (var y = 0; y < axisMax; y+=axisStep) {
  56. var value = Math.sin(x/50 + t/10) * Math.cos(y/50 + t/10) * 50 + 50;
  57. csv += round(x, 2) + ', ' + round(y, 2) + ', ' + round(value, 2) + ', ' + t + '\n';
  58. }
  59. }
  60. }
  61. document.getElementById("csvTextarea").innerHTML = csv;
  62. // also adjust some settings
  63. document.getElementById("style").value = "surface";
  64. document.getElementById("verticalRatio").value = "0.5";
  65. document.getElementById("animationInterval").value = 100;
  66. document.getElementById("xLabel").value = "x";
  67. document.getElementById("yLabel").value = "y";
  68. document.getElementById("zLabel").value = "value";
  69. document.getElementById("filterLabel").value = "time";
  70. document.getElementById("legendLabel").value = "";
  71. drawCsv();
  72. }
  73. function loadCsvLineExample() {
  74. var csv = "";
  75. // headers
  76. csv += '"sin(t)", "cos(t)", "t"\n';
  77. // create some nice looking data with sin/cos
  78. var steps = 100;
  79. var axisMax = 314;
  80. var tmax = 4 * 2 * Math.PI;
  81. var axisStep = axisMax / steps;
  82. for (t = 0; t < tmax; t += tmax / steps) {
  83. var r = 1;
  84. var x = r * Math.sin(t);
  85. var y = r * Math.cos(t);
  86. var z = t;
  87. csv += round(x, 2) + ', ' + round(y, 2) + ', ' + round(z, 2) + '\n';
  88. }
  89. document.getElementById("csvTextarea").innerHTML = csv;
  90. // also adjust some settings
  91. document.getElementById("style").value = "line";
  92. document.getElementById("verticalRatio").value = "1.0";
  93. document.getElementById("showPerspective").checked = false;
  94. document.getElementById("xLabel").value = "sin(t)";
  95. document.getElementById("yLabel").value = "cos(t)";
  96. document.getElementById("zLabel").value = "t";
  97. document.getElementById("filterLabel").value = "";
  98. document.getElementById("legendLabel").value = "";
  99. drawCsv();
  100. }
  101. function loadCsvMovingDotsExample() {
  102. var csv = "";
  103. // headers
  104. csv += '"x", "y", "z", "color value", "time"\n';
  105. // create some shortcuts to math functions
  106. var sin = Math.sin;
  107. var cos = Math.cos;
  108. var pi = Math.PI;
  109. // create the animation data
  110. var tmax = 2.0 * pi;
  111. var tstep = tmax / 75;
  112. var dotCount = 1; // set this to 1, 2, 3, 4, ...
  113. for (var t = 0; t < tmax; t += tstep) {
  114. var tgroup = parseFloat(t.toFixed(2));
  115. var value = t;
  116. // a dot in the center
  117. var x = 0;
  118. var y = 0;
  119. var z = 0;
  120. csv += round(x, 2) + ', ' + round(y, 2) + ', ' + round(z, 2) + ', ' + round(value, 2)+ ', ' + round(tgroup, 2) + '\n';
  121. // one or multiple dots moving around the center
  122. for (var dot = 0; dot < dotCount; dot++) {
  123. var tdot = t + 2*pi * dot / dotCount;
  124. //data.addRow([sin(tdot), cos(tdot), sin(tdot), value, tgroup]);
  125. //data.addRow([sin(tdot), -cos(tdot), sin(tdot + tmax*1/2), value, tgroup]);
  126. var x = sin(tdot);
  127. var y = cos(tdot);
  128. var z = sin(tdot);
  129. csv += round(x, 2) + ', ' + round(y, 2) + ', ' + round(z, 2) + ', ' + round(value, 2)+ ', ' + round(tgroup, 2) + '\n';
  130. var x = sin(tdot);
  131. var y = -cos(tdot);
  132. var z = sin(tdot + tmax*1/2);
  133. csv += round(x, 2) + ', ' + round(y, 2) + ', ' + round(z, 2) + ', ' + round(value, 2)+ ', ' + round(tgroup, 2) + '\n';
  134. }
  135. }
  136. document.getElementById("csvTextarea").innerHTML = csv;
  137. // also adjust some settings
  138. document.getElementById("style").value = "dot-color";
  139. document.getElementById("verticalRatio").value = "1.0";
  140. document.getElementById("animationInterval").value = "35";
  141. document.getElementById("animationAutoStart").checked = true;
  142. document.getElementById("showPerspective").checked = true;
  143. document.getElementById("xLabel").value = "x";
  144. document.getElementById("yLabel").value = "y";
  145. document.getElementById("zLabel").value = "z";
  146. document.getElementById("filterLabel").value = "time";
  147. document.getElementById("legendLabel").value = "color value";
  148. drawCsv();
  149. }
  150. function loadCsvColoredDotsExample() {
  151. var csv = "";
  152. // headers
  153. csv += '"x", "y", "z", "distance"\n';
  154. // create some shortcuts to math functions
  155. var sqrt = Math.sqrt;
  156. var pow = Math.pow;
  157. var random = Math.random;
  158. // create the animation data
  159. var imax = 200;
  160. for (var i = 0; i < imax; i++) {
  161. var x = pow(random(), 2);
  162. var y = pow(random(), 2);
  163. var z = pow(random(), 2);
  164. var dist = sqrt(pow(x, 2) + pow(y, 2) + pow(z, 2));
  165. csv += round(x, 2) + ', ' + round(y, 2) + ', ' + round(z, 2) + ', ' + round(dist, 2)+ '\n';
  166. }
  167. document.getElementById("csvTextarea").innerHTML = csv;
  168. // also adjust some settings
  169. document.getElementById("style").value = "dot-color";
  170. document.getElementById("verticalRatio").value = "1.0";
  171. document.getElementById("showPerspective").checked = true;
  172. document.getElementById("xLabel").value = "x";
  173. document.getElementById("yLabel").value = "y";
  174. document.getElementById("zLabel").value = "value";
  175. document.getElementById("legendLabel").value = "distance"
  176. document.getElementById("filterLabel").value = "";
  177. drawCsv();
  178. }
  179. function loadCsvSizedDotsExample() {
  180. var csv = "";
  181. // headers
  182. csv += '"x", "y", "z", "range"\n';
  183. // create some shortcuts to math functions
  184. var sqrt = Math.sqrt;
  185. var pow = Math.pow;
  186. var random = Math.random;
  187. // create the animation data
  188. var imax = 200;
  189. for (var i = 0; i < imax; i++) {
  190. var x = pow(random(), 2);
  191. var y = pow(random(), 2);
  192. var z = pow(random(), 2);
  193. var dist = sqrt(pow(x, 2) + pow(y, 2) + pow(z, 2));
  194. var range = sqrt(2) - dist;
  195. csv += round(x, 2) + ', ' + round(y, 2) + ', ' + round(z, 2) + ', ' + round(range, 2)+ '\n';
  196. }
  197. document.getElementById("csvTextarea").innerHTML = csv;
  198. // also adjust some settings
  199. document.getElementById("style").value = "dot-size";
  200. document.getElementById("verticalRatio").value = "1.0";
  201. document.getElementById("showPerspective").checked = true;
  202. document.getElementById("xLabel").value = "x";
  203. document.getElementById("yLabel").value = "y";
  204. document.getElementById("zLabel").value = "z";
  205. document.getElementById("legendLabel").value = "range";
  206. document.getElementById("filterLabel").value = "";
  207. drawCsv();
  208. }
  209. function loadJsonExample() {
  210. }
  211. function loadJavascriptExample() {
  212. }
  213. function loadJavascriptFunctionExample() {
  214. }
  215. function loadGooglespreadsheetExample() {
  216. }
  217. function loadDatasourceExample() {
  218. }
  219. /**
  220. * Retrieve teh currently selected datatype
  221. * @return {string} datatype
  222. */
  223. function getDataType() {
  224. return "csv";
  225. }
  226. /**
  227. * Retrieve the datatable from the entered contents of the csv text
  228. * @param {boolean} [skipValue] | if true, the 4th element is a filter value
  229. * @return {vis DataSet}
  230. */
  231. function getDataCsv() {
  232. var csv = document.getElementById("csvTextarea").value;
  233. // parse the csv content
  234. var csvArray = csv2array(csv);
  235. var data = new vis.DataSet();
  236. var skipValue = false;
  237. if (document.getElementById("filterLabel").value != "" && document.getElementById("legendLabel").value == "") {
  238. skipValue = true;
  239. }
  240. // read all data
  241. for (var row = 1; row < csvArray.length; row++) {
  242. if (csvArray[row].length == 4 && skipValue == false) {
  243. data.add({x:parseFloat(csvArray[row][0]),
  244. y:parseFloat(csvArray[row][1]),
  245. z:parseFloat(csvArray[row][2]),
  246. style:parseFloat(csvArray[row][3])});
  247. }
  248. else if (csvArray[row].length == 4 && skipValue == true) {
  249. data.add({x:parseFloat(csvArray[row][0]),
  250. y:parseFloat(csvArray[row][1]),
  251. z:parseFloat(csvArray[row][2]),
  252. filter:parseFloat(csvArray[row][3])});
  253. }
  254. else if (csvArray[row].length == 5) {
  255. data.add({x:parseFloat(csvArray[row][0]),
  256. y:parseFloat(csvArray[row][1]),
  257. z:parseFloat(csvArray[row][2]),
  258. style:parseFloat(csvArray[row][3]),
  259. filter:parseFloat(csvArray[row][4])});
  260. }
  261. else {
  262. data.add({x:parseFloat(csvArray[row][0]),
  263. y:parseFloat(csvArray[row][1]),
  264. z:parseFloat(csvArray[row][2]),
  265. style:parseFloat(csvArray[row][2])});
  266. }
  267. }
  268. return data;
  269. }
  270. /**
  271. * remove leading and trailing spaces
  272. */
  273. function trim(text) {
  274. while (text.length && text.charAt(0) == ' ')
  275. text = text.substr(1);
  276. while (text.length && text.charAt(text.length-1) == ' ')
  277. text = text.substr(0, text.length-1);
  278. return text;
  279. }
  280. /**
  281. * Retrieve the datatable from the entered contents of the javascript text
  282. * @return {vis Dataset}
  283. */
  284. function getDataJson() {
  285. var json = document.getElementById("jsonTextarea").value;
  286. var data = new google.visualization.DataTable(json);
  287. return data;
  288. }
  289. /**
  290. * Retrieve the datatable from the entered contents of the javascript text
  291. * @return {vis Dataset}
  292. */
  293. function getDataJavascript() {
  294. var js = document.getElementById("javascriptTextarea").value;
  295. eval(js);
  296. return data;
  297. }
  298. /**
  299. * Retrieve the datatable from the entered contents of the datasource text
  300. * @return {vis Dataset}
  301. */
  302. function getDataDatasource() {
  303. }
  304. /**
  305. * Retrieve a JSON object with all options
  306. */
  307. function getOptions() {
  308. return {
  309. width: document.getElementById("width").value,
  310. height: document.getElementById("height").value,
  311. style: document.getElementById("style").value,
  312. showAnimationControls: (document.getElementById("showAnimationControls").checked != false),
  313. showGrid: (document.getElementById("showGrid").checked != false),
  314. showXAxis: (document.getElementById("showXAxis").checked != false),
  315. showYAxis: (document.getElementById("showYAxis").checked != false),
  316. showZAxis: (document.getElementById("showZAxis").checked != false),
  317. showPerspective: (document.getElementById("showPerspective").checked != false),
  318. showLegend: (document.getElementById("showLegend").checked != false),
  319. showShadow: (document.getElementById("showShadow").checked != false),
  320. keepAspectRatio: (document.getElementById("keepAspectRatio").checked != false),
  321. verticalRatio: document.getElementById("verticalRatio").value,
  322. animationInterval: document.getElementById("animationInterval").value,
  323. xLabel: document.getElementById("xLabel").value,
  324. yLabel: document.getElementById("yLabel").value,
  325. zLabel: document.getElementById("zLabel").value,
  326. filterLabel: document.getElementById("filterLabel").value,
  327. legendLabel: document.getElementById("legendLabel").value,
  328. animationPreload: (document.getElementById("animationPreload").checked != false),
  329. animationAutoStart:(document.getElementById("animationAutoStart").checked != false),
  330. xCenter: Number(document.getElementById("xCenter").value) || undefined,
  331. yCenter: Number(document.getElementById("yCenter").value) || undefined,
  332. xMin: Number(document.getElementById("xMin").value) || undefined,
  333. xMax: Number(document.getElementById("xMax").value) || undefined,
  334. xStep: Number(document.getElementById("xStep").value) || undefined,
  335. yMin: Number(document.getElementById("yMin").value) || undefined,
  336. yMax: Number(document.getElementById("yMax").value) || undefined,
  337. yStep: Number(document.getElementById("yStep").value) || undefined,
  338. zMin: Number(document.getElementById("zMin").value) || undefined,
  339. zMax: Number(document.getElementById("zMax").value) || undefined,
  340. zStep: Number(document.getElementById("zStep").value) || undefined,
  341. valueMin: Number(document.getElementById("valueMin").value) || undefined,
  342. valueMax: Number(document.getElementById("valueMax").value) || undefined,
  343. xBarWidth: Number(document.getElementById("xBarWidth").value) || undefined,
  344. yBarWidth: Number(document.getElementById("yBarWidth").value) || undefined
  345. };
  346. }
  347. /**
  348. * Redraw the graph with the entered data and options
  349. */
  350. function draw() {
  351. return drawCsv();
  352. }
  353. function drawCsv() {
  354. // retrieve data and options
  355. var data = getDataCsv();
  356. var options = getOptions();
  357. // Creat a graph
  358. var graph = new vis.Graph3d(document.getElementById('graph'), data, options);
  359. }
  360. function drawJson() {
  361. // retrieve data and options
  362. var data = getDataJson();
  363. var options = getOptions();
  364. // Creat a graph
  365. var graph = new vis.Graph3d(document.getElementById('graph'), data, options);
  366. }
  367. function drawJavascript() {
  368. // retrieve data and options
  369. var data = getDataJavascript();
  370. var options = getOptions();
  371. // Creat a graph
  372. var graph = new vis.Graph3d(document.getElementById('graph'), data, options);
  373. }
  374. function drawGooglespreadsheet() {
  375. // Instantiate our graph object.
  376. drawGraph = function(response) {
  377. document.getElementById("draw").disabled = "";
  378. if (response.isError()) {
  379. error = 'Error: ' + response.getMessage();
  380. document.getElementById('graph').innerHTML =
  381. "<span style='color: red; font-weight: bold;'>" + error + "</span>"; ;
  382. }
  383. // retrieve the data from the query response
  384. data = response.getDataTable();
  385. // specify options
  386. options = getOptions();
  387. // Instantiate our graph object.
  388. var graph = new vis.Graph3d(document.getElementById('graph'), data, options);
  389. }
  390. url = document.getElementById("googlespreadsheetText").value;
  391. document.getElementById("draw").disabled = "disabled";
  392. // send the request
  393. query && query.abort();
  394. query = new google.visualization.Query(url);
  395. query.send(drawGraph);
  396. }
  397. function drawDatasource() {
  398. // Instantiate our graph object.
  399. drawGraph = function(response) {
  400. document.getElementById("draw").disabled = "";
  401. if (response.isError()) {
  402. error = 'Error: ' + response.getMessage();
  403. document.getElementById('graph').innerHTML =
  404. "<span style='color: red; font-weight: bold;'>" + error + "</span>"; ;
  405. }
  406. // retrieve the data from the query response
  407. data = response.getDataTable();
  408. // specify options
  409. options = getOptions();
  410. // Instantiate our graph object.
  411. var graph = new vis.Graph3d(document.getElementById('graph'), data, options);
  412. };
  413. url = document.getElementById("datasourceText").value;
  414. document.getElementById("draw").disabled = "disabled";
  415. // if the entered url is a google spreadsheet url, replace the part
  416. // "/ccc?" with "/tq?" in order to retrieve a neat data query result
  417. if (url.indexOf("/ccc?")) {
  418. url.replace("/ccc?", "/tq?");
  419. }
  420. // send the request
  421. query && query.abort();
  422. query = new google.visualization.Query(url);
  423. query.send(drawGraph);
  424. }