pipechart 1.0.1 → 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/detect.js +22 -3
- package/package.json +1 -1
package/lib/detect.js
CHANGED
|
@@ -64,6 +64,22 @@ function findNumericField(obj) {
|
|
|
64
64
|
return null;
|
|
65
65
|
}
|
|
66
66
|
|
|
67
|
+
/**
|
|
68
|
+
* Find the first string field in an object that is not the excluded field.
|
|
69
|
+
* @param {object} obj
|
|
70
|
+
* @param {string} excludeField — the numeric value field to skip
|
|
71
|
+
* @returns {string|null}
|
|
72
|
+
*/
|
|
73
|
+
function findStringField(obj, excludeField) {
|
|
74
|
+
if (!obj || typeof obj !== 'object') return null;
|
|
75
|
+
var keys = Object.keys(obj);
|
|
76
|
+
for (var i = 0; i < keys.length; i++) {
|
|
77
|
+
if (keys[i] === excludeField) continue;
|
|
78
|
+
if (typeof obj[keys[i]] === 'string') return keys[i];
|
|
79
|
+
}
|
|
80
|
+
return null;
|
|
81
|
+
}
|
|
82
|
+
|
|
67
83
|
/**
|
|
68
84
|
* Detect the shape of the input data and return a descriptor.
|
|
69
85
|
*
|
|
@@ -126,14 +142,16 @@ function detect(data, opts) {
|
|
|
126
142
|
// Array of objects with a specified or auto-detected numeric field
|
|
127
143
|
var field = opts.field || findNumericField(data[0]);
|
|
128
144
|
if (field) {
|
|
145
|
+
// Find the first string field in the first object that isn't the value field
|
|
146
|
+
var labelField = findStringField(data[0], field);
|
|
129
147
|
var objValues = [];
|
|
130
148
|
var objLabels = [];
|
|
131
149
|
for (var j = 0; j < data.length; j++) {
|
|
132
150
|
var v = data[j][field];
|
|
133
151
|
if (!isNum(v)) continue;
|
|
134
152
|
objValues.push(v);
|
|
135
|
-
// Use
|
|
136
|
-
var label =
|
|
153
|
+
// Use the detected string label field, or fall back to index
|
|
154
|
+
var label = (labelField && data[j][labelField] != null) ? data[j][labelField] : String(j);
|
|
137
155
|
objLabels.push(String(label));
|
|
138
156
|
}
|
|
139
157
|
return {
|
|
@@ -173,5 +191,6 @@ module.exports = {
|
|
|
173
191
|
isTimeSeries: isTimeSeries,
|
|
174
192
|
extractTime: extractTime,
|
|
175
193
|
extractValue: extractValue,
|
|
176
|
-
findNumericField: findNumericField
|
|
194
|
+
findNumericField: findNumericField,
|
|
195
|
+
findStringField: findStringField
|
|
177
196
|
};
|