deno.land / x / billboardjs@3.6.0 / ChartInternal / data / convert.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
/** * Copyright (c) 2017 ~ present NAVER Corp. * billboard.js project is licensed under the MIT license */import {isUndefined, isDefined, isObject, isValue, notEmpty, isArray} from "../../module/util";import {runWorker} from "../../module/worker";import {columns, json, rows, url} from "./convert.helper";import type {IData} from "../data/IData";
/** * Get data key for JSON * @param {string|object} keysParam Key params * @param {object} config Config object * @returns {string} Data key * @private */function getDataKeyForJson(keysParam, config) { const keys = keysParam || config?.data_keys;
if (keys?.x) { config.data_x = keys.x; }
return keys;}
/** * Data convert * @memberof ChartInternal * @private */export default { /** * Convert data according its type * @param {object} args data object * @param {Function} [callback] callback for url(XHR) type loading * @private */ convertData(args, callback: Function): void { const {config} = this; const useWorker = config.boost_useWorker; let data = args;
if (args.bindto) { data = {};
["url", "mimeType", "headers", "keys", "json", "keys", "rows", "columns"] .forEach(v => { const key = `data_${v}`;
if (key in args) { data[v] = args[key]; } }); }
if (data.url && callback) { url(data.url, data.mimeType, data.headers, getDataKeyForJson(data.keys, config), callback ); } else if (data.json) { runWorker(useWorker, json, callback, [columns, rows])( data.json, getDataKeyForJson(data.keys, config) ); } else if (data.rows) { runWorker(useWorker, rows, callback)(data.rows); } else if (data.columns) { runWorker(useWorker, columns, callback)(data.columns); } else if (args.bindto) { throw Error("url or json or rows or columns is required."); } },
convertDataToTargets(data: {[key:string]: number|null}[], appendXs: boolean): IData[] { const $$ = this; const {axis, config, state} = $$; let isCategorized = false; let isTimeSeries = false; let isCustomX = false;
if (axis) { isCategorized = axis.isCategorized(); isTimeSeries = axis.isTimeSeries(); isCustomX = axis.isCustomX(); }
const dataKeys = Object.keys(data[0] || {}); const ids = dataKeys.length ? dataKeys.filter($$.isNotX, $$) : []; const xs = dataKeys.length ? dataKeys.filter($$.isX, $$) : [];
let xsData;
// save x for update data by load when custom x and bb.x API ids.forEach(id => { const xKey = this.getXKey(id);
if (isCustomX || isTimeSeries) { // if included in input data if (xs.indexOf(xKey) >= 0) { xsData = ((appendXs && $$.data.xs[id]) || []) .concat( data.map(d => d[xKey]) .filter(isValue) .map((rawX, i) => $$.generateTargetX(rawX, id, i)) ); } else if (config.data_x) { // if not included in input data, find from preloaded data of other id's x xsData = this.getOtherTargetXs(); } else if (notEmpty(config.data_xs)) { // if not included in input data, find from preloaded data xsData = $$.getXValuesOfXKey(xKey, $$.data.targets); } // MEMO: if no x included, use same x of current will be used } else { xsData = data.map((d, i) => i); }
xsData && (this.data.xs[id] = xsData); });
// check x is defined ids.forEach(id => { if (!this.data.xs[id]) { throw new Error(`x is not defined for id = "${id}".`); } });
// convert to target const targets = ids.map((id, index) => { const convertedId = config.data_idConverter.bind($$.api)(id); const xKey = $$.getXKey(id); const isCategory = isCustomX && isCategorized; const hasCategory = isCategory && data.map(v => v.x) .every(v => config.axis_x_categories.indexOf(v) > -1);
// when .load() with 'append' option is used for indexed axis // @ts-ignore const isDataAppend = data.__append__; const xIndex = xKey === null && isDataAppend ? $$.api.data.values(id).length : 0;
return { id: convertedId, id_org: id, values: data.map((d, i) => { const rawX = d[xKey]; let value = d[id]; let x;
value = value !== null && !isNaN(value) && !isObject(value) ? +value : (isArray(value) || isObject(value) ? value : null);
// use x as categories if custom x and categorized if ((isCategory || state.hasRadar) && index === 0 && !isUndefined(rawX)) { if (!hasCategory && index === 0 && i === 0 && !isDataAppend) { config.axis_x_categories = []; }
x = config.axis_x_categories.indexOf(rawX);
if (x === -1) { x = config.axis_x_categories.length; config.axis_x_categories.push(rawX); } } else { x = $$.generateTargetX(rawX, id, xIndex + i); }
// mark as x = undefined if value is undefined and filter to remove after mapped if (isUndefined(value) || $$.data.xs[id].length <= i) { x = undefined; }
return { x, value, id: convertedId, index: -1 }; }).filter(v => isDefined(v.x)) }; });
// finish targets targets.forEach(t => { // sort values by its x if (config.data_xSort) { t.values = t.values.sort((v1, v2) => { const x1 = v1.x || v1.x === 0 ? v1.x : Infinity; const x2 = v2.x || v2.x === 0 ? v2.x : Infinity;
return x1 - x2; }); }
// indexing each value t.values.forEach((v, i) => (v.index = i));
// this needs to be sorted because its index and value.index is identical $$.data.xs[t.id]?.sort((v1, v2) => v1 - v2); });
// cache information about values state.hasNegativeValue = $$.hasNegativeValueInTargets(targets); state.hasPositiveValue = $$.hasPositiveValueInTargets(targets);
// set target types if (config.data_type) { $$.setTargetType($$.mapToIds(targets) .filter(id => !(id in config.data_types)), config.data_type); }
// cache as original id keyed targets.forEach(d => $$.cache.add(d.id_org, d, true));
return targets as IData[]; }};
billboardjs

Version Info

Tagged at
a year ago