deno.land / x / billboardjs@3.6.0 / ChartInternal / internals / domain.ts

نووسراو ببینە
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
/** * Copyright (c) 2017 ~ present NAVER Corp. * billboard.js project is licensed under the MIT license */import {TYPE, TYPE_BY_CATEGORY} from "../../config/const";import type {IData} from "../data/IData";import {brushEmpty, getBrushSelection, getMinMax, isDefined, notEmpty, isValue, isObject, isNumber, diffDomain, parseDate, sortValue} from "../../module/util";
export default { getYDomainMinMax(targets, type: "min" | "max"): number | Date | undefined { const $$ = this; const {axis, config} = $$; const isMin = type === "min";
const dataGroups = config.data_groups; const ids = $$.mapToIds(targets); const ys = $$.getValuesAsIdKeyed(targets);
if (dataGroups.length > 0) { const hasValue = $$[`has${isMin ? "Negative" : "Positive"}ValueInTargets`](targets);
dataGroups.forEach(groupIds => { // Determine baseId const idsInGroup = groupIds .filter(v => ids.indexOf(v) >= 0);
if (idsInGroup.length) { const baseId = idsInGroup[0]; const baseAxisId = axis.getId(baseId);
// Initialize base value. Set to 0 if not match with the condition if (hasValue && ys[baseId]) { ys[baseId] = ys[baseId] .map(v => ((isMin ? v < 0 : v > 0) ? v : 0)); }
idsInGroup .filter((v, i) => i > 0) .forEach(id => { if (ys[id]) { const axisId = axis.getId(id);
ys[id].forEach((v, i) => { const val = +v; const meetCondition = isMin ? val > 0 : val < 0;
if (axisId === baseAxisId && !(hasValue && meetCondition)) { ys[baseId][i] += val; } }); } }); } }); }
return getMinMax(type, Object.keys(ys).map(key => getMinMax(type, ys[key]))); },
/** * Check if hidden targets bound to the given axis id * @param {string} id ID to be checked * @returns {boolean} * @private */ isHiddenTargetWithYDomain(id): boolean { const $$ = this;
return $$.state.hiddenTargetIds .some(v => $$.axis.getId(v) === id); },
getYDomain(targets, axisId: string, xDomain) { const $$ = this; const {axis, config, scale} = $$; const pfx = `axis_${axisId}`;
if ($$.isStackNormalized()) { return [0, 100]; }
const isLog = scale?.[axisId] && scale[axisId].type === "log"; const targetsByAxisId = targets.filter(t => axis.getId(t.id) === axisId); const yTargets = xDomain ? $$.filterByXDomain(targetsByAxisId, xDomain) : targetsByAxisId;
if (yTargets.length === 0) { // use domain of the other axis if target of axisId is none if ($$.isHiddenTargetWithYDomain(axisId)) { return scale[axisId].domain(); } else { return axisId === "y2" ? scale.y.domain() : // When all data bounds to y2, y Axis domain is called prior y2. // So, it needs to call to get y2 domain here $$.getYDomain(targets, "y2", xDomain); } }
const yMin = config[`${pfx}_min`]; const yMax = config[`${pfx}_max`]; const center = config[`${pfx}_center`]; const isInverted = config[`${pfx}_inverted`]; const showHorizontalDataLabel = $$.hasDataLabel() && config.axis_rotated; const showVerticalDataLabel = $$.hasDataLabel() && !config.axis_rotated;
let yDomainMin = $$.getYDomainMinMax(yTargets, "min"); let yDomainMax = $$.getYDomainMinMax(yTargets, "max");
let isZeroBased = [TYPE.BAR, TYPE.BUBBLE, TYPE.SCATTER, ...TYPE_BY_CATEGORY.Line] .some(v => { const type = v.indexOf("area") > -1 ? "area" : v;
return $$.hasType(v, yTargets, true) && config[`${type}_zerobased`]; });
// MEMO: avoid inverting domain unexpectedly yDomainMin = isValue(yMin) ? yMin : (isValue(yMax) ? (yDomainMin < yMax ? yDomainMin : yMax - 10) : yDomainMin); yDomainMax = isValue(yMax) ? yMax : (isValue(yMin) ? (yMin < yDomainMax ? yDomainMax : yMin + 10) : yDomainMax);
if (isNaN(yDomainMin)) { // set minimum to zero when not number yDomainMin = 0; }
if (isNaN(yDomainMax)) { // set maximum to have same value as yDomainMin yDomainMax = yDomainMin; }
if (yDomainMin === yDomainMax) { yDomainMin < 0 ? yDomainMax = 0 : yDomainMin = 0; }
const isAllPositive = yDomainMin >= 0 && yDomainMax >= 0; const isAllNegative = yDomainMin <= 0 && yDomainMax <= 0;
// Cancel zerobased if axis_*_min / axis_*_max specified if ((isValue(yMin) && isAllPositive) || (isValue(yMax) && isAllNegative)) { isZeroBased = false; }
// Bar/Area chart should be 0-based if all positive|negative if (isZeroBased) { isAllPositive && (yDomainMin = 0); isAllNegative && (yDomainMax = 0); }
const domainLength = Math.abs(yDomainMax - yDomainMin); let padding = {top: domainLength * 0.1, bottom: domainLength * 0.1};
if (isDefined(center)) { const yDomainAbs = Math.max(Math.abs(yDomainMin), Math.abs(yDomainMax));
yDomainMax = center + yDomainAbs; yDomainMin = center - yDomainAbs; }
// add padding for data label if (showHorizontalDataLabel) { const diff = diffDomain(scale.y.range()); const ratio = $$.getDataLabelLength(yDomainMin, yDomainMax, "width") .map(v => v / diff);
["bottom", "top"].forEach((v, i) => { padding[v] += domainLength * (ratio[i] / (1 - ratio[0] - ratio[1])); }); } else if (showVerticalDataLabel) { const lengths = $$.getDataLabelLength(yDomainMin, yDomainMax, "height");
["bottom", "top"].forEach((v, i) => { padding[v] += $$.convertPixelToScale("y", lengths[i], domainLength); }); }
padding = $$.getResettedPadding(padding);
// if padding is set, the domain will be updated relative the current domain value // ex) $$.height=300, padding.top=150, domainLength=4 --> domain=6 const p = config[`${pfx}_padding`];
if (notEmpty(p)) { ["bottom", "top"].forEach(v => { padding[v] = axis.getPadding(p, v, padding[v], domainLength); }); }
// Bar/Area chart should be 0-based if all positive|negative if (isZeroBased) { isAllPositive && (padding.bottom = yDomainMin); isAllNegative && (padding.top = -yDomainMax); }
const domain = isLog ? [yDomainMin, yDomainMax].map(v => (v < 0 ? 0 : v)) : [yDomainMin - padding.bottom, yDomainMax + padding.top];
return isInverted ? domain.reverse() : domain; },
getXDomainMinMax(targets, type) { const $$ = this; const configValue = $$.config[`axis_x_${type}`]; const dataValue = getMinMax(type, targets.map(t => getMinMax(type, t.values.map(v => v.x)))); let value = isObject(configValue) ? configValue.value : configValue;
value = isDefined(value) && $$.axis?.isTimeSeries() ? parseDate.bind(this)(value) : value;
if (isObject(configValue) && configValue.fit && ( (type === "min" && value < dataValue) || (type === "max" && value > dataValue) )) { value = undefined; }
return isDefined(value) ? value : dataValue; },
/** * Get x Axis padding * @param {Array} domain x Axis domain * @param {number} tickCount Tick count * @returns {object} Padding object values with 'left' & 'right' key * @private */ getXDomainPadding(domain, tickCount: number): {left: number, right: number} { const $$ = this; const {axis, config} = $$; const padding = config.axis_x_padding; const isTimeSeriesTickCount = axis.isTimeSeries() && tickCount; const diff = diffDomain(domain); let defaultValue;
// determine default padding value if (axis.isCategorized() || isTimeSeriesTickCount) { defaultValue = 0; } else if ($$.hasType("bar")) { const maxDataCount = $$.getMaxDataCount();
defaultValue = maxDataCount > 1 ? (diff / (maxDataCount - 1)) / 2 : 0.5; } else { defaultValue = $$.getResettedPadding(diff * 0.01); }
let {left = defaultValue, right = defaultValue} = isNumber(padding) ? {left: padding, right: padding} : padding;
// when the unit is pixel, convert pixels to axis scale value if (padding.unit === "px") { const domainLength = Math.abs(diff + (diff * 0.2));
left = axis.getPadding(padding, "left", defaultValue, domainLength); right = axis.getPadding(padding, "right", defaultValue, domainLength); } else { const range = diff + left + right;
if (isTimeSeriesTickCount && range) { const relativeTickWidth = (diff / tickCount) / range;
left = left / range / relativeTickWidth; right = right / range / relativeTickWidth; } }
return {left, right}; },
/** * Get x Axis domain * @param {Array} targets targets * @returns {Array} x Axis domain * @private */ getXDomain(targets?: IData[]): (Date|number)[] { const $$ = this; const {axis, scale: {x}} = $$; const domain = [ $$.getXDomainMinMax(targets, "min"), $$.getXDomainMinMax(targets, "max") ]; let [min = 0, max = 0] = domain;
if (x.type !== "log") { const isCategorized = axis.isCategorized(); const isTimeSeries = axis.isTimeSeries(); const padding = $$.getXDomainPadding(domain); let [firstX, lastX] = domain;
// show center of x domain if min and max are the same if ((firstX - lastX) === 0 && !isCategorized) { if (isTimeSeries) { firstX = new Date(firstX.getTime() * 0.5); lastX = new Date(lastX.getTime() * 1.5); } else { firstX = firstX === 0 ? 1 : (firstX * 0.5); lastX = lastX === 0 ? -1 : (lastX * 1.5); } }
if (firstX || firstX === 0) { min = isTimeSeries ? new Date(firstX.getTime() - padding.left) : firstX - padding.left; }
if (lastX || lastX === 0) { max = isTimeSeries ? new Date(lastX.getTime() + padding.right) : lastX + padding.right; } }
return [min, max]; },
updateXDomain(targets, withUpdateXDomain, withUpdateOrgXDomain, withTrim, domain) { const $$ = this; const {config, org, scale: {x, subX}} = $$; const zoomEnabled = config.zoom_enabled;
if (withUpdateOrgXDomain) { x.domain(domain || sortValue($$.getXDomain(targets))); org.xDomain = x.domain();
zoomEnabled && $$.zoom.updateScaleExtent();
subX.domain(x.domain()); $$.brush?.scale(subX); }
if (withUpdateXDomain) { const domainValue = domain || (!$$.brush || brushEmpty($$)) ? org.xDomain : getBrushSelection($$).map(subX.invert);
x.domain(domainValue); zoomEnabled && $$.zoom.updateScaleExtent(); }
// Trim domain when too big by zoom mousemove event withTrim && x.domain($$.trimXDomain(x.orgDomain()));
return x.domain(); },
trimXDomain(domain) { const zoomDomain = this.getZoomDomain(); const [min, max] = zoomDomain;
if (domain[0] <= min) { domain[1] = +domain[1] + (min - domain[0]); domain[0] = min; }
if (max <= domain[1]) { domain[0] = +domain[0] - (domain[1] - max); domain[1] = max; }
return domain; },
/** * Get zoom domain * @returns {Array} zoom domain * @private */ getZoomDomain(): [number|Date, number|Date] { const $$ = this; const {config, org} = $$; let [min, max] = org.xDomain;
if (isDefined(config.zoom_x_min)) { min = getMinMax("min", [min, config.zoom_x_min]); }
if (isDefined(config.zoom_x_max)) { max = getMinMax("max", [max, config.zoom_x_max]); }
return [min, max]; },
/** * Converts pixels to axis' scale values * @param {string} type Axis type * @param {number} pixels Pixels * @param {number} domainLength Domain length * @returns {number} * @private */ convertPixelToScale(type: "x"|"y", pixels: number, domainLength: number): number { const $$ = this; const {config, state} = $$; const isRotated = config.axis_rotated; let length;
if (type === "x") { length = isRotated ? "height" : "width"; } else { length = isRotated ? "width" : "height"; }
return domainLength * (pixels / state[length]); }};
billboardjs

Version Info

Tagged at
a year ago