Web Interface For Lite Argus Control modules
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.

389 lines
13 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /* Copyright 2014 Gagarine Yaikhom (The MIT License) */
  2. (function() {
  3. ActivityMap = function(data, config) {
  4. /* Contains an array of data points in the following format:
  5. [
  6. {
  7. "t": 1393009249000, // timestamp in milliseconds, or JS Date() object
  8. "v": 3, // value
  9. } ...
  10. ]
  11. */
  12. this.data = data;
  13. this.id = 'example-activity-map';
  14. this.parent = 'body';
  15. this.hue = 85;
  16. this.title = 'Activity map: ';
  17. this.showTitle = true;
  18. this.showYears = true;
  19. this.timeColumn = 't';
  20. this.valueColumn = 'v';
  21. this.fit = false;
  22. this.compact = true;
  23. if (config) {
  24. if (config.id !== undefined)
  25. this.id = config.id;
  26. if (config.parent !== undefined)
  27. this.parent = config.parent;
  28. if (config.colours !== undefined)
  29. this.colours = config.colours;
  30. if (config.title !== undefined)
  31. this.title = config.title;
  32. if (config.timeColumn !== undefined)
  33. this.timeColumn = config.timeColumn;
  34. if (config.valueColumn !== undefined)
  35. this.valueColumn = config.valueColumn;
  36. if (config.fit !== undefined)
  37. this.fit = config.fit;
  38. if (config.compact !== undefined)
  39. this.compact = config.compact;
  40. if (config.hue !== undefined) {
  41. if (isNaN(config.hue))
  42. throw 'Invalid Hue specification: should be an integer';
  43. else if (config.hue < 0 || config.hue > 360)
  44. throw 'Invalid Hue specification: should be >= 0 and <= 360';
  45. else
  46. this.hue = config.hue;
  47. }
  48. if( config.showTitle != undefined ) this.showTitle = config.showTitle;
  49. if( config.showYears != undefined ) this.showYears = config.showYears;
  50. }
  51. this.luminosityScale = d3.scaleLinear();
  52. this.init();
  53. };
  54. ActivityMap.prototype = {
  55. init: function() {
  56. var me = this, parent;
  57. me.process();
  58. if (typeof me.parent === 'string')
  59. parent = d3.select(me.parent);
  60. me.node = parent.append('div')
  61. .attr('id', me.id)
  62. .attr('class', 'activity-map');
  63. me.parent = parent;
  64. me.months = {
  65. '0': {
  66. 'n': 31,
  67. 'l': 'January'
  68. },
  69. '1': {
  70. 'n': 28,
  71. 'l': 'February'
  72. },
  73. '2': {
  74. 'n': 31,
  75. 'l': 'March'
  76. },
  77. '3': {
  78. 'n': 30,
  79. 'l': 'April'
  80. },
  81. '4': {
  82. 'n': 31,
  83. 'l': 'May'
  84. },
  85. '5': {
  86. 'n': 30,
  87. 'l': 'June'
  88. },
  89. '6': {
  90. 'n': 31,
  91. 'l': 'July'
  92. },
  93. '7': {
  94. 'n': 31,
  95. 'l': 'August'
  96. },
  97. '8': {
  98. 'n': 30,
  99. 'l': 'September'
  100. },
  101. '9': {
  102. 'n': 31,
  103. 'l': 'October'
  104. },
  105. '10': {
  106. 'n': 30,
  107. 'l': 'November'
  108. },
  109. '11': {
  110. 'n': 31,
  111. 'l': 'December'
  112. }
  113. };
  114. me.weeks = 'SMTWTFS';
  115. },
  116. reorderColours: function() {
  117. var me = this, c = me.colours, t, i = 0, j = c.length - 1;
  118. while (i < j) {
  119. t = c[i];
  120. c[i++] = c[j];
  121. c[j--] = t;
  122. }
  123. },
  124. process: function() {
  125. var me = this, data = me.data, record, ltab = { }, lts = {},
  126. y, m, d, i, c, t, v, years = [ ], temp = { },
  127. minY = Number.MAX_VALUE, maxY = 0,
  128. minV = Number.MAX_VALUE, maxV = 0,
  129. tc = me.timeColumn, vc = me.valueColumn;
  130. /* Create a three-dimensional lookup table from the array
  131. of activity data points */
  132. for (i = 0, c = data.length; i < c; ++i) {
  133. record = data[i];
  134. if (record) {
  135. t = record[tc];
  136. if (!(t instanceof Date))
  137. t = new Date(parseInt(t));
  138. y = t.getFullYear();
  139. m = t.getMonth();
  140. d = t.getDate() - 1;
  141. v = parseFloat(record[vc]);
  142. if (ltab[y] === undefined){
  143. ltab[y] = { };
  144. lts[y] = {};
  145. };
  146. if (ltab[y][m] === undefined){
  147. ltab[y][m] = { };
  148. lts[y][m] = {};
  149. };
  150. if (ltab[y][m][d] === undefined){
  151. ltab[y][m][d] = v;
  152. lts[y][m][d] = parseInt(record[tc]) / 1000;
  153. };
  154. /* Get minimum and maximum years that defines the
  155. activity data range */
  156. if (minY > y)
  157. minY = y;
  158. if (maxY < y)
  159. maxY = y;
  160. if (minV > v)
  161. minV = v;
  162. if (maxV < v)
  163. maxV = v;
  164. /* Make an array of the years in range */
  165. if (temp[y] === undefined) {
  166. temp[y] = y;
  167. years.push(y);
  168. }
  169. }
  170. }
  171. me.luminosityScale.domain([0, maxV]);
  172. me.luminosityScale.range([100, 0]);
  173. me.processed = {
  174. 'm': minY,
  175. 'M': maxY,
  176. 'v': minV,
  177. 'V': maxV,
  178. 'y': years,
  179. 'l': ltab,
  180. 't': lts
  181. };
  182. },
  183. fillWeekDayInitials: function(parent) {
  184. var me = this, i;
  185. for (i = 0; i < 7; ++i)
  186. parent.append('div')
  187. .attr('class', 'amap-week-name')
  188. .text(me.weeks[i]);
  189. },
  190. addWeekDayContainer: function(parent) {
  191. return parent.append('div')
  192. .attr('class', 'amap-week-days');
  193. },
  194. renderMonth: function(node, y, m, d) {
  195. var me = this, i, j, w, v, t, month = me.months[m],
  196. ltab = me.processed.l, colours = me.colours,
  197. percent;
  198. if (me.compact) {
  199. node.classed('amap-compact', true);
  200. n = node.append('div')
  201. .attr('class', 'amap-month-days');
  202. n.append('div')
  203. .attr('class', 'amap-month-name')
  204. .text(month.l.substring(0,3));
  205. if (me.cellCount === 0) {
  206. week = me.addWeekDayContainer(n);
  207. me.fillWeekDayInitials(week);
  208. week = me.addWeekDayContainer(n);
  209. /* Fill this with empty cells, so that the valid days are
  210. aligned correctly to the week-days */
  211. for (i = 0; i < d; ++i, ++me.cellCount)
  212. week.append('div')
  213. .attr('class', 'amap-empty-day');
  214. }
  215. } else {
  216. n = node.append('div')
  217. .attr('class', 'amap-month');
  218. n.append('div')
  219. .attr('class', 'amap-month-name')
  220. .text(month.l);
  221. n = n.append('div')
  222. .attr('class', 'amap-month-days');
  223. week = me.addWeekDayContainer(n);
  224. me.fillWeekDayInitials(week);
  225. week = me.addWeekDayContainer(n);
  226. /* Fill this with empty cells, so that the valid days are
  227. aligned correctly to the week-days */
  228. for (i = 0; i < d; ++i)
  229. week.append('div')
  230. .attr('class', 'amap-empty-day')
  231. .attr('title', (j + 1) + ' '
  232. + month.l + ' ' + y);
  233. }
  234. /* Now add cells for the valid days */
  235. for (j = 0, d = month.n; j < d; ++i, ++j) {
  236. if (me.cellCount % 7 === 0)
  237. week = n.append('div')
  238. .attr('class', 'amap-week-days');
  239. try {
  240. v = ltab[y][m][j];
  241. t = me.processed.t[y][m][j];
  242. } catch(e) {
  243. t = -1;
  244. }
  245. if( t > 0 ){
  246. w = week.append('div')
  247. .attr('class', 'amap-week-day')
  248. .attr('title', (j + 1) + ' '
  249. + month.l + ' ' + y +
  250. (v ? (' : ' + v) : ''))
  251. .attr('onclick', 'loadReportDayTree('+t+');');
  252. } else {
  253. w = week.append('div')
  254. .attr('class', 'amap-week-day')
  255. .attr('title', (j + 1) + ' '
  256. + month.l + ' ' + y +
  257. (v ? (' : ' + v) : ''));
  258. };
  259. me.cellCount++;
  260. if (i % 7 === 0)
  261. w.classed('amap-week-start', true);
  262. if (v) {
  263. percent = me.luminosityScale(v) + '%';
  264. w.classed('has-value', true)
  265. .style('background-color',
  266. d3.hsl('hsl('
  267. + me.hue + ','
  268. + 100 + '%,'
  269. + percent + ')')
  270. );
  271. }
  272. }
  273. /* weekday for next month */
  274. d = i % 7;
  275. if (!me.compact) {
  276. /* Fill remaining invalid days with empty cells */
  277. while (i++ % 7)
  278. week.append('div')
  279. .attr('class', 'amap-empty-day')
  280. .attr('title', (j + 1) + ' '
  281. + month.l + ' ' + y);
  282. }
  283. return d;
  284. },
  285. renderYear: function(y) {
  286. var me = this, d, i, m, n;
  287. /* Account for leap-year */
  288. me.months[1].n = y % 4 ? 28 : 29;
  289. n = me.blocks.append('div')
  290. .attr('class', 'amap-year-block')
  291. .attr('year', y);
  292. if( me.showYears ){
  293. n.append('div')
  294. .attr('class', 'amap-year-name')
  295. .text(y);
  296. };
  297. m = n.append('div')
  298. .attr('class', 'amap-month-names')
  299. n = n.append('div')
  300. .attr('class', 'amap-months');
  301. /* Find at what week day the first month of year begins */
  302. d = new Date(y, 0, 1);
  303. d = d.getDay();
  304. for (i = 0; i < 12; ++i)
  305. d = me.renderMonth(n, y, i, d);
  306. if (me.compact) {
  307. n = n.append('div')
  308. .attr('class', 'amap-month-days');
  309. n.append('div')
  310. .attr('class', 'amap-month-name-filler');
  311. week = me.addWeekDayContainer(n);
  312. me.fillWeekDayInitials(week);
  313. }
  314. },
  315. isVisible: function(block, parent) {
  316. var yearDim = block.getBoundingClientRect(),
  317. parentDim = parent.getBoundingClientRect();
  318. return !(yearDim.top > parentDim.bottom ||
  319. yearDim.bottom < parentDim.top);
  320. },
  321. onScroll: function(me) {
  322. var minYear = Number.MAX_VALUE, maxYear = 0, year;
  323. me.blocks.selectAll('.amap-year-block')
  324. .each(function() {
  325. if (me.isVisible(this, me.blocks.node())) {
  326. year = parseInt(d3.select(this).attr('year'));
  327. if (minYear > year)
  328. minYear = year;
  329. if (maxYear < year)
  330. maxYear = year;
  331. }
  332. });
  333. if( me.showTitle ){
  334. if (minYear === maxYear)
  335. me.year.text(me.title + maxYear);
  336. else
  337. me.year.text(me.title + minYear + '-' + maxYear);
  338. };
  339. },
  340. render: function() {
  341. var me = this, i, c, years = me.processed.y;
  342. if( me.showTitle ){
  343. me.year = me.node.append('div')
  344. .attr('class', 'amap-title');
  345. };
  346. me.blocks = me.node.append('div')
  347. .attr('class', 'amap-year-blocks');
  348. me.blocks.on('scroll', function() {
  349. me.onScroll(me);
  350. });
  351. for (i = 0, c = years.length; i < c; ++i) {
  352. me.cellCount = 0;
  353. me.renderYear(years[i]);
  354. }
  355. me.onScroll(me);
  356. me.refit();
  357. },
  358. refit: function() {
  359. var me = this;
  360. if (!me.fit)
  361. me.blocks.style('height',
  362. (parseInt(me.node.style('height'))
  363. - parseInt(me.year.style('height'))) + 'px');
  364. }
  365. };
  366. })();