Feat: Cast datatable to pointseries (#28872)

* feat: allow casting datatable to pointseries

* fix: use correct column type for pointseries

columns are an object, not an array
This commit is contained in:
Joe Fleming 2019-01-17 14:34:29 -07:00 committed by GitHub
parent 2bea9afbfe
commit 1038c77e84
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 2 deletions

View file

@ -17,7 +17,7 @@
* under the License.
*/
import { map, zipObject } from 'lodash';
import { map, pick, zipObject } from 'lodash';
export const datatable = () => ({
name: 'datatable',
@ -78,5 +78,32 @@ export const datatable = () => ({
},
};
},
pointseries: datatable => {
// datatable columns are an array that looks like [{ name: "one", type: "string" }, { name: "two", type: "string" }]
// rows look like [{ one: 1, two: 2}, { one: 3, two: 4}, ...]
const validFields = ['x', 'y', 'color', 'size', 'text'];
const columns = datatable.columns.filter(column => validFields.includes(column.name));
const rows = datatable.rows.map(row => pick(row, validFields));
return {
type: 'pointseries',
columns: columns.reduce((acc, column) => {
/* pointseries columns are an object that looks like this
* {
* x: { type: "string", expression: "x", role: "dimension" },
* y: { type: "string", expression: "y", role: "dimension" }
* }
*/
acc[column.name] = {
type: column.type,
expression: column.name,
role: 'dimension',
};
return acc;
}, {}),
rows,
};
},
},
});

View file

@ -24,7 +24,7 @@ export const pointseries = () => ({
return {
type: 'pointseries',
rows: [],
columns: [],
columns: {},
};
},
},