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.

537 lines
16 KiB

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