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.

117 lines
3.6 KiB

  1. <html>
  2. <head>
  3. <title>Timeline animate visible range</title>
  4. <style type="text/css">
  5. body {
  6. font-size: 10pt;
  7. font-family: verdana, sans, arial, sans-serif;
  8. }
  9. </style>
  10. <script type="text/javascript" src="../timeline.js"></script>
  11. <link rel="stylesheet" type="text/css" href="../timeline.css">
  12. <script type="text/javascript">
  13. var timeline;
  14. var data;
  15. // Called when the Visualization API is loaded.
  16. function drawVisualization() {
  17. // Create a JSON data table
  18. data = [];
  19. for (var i = 0; i < 10; i++) {
  20. var date = new Date(2012, i, 1);
  21. data.push({
  22. "start": date,
  23. "content": "item " + i
  24. });
  25. }
  26. // specify options
  27. var options = {
  28. 'width': '100%',
  29. 'height': '300px',
  30. 'showCustomTime': true
  31. };
  32. // Instantiate our timeline object.
  33. timeline = new links.Timeline(document.getElementById('mytimeline'), options);
  34. // cancel any running animation as soon as the user changes the range
  35. links.events.addListener(timeline, 'rangechange', function (properties) {
  36. animateCancel();
  37. });
  38. // Draw our timeline with the created data and options
  39. timeline.draw(data);
  40. }
  41. // create a simple animation
  42. var animateTimeout = undefined;
  43. var animateFinal = undefined;
  44. function animateTo(date) {
  45. // get the new final date
  46. animateFinal = date.valueOf();
  47. timeline.setCustomTime(date);
  48. // cancel any running animation
  49. animateCancel();
  50. // animate towards the final date
  51. var animate = function () {
  52. var range = timeline.getVisibleChartRange();
  53. var current = (range.start.getTime() + range.end.getTime())/ 2;
  54. var width = (range.end.getTime() - range.start.getTime());
  55. var minDiff = Math.max(width / 1000, 1);
  56. var diff = (animateFinal - current);
  57. if (Math.abs(diff) > minDiff) {
  58. // move towards the final date
  59. var start = new Date(range.start.getTime() + diff / 4);
  60. var end = new Date(range.end.getTime() + diff / 4);
  61. timeline.setVisibleChartRange(start, end);
  62. // start next timer
  63. animateTimeout = setTimeout(animate, 50);
  64. }
  65. };
  66. animate();
  67. }
  68. function animateCancel () {
  69. if (animateTimeout) {
  70. clearTimeout(animateTimeout);
  71. animateTimeout = undefined;
  72. }
  73. }
  74. function go () {
  75. // interpret the value as a date formatted as "yyyy-MM-dd"
  76. var v = document.getElementById('animateDate').value.split('-');
  77. var date = new Date(v[0], v[1], v[2]);
  78. if (date.toString() == "Invalid Date") {
  79. alert("Invalid Date");
  80. }
  81. else {
  82. animateTo(date);
  83. }
  84. }
  85. </script>
  86. </head>
  87. <body onload="drawVisualization();">
  88. <h1>Timeline - animate visible range</h1>
  89. <p>
  90. This demo shows how to to animate the timeline visible range to another date
  91. </p>
  92. <div>
  93. <input id="animateDate" value="2012-09-13" placeholder="yyyy-MM-dd">
  94. <button id="go" onclick="go()">Animate to</button>
  95. </div>
  96. <br>
  97. <div id="mytimeline"></div>
  98. </body>
  99. </html>