Code Monkey home page Code Monkey logo

elm-vega's Introduction

elm-vega

alt text

Declarative visualization for Elm

This library allows you to create Vega-Lite specifications in Elm providing a pure functional interface to declarative visualization construction.

The library does not generate graphical output directly, but instead it allows you to create a JSON specification that can be sent to the Vega-Lite runtime to create the output. This is therefore a 'pure' Elm package without any external non-Elm dependencies.

Example

A simple scatterplot encoding engine power and efficiency as x- and y-position and country of origin with colour:

let
    cars =
        dataFromUrl "data/cars.json" []

    enc =
        encoding
            << position X [ PName "Horsepower", PmType Quantitative ]
            << position Y [ PName "Miles_per_Gallon", PmType Quantitative ]
            << color [ MName "Origin", MmType Nominal ]
in
toVegaLite [ cars, mark Circle [], enc [] ]

This generates a JSON specification that when sent to the Vega-Lite runtime produces the following output:

alt text

The specification generated by elm-vega for this example looks like this:

{
  "$schema": "https://vega.github.io/schema/vega-lite/v2.json",
  "data": {
    "url": "data/cars.json",
    "format": {
      "type": "json"
    }
  },
  "mark": "circle",
  "encoding": {
    "x": {
      "field": "Horsepower",
      "type": "quantitative"
    },
    "y": {
      "field": "Miles_per_Gallon",
      "type": "quantitative"
    },
    "color": {
      "field": "Origin",
      "type": "nominal"
    }
  }
}

Why elm-vega?

A rationale for Elm programmers

There is a demand for good visualization packages with Elm. And there are certainly plenty of data visualization packages available, ranging from low level SVG rendering through a host of charting packages (e.g. Charty and elm-charts) to elegant, opinionated chart construction and more comprehensive visualization libraries. The designs of each reflects a trade-off between concise expression, generalisability and comprehensive functionality.

Despite the numbers of libraries, there is a space for a higher level data visualization package (avoiding, for example the need for explicit construction of chart axes) but one that offers the flexibility to create a wide range data visualization types and styles. In particular no existing libraries offer easily constructed interaction and view composition (building 'dashboards' comprising many chart types). elm-vega is designed to fill that gap.

Characteristics of elm-vega

  • Built upon the widely used Vega-Lite specification that has an academic robustness and momentum behind its development (Vega-Lite is itself built upon the hugely influential Grammar of Graphics).

  • High-level declarative specification (a chart can be fully specified in as few as five lines of code)

  • Extensive use of union types means "the compiler is your friend" when building and debugging complex visualizations.

  • Flexible interaction for selecting, filtering and zooming built-in to the specification.

  • Hierarchical view composition allows complex visualization dashboards to be built from trees of simpler views.

A rationale for data visualisers

Vega-Lite hits the sweet spot of abstraction between lower-level specifications such as D3 and higher level visualization software such as Tableau. By using JSON to fully encode a visualization specification Vega-Lite is portable across a range of platforms and sits well in the JavaScript / Web ecosystem. Yet JSON is really an interchange format rather than one suited directly for visualization design and construction.

By wrapping Vega-Lite within the Elm language, we can avoid working with JSON directly and instead take advantage of a typed functional programming environment for improved error support and customisation. This greatly improves reusability of code (for example, it is easy to create custom chart types such as box-and-whisker plots that can be used with a range of datasets) and integration with larger programming projects.

Elm and elm-vega provide an ideal environment for educators wishing to teach Data Visualization combining the beginner-friendly design of Elm with the robust and theoretically underpinned design of a grammar of graphics.

Limitations

  • elm-vega does not render graphics directly, but instead generates data visualization specifications that may be passed to JavaScript for rendering.

  • elm-vega is currently limited to the Vega-Lite specification (and a small amount of the Vega specification) so is limited to Cartesian coordinate systems (e.g. no pie charts or rose diagrams). However, as Vega-Lite develops (e.g. geospatial mapping will be available shortly), so will elm-vega. And the door remains open for full Vega integration in the future.

  • While limited animation is possible through interaction and dynamic data generation, there is no direct support for animated transitions (unlike D3 for example).

Further Reading

elm-vega's People

Contributors

jwolondon avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.