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.

544 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
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
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
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
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
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
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. }
  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. showPerspective: (document.getElementById("showPerspective").checked != false),
  315. showShadow: (document.getElementById("showShadow").checked != false),
  316. keepAspectRatio: (document.getElementById("keepAspectRatio").checked != false),
  317. verticalRatio: document.getElementById("verticalRatio").value,
  318. animationInterval: document.getElementById("animationInterval").value,
  319. xLabel: document.getElementById("xLabel").value,
  320. yLabel: document.getElementById("yLabel").value,
  321. zLabel: document.getElementById("zLabel").value,
  322. filterLabel: document.getElementById("filterLabel").value,
  323. legendLabel: document.getElementById("legendLabel").value,
  324. animationPreload: (document.getElementById("animationPreload").checked != false),
  325. animationAutoStart:(document.getElementById("animationAutoStart").checked != false),
  326. xCenter: Number(document.getElementById("xCenter").value) || undefined,
  327. yCenter: Number(document.getElementById("yCenter").value) || undefined,
  328. xMin: Number(document.getElementById("xMin").value) || undefined,
  329. xMax: Number(document.getElementById("xMax").value) || undefined,
  330. xStep: Number(document.getElementById("xStep").value) || undefined,
  331. yMin: Number(document.getElementById("yMin").value) || undefined,
  332. yMax: Number(document.getElementById("yMax").value) || undefined,
  333. yStep: Number(document.getElementById("yStep").value) || undefined,
  334. zMin: Number(document.getElementById("zMin").value) || undefined,
  335. zMax: Number(document.getElementById("zMax").value) || undefined,
  336. zStep: Number(document.getElementById("zStep").value) || undefined,
  337. valueMin: Number(document.getElementById("valueMin").value) || undefined,
  338. valueMax: Number(document.getElementById("valueMax").value) || undefined,
  339. xBarWidth: Number(document.getElementById("xBarWidth").value) || undefined,
  340. yBarWidth: Number(document.getElementById("yBarWidth").value) || undefined
  341. };
  342. }
  343. /**
  344. * Redraw the graph with the entered data and options
  345. */
  346. function draw() {
  347. return drawCsv();
  348. }
  349. function drawCsv() {
  350. // retrieve data and options
  351. var data = getDataCsv();
  352. var options = getOptions();
  353. // Creat a graph
  354. var graph = new vis.Graph3d(document.getElementById('graph'), data, options);
  355. }
  356. function drawJson() {
  357. // retrieve data and options
  358. var data = getDataJson();
  359. var options = getOptions();
  360. // Creat a graph
  361. var graph = new vis.Graph3d(document.getElementById('graph'), data, options);
  362. }
  363. function drawJavascript() {
  364. // retrieve data and options
  365. var data = getDataJavascript();
  366. var options = getOptions();
  367. // Creat a graph
  368. var graph = new vis.Graph3d(document.getElementById('graph'), data, options);
  369. }
  370. function drawGooglespreadsheet() {
  371. // Instantiate our graph object.
  372. drawGraph = function(response) {
  373. document.getElementById("draw").disabled = "";
  374. if (response.isError()) {
  375. error = 'Error: ' + response.getMessage();
  376. document.getElementById('graph').innerHTML =
  377. "<span style='color: red; font-weight: bold;'>" + error + "</span>"; ;
  378. }
  379. // retrieve the data from the query response
  380. data = response.getDataTable();
  381. // specify options
  382. options = getOptions();
  383. // Instantiate our graph object.
  384. var graph = new vis.Graph3d(document.getElementById('graph'), data, options);
  385. }
  386. url = document.getElementById("googlespreadsheetText").value;
  387. document.getElementById("draw").disabled = "disabled";
  388. // send the request
  389. query && query.abort();
  390. query = new google.visualization.Query(url);
  391. query.send(drawGraph);
  392. }
  393. function drawDatasource() {
  394. // Instantiate our graph object.
  395. drawGraph = function(response) {
  396. document.getElementById("draw").disabled = "";
  397. if (response.isError()) {
  398. error = 'Error: ' + response.getMessage();
  399. document.getElementById('graph').innerHTML =
  400. "<span style='color: red; font-weight: bold;'>" + error + "</span>"; ;
  401. }
  402. // retrieve the data from the query response
  403. data = response.getDataTable();
  404. // specify options
  405. options = getOptions();
  406. // Instantiate our graph object.
  407. var graph = new vis.Graph3d(document.getElementById('graph'), data, options);
  408. };
  409. url = document.getElementById("datasourceText").value;
  410. document.getElementById("draw").disabled = "disabled";
  411. // if the entered url is a google spreadsheet url, replace the part
  412. // "/ccc?" with "/tq?" in order to retrieve a neat data query result
  413. if (url.indexOf("/ccc?")) {
  414. url.replace("/ccc?", "/tq?");
  415. }
  416. // send the request
  417. query && query.abort();
  418. query = new google.visualization.Query(url);
  419. query.send(drawGraph);
  420. }