Calendar.js 12 KB

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