Calendar.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. var zrUtil = require("zrender/lib/core/util");
  2. var layout = require("../../util/layout");
  3. var numberUtil = require("../../util/number");
  4. var CoordinateSystem = require("../../CoordinateSystem");
  5. // (24*60*60*1000)
  6. var PROXIMATE_ONE_DAY = 86400000;
  7. /**
  8. * Calendar
  9. *
  10. * @constructor
  11. *
  12. * @param {Object} calendarModel calendarModel
  13. * @param {Object} ecModel ecModel
  14. * @param {Object} api api
  15. */
  16. function Calendar(calendarModel, ecModel, api) {
  17. this._model = calendarModel;
  18. }
  19. Calendar.prototype = {
  20. constructor: Calendar,
  21. type: 'calendar',
  22. dimensions: ['time', 'value'],
  23. // Required in createListFromData
  24. getDimensionsInfo: function () {
  25. return [{
  26. name: 'time',
  27. type: 'time'
  28. }];
  29. },
  30. getRangeInfo: function () {
  31. return this._rangeInfo;
  32. },
  33. getModel: function () {
  34. return this._model;
  35. },
  36. getRect: function () {
  37. return this._rect;
  38. },
  39. getCellWidth: function () {
  40. return this._sw;
  41. },
  42. getCellHeight: function () {
  43. return this._sh;
  44. },
  45. getOrient: function () {
  46. return this._orient;
  47. },
  48. /**
  49. * getFirstDayOfWeek
  50. *
  51. * @example
  52. * 0 : start at Sunday
  53. * 1 : start at Monday
  54. *
  55. * @return {number}
  56. */
  57. getFirstDayOfWeek: function () {
  58. return this._firstDayOfWeek;
  59. },
  60. /**
  61. * get date info
  62. *
  63. * @param {string|number} date date
  64. * @return {Object}
  65. * {
  66. * y: string, local full year, eg., '1940',
  67. * m: string, local month, from '01' ot '12',
  68. * d: string, local date, from '01' to '31' (if exists),
  69. * day: It is not date.getDay(). It is the location of the cell in a week, from 0 to 6,
  70. * time: timestamp,
  71. * formatedDate: string, yyyy-MM-dd,
  72. * date: original date object.
  73. * }
  74. */
  75. getDateInfo: function (date) {
  76. date = numberUtil.parseDate(date);
  77. var y = date.getFullYear();
  78. var m = date.getMonth() + 1;
  79. m = m < 10 ? '0' + m : m;
  80. var d = date.getDate();
  81. d = d < 10 ? '0' + d : d;
  82. var day = date.getDay();
  83. day = Math.abs((day + 7 - this.getFirstDayOfWeek()) % 7);
  84. return {
  85. y: y,
  86. m: m,
  87. d: d,
  88. day: day,
  89. time: date.getTime(),
  90. formatedDate: y + '-' + m + '-' + d,
  91. date: date
  92. };
  93. },
  94. getNextNDay: function (date, n) {
  95. n = n || 0;
  96. if (n === 0) {
  97. return this.getDateInfo(date);
  98. }
  99. date = new Date(this.getDateInfo(date).time);
  100. date.setDate(date.getDate() + n);
  101. return this.getDateInfo(date);
  102. },
  103. update: function (ecModel, api) {
  104. this._firstDayOfWeek = +this._model.getModel('dayLabel').get('firstDay');
  105. this._orient = this._model.get('orient');
  106. this._lineWidth = this._model.getModel('itemStyle.normal').getItemStyle().lineWidth || 0;
  107. this._rangeInfo = this._getRangeInfo(this._initRangeOption());
  108. var weeks = this._rangeInfo.weeks || 1;
  109. var whNames = ['width', 'height'];
  110. var cellSize = this._model.get('cellSize').slice();
  111. var layoutParams = this._model.getBoxLayoutParams();
  112. var cellNumbers = this._orient === 'horizontal' ? [weeks, 7] : [7, weeks];
  113. zrUtil.each([0, 1], function (idx) {
  114. if (cellSizeSpecified(cellSize, idx)) {
  115. layoutParams[whNames[idx]] = cellSize[idx] * cellNumbers[idx];
  116. }
  117. });
  118. var whGlobal = {
  119. width: api.getWidth(),
  120. height: api.getHeight()
  121. };
  122. var calendarRect = this._rect = layout.getLayoutRect(layoutParams, whGlobal);
  123. zrUtil.each([0, 1], function (idx) {
  124. if (!cellSizeSpecified(cellSize, idx)) {
  125. cellSize[idx] = calendarRect[whNames[idx]] / cellNumbers[idx];
  126. }
  127. });
  128. function cellSizeSpecified(cellSize, idx) {
  129. return cellSize[idx] != null && cellSize[idx] !== 'auto';
  130. }
  131. this._sw = cellSize[0];
  132. this._sh = cellSize[1];
  133. },
  134. /**
  135. * Convert a time data(time, value) item to (x, y) point.
  136. *
  137. * @override
  138. * @param {Array|number} data data
  139. * @param {boolean} [clamp=true] out of range
  140. * @return {Array} point
  141. */
  142. dataToPoint: function (data, clamp) {
  143. zrUtil.isArray(data) && (data = data[0]);
  144. clamp == null && (clamp = true);
  145. var dayInfo = this.getDateInfo(data);
  146. var range = this._rangeInfo;
  147. var date = dayInfo.formatedDate; // if not in range return [NaN, NaN]
  148. if (clamp && !(dayInfo.time >= range.start.time && dayInfo.time <= range.end.time)) {
  149. return [NaN, NaN];
  150. }
  151. var week = dayInfo.day;
  152. var nthWeek = this._getRangeInfo([range.start.time, date]).nthWeek;
  153. if (this._orient === 'vertical') {
  154. return [this._rect.x + week * this._sw + this._sw / 2, this._rect.y + nthWeek * this._sh + this._sh / 2];
  155. }
  156. return [this._rect.x + nthWeek * this._sw + this._sw / 2, this._rect.y + week * this._sh + this._sh / 2];
  157. },
  158. /**
  159. * Convert a (x, y) point to time data
  160. *
  161. * @override
  162. * @param {string} point point
  163. * @return {string} data
  164. */
  165. pointToData: function (point) {
  166. var date = this.pointToDate(point);
  167. return date && date.time;
  168. },
  169. /**
  170. * Convert a time date item to (x, y) four point.
  171. *
  172. * @param {Array} data date[0] is date
  173. * @param {boolean} [clamp=true] out of range
  174. * @return {Object} point
  175. */
  176. dataToRect: function (data, clamp) {
  177. var point = this.dataToPoint(data, clamp);
  178. return {
  179. contentShape: {
  180. x: point[0] - (this._sw - this._lineWidth) / 2,
  181. y: point[1] - (this._sh - this._lineWidth) / 2,
  182. width: this._sw - this._lineWidth,
  183. height: this._sh - this._lineWidth
  184. },
  185. center: point,
  186. tl: [point[0] - this._sw / 2, point[1] - this._sh / 2],
  187. tr: [point[0] + this._sw / 2, point[1] - this._sh / 2],
  188. br: [point[0] + this._sw / 2, point[1] + this._sh / 2],
  189. bl: [point[0] - this._sw / 2, point[1] + this._sh / 2]
  190. };
  191. },
  192. /**
  193. * Convert a (x, y) point to time date
  194. *
  195. * @param {Array} point point
  196. * @return {Object} date
  197. */
  198. pointToDate: function (point) {
  199. var nthX = Math.floor((point[0] - this._rect.x) / this._sw) + 1;
  200. var nthY = Math.floor((point[1] - this._rect.y) / this._sh) + 1;
  201. var range = this._rangeInfo.range;
  202. if (this._orient === 'vertical') {
  203. return this._getDateByWeeksAndDay(nthY, nthX - 1, range);
  204. }
  205. return this._getDateByWeeksAndDay(nthX, nthY - 1, range);
  206. },
  207. /**
  208. * @inheritDoc
  209. */
  210. convertToPixel: zrUtil.curry(doConvert, 'dataToPoint'),
  211. /**
  212. * @inheritDoc
  213. */
  214. convertFromPixel: zrUtil.curry(doConvert, 'pointToData'),
  215. /**
  216. * initRange
  217. *
  218. * @private
  219. * @return {Array} [start, end]
  220. */
  221. _initRangeOption: function () {
  222. var range = this._model.get('range');
  223. var rg = range;
  224. if (zrUtil.isArray(rg) && rg.length === 1) {
  225. rg = rg[0];
  226. }
  227. if (/^\d{4}$/.test(rg)) {
  228. range = [rg + '-01-01', rg + '-12-31'];
  229. }
  230. if (/^\d{4}[\/|-]\d{1,2}$/.test(rg)) {
  231. var start = this.getDateInfo(rg);
  232. var firstDay = start.date;
  233. firstDay.setMonth(firstDay.getMonth() + 1);
  234. var end = this.getNextNDay(firstDay, -1);
  235. range = [start.formatedDate, end.formatedDate];
  236. }
  237. if (/^\d{4}[\/|-]\d{1,2}[\/|-]\d{1,2}$/.test(rg)) {
  238. range = [rg, rg];
  239. }
  240. var tmp = this._getRangeInfo(range);
  241. if (tmp.start.time > tmp.end.time) {
  242. range.reverse();
  243. }
  244. return range;
  245. },
  246. /**
  247. * range info
  248. *
  249. * @private
  250. * @param {Array} range range ['2017-01-01', '2017-07-08']
  251. * If range[0] > range[1], they will not be reversed.
  252. * @return {Object} obj
  253. */
  254. _getRangeInfo: function (range) {
  255. range = [this.getDateInfo(range[0]), this.getDateInfo(range[1])];
  256. var reversed;
  257. if (range[0].time > range[1].time) {
  258. reversed = true;
  259. range.reverse();
  260. }
  261. var allDay = Math.floor(range[1].time / PROXIMATE_ONE_DAY) - Math.floor(range[0].time / PROXIMATE_ONE_DAY) + 1; // Consider case:
  262. // Firstly set system timezone as "Time Zone: America/Toronto",
  263. // ```
  264. // var first = new Date(1478412000000 - 3600 * 1000 * 2.5);
  265. // var second = new Date(1478412000000);
  266. // var allDays = Math.floor(second / ONE_DAY) - Math.floor(first / ONE_DAY) + 1;
  267. // ```
  268. // will get wrong result because of DST. So we should fix it.
  269. var date = new Date(range[0].time);
  270. var startDateNum = date.getDate();
  271. var endDateNum = range[1].date.getDate();
  272. date.setDate(startDateNum + allDay - 1); // The bias can not over a month, so just compare date.
  273. if (date.getDate() !== endDateNum) {
  274. var sign = date.getTime() - range[1].time > 0 ? 1 : -1;
  275. while (date.getDate() !== endDateNum && (date.getTime() - range[1].time) * sign > 0) {
  276. allDay -= sign;
  277. date.setDate(startDateNum + allDay - 1);
  278. }
  279. }
  280. var weeks = Math.floor((allDay + range[0].day + 6) / 7);
  281. var nthWeek = reversed ? -weeks + 1 : weeks - 1;
  282. reversed && range.reverse();
  283. return {
  284. range: [range[0].formatedDate, range[1].formatedDate],
  285. start: range[0],
  286. end: range[1],
  287. allDay: allDay,
  288. weeks: weeks,
  289. // From 0.
  290. nthWeek: nthWeek,
  291. fweek: range[0].day,
  292. lweek: range[1].day
  293. };
  294. },
  295. /**
  296. * get date by nthWeeks and week day in range
  297. *
  298. * @private
  299. * @param {number} nthWeek the week
  300. * @param {number} day the week day
  301. * @param {Array} range [d1, d2]
  302. * @return {Object}
  303. */
  304. _getDateByWeeksAndDay: function (nthWeek, day, range) {
  305. var rangeInfo = this._getRangeInfo(range);
  306. if (nthWeek > rangeInfo.weeks || nthWeek === 0 && day < rangeInfo.fweek || nthWeek === rangeInfo.weeks && day > rangeInfo.lweek) {
  307. return false;
  308. }
  309. var nthDay = (nthWeek - 1) * 7 - rangeInfo.fweek + day;
  310. var date = new Date(rangeInfo.start.time);
  311. date.setDate(rangeInfo.start.d + nthDay);
  312. return this.getDateInfo(date);
  313. }
  314. };
  315. Calendar.dimensions = Calendar.prototype.dimensions;
  316. Calendar.getDimensionsInfo = Calendar.prototype.getDimensionsInfo;
  317. Calendar.create = function (ecModel, api) {
  318. var calendarList = [];
  319. ecModel.eachComponent('calendar', function (calendarModel) {
  320. var calendar = new Calendar(calendarModel, ecModel, api);
  321. calendarList.push(calendar);
  322. calendarModel.coordinateSystem = calendar;
  323. });
  324. ecModel.eachSeries(function (calendarSeries) {
  325. if (calendarSeries.get('coordinateSystem') === 'calendar') {
  326. // Inject coordinate system
  327. calendarSeries.coordinateSystem = calendarList[calendarSeries.get('calendarIndex') || 0];
  328. }
  329. });
  330. return calendarList;
  331. };
  332. function doConvert(methodName, ecModel, finder, value) {
  333. var calendarModel = finder.calendarModel;
  334. var seriesModel = finder.seriesModel;
  335. var coordSys = calendarModel ? calendarModel.coordinateSystem : seriesModel ? seriesModel.coordinateSystem : null;
  336. return coordSys === this ? coordSys[methodName](value) : null;
  337. }
  338. CoordinateSystem.register('calendar', Calendar);
  339. var _default = Calendar;
  340. module.exports = _default;