npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@gjmcn/vizsla

v0.1.1

Published

A simple JavaScript API for Vega-Lite.

Downloads

3

Readme

Vizsla is a simple JavaScript API for Vega-Lite:

//scatter plot
vz(cars)
  .x('Horsepower')
  .y('Miles_per_Gallon')
  .plot()

See this Observable notebook for some interactive examples.

Install/Load

Vizsla uses the Universal Module Definition (UMD) so should work in any JavaScript environment. For example:

  • Node.js:

    • install: npm install --save @gjmcn/vizsla
    • load: const vz = require('@gjmcn/vizsla');
  • Browser, using <script>: creates a global variable vz

API Reference

Constructors

  • Except for vz.Vizsla, do not use new when calling constructors.

  • Constructors can be called with no arguments. For example, vz.repeat() creates a repeat spec with no inner spec; use inner to set the inner spec.


# vz(d, ops)

  d: string (URL of data) or object/array (data)

  ops: options object

Creates a unit spec and, if d is truthy, sets its data — see data.

Unit specs have mark type 'point' by default.

Returns a new unit spec.


# vz.layer(sub0, sub1, sub2, ...)

  sub0, sub1, sub2, ... : specs to be layered; unit and layer specs only

Returns a new layer spec.


# vz.hconcat(sub0, sub1, sub2, ...) # vz.vconcat(sub0, sub1, sub2, ...)

  sub0, sub1, sub2, ... : specs to be concatenated

Returns a new hconcat or vconcat spec.


# vz.facet(sub)

  sub: spec to be faceted

Use row and column (see encode) to set the channels.

Use data to set the data property of a facet. If a facet does not have a data property, it uses the data of the first unit in its inner spec that has a data property — all units in the inner spec that use this data ignore their own data property.

Note: nested facets are not currently supported in Vizsla.

Returns a new facet spec.


# vz.repeat(sub)

  sub: spec to be repeated

Use across and down to set the repeat directions and channels.

Note: Vizsla currently only allows a unit or facet spec to be repeated.

Returns a new repeat spec.


# vz.Vizsla(type = 'unit')

  type: string, 'unit', 'layer', 'hconcat', 'vconcat', 'facet' or 'repeat'

vz.Vizsla is a standard constructor, so e.g. new vz.Vizsla() returns a new unit spec.


Methods

  • The following 'spec' methods are actually methods of vz.Vizsla.prototype.

  • Various methods accept an 'options object' as their final argument. This is used to specify properties which are not covered by a dedicated method or argument. For example:

    vz(cars)
      .bar()
      .x('Cylinders', 'o')
      .y('Acceleration', 'q', {aggregate: 'mean'})  //options object
      .plot();

# spec.mark(type, ops)

  spec type: unit

  type: string, the mark type

  ops: options object

Set mark: type to type plus any additional mark properties specified in ops.

There are also convenience mark methods:

area, bar, boxplot, circle, errorband, errorbar, geoshape, line, point, rect, rule, square, text, tick, trail

//these are equivalent
vz().bar();
vz().mark('bar');

//these are equivalent
vz().bar(ops);
vz().mark('bar', ops);

Mark methods return the spec.


# spec.encode(chn, field, type = 'q', ops)

  spec type: unit, layer, facet

  chn: string, channel name

  field: string, field name

  type: string, 'q', 'quantitative', 't', 'temporal', 'o', 'ordinal', 'n' or 'nominal'

  ops: options object

Set channel chn: field to field, type to type plus any additional properties specified in ops.

There are also convenience methods:

x, y, x2, y2, longitude, latitude, longitude2, latitude2, color, opacity, fillOpacity, strokeOpacity, strokeWidth, size, shape, label, tooltip, href, key, order, detail, row, column

//these are equivalent
vz(cars).x('Horsepower');
vz(cars).encode('x', 'Horsepower');

//these are equivalent
vz(cars).x('Horsepower', 'q', ops);
vz(cars).encode('x', 'Horsepower', 'q', ops);

The label method actually corresponds to the text channel — text is a mark method.

Pass a non-null falsy value as field to omit the field property from the channel object.

Pass true as field as a shortcut for the count aggregate:

//these are equivalent
vz().y(true);
vz().y(false, 'q', {aggregate: 'count'});

//these are equivalent
vz().y(true, 'q', {title: 'Total'});
vz().y(false, 'q', {aggregate: 'count', title: 'Total'});

These methods return the spec.


# spec.transform(t0, t1, t2, ...)

  spec type: any

  t0, t1, t2, ... : object

Set transform.

Returns the spec.


# spec.projection(type = 'mercator', ops)

  spec type: unit, layer

  type: string

  ops: options object

Set projection: type to type plus any additional properties specified in ops.

Returns the spec.


# spec.across(chn, fields = []) # spec.down(chn, fields = [])

  spec type: repeat

  chn: string, channel name

  fields: array of strings, field names

Set across/down repeat to use channel chn and fields fields.


# spec.data(d, ops)

  spec type: unit, facet

  d: string (URL of data) or object/array (data)

  ops: options object

Set data: url/values to d plus any additional properties specified in ops.

Note: only unit and facet specs can have data in Vizsla.

Returns the spec.


# spec.inner(sub0, sub1, sub2, ...)

  spec type: layer, hconcat, vconcat, facet, repeat

   sub0, sub1, sub2, ... : spec object

Set inner spec(s) to sub0, sub1, sub2, ...

Returns the spec.


The following methods set the property of the same name to the argument and return the spec.

| Name | Spec type | | ---- | --------- | | description(string) | any | | title(string) | any | | name(string) | any (only used at top-level)| | $schema(string) | any (only used at top-level) | | background(string) | any (only used at top-level) | | padding(number/object) | any (only used at top-level) | | autosize(string/object) | any (only used at top-level) | | config(object) | any (only used at top-level) | | resolve(object) | layer, hconcat, vconcat, facet, repeat | | center(boolean/object) | hconcat, vconcat, facet, repeat | | spacing(number/object) | hconcat, vconcat, facet, repeat | | bounds(string) | hconcat, vconcat, facet, repeat | | align(string/object) | facet, repeat | | width(number) | unit, layer | | height(number) | unit, layer | | selection(object) | unit |

Example:

vz()
  .width(300)
  .height(200);

# spec.copy(recursive = true)

Copy spec. All properties are deep copied except that:

  • If recursive is falsy, the inner spec(s) are not included in the copy.

  • Inline data is not copied. Specifically, the data object is deep copied except for its values property — the spec and the copy refer to the same data structure.

Returns the copy.


# spec.prep()

Returns a Vega-Lite spec object ready to be rendered. The returned spec is independent of the calling spec with the possible exception that they share inline data (since Vizsla does not copy inline data).

The returned spec uses the Vega-Lite datasets property to avoid repeating inline data and for repeat specs, includes appropriately modified versions of the inner specs.


# vz.Vizsla.prototype.plot()

The plot property is initially undefined. It should be set to a function that renders the spec.

Vega-Embed examples:

  • use Vizsla in an HTML document where Vega-Embed is loaded in a <script> tag:

    vz.Vizsla.prototype.plot = function(elm, opt) {
      return vegaEmbed(elm, this.prep(), opt);
    };
  • use Vizsla in an Observable notebook:

    //in one cell
    vegaEmbed = require("vega-embed@3")
    
    //in another cell
    vz = {
      const vz = await require('@gjmcn/vizsla');
      vz.Vizsla.prototype.plot = function(opt) {
        return vegaEmbed(this.prep(), opt);
      };
      return vz;
    }

Notes

  • Options arguments (ops) take precedence over other arguments and method names. For example, vz().bar({type: 'line'}) will set the mark type to 'line', not 'bar'.

  • Pass null to delete a spec property:

    • encode method: spec.encode('x', null)

    • other methods: pass null as the first argument; e.g. spec.x(null) or spec.mark(null) or spec.inner(null)

  • Spec objects have the structure:

    object
      spec           //object, spec manipulated by vz methods
      type           //string, spec type
      use            //object (unit specs only), data
      repeatChannel  //object (repeat spec only), repeat channel(s)

Contributions

Are welcome! Open an issue or create a pull request.

Also See