Spaces:
Sleeping
Sleeping
File size: 3,586 Bytes
216c0a4 | 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 | // Data schema and channel helpers.
(function (root) {
const chartUtils = root.chartUtils;
const schema = chartUtils.schema;
schema.normalizeUnit = unit => {
if (unit == null) return "";
const text = String(unit).trim();
const lower = text.toLowerCase();
return ["", "none", "null", "nan", "undefined", "n/a", "na"].includes(lower) ? "" : text;
};
schema.columns = jsonData => {
const columns = jsonData && jsonData.data && jsonData.data.columns;
return Array.isArray(columns) ? columns : [];
};
const buildChannel = (column, role, index, fallbackKey) => {
const key = column && column.name != null ? column.name : fallbackKey;
const rawLabel = column
? (column.label || column.display_name || column.name || fallbackKey || "")
: (fallbackKey || "");
const unit = schema.normalizeUnit(column && column.unit);
const description = column && column.description ? column.description : rawLabel;
const displayName = column && column.display_name ? column.display_name : rawLabel;
return {
role: column && column.role != null ? column.role : role,
index: index == null ? -1 : index,
key: key || "",
type: column && column.data_type ? column.data_type : "",
unit,
rawLabel,
description,
displayName,
label: unit ? `${rawLabel} (${unit})` : rawLabel,
raw: column || null,
};
};
schema.channel = (jsonData, role, options = {}) => {
const columns = schema.columns(jsonData);
let index = columns.findIndex(column => column.role === role);
if (index < 0 && options.fallbackIndex != null) {
index = options.fallbackIndex;
}
const column = index >= 0 ? columns[index] : null;
const fallbackKey = Object.prototype.hasOwnProperty.call(options, "fallbackKey")
? options.fallbackKey
: role;
return buildChannel(column, role, index, fallbackKey);
};
schema.channelByKey = (jsonData, key, options = {}) => {
const columns = schema.columns(jsonData);
let index = columns.findIndex(column => column.name === key);
if (index < 0 && options.fallbackIndex != null) {
index = options.fallbackIndex;
}
const column = index >= 0 ? columns[index] : null;
const fallbackKey = Object.prototype.hasOwnProperty.call(options, "fallbackKey")
? options.fallbackKey
: key;
return buildChannel(column, options.role || (column && column.role) || "", index, fallbackKey);
};
schema.column = (columns, index, options = {}) => {
const list = Array.isArray(columns) ? columns : [];
const column = index >= 0 ? list[index] : null;
return buildChannel(column, options.role || (column && column.role) || "", index, options.fallbackKey);
};
schema.columnField = (columns, index, fallbackKey = "", options = {}) => (
schema.column(columns, index, { ...options, fallbackKey }).key
);
schema.field = (jsonData, index, fallbackKey = "", options = {}) => (
schema.columnField(schema.columns(jsonData), index, fallbackKey, options)
);
schema.channels = (jsonData, spec) => {
const result = {};
Object.keys(spec || {}).forEach(role => {
result[role] = schema.channel(jsonData, role, spec[role]);
});
return result;
};
})(globalThis);
|