Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
export function guessMode(spec: VisualizationSpec, providedMode?: Mode): Mode {
// Decide mode
if (spec.$schema) {
const parsed = schemaParser(spec.$schema);
if (providedMode && providedMode !== parsed.library) {
console.warn(
`The given visualization spec is written in ${NAMES[parsed.library]}, but mode argument sets ${NAMES[
providedMode
] ?? providedMode}.`
);
}
const mode = parsed.library as Mode;
if (!satisfies(VERSION[mode], `^${parsed.version.slice(1)}`)) {
console.warn(
`The input spec uses ${NAMES[mode]} ${parsed.version}, but the current version of ${NAMES[mode]} is v${VERSION[mode]}.`
);
}
style.innerText =
opts.defaultStyle === undefined || opts.defaultStyle === true
? (embedStyle ?? '').toString()
: opts.defaultStyle;
document.head.appendChild(style);
}
}
const mode = guessMode(spec, opts.mode);
let vgSpec: VgSpec = PREPROCESSOR[mode](spec, config);
if (mode === 'vega-lite') {
if (vgSpec.$schema) {
const parsed = schemaParser(vgSpec.$schema);
if (!satisfies(VERSION.vega, `^${parsed.version.slice(1)}`)) {
console.warn(`The compiled spec uses Vega ${parsed.version}, but current version is v${VERSION.vega}.`);
}
}
}
const div = typeof el === 'string' ? document.querySelector(el) : el;
if (!div) {
throw Error('${el} does not exist');
}
div.classList.add('vega-embed');
if (actions) {
div.classList.add('has-actions');
}
_parseSchema() {
if (!this.spec.$schema) {
this._onWarning(i18n.translate('visTypeVega.vegaParser.inputSpecDoesNotSpecifySchemaWarningMessage', {
defaultMessage: 'The input spec does not specify a {schemaParam}, defaulting to {defaultSchema}',
values: { defaultSchema: `"${DEFAULT_SCHEMA}"`, schemaParam: '"$schema"' },
}));
this.spec.$schema = DEFAULT_SCHEMA;
}
const schema = schemaParser(this.spec.$schema);
const isVegaLite = schema.library === 'vega-lite';
const libVersion = isVegaLite ? vegaLite.version : vega.version;
if (versionCompare(schema.version, libVersion) > 0) {
this._onWarning(i18n.translate('visTypeVega.vegaParser.notValidLibraryVersionForInputSpecWarningMessage', {
defaultMessage: 'The input spec uses {schemaLibrary} {schemaVersion}, but current version of {schemaLibrary} is {libraryVersion}.',
values: {
schemaLibrary: schema.library,
schemaVersion: schema.version,
libraryVersion: libVersion,
},
}));
}
return isVegaLite;
}
export function parseInputSpec(inputSpec, onWarning) {
let spec = { ...inputSpec };
if (!spec.$schema) {
onWarning(`The input spec does not specify a "$schema", defaulting to "${DEFAULT_SCHEMA}"`);
spec.$schema = DEFAULT_SCHEMA;
}
const schema = schemaParser(spec.$schema);
const isVegaLite = schema.library === 'vega-lite';
const libVersion = isVegaLite ? vegaLite.version : vega.version;
if (versionCompare(schema.version, libVersion) > 0) {
onWarning(
`The input spec uses "${schema.library}" ${schema.version}, but ` +
`current version of "${schema.library}" is ${libVersion}.`
);
}
const hostConfig = spec._hostConfig;
if (hostConfig !== undefined) {
delete spec.hostConfig;
if (typeof hostConfig !== 'object') {
throw new Error('_hostConfig must be an object');
}
_parseSchema() {
if (!this.spec.$schema) {
this._onWarning(i18n.translate('vega.vegaParser.inputSpecDoesNotSpecifySchemaWarningMessage', {
defaultMessage: 'The input spec does not specify a {schemaParam}, defaulting to {defaultSchema}',
values: { defaultSchema: `"${DEFAULT_SCHEMA}"`, schemaParam: '"$schema"' },
}));
this.spec.$schema = DEFAULT_SCHEMA;
}
const schema = schemaParser(this.spec.$schema);
const isVegaLite = schema.library === 'vega-lite';
const libVersion = isVegaLite ? vegaLite.version : vega.version;
if (versionCompare(schema.version, libVersion) > 0) {
this._onWarning(i18n.translate('vega.vegaParser.notValidLibraryVersionForInputSpecWarningMessage', {
defaultMessage: 'The input spec uses {schemaLibrary} {schemaVersion}, but current version of {schemaLibrary} is {libraryVersion}.',
values: {
schemaLibrary: schema.library,
schemaVersion: schema.version,
libraryVersion: libVersion,
},
}));
}
return isVegaLite;
}
export function getFormatFromSpec(spec, fallback = FORMAT.UNKNOWN) {
if (spec.$schema) {
const {library} = vegaSchemaUrlParser(spec.$schema);
if (library === 'vega-lite') {
return FORMAT.VEGA_LITE;
}
if (library === 'vega') {
return FORMAT.VEGA;
}
}
return fallback;
}
_parseSchema() {
if (!this.spec.$schema) {
this._onWarning(`The input spec does not specify a "$schema", defaulting to "${DEFAULT_SCHEMA}"`);
this.spec.$schema = DEFAULT_SCHEMA;
}
const schema = schemaParser(this.spec.$schema);
const isVegaLite = schema.library === 'vega-lite';
const libVersion = isVegaLite ? vegaLite.version : vega.version;
if (versionCompare(schema.version, libVersion) > 0) {
this._onWarning(
`The input spec uses ${schema.library} ${schema.version}, but ` +
`current version of ${schema.library} is ${libVersion}.`
);
}
return isVegaLite;
}
_resizeContent (spec, size) {
if (spec.spec) {
spec.spec.width = size.width;
spec.spec.height = size.height;
} else {
spec.width = size.width;
spec.height = size.height;
}
let vegaSpec = spec;
if (spec.$schema && schemaParser(spec.$schema).library === 'vega-lite') {
vegaSpec = vegaLiteCompile(spec).spec;
}
let handler = new Handler();
this.view = new View(parse(vegaSpec))
.renderer(this.options.renderer || 'canvas')
.tooltip(handler.call)
.initialize(this.content)
.hover()
.run();
return {
width: window.parseInt(this.content.firstChild.getAttribute('width')),
height: window.parseInt(this.content.firstChild.getAttribute('height'))
};
}