UNPKG

7.31 kBJavaScriptView Raw
1import { a as __extends } from './tslib.es6-f952ba6f.js';
2import { U as Utils } from './Utils-38a0872e.js';
3import { axisBottom, select } from 'd3';
4import { C as ChartVisualizationComponent } from './ChartVisualizationComponent-295c909a.js';
5
6var TemporalXAxisComponent = /** @class */ (function (_super) {
7 __extends(TemporalXAxisComponent, _super);
8 function TemporalXAxisComponent(renderTarget) {
9 return _super.call(this, renderTarget) || this;
10 }
11 TemporalXAxisComponent.prototype.createOffsetXAxis = function () {
12 var xCopy = this.x.copy();
13 var rawStart = this.chartOptions.timeFrame ? (new Date(this.chartOptions.timeFrame[0])) : xCopy.domain()[0];
14 var rawEnd = this.chartOptions.timeFrame ? (new Date(this.chartOptions.timeFrame[1])) : xCopy.domain()[1];
15 xCopy.domain([
16 new Date(rawStart), new Date(rawEnd)
17 ]);
18 return xCopy;
19 };
20 TemporalXAxisComponent.prototype.createXAxis = function (singleLineXAxisLabel, snapFirst, snapLast) {
21 if (snapFirst === void 0) { snapFirst = false; }
22 if (snapLast === void 0) { snapLast = false; }
23 var offsetX = this.createOffsetXAxis();
24 var ticks = offsetX.ticks(this.getXTickNumber(singleLineXAxisLabel));
25 if (ticks.length <= 2) {
26 ticks = this.x.domain();
27 }
28 if (snapFirst) {
29 ticks[0] = this.x.domain()[0];
30 }
31 if (snapLast) {
32 ticks[ticks.length - 1] = this.x.domain()[1];
33 }
34 this.smartTickFormat = this.createSmartTickFormat(ticks, offsetX);
35 return axisBottom(this.x)
36 .tickValues(ticks)
37 .tickFormat(Utils.timeFormat(this.labelFormatUsesSeconds(ticks), this.labelFormatUsesMillis(ticks), this.chartOptions.offset, this.chartOptions.is24HourTime, null, null, this.chartOptions.dateLocale));
38 };
39 TemporalXAxisComponent.prototype.getXTickNumber = function (singleLineXAxisLabel) {
40 return Math.max((singleLineXAxisLabel ? Math.floor(this.chartWidth / 300) : Math.floor(this.chartWidth / 160)), 1);
41 };
42 TemporalXAxisComponent.prototype.labelFormatUsesSeconds = function (ticks) {
43 if (ticks === void 0) { ticks = null; }
44 var tickSpanSubMinute = ticks ? !this.isTickSpanGreaterThan(ticks, 59 * 1000) : false;
45 return !this.chartOptions.minutesForTimeLabels && tickSpanSubMinute;
46 };
47 TemporalXAxisComponent.prototype.labelFormatUsesMillis = function (ticks) {
48 if (ticks === void 0) { ticks = null; }
49 var tickSpanSubSecond = ticks ? !this.isTickSpanGreaterThan(ticks, 999) : false;
50 return !this.chartOptions.minutesForTimeLabels && tickSpanSubSecond;
51 };
52 TemporalXAxisComponent.prototype.updateXAxis = function (forceFirst, forceLast) {
53 if (forceFirst === void 0) { forceFirst = false; }
54 if (forceLast === void 0) { forceLast = false; }
55 this.xAxisEntered.call(this.createXAxis(this.chartOptions.singleLineXAxisLabel, forceFirst, forceLast));
56 this.updateAxisText(forceFirst, forceLast);
57 };
58 TemporalXAxisComponent.prototype.updateAxisText = function (forceFirst, forceLast) {
59 if (forceFirst === void 0) { forceFirst = false; }
60 if (forceLast === void 0) { forceLast = false; }
61 //update text by applying function
62 if (this.chartOptions.xAxisTimeFormat) {
63 var indexOfLast_1 = this.xAxisEntered.selectAll('.tick').size() - 1;
64 var self_1 = this;
65 this.xAxisEntered.selectAll('.tick').each(function (d, i) {
66 select(this).select('text').text(function (d) {
67 var momentTimeFormatString = String(self_1.chartOptions.xAxisTimeFormat(d, i, i === 0, i === indexOfLast_1));
68 return Utils.timeFormat(self_1.labelFormatUsesSeconds(), self_1.labelFormatUsesMillis(), self_1.chartOptions.offset, self_1.chartOptions.is24HourTime, null, momentTimeFormatString, self_1.chartOptions.dateLocale)(d);
69 });
70 });
71 }
72 else {
73 var indexOfLast_2 = this.xAxisEntered.selectAll('.tick').size() - 1;
74 var self_2 = this;
75 this.xAxisEntered.selectAll('.tick').each(function (d, i) {
76 select(this).select('text').text(function (d) {
77 var momentTimeFormatString = String(self_2.smartTickFormat(d, i, i === 0, i === indexOfLast_2));
78 //harcode format of first and last to include hours/minutes if force first/last
79 if ((i === 0 && forceFirst) || (i === indexOfLast_2 && forceLast)) {
80 momentTimeFormatString = 'L ' + Utils.subDateTimeFormat(self_2.chartOptions.is24HourTime, false, false);
81 }
82 return Utils.timeFormat(self_2.labelFormatUsesSeconds(), self_2.labelFormatUsesMillis(), self_2.chartOptions.offset, self_2.chartOptions.is24HourTime, null, momentTimeFormatString, self_2.chartOptions.dateLocale)(d);
83 });
84 });
85 }
86 if (!this.chartOptions.singleLineXAxisLabel)
87 this.xAxisEntered.selectAll('text').call(Utils.splitTimeLabel);
88 this.xAxisEntered.select(".domain").style("display", "none");
89 };
90 TemporalXAxisComponent.prototype.drawXAxis = function (yOffset, snapFirst, snapLast) {
91 if (snapFirst === void 0) { snapFirst = false; }
92 if (snapLast === void 0) { snapLast = false; }
93 this.xAxisEntered = this.xAxis.enter()
94 .append("g")
95 .attr("class", "xAxis")
96 .merge(this.xAxis)
97 .attr("transform", "translate(0," + yOffset + ")")
98 .call(this.createXAxis(this.chartOptions.singleLineXAxisLabel, snapFirst, snapLast));
99 this.updateAxisText(snapFirst, snapLast);
100 };
101 TemporalXAxisComponent.prototype.isSameDate = function (d1, d2) {
102 return (d1.getYear() === d2.getYear() && d1.getMonth() === d2.getMonth() && d1.getDate() === d2.getDate());
103 };
104 TemporalXAxisComponent.prototype.isTickSpanGreaterThan = function (ticks, minValue) {
105 return (ticks[1].valueOf() - ticks[0].valueOf() >= minValue);
106 };
107 TemporalXAxisComponent.prototype.createSmartTickFormat = function (ticks, offsetX) {
108 var _this = this;
109 var spansMultipleDays = !this.isSameDate(offsetX.domain()[0], offsetX.domain()[1]);
110 var lessTicksThanDays = this.isTickSpanGreaterThan(ticks, 23 * 60 * 60 * 1000);
111 var timeFormat = Utils.subDateTimeFormat(this.chartOptions.is24HourTime, this.labelFormatUsesSeconds(ticks), this.labelFormatUsesMillis(ticks));
112 return function (d, i, isFirst, isLast) {
113 var timeAndDate = _this.chartOptions.singleLineXAxisLabel ? ('L ' + timeFormat) : (timeFormat + ' L');
114 if (lessTicksThanDays) {
115 return 'L';
116 }
117 if (isFirst || isLast) {
118 return timeAndDate;
119 }
120 if (!spansMultipleDays) {
121 return timeFormat;
122 }
123 return timeAndDate;
124 };
125 };
126 return TemporalXAxisComponent;
127}(ChartVisualizationComponent));
128
129export { TemporalXAxisComponent as T };