Guest User

Untitled

a guest
Jul 16th, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.00 KB | None | 0 0
  1. // Constants
  2. var box_height = 12;
  3. var box_offset = box_height * 2;
  4. var small_offset = box_height;
  5. var box_radius = box_height / 6;
  6. var timeline_size = 20;
  7. var box_color = "#939393";
  8. var box_gradient = "0-"+ box_color +"-#FFF";
  9.  
  10.  
  11.  
  12. // Date class monkeypatch
  13. Date.prototype.toString = function(){
  14. var months = new Array(12);
  15. months[0] = "Jan";
  16. months[1] = "Feb";
  17. months[2] = "Mar";
  18. months[3] = "Apr";
  19. months[4] = "May";
  20. months[5] = "Jun";
  21. months[6] = "Jul";
  22. months[7] = "Aug";
  23. months[8] = "Sep";
  24. months[9] = "Oct";
  25. months[10] = "Nov";
  26. months[11] = "Dec";
  27.  
  28. return months[this.getMonth()] +" "+ this.getFullYear();
  29. }
  30.  
  31. Date.prototype.daysUntil = function(end_date){
  32. // The number of milliseconds in one day
  33. var ONE_DAY = 1000 * 60 * 60 * 24
  34.  
  35. // Convert both dates to milliseconds
  36. var this_ms = this.getTime()
  37. var end_date_ms = end_date.getTime()
  38.  
  39. // Calculate the difference in milliseconds
  40. var difference_ms = Math.abs(this_ms - end_date_ms)
  41.  
  42. // Convert back to days and return
  43. return Math.round(difference_ms/ONE_DAY)
  44. }
  45.  
  46. Date.prototype.monthsUntil = function(end_date){
  47. var number = 0;
  48.  
  49. if (end_date.getFullYear() > this.getFullYear()) {
  50. number = number + (end_date.getFullYear() - this.getFullYear() - 1) * 12;
  51. } else {
  52. return end_date.getMonth() - this.getMonth() + 1;
  53. }
  54.  
  55. if (end_date.getMonth() > this.getMonth()) {
  56. number = number + 12 + end_date.getMonth() - this.getMonth();
  57. } else {
  58. number = number + (12 - this.getMonth()) + end_date.getMonth();
  59. }
  60.  
  61. return number;
  62. }
  63.  
  64.  
  65.  
  66. // Chart class
  67. function Chart(num_boxes, start_date, end_date, canvas){
  68. this.num_boxes = num_boxes;
  69. this.start_date = start_date;
  70. this.end_date = end_date;
  71. this.num_days = this.start_date.daysUntil(this.end_date);
  72. this.height = (timeline_size * 3) + (this.num_boxes * (box_height + box_offset));
  73. this.width = this.num_days * timeline_size;
  74. this.canvas = Raphael(0, 0, this.width, this.height);
  75. this.timeline = new Timeline(this.num_days, this);
  76. this.boxes = this.draw_boxes();
  77.  
  78. return this;
  79. }
  80.  
  81. Chart.prototype.draw_boxes = function(){
  82. var boxes = new Array();
  83.  
  84. for(i=0; i < this.num_boxes; i++){
  85. var x = box_offset * i;
  86. var y = (timeline_size * 3) + (box_offset * i);
  87. boxes[i] = new Box(x, y, 200, box_height, this.canvas);
  88. }
  89.  
  90. return boxes;
  91. }
  92.  
  93.  
  94.  
  95. // Timeline class
  96. function Timeline(num_days, chart){
  97. this.chart = chart;
  98. this.canvas = this.chart.canvas;
  99. this.num_days = num_days;
  100.  
  101. return this.draw();
  102. }
  103.  
  104. Timeline.prototype.draw = function(start, end){
  105. this.draw_months();
  106. this.draw_days();
  107. return this;
  108. }
  109.  
  110. Timeline.prototype.draw_months = function(){
  111. var start_date = this.chart.start_date;
  112. var end_date = this.chart.end_date;
  113. var num_months = start_date.monthsUntil(end_date);
  114. var month_width = this.chart.width / num_months;
  115.  
  116. for(i=0; i<num_months; i++){
  117. var curr_date = start_date.add(i).months();
  118. var x = (i * month_width);
  119.  
  120. new Month(x, timeline_size, month_width, curr_date, this.canvas);
  121. }
  122.  
  123. }
  124.  
  125. Timeline.prototype.draw_days = function(){
  126. var days = new Array("S","M","T","W","T","F","S");
  127. var y = timeline_size;
  128.  
  129. for(i=0; i<this.num_days; i+=1){
  130. var x = i * timeline_size;
  131. var label = days[i%7];
  132. new Day(x, y, label, this.canvas);
  133. }
  134. }
  135.  
  136.  
  137.  
  138. // Month class
  139. function Month(x, y, w, label, canvas){
  140. this.x = x;
  141. this.y = y;
  142. this.w = w;
  143. this.label = label;
  144. this.label_x = this.x + (this.w / 2)
  145. this.canvas = canvas;
  146.  
  147. return this.draw();
  148. }
  149.  
  150. Month.prototype.draw = function(){
  151. this.canvas.rect(this.x, -1, this.w, timeline_size + 1).attr({fill: "#BD0000", stroke: "#FFF"});
  152. this.canvas.text(this.label_x, this.y / 2, this.label).attr({fill: "#FFF"});
  153. return this;
  154. }
  155.  
  156.  
  157.  
  158. // Day class
  159. function Day(x, y, text, canvas){
  160. this.x = x;
  161. this.y = y;
  162. this.text = text;
  163. this.canvas = canvas;
  164.  
  165. return this.draw();
  166. }
  167.  
  168. Day.prototype.center_point = function(){
  169. var x = this.x + (timeline_size / 2);
  170. var y = this.y + (timeline_size / 2) + 2;
  171.  
  172. return new Point(x,y);
  173. }
  174.  
  175. Day.prototype.draw = function(){
  176. var p = this.center_point();
  177.  
  178. this.canvas.rect(this.x, this.y, timeline_size, timeline_size).attr({fill: "#efefef"});
  179. this.canvas.text(p.x, p.y, this.text);
  180.  
  181. return this;
  182. }
  183.  
  184.  
  185.  
  186. // Point class
  187. function Point(x,y){
  188. this.x = parseInt(x);
  189. this.y = parseInt(y);
  190. }
  191.  
  192. Point.prototype.toString = function(){
  193. return this.x +","+ this.y;
  194. }
  195.  
  196. Point.prototype.shift = function(x,y){
  197. var x = (this.x + x);
  198. var y = (this.y + y);
  199. return new Point(x,y);
  200. }
  201.  
  202. Point.prototype.draw = function(canvas){
  203. canvas.text(this.x, (this.y - 10), "("+ this +")").attr({fill: "red"});
  204. canvas.circle(this.x, this.y, 3).attr({fill: "red", stroke: "#FFF"});
  205.  
  206. return this;
  207. }
  208.  
  209.  
  210. // Box class
  211. function Box(x,y,w,h,canvas){
  212. this.canvas = canvas;
  213. this.shape = this.canvas.rect(x,y,w,h,box_radius);
  214. this.x = x;
  215. this.y = y;
  216. this.w = w;
  217. this.h = h;
  218.  
  219. this.shape.attr({fill: box_color, stroke: "#999"});
  220.  
  221. return this;
  222. }
  223.  
  224. Box.prototype.bottom_left_point = function(){
  225. var x = this.x + small_offset;
  226. var y = this.y + this.shape.getBBox().height;
  227.  
  228. return new Point(x,y);
  229. }
  230.  
  231. Box.prototype.left_center_point = function(){
  232. var y = this.y + (this.shape.getBBox().height / 2);
  233.  
  234. return new Point(this.x, y);
  235. }
  236.  
  237. Box.prototype.right_center_point = function(){
  238. var x = this.x + this.shape.getBBox().width;
  239. var y = this.left_center_point().y;
  240.  
  241. return new Point(x, y);
  242. }
  243.  
  244. Box.prototype.points_to = function(arr){
  245. if(arr.length == undefined){
  246. arr = new Array(arr);
  247. }
  248.  
  249. for(i=0; i < arr.length; i++){
  250. new Arrow(this, arr[i], this.shape.paper);
  251. }
  252.  
  253. return this;
  254. }
  255.  
  256. Box.prototype.says = function(text){
  257. var p = this.right_center_point();
  258. var t = this.shape.paper.text(p.x, p.y, text);
  259. var offset = (t.getBBox().width / 2) + small_offset;
  260.  
  261. t.translate(offset, 0).attr({fill: "#000"}).toFront;
  262.  
  263. return this;
  264. }
  265.  
  266. Box.prototype.is_this_complete = function(percentage){
  267. var x = this.x + 2;
  268. var y = this.y + 2;
  269. var h = box_height - 4;
  270. var w = this.shape.getBBox().width * percentage;
  271.  
  272. this.shape.paper.rect(x, y, w, h, box_radius).attr({gradient: box_gradient, "stroke-width": 0}).toFront();
  273.  
  274. return this;
  275. }
  276.  
  277.  
  278.  
  279. // Arrow class
  280. function Arrow(start_box, end_box, canvas){
  281. this.from = start_box;
  282. this.to = end_box;
  283. this.canvas = canvas;
  284.  
  285. var str = "";
  286.  
  287. str = str.concat("M"+ this.from.bottom_left_point());
  288. str = str.concat("L"+ this.crux_point());
  289. str = str.concat("L"+ this.to.left_center_point());
  290.  
  291. this.canvas.path(str).toBack();
  292. this.nib();
  293.  
  294. return this;
  295. }
  296.  
  297. Arrow.prototype.crux_point = function(){
  298. var x = this.from.bottom_left_point().x;
  299. var y = this.to.left_center_point().y;
  300.  
  301. return new Point(x, y);
  302. }
  303.  
  304. Arrow.prototype.nib = function(){
  305. var point = this.to.left_center_point();
  306. var str = "";
  307. var tip = point.shift(-2, 0);
  308. var top = tip.shift(-3, -3);
  309. var bottom = tip.shift(-3, 3);
  310.  
  311. str = str.concat("M"+ tip);
  312. str = str.concat("L"+ top);
  313. str = str.concat("L"+ bottom);
  314. str = str.concat("L"+ tip);
  315.  
  316. this.canvas.path(str).attr({fill: "#000"}).toBack();
  317.  
  318. return this;
  319. }
  320.  
  321.  
  322.  
  323. // Helper function
  324. function interrogate(obj){
  325. var output = '';
  326. for(var m in obj){
  327. output += m + ', ';
  328. }
  329. alert(output);
  330. }
  331.  
  332.  
  333.  
  334. $(document).ready(function(){
  335. var start_date = new Date("Nov 1, 2009");
  336. var end_date = new Date("Dec 31, 2009");
  337. var chart = new Chart(10, start_date, end_date);
  338. var boxes = chart.boxes;
  339.  
  340. boxes[0].points_to(new Array(boxes[1], boxes[7])).is_this_complete(0.5).says("Testing...");
  341. boxes[1].points_to(boxes[2]).is_this_complete(0.3).says("More arrows!");
  342. boxes[5].points_to(boxes[9]).is_this_complete(0.9).says("Let's see how this looks");
  343. });
Add Comment
Please, Sign In to add comment