Calendar.js 13 KB

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