example14_past_and_future.html 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <html>
  2. <head>
  3. <title>Timeline demo</title>
  4. <style>
  5. body {font: 10pt arial;}
  6. </style>
  7. <script type="text/javascript" src="http://www.google.com/jsapi"></script>
  8. <script type="text/javascript" src="../timeline.js"></script>
  9. <link rel="stylesheet" type="text/css" href="../timeline.css">
  10. <script type="text/javascript">
  11. google.load("visualization", "1");
  12. var data = undefined;
  13. var timeline = undefined;
  14. // Set callback to run when API is loaded
  15. google.setOnLoadCallback(drawVisualization);
  16. function onTimeChange(event) {
  17. document.getElementById("customTime").innerHTML = "Custom Time: " + event.time;
  18. // adjust the end date of the event in the data table
  19. var start = data.getValue(0, 0);
  20. if (event.time > start) {
  21. data.setValue(0, 1, new Date(event.time));
  22. var now = new Date();
  23. if (event.time < now) {
  24. data.setValue(0, 2, "Dynamic Event (past)");
  25. }
  26. else if (event.time > now) {
  27. data.setValue(0, 2, "Dynamic Event (future)");
  28. }
  29. else {
  30. data.setValue(0, 2, "Dynamic Event (now)");
  31. }
  32. timeline.redraw();
  33. }
  34. }
  35. // Called when the Visualization API is loaded.
  36. function drawVisualization() {
  37. // Create and populate a data table.
  38. data = new google.visualization.DataTable();
  39. data.addColumn('datetime', 'start');
  40. data.addColumn('datetime', 'end');
  41. data.addColumn('string', 'content');
  42. data.addRows([[
  43. new Date((new Date()).getTime() - 1 * 60 * 1000),
  44. new Date(),
  45. 'Dynamic event']]);
  46. // specify options
  47. options = {
  48. 'width': "100%",
  49. 'height': "auto",
  50. 'style': "box",
  51. 'showCustomTime': true
  52. };
  53. // Instantiate our timeline object.
  54. timeline = new links.Timeline(document.getElementById('mytimeline'));
  55. // Add event listeners
  56. google.visualization.events.addListener(timeline, 'timechange', onTimeChange);
  57. // Draw our timeline with the created data and options
  58. timeline.draw(data, options);
  59. // set a custom range from -2 minute to +3 minutes current time
  60. var start = new Date((new Date()).getTime() - 2 * 60 * 1000 )
  61. var end = new Date((new Date()).getTime() + 3 * 60 * 1000);
  62. timeline.setVisibleChartRange(start, end);
  63. }
  64. </script>
  65. </head>
  66. <body>
  67. <p style="width: 600px;">
  68. When the custom time bar is shown, the user can drag this bar to a specific
  69. time. The Timeline sends an event that the custom time is changed, after
  70. which the contents of the timeline can be changed according to the specified
  71. time in past or future.
  72. </p>
  73. <div id="customTime">&nbsp;</div>
  74. <p></p>
  75. <div id="mytimeline"></div>
  76. </body>
  77. </html>