datahub/examples/simple-example/lib/viewSpecConversion.ts
Luccas Mateus 20ac80a5e8
Example of how to create a data portal in 5 minutes (#769)
* [example][m] - start of a simple-example

* Empty-Commit

* [simple-example][sm] - change from repos.json to datasets.json

* [example][m] - changed styling and added octokit

* [build][sm] - fix build
2023-04-18 13:51:48 -03:00

48 lines
996 B
TypeScript

export function convertSimpleToVegaLite(view, resource) {
const x = resource.schema.fields.find((f) => f.name === view.spec.group);
const y = resource.schema.fields.find((f) => f.name === view.spec.series[0]);
const xType = inferVegaType(x.type);
const yType = inferVegaType(y.type);
let vegaLiteSpec = {
$schema: "https://vega.github.io/schema/vega-lite/v5.json",
mark: {
type: view.spec.type,
color: "black",
strokeWidth: 1,
tooltip: true,
},
title: view.title,
width: "container",
height: 300,
selection: {
grid: {
type: "interval",
bind: "scales",
},
},
encoding: {
x: {
field: x.name,
type: xType,
},
y: {
field: y.name,
type: yType,
},
},
};
return vegaLiteSpec;
}
const inferVegaType = (fieldType) => {
switch (fieldType) {
case "date":
return "Temporal";
case "number":
return "Quantitative";
}
};