Calendar.js 14 KB

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