UNPKG

42.3 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, '__esModule', { value: true });
4
5function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
6
7var _setDisplayName = _interopDefault(require('recompose/setDisplayName'));
8var React = require('react');
9var React__default = _interopDefault(React);
10var core = require('@nivo/core');
11var legends = require('@nivo/legends');
12var PropTypes = _interopDefault(require('prop-types'));
13var d3TimeFormat = require('d3-time-format');
14var _pure = _interopDefault(require('recompose/pure'));
15var _withPropsOnChange = _interopDefault(require('recompose/withPropsOnChange'));
16var _defaultProps = _interopDefault(require('recompose/defaultProps'));
17var _compose = _interopDefault(require('recompose/compose'));
18var d3Scale = require('d3-scale');
19var memoize = _interopDefault(require('lodash.memoize'));
20var isDate = _interopDefault(require('lodash.isdate'));
21var range = _interopDefault(require('lodash.range'));
22var d3Time = require('d3-time');
23var tooltip = require('@nivo/tooltip');
24
25function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; var ownKeys = Object.keys(source); if (typeof Object.getOwnPropertySymbols === 'function') { ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) { return Object.getOwnPropertyDescriptor(source, sym).enumerable; })); } ownKeys.forEach(function (key) { _defineProperty(target, key, source[key]); }); } return target; }
26function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
27var monthLabelFormat = d3TimeFormat.timeFormat('%b');
28var commonPropTypes = {
29 from: PropTypes.oneOfType([PropTypes.string, PropTypes.instanceOf(Date)]).isRequired,
30 to: PropTypes.oneOfType([PropTypes.string, PropTypes.instanceOf(Date)]).isRequired,
31 data: PropTypes.arrayOf(PropTypes.shape({
32 day: PropTypes.string.isRequired,
33 value: PropTypes.number.isRequired
34 })).isRequired,
35 align: PropTypes.oneOf(core.boxAlignments).isRequired,
36 originX: PropTypes.number.isRequired,
37 originY: PropTypes.number.isRequired,
38 calendarWidth: PropTypes.number.isRequired,
39 calendarHeight: PropTypes.number.isRequired,
40 minValue: PropTypes.oneOfType([PropTypes.oneOf(['auto']), PropTypes.number]).isRequired,
41 maxValue: PropTypes.oneOfType([PropTypes.oneOf(['auto']), PropTypes.number]).isRequired,
42 colors: PropTypes.arrayOf(PropTypes.string).isRequired,
43 colorScale: PropTypes.func.isRequired,
44 direction: PropTypes.oneOf(['horizontal', 'vertical']),
45 emptyColor: PropTypes.string.isRequired,
46 yearLegend: PropTypes.func.isRequired,
47 yearSpacing: PropTypes.number.isRequired,
48 yearLegendPosition: PropTypes.oneOf(['before', 'after']).isRequired,
49 yearLegendOffset: PropTypes.number.isRequired,
50 monthBorderWidth: PropTypes.number.isRequired,
51 monthBorderColor: PropTypes.string.isRequired,
52 monthLegend: PropTypes.func.isRequired,
53 monthLegendPosition: PropTypes.oneOf(['before', 'after']).isRequired,
54 monthLegendOffset: PropTypes.number.isRequired,
55 daySpacing: PropTypes.number.isRequired,
56 dayBorderWidth: PropTypes.number.isRequired,
57 dayBorderColor: PropTypes.string.isRequired,
58 isInteractive: PropTypes.bool,
59 onClick: PropTypes.func.isRequired,
60 tooltipFormat: PropTypes.oneOfType([PropTypes.func, PropTypes.string]),
61 tooltip: PropTypes.func,
62 legends: PropTypes.arrayOf(PropTypes.shape(_objectSpread({}, legends.LegendPropShape, {
63 itemCount: PropTypes.number.isRequired
64 }))).isRequired
65};
66var CalendarPropTypes = commonPropTypes;
67var CalendarCanvasPropTypes = _objectSpread({}, commonPropTypes, {
68 pixelRatio: PropTypes.number.isRequired
69});
70var commonDefaultProps = {
71 colors: ['#61cdbb', '#97e3d5', '#e8c1a0', '#f47560'],
72 align: 'center',
73 direction: 'horizontal',
74 emptyColor: '#fff',
75 minValue: 0,
76 maxValue: 'auto',
77 yearSpacing: 30,
78 yearLegend: function yearLegend(year) {
79 return year;
80 },
81 yearLegendPosition: 'before',
82 yearLegendOffset: 10,
83 monthBorderWidth: 2,
84 monthBorderColor: '#000',
85 monthLegend: function monthLegend(year, month, date) {
86 return monthLabelFormat(date);
87 },
88 monthLegendPosition: 'before',
89 monthLegendOffset: 10,
90 weekdayLegend: function weekdayLegend(d) {
91 return d;
92 },
93 daySpacing: 0,
94 dayBorderWidth: 1,
95 dayBorderColor: '#000',
96 isInteractive: true,
97 onClick: core.noop,
98 legends: []
99};
100var CalendarDefaultProps = commonDefaultProps;
101var CalendarCanvasDefaultProps = _objectSpread({}, commonDefaultProps, {
102 pixelRatio: global.window && global.window.devicePixelRatio ? global.window.devicePixelRatio : 1
103});
104
105function _objectSpread$1(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; var ownKeys = Object.keys(source); if (typeof Object.getOwnPropertySymbols === 'function') { ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) { return Object.getOwnPropertyDescriptor(source, sym).enumerable; })); } ownKeys.forEach(function (key) { _defineProperty$1(target, key, source[key]); }); } return target; }
106function _defineProperty$1(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
107function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _nonIterableRest(); }
108function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance"); }
109function _iterableToArrayLimit(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
110function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
111function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _nonIterableSpread(); }
112function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance"); }
113function _iterableToArray(iter) { if (Symbol.iterator in Object(iter) || Object.prototype.toString.call(iter) === "[object Arguments]") return Array.from(iter); }
114function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = new Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } }
115var computeDomain = function computeDomain(data, minSpec, maxSpec) {
116 var allValues = data.map(function (d) {
117 return d.value;
118 });
119 var minValue = minSpec === 'auto' ? Math.min.apply(Math, _toConsumableArray(allValues)) : minSpec;
120 var maxValue = maxSpec === 'auto' ? Math.max.apply(Math, _toConsumableArray(allValues)) : maxSpec;
121 return [minValue, maxValue];
122};
123var computeCellSize = function computeCellSize(_ref) {
124 var width = _ref.width,
125 height = _ref.height,
126 direction = _ref.direction,
127 yearRange = _ref.yearRange,
128 yearSpacing = _ref.yearSpacing,
129 daySpacing = _ref.daySpacing,
130 maxWeeks = _ref.maxWeeks;
131 var hCellSize;
132 var vCellSize;
133 if (direction === 'horizontal') {
134 hCellSize = (width - daySpacing * maxWeeks) / maxWeeks;
135 vCellSize = (height - (yearRange.length - 1) * yearSpacing - yearRange.length * (8 * daySpacing)) / (yearRange.length * 7);
136 } else {
137 hCellSize = (width - (yearRange.length - 1) * yearSpacing - yearRange.length * (8 * daySpacing)) / (yearRange.length * 7);
138 vCellSize = (height - daySpacing * maxWeeks) / maxWeeks;
139 }
140 return Math.min(hCellSize, vCellSize);
141};
142var monthPathAndBBox = function monthPathAndBBox(_ref2) {
143 var date = _ref2.date,
144 cellSize = _ref2.cellSize,
145 yearIndex = _ref2.yearIndex,
146 yearSpacing = _ref2.yearSpacing,
147 daySpacing = _ref2.daySpacing,
148 direction = _ref2.direction,
149 originX = _ref2.originX,
150 originY = _ref2.originY;
151 var t1 = new Date(date.getFullYear(), date.getMonth() + 1, 0);
152 var firstWeek = d3Time.timeWeek.count(d3Time.timeYear(date), date);
153 var lastWeek = d3Time.timeWeek.count(d3Time.timeYear(t1), t1);
154 var firstDay = date.getDay();
155 var lastDay = t1.getDay();
156 var xO = originX;
157 var yO = originY;
158 var yearOffset = yearIndex * (7 * (cellSize + daySpacing) + yearSpacing);
159 if (direction === 'horizontal') {
160 yO += yearOffset;
161 } else {
162 xO += yearOffset;
163 }
164 var path;
165 var bbox = {
166 x: xO,
167 y: yO,
168 width: 0,
169 height: 0
170 };
171 if (direction === 'horizontal') {
172 path = ["M".concat(xO + (firstWeek + 1) * (cellSize + daySpacing), ",").concat(yO + firstDay * (cellSize + daySpacing)), "H".concat(xO + firstWeek * (cellSize + daySpacing), "V").concat(yO + 7 * (cellSize + daySpacing)), "H".concat(xO + lastWeek * (cellSize + daySpacing), "V").concat(yO + (lastDay + 1) * (cellSize + daySpacing)), "H".concat(xO + (lastWeek + 1) * (cellSize + daySpacing), "V").concat(yO), "H".concat(xO + (firstWeek + 1) * (cellSize + daySpacing), "Z")].join('');
173 bbox.x = xO + firstWeek * (cellSize + daySpacing);
174 bbox.width = xO + (lastWeek + 1) * (cellSize + daySpacing) - bbox.x;
175 bbox.height = 7 * (cellSize + daySpacing);
176 } else {
177 path = ["M".concat(xO + firstDay * (cellSize + daySpacing), ",").concat(yO + (firstWeek + 1) * (cellSize + daySpacing)), "H".concat(xO, "V").concat(yO + (lastWeek + 1) * (cellSize + daySpacing)), "H".concat(xO + (lastDay + 1) * (cellSize + daySpacing), "V").concat(yO + lastWeek * (cellSize + daySpacing)), "H".concat(xO + 7 * (cellSize + daySpacing), "V").concat(yO + firstWeek * (cellSize + daySpacing)), "H".concat(xO + firstDay * (cellSize + daySpacing), "Z")].join('');
178 bbox.y = yO + firstWeek * (cellSize + daySpacing);
179 bbox.width = 7 * (cellSize + daySpacing);
180 bbox.height = yO + (lastWeek + 1) * (cellSize + daySpacing) - bbox.y;
181 }
182 return {
183 path: path,
184 bbox: bbox
185 };
186};
187var memoMonthPathAndBBox = memoize(monthPathAndBBox, function (_ref3) {
188 var date = _ref3.date,
189 cellSize = _ref3.cellSize,
190 yearIndex = _ref3.yearIndex,
191 yearSpacing = _ref3.yearSpacing,
192 daySpacing = _ref3.daySpacing,
193 direction = _ref3.direction,
194 originX = _ref3.originX,
195 originY = _ref3.originY;
196 return "".concat(date.toString(), ".").concat(cellSize, ".").concat(yearIndex, ".").concat(yearSpacing, ".").concat(daySpacing, ".").concat(direction, ".").concat(originX, ".").concat(originY);
197});
198var cellPositionHorizontal = function cellPositionHorizontal(cellSize, yearSpacing, daySpacing) {
199 return function (originX, originY, d, yearIndex) {
200 var weekOfYear = d3Time.timeWeek.count(d3Time.timeYear(d), d);
201 return {
202 x: originX + weekOfYear * (cellSize + daySpacing) + daySpacing / 2,
203 y: originY + d.getDay() * (cellSize + daySpacing) + daySpacing / 2 + yearIndex * (yearSpacing + 7 * (cellSize + daySpacing))
204 };
205 };
206};
207var cellPositionVertical = function cellPositionVertical(cellSize, yearSpacing, daySpacing) {
208 return function (originX, originY, d, yearIndex) {
209 var weekOfYear = d3Time.timeWeek.count(d3Time.timeYear(d), d);
210 return {
211 x: originX + d.getDay() * (cellSize + daySpacing) + daySpacing / 2 + yearIndex * (yearSpacing + 7 * (cellSize + daySpacing)),
212 y: originY + weekOfYear * (cellSize + daySpacing) + daySpacing / 2
213 };
214 };
215};
216var dayFormat = d3TimeFormat.timeFormat('%Y-%m-%d');
217var computeLayout = function computeLayout(_ref4) {
218 var width = _ref4.width,
219 height = _ref4.height,
220 from = _ref4.from,
221 to = _ref4.to,
222 direction = _ref4.direction,
223 yearSpacing = _ref4.yearSpacing,
224 daySpacing = _ref4.daySpacing,
225 align = _ref4.align;
226 var fromDate = isDate(from) ? from : new Date(from);
227 var toDate = isDate(to) ? to : new Date(to);
228 var yearRange = range(fromDate.getFullYear(), toDate.getFullYear() + 1);
229 var maxWeeks = Math.max.apply(Math, _toConsumableArray(yearRange.map(function (year) {
230 return d3Time.timeWeeks(new Date(year, 0, 1), new Date(year + 1, 0, 1)).length;
231 }))) + 1;
232 var cellSize = computeCellSize({
233 width: width,
234 height: height,
235 direction: direction,
236 yearRange: yearRange,
237 yearSpacing: yearSpacing,
238 daySpacing: daySpacing,
239 maxWeeks: maxWeeks
240 });
241 var monthsSize = cellSize * maxWeeks + daySpacing * maxWeeks;
242 var yearsSize = (cellSize + daySpacing) * 7 * yearRange.length + yearSpacing * (yearRange.length - 1);
243 var calendarWidth = direction === 'horizontal' ? monthsSize : yearsSize;
244 var calendarHeight = direction === 'horizontal' ? yearsSize : monthsSize;
245 var _alignBox = core.alignBox({
246 x: 0,
247 y: 0,
248 width: calendarWidth,
249 height: calendarHeight
250 }, {
251 x: 0,
252 y: 0,
253 width: width,
254 height: height
255 }, align),
256 _alignBox2 = _slicedToArray(_alignBox, 2),
257 originX = _alignBox2[0],
258 originY = _alignBox2[1];
259 var cellPosition;
260 if (direction === 'horizontal') {
261 cellPosition = cellPositionHorizontal(cellSize, yearSpacing, daySpacing);
262 } else {
263 cellPosition = cellPositionVertical(cellSize, yearSpacing, daySpacing);
264 }
265 var years = [];
266 var months = [];
267 var days = [];
268 yearRange.forEach(function (year, i) {
269 var yearStart = new Date(year, 0, 1);
270 var yearEnd = new Date(year + 1, 0, 1);
271 days = days.concat(d3Time.timeDays(yearStart, yearEnd).map(function (dayDate) {
272 return _objectSpread$1({
273 date: dayDate,
274 day: dayFormat(dayDate),
275 size: cellSize
276 }, cellPosition(originX, originY, dayDate, i));
277 }));
278 var yearMonths = d3Time.timeMonths(yearStart, yearEnd).map(function (monthDate) {
279 return _objectSpread$1({
280 date: monthDate,
281 year: monthDate.getFullYear(),
282 month: monthDate.getMonth()
283 }, memoMonthPathAndBBox({
284 originX: originX,
285 originY: originY,
286 date: monthDate,
287 direction: direction,
288 yearIndex: i,
289 yearSpacing: yearSpacing,
290 daySpacing: daySpacing,
291 cellSize: cellSize
292 }));
293 });
294 months = months.concat(yearMonths);
295 years.push({
296 year: year,
297 bbox: {
298 x: yearMonths[0].bbox.x,
299 y: yearMonths[0].bbox.y,
300 width: yearMonths[11].bbox.x - yearMonths[0].bbox.x + yearMonths[11].bbox.width,
301 height: yearMonths[11].bbox.y - yearMonths[0].bbox.y + yearMonths[11].bbox.height
302 }
303 });
304 });
305 return {
306 years: years,
307 months: months,
308 days: days,
309 cellSize: cellSize,
310 calendarWidth: calendarWidth,
311 calendarHeight: calendarHeight,
312 originX: originX,
313 originY: originY
314 };
315};
316var bindDaysData = function bindDaysData(_ref5) {
317 var days = _ref5.days,
318 data = _ref5.data,
319 colorScale = _ref5.colorScale,
320 emptyColor = _ref5.emptyColor;
321 return days.map(function (day) {
322 day.color = emptyColor;
323 data.forEach(function (dayData) {
324 if (dayData.day === day.day) {
325 day.value = dayData.value;
326 day.color = colorScale(dayData.value);
327 day.data = dayData;
328 }
329 });
330 return day;
331 });
332};
333var computeYearLegendPositions = function computeYearLegendPositions(_ref6) {
334 var years = _ref6.years,
335 direction = _ref6.direction,
336 position = _ref6.position,
337 offset = _ref6.offset;
338 return years.map(function (year) {
339 var x = 0;
340 var y = 0;
341 var rotation = 0;
342 if (direction === 'horizontal' && position === 'before') {
343 x = year.bbox.x - offset;
344 y = year.bbox.y + year.bbox.height / 2;
345 rotation = -90;
346 } else if (direction === 'horizontal' && position === 'after') {
347 x = year.bbox.x + year.bbox.width + offset;
348 y = year.bbox.y + year.bbox.height / 2;
349 rotation = -90;
350 } else if (direction === 'vertical' && position === 'before') {
351 x = year.bbox.x + year.bbox.width / 2;
352 y = year.bbox.y - offset;
353 } else {
354 x = year.bbox.x + year.bbox.width / 2;
355 y = year.bbox.y + year.bbox.height + offset;
356 }
357 return _objectSpread$1({}, year, {
358 x: x,
359 y: y,
360 rotation: rotation
361 });
362 });
363};
364var computeMonthLegendPositions = function computeMonthLegendPositions(_ref7) {
365 var months = _ref7.months,
366 direction = _ref7.direction,
367 position = _ref7.position,
368 offset = _ref7.offset;
369 return months.map(function (month) {
370 var x = 0;
371 var y = 0;
372 var rotation = 0;
373 if (direction === 'horizontal' && position === 'before') {
374 x = month.bbox.x + month.bbox.width / 2;
375 y = month.bbox.y - offset;
376 } else if (direction === 'horizontal' && position === 'after') {
377 x = month.bbox.x + month.bbox.width / 2;
378 y = month.bbox.y + month.bbox.height + offset;
379 } else if (direction === 'vertical' && position === 'before') {
380 x = month.bbox.x - offset;
381 y = month.bbox.y + month.bbox.height / 2;
382 rotation = -90;
383 } else {
384 x = month.bbox.x + month.bbox.width + offset;
385 y = month.bbox.y + month.bbox.height / 2;
386 rotation = -90;
387 }
388 return _objectSpread$1({}, month, {
389 x: x,
390 y: y,
391 rotation: rotation
392 });
393 });
394};
395
396var commonEnhancers = [core.withTheme(), core.withDimensions(), _withPropsOnChange(['data', 'minValue', 'maxValue', 'colors'], function (_ref) {
397 var data = _ref.data,
398 minValue = _ref.minValue,
399 maxValue = _ref.maxValue,
400 colors = _ref.colors;
401 var domain = computeDomain(data, minValue, maxValue);
402 var colorScale = d3Scale.scaleQuantize().domain(domain).range(colors);
403 return {
404 colorScale: colorScale
405 };
406}), _withPropsOnChange(['width', 'height', 'from', 'to', 'direction', 'yearSpacing', 'daySpacing', 'align'], function (_ref2) {
407 var width = _ref2.width,
408 height = _ref2.height,
409 from = _ref2.from,
410 to = _ref2.to,
411 direction = _ref2.direction,
412 yearSpacing = _ref2.yearSpacing,
413 daySpacing = _ref2.daySpacing,
414 align = _ref2.align;
415 return computeLayout({
416 width: width,
417 height: height,
418 from: from,
419 to: to,
420 direction: direction,
421 yearSpacing: yearSpacing,
422 daySpacing: daySpacing,
423 align: align
424 });
425}), _withPropsOnChange(['years', 'direction', 'yearLegendPosition', 'yearLegendOffset'], function (_ref3) {
426 var years = _ref3.years,
427 direction = _ref3.direction,
428 yearLegendPosition = _ref3.yearLegendPosition,
429 yearLegendOffset = _ref3.yearLegendOffset;
430 return {
431 yearLegends: computeYearLegendPositions({
432 years: years,
433 direction: direction,
434 position: yearLegendPosition,
435 offset: yearLegendOffset
436 })
437 };
438}), _withPropsOnChange(['months', 'direction', 'monthLegendPosition', 'monthLegendOffset'], function (_ref4) {
439 var months = _ref4.months,
440 direction = _ref4.direction,
441 monthLegendPosition = _ref4.monthLegendPosition,
442 monthLegendOffset = _ref4.monthLegendOffset;
443 return {
444 monthLegends: computeMonthLegendPositions({
445 months: months,
446 direction: direction,
447 position: monthLegendPosition,
448 offset: monthLegendOffset
449 })
450 };
451}), _withPropsOnChange(['days', 'data', 'colorScale', 'emptyColor'], function (_ref5) {
452 var days = _ref5.days,
453 data = _ref5.data,
454 colorScale = _ref5.colorScale,
455 emptyColor = _ref5.emptyColor;
456 return {
457 days: bindDaysData({
458 days: days,
459 data: data,
460 colorScale: colorScale,
461 emptyColor: emptyColor
462 })
463 };
464})];
465var enhance = (function (Component) {
466 switch (Component.displayName) {
467 case 'Calendar':
468 return _compose.apply(void 0, [_defaultProps(CalendarDefaultProps)].concat(commonEnhancers, [_pure]))(Component);
469 case 'CalendarCanvas':
470 return _compose.apply(void 0, [_defaultProps(CalendarCanvasDefaultProps)].concat(commonEnhancers, [_pure]))(Component);
471 }
472 return Component;
473});
474
475var CalendarYearLegends = React.memo(function (_ref) {
476 var years = _ref.years,
477 legend = _ref.legend,
478 theme = _ref.theme;
479 return React__default.createElement(React__default.Fragment, null, years.map(function (year) {
480 return React__default.createElement("text", {
481 key: year.year,
482 transform: "translate(".concat(year.x, ",").concat(year.y, ") rotate(").concat(year.rotation, ")"),
483 textAnchor: "middle",
484 style: theme.labels.text
485 }, legend(year.year));
486 }));
487});
488CalendarYearLegends.propTypes = {
489 years: PropTypes.array.isRequired,
490 legend: PropTypes.func.isRequired,
491 theme: PropTypes.object.isRequired
492};
493CalendarYearLegends.displayName = 'CalendarYearLegends';
494
495var CalendarMonthPath = function CalendarMonthPath(_ref) {
496 var path = _ref.path,
497 borderWidth = _ref.borderWidth,
498 borderColor = _ref.borderColor;
499 return React__default.createElement("path", {
500 d: path,
501 style: {
502 fill: 'none',
503 strokeWidth: borderWidth,
504 stroke: borderColor,
505 pointerEvents: 'none'
506 }
507 });
508};
509CalendarMonthPath.propTypes = {
510 path: PropTypes.string.isRequired,
511 borderWidth: PropTypes.number.isRequired,
512 borderColor: PropTypes.string.isRequired
513};
514var CalendarMonthPath$1 = _pure(CalendarMonthPath);
515
516var CalendarMonthLegends = React.memo(function (_ref) {
517 var months = _ref.months,
518 legend = _ref.legend,
519 theme = _ref.theme;
520 return React__default.createElement(React__default.Fragment, null, months.map(function (month) {
521 return React__default.createElement("text", {
522 key: "".concat(month.date.toString(), ".legend"),
523 transform: "translate(".concat(month.x, ",").concat(month.y, ") rotate(").concat(month.rotation, ")"),
524 textAnchor: "middle",
525 style: theme.labels.text
526 }, legend(month.year, month.month, month.date));
527 }));
528});
529CalendarMonthLegends.propTypes = {
530 months: PropTypes.array.isRequired,
531 legend: PropTypes.func.isRequired,
532 theme: PropTypes.object.isRequired
533};
534CalendarMonthLegends.displayName = 'CalendarMonthLegends';
535
536function _objectSpread$2(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; var ownKeys = Object.keys(source); if (typeof Object.getOwnPropertySymbols === 'function') { ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) { return Object.getOwnPropertyDescriptor(source, sym).enumerable; })); } ownKeys.forEach(function (key) { _defineProperty$2(target, key, source[key]); }); } return target; }
537function _defineProperty$2(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
538var CalendarDay = React.memo(function (_ref) {
539 var x = _ref.x,
540 y = _ref.y,
541 size = _ref.size,
542 spacing = _ref.spacing,
543 color = _ref.color,
544 borderWidth = _ref.borderWidth,
545 borderColor = _ref.borderColor,
546 onClick = _ref.onClick,
547 showTooltip = _ref.showTooltip,
548 hideTooltip = _ref.hideTooltip;
549 return React__default.createElement(React__default.Fragment, null, React__default.createElement("rect", {
550 x: x,
551 y: y,
552 width: size,
553 height: size,
554 style: {
555 fill: color,
556 strokeWidth: borderWidth,
557 stroke: borderColor
558 }
559 }), React__default.createElement("rect", {
560 fill: "rgba(0, 0, 0, 0)",
561 x: x - spacing / 2,
562 y: y - spacing / 2,
563 width: size + spacing,
564 height: size + spacing,
565 onClick: onClick,
566 onMouseEnter: showTooltip,
567 onMouseMove: showTooltip,
568 onMouseLeave: hideTooltip
569 }));
570});
571CalendarDay.propTypes = {
572 onClick: PropTypes.func.isRequired,
573 data: PropTypes.object.isRequired,
574 x: PropTypes.number.isRequired,
575 y: PropTypes.number.isRequired,
576 size: PropTypes.number.isRequired,
577 spacing: PropTypes.number.isRequired,
578 color: PropTypes.string.isRequired,
579 borderWidth: PropTypes.number.isRequired,
580 borderColor: PropTypes.string.isRequired,
581 tooltipFormat: PropTypes.oneOfType([PropTypes.func, PropTypes.string]),
582 tooltip: PropTypes.func,
583 showTooltip: PropTypes.func.isRequired,
584 hideTooltip: PropTypes.func.isRequired,
585 theme: PropTypes.shape({
586 tooltip: PropTypes.shape({}).isRequired
587 }).isRequired
588};
589CalendarDay.displayName = 'CalendarDay';
590var enhance$1 = _compose(_withPropsOnChange(['data', 'onClick'], function (_ref2) {
591 var data = _ref2.data,
592 _onClick = _ref2.onClick;
593 return {
594 onClick: function onClick(event) {
595 return _onClick(data, event);
596 }
597 };
598}), _withPropsOnChange(['data', 'color', 'showTooltip', 'tooltipFormat', 'tooltip', 'theme'], function (_ref3) {
599 var data = _ref3.data,
600 color = _ref3.color,
601 _showTooltip = _ref3.showTooltip,
602 tooltipFormat = _ref3.tooltipFormat,
603 tooltip$1 = _ref3.tooltip,
604 theme = _ref3.theme;
605 if (data.value === undefined) return {
606 showTooltip: core.noop
607 };
608 return {
609 showTooltip: function showTooltip(event) {
610 return _showTooltip(React__default.createElement(tooltip.BasicTooltip, {
611 id: "".concat(data.day),
612 value: data.value,
613 enableChip: true,
614 color: color,
615 theme: theme,
616 format: tooltipFormat,
617 renderContent: typeof tooltip$1 === 'function' ? tooltip$1.bind(null, _objectSpread$2({
618 color: color
619 }, data)) : null
620 }), event);
621 }
622 };
623}), _pure);
624var CalendarDay$1 = enhance$1(CalendarDay);
625
626function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
627var Calendar = function Calendar(_ref) {
628 var colorScale = _ref.colorScale,
629 margin = _ref.margin,
630 width = _ref.width,
631 height = _ref.height,
632 outerWidth = _ref.outerWidth,
633 outerHeight = _ref.outerHeight,
634 yearLegends = _ref.yearLegends,
635 yearLegend = _ref.yearLegend,
636 monthLegends = _ref.monthLegends,
637 monthLegend = _ref.monthLegend,
638 monthBorderWidth = _ref.monthBorderWidth,
639 monthBorderColor = _ref.monthBorderColor,
640 daySpacing = _ref.daySpacing,
641 dayBorderWidth = _ref.dayBorderWidth,
642 dayBorderColor = _ref.dayBorderColor,
643 theme = _ref.theme,
644 isInteractive = _ref.isInteractive,
645 tooltipFormat = _ref.tooltipFormat,
646 tooltip = _ref.tooltip,
647 onClick = _ref.onClick,
648 legends$1 = _ref.legends,
649 months = _ref.months,
650 days = _ref.days;
651 return React__default.createElement(core.Container, {
652 isInteractive: isInteractive,
653 theme: theme,
654 animate: false
655 }, function (_ref2) {
656 var showTooltip = _ref2.showTooltip,
657 hideTooltip = _ref2.hideTooltip;
658 return React__default.createElement(core.SvgWrapper, {
659 width: outerWidth,
660 height: outerHeight,
661 margin: margin,
662 theme: theme
663 }, days.map(function (d) {
664 return React__default.createElement(CalendarDay$1, {
665 key: d.date.toString(),
666 data: d,
667 x: d.x,
668 y: d.y,
669 size: d.size,
670 spacing: daySpacing,
671 color: d.color,
672 borderWidth: dayBorderWidth,
673 borderColor: dayBorderColor,
674 showTooltip: showTooltip,
675 hideTooltip: hideTooltip,
676 tooltipFormat: tooltipFormat,
677 tooltip: tooltip,
678 theme: theme,
679 onClick: onClick
680 });
681 }), months.map(function (m) {
682 return React__default.createElement(CalendarMonthPath$1, {
683 key: m.date.toString(),
684 path: m.path,
685 borderWidth: monthBorderWidth,
686 borderColor: monthBorderColor
687 });
688 }), React__default.createElement(CalendarMonthLegends, {
689 months: monthLegends,
690 legend: monthLegend,
691 theme: theme
692 }), React__default.createElement(CalendarYearLegends, {
693 years: yearLegends,
694 legend: yearLegend,
695 theme: theme
696 }), legends$1.map(function (legend, i) {
697 var legendData = colorScale.ticks(legend.itemCount).map(function (value) {
698 return {
699 id: value,
700 label: value,
701 color: colorScale(value)
702 };
703 });
704 return React__default.createElement(legends.BoxLegendSvg, _extends({
705 key: i
706 }, legend, {
707 containerWidth: width,
708 containerHeight: height,
709 data: legendData,
710 theme: theme
711 }));
712 }));
713 });
714};
715Calendar.displayName = 'Calendar';
716Calendar.propTypes = CalendarPropTypes;
717var Calendar$1 = _setDisplayName('Calendar')(enhance(Calendar));
718
719function _extends$1() { _extends$1 = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends$1.apply(this, arguments); }
720var ResponsiveCalendar = function ResponsiveCalendar(props) {
721 return React__default.createElement(core.ResponsiveWrapper, null, function (_ref) {
722 var width = _ref.width,
723 height = _ref.height;
724 return React__default.createElement(Calendar$1, _extends$1({
725 width: width,
726 height: height
727 }, props));
728 });
729};
730
731function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
732function _objectSpread$3(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; var ownKeys = Object.keys(source); if (typeof Object.getOwnPropertySymbols === 'function') { ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) { return Object.getOwnPropertyDescriptor(source, sym).enumerable; })); } ownKeys.forEach(function (key) { _defineProperty$3(target, key, source[key]); }); } return target; }
733function _slicedToArray$1(arr, i) { return _arrayWithHoles$1(arr) || _iterableToArrayLimit$1(arr, i) || _nonIterableRest$1(); }
734function _nonIterableRest$1() { throw new TypeError("Invalid attempt to destructure non-iterable instance"); }
735function _iterableToArrayLimit$1(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
736function _arrayWithHoles$1(arr) { if (Array.isArray(arr)) return arr; }
737function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
738function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
739function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
740function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }
741function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
742function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
743function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }
744function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
745function _defineProperty$3(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
746var findDayUnderCursor = function findDayUnderCursor(days, size, spacing, margin, x, y) {
747 return days.find(function (day) {
748 return day.value !== undefined && core.isCursorInRect(day.x + margin.left - spacing / 2, day.y + margin.top - spacing / 2, size + spacing, size + spacing, x, y);
749 });
750};
751var CalendarCanvas =
752function (_Component) {
753 _inherits(CalendarCanvas, _Component);
754 function CalendarCanvas() {
755 var _getPrototypeOf2;
756 var _this;
757 _classCallCheck(this, CalendarCanvas);
758 for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
759 args[_key] = arguments[_key];
760 }
761 _this = _possibleConstructorReturn(this, (_getPrototypeOf2 = _getPrototypeOf(CalendarCanvas)).call.apply(_getPrototypeOf2, [this].concat(args)));
762 _defineProperty$3(_assertThisInitialized(_this), "handleMouseHover", function (showTooltip, hideTooltip) {
763 return function (event) {
764 var _this$props = _this.props,
765 isInteractive = _this$props.isInteractive,
766 margin = _this$props.margin,
767 theme = _this$props.theme,
768 days = _this$props.days,
769 daySpacing = _this$props.daySpacing,
770 tooltipFormat = _this$props.tooltipFormat,
771 tooltip$1 = _this$props.tooltip;
772 if (!isInteractive || !days || days.length === 0) return;
773 var _getRelativeCursor = core.getRelativeCursor(_this.surface, event),
774 _getRelativeCursor2 = _slicedToArray$1(_getRelativeCursor, 2),
775 x = _getRelativeCursor2[0],
776 y = _getRelativeCursor2[1];
777 var currentDay = findDayUnderCursor(days, days[0].size, daySpacing, margin, x, y);
778 if (currentDay !== undefined) {
779 showTooltip(React__default.createElement(tooltip.BasicTooltip, {
780 id: "".concat(currentDay.day),
781 value: currentDay.value,
782 enableChip: true,
783 color: currentDay.color,
784 theme: theme,
785 format: tooltipFormat,
786 renderContent: typeof tooltip$1 === 'function' ? tooltip$1.bind(null, currentDay) : null
787 }), event);
788 } else {
789 hideTooltip();
790 }
791 };
792 });
793 _defineProperty$3(_assertThisInitialized(_this), "handleMouseLeave", function (hideTooltip) {
794 return function () {
795 if (_this.props.isInteractive !== true) return;
796 hideTooltip();
797 };
798 });
799 _defineProperty$3(_assertThisInitialized(_this), "handleClick", function (event) {
800 var _this$props2 = _this.props,
801 isInteractive = _this$props2.isInteractive,
802 margin = _this$props2.margin,
803 onClick = _this$props2.onClick,
804 days = _this$props2.days,
805 daySpacing = _this$props2.daySpacing;
806 if (!isInteractive || !days || days.length === 0) return;
807 var _getRelativeCursor3 = core.getRelativeCursor(_this.surface, event),
808 _getRelativeCursor4 = _slicedToArray$1(_getRelativeCursor3, 2),
809 x = _getRelativeCursor4[0],
810 y = _getRelativeCursor4[1];
811 var currentDay = findDayUnderCursor(days, days[0].size, daySpacing, margin, x, y);
812 if (currentDay !== undefined) onClick(currentDay, event);
813 });
814 return _this;
815 }
816 _createClass(CalendarCanvas, [{
817 key: "componentDidMount",
818 value: function componentDidMount() {
819 this.ctx = this.surface.getContext('2d');
820 this.draw(this.props);
821 }
822 }, {
823 key: "shouldComponentUpdate",
824 value: function shouldComponentUpdate(props) {
825 if (this.props.outerWidth !== props.outerWidth || this.props.outerHeight !== props.outerHeight || this.props.isInteractive !== props.isInteractive || this.props.theme !== props.theme) {
826 return true;
827 } else {
828 this.draw(props);
829 return false;
830 }
831 }
832 }, {
833 key: "componentDidUpdate",
834 value: function componentDidUpdate() {
835 this.ctx = this.surface.getContext('2d');
836 this.draw(this.props);
837 }
838 }, {
839 key: "draw",
840 value: function draw(props) {
841 var _this2 = this;
842 var pixelRatio = props.pixelRatio,
843 margin = props.margin,
844 width = props.width,
845 height = props.height,
846 outerWidth = props.outerWidth,
847 outerHeight = props.outerHeight,
848 colorScale = props.colorScale,
849 yearLegends = props.yearLegends,
850 yearLegend = props.yearLegend,
851 monthLegends = props.monthLegends,
852 monthLegend = props.monthLegend,
853 days = props.days,
854 dayBorderWidth = props.dayBorderWidth,
855 dayBorderColor = props.dayBorderColor,
856 legends$1 = props.legends,
857 theme = props.theme;
858 this.surface.width = outerWidth * pixelRatio;
859 this.surface.height = outerHeight * pixelRatio;
860 this.ctx.scale(pixelRatio, pixelRatio);
861 this.ctx.fillStyle = theme.background;
862 this.ctx.fillRect(0, 0, outerWidth, outerHeight);
863 this.ctx.translate(margin.left, margin.top);
864 days.forEach(function (day) {
865 _this2.ctx.fillStyle = day.color;
866 if (dayBorderWidth > 0) {
867 _this2.ctx.strokeStyle = dayBorderColor;
868 _this2.ctx.lineWidth = dayBorderWidth;
869 }
870 _this2.ctx.beginPath();
871 _this2.ctx.rect(day.x, day.y, day.size, day.size);
872 _this2.ctx.fill();
873 if (dayBorderWidth > 0) {
874 _this2.ctx.stroke();
875 }
876 });
877 this.ctx.textAlign = 'center';
878 this.ctx.textBaseline = 'middle';
879 this.ctx.fillStyle = theme.labels.text.fill;
880 this.ctx.font = "".concat(theme.labels.text.fontSize, "px ").concat(theme.labels.text.fontFamily);
881 monthLegends.forEach(function (month) {
882 _this2.ctx.save();
883 _this2.ctx.translate(month.x, month.y);
884 _this2.ctx.rotate(core.degreesToRadians(month.rotation));
885 _this2.ctx.fillText(monthLegend(month.year, month.month, month.date), 0, 0);
886 _this2.ctx.restore();
887 });
888 yearLegends.forEach(function (year) {
889 _this2.ctx.save();
890 _this2.ctx.translate(year.x, year.y);
891 _this2.ctx.rotate(core.degreesToRadians(year.rotation));
892 _this2.ctx.fillText(yearLegend(year.year), 0, 0);
893 _this2.ctx.restore();
894 });
895 legends$1.forEach(function (legend) {
896 var legendData = colorScale.ticks(legend.itemCount).map(function (value) {
897 return {
898 id: value,
899 label: value,
900 color: colorScale(value)
901 };
902 });
903 legends.renderLegendToCanvas(_this2.ctx, _objectSpread$3({}, legend, {
904 data: legendData,
905 containerWidth: width,
906 containerHeight: height,
907 theme: theme
908 }));
909 });
910 }
911 }, {
912 key: "render",
913 value: function render() {
914 var _this3 = this;
915 var _this$props3 = this.props,
916 outerWidth = _this$props3.outerWidth,
917 outerHeight = _this$props3.outerHeight,
918 pixelRatio = _this$props3.pixelRatio,
919 isInteractive = _this$props3.isInteractive,
920 theme = _this$props3.theme;
921 return React__default.createElement(core.Container, {
922 isInteractive: isInteractive,
923 theme: theme,
924 animate: false
925 }, function (_ref) {
926 var showTooltip = _ref.showTooltip,
927 hideTooltip = _ref.hideTooltip;
928 return React__default.createElement("canvas", {
929 ref: function ref(surface) {
930 _this3.surface = surface;
931 },
932 width: outerWidth * pixelRatio,
933 height: outerHeight * pixelRatio,
934 style: {
935 width: outerWidth,
936 height: outerHeight
937 },
938 onMouseEnter: _this3.handleMouseHover(showTooltip, hideTooltip),
939 onMouseMove: _this3.handleMouseHover(showTooltip, hideTooltip),
940 onMouseLeave: _this3.handleMouseLeave(hideTooltip),
941 onClick: _this3.handleClick
942 });
943 });
944 }
945 }]);
946 return CalendarCanvas;
947}(React.Component);
948_defineProperty$3(CalendarCanvas, "propTypes", CalendarCanvasPropTypes);
949CalendarCanvas.displayName = 'CalendarCanvas';
950var CalendarCanvas$1 = _setDisplayName(CalendarCanvas.displayName)(enhance(CalendarCanvas));
951
952function _extends$2() { _extends$2 = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends$2.apply(this, arguments); }
953var ResponsiveCalendarCanvas = function ResponsiveCalendarCanvas(props) {
954 return React__default.createElement(core.ResponsiveWrapper, null, function (_ref) {
955 var width = _ref.width,
956 height = _ref.height;
957 return React__default.createElement(CalendarCanvas$1, _extends$2({
958 width: width,
959 height: height
960 }, props));
961 });
962};
963
964exports.Calendar = Calendar$1;
965exports.CalendarCanvas = CalendarCanvas$1;
966exports.CalendarCanvasDefaultProps = CalendarCanvasDefaultProps;
967exports.CalendarCanvasPropTypes = CalendarCanvasPropTypes;
968exports.CalendarDefaultProps = CalendarDefaultProps;
969exports.CalendarPropTypes = CalendarPropTypes;
970exports.ResponsiveCalendar = ResponsiveCalendar;
971exports.ResponsiveCalendarCanvas = ResponsiveCalendarCanvas;