GithubHelp home page GithubHelp logo

matteverson / vegas Goto Github PK

View Code? Open in Web Editor NEW

This project forked from vegas-viz/vegas

0.0 2.0 0.0 811 KB

The missing MatPlotLib for Scala + Spark

License: MIT License

Scala 95.58% Jupyter Notebook 4.42%

vegas's Introduction

Vegas

Vegas

TravisCI codecov

Vegas aims to be the missing MatPlotLib for the Scala and Spark world. Vegas wraps around Vega-Lite but provides syntax more familar (and type checked) for use within Scala.

Quick start

Add the following jar as an SBT dependacy

libraryDependencies += "com.github.aishfenton" %% "vegas_2.11" % "0.2.3"

And then use the following code to render a plot into a pop-up window (see below for more details on controlling how and where Vegas renders).

import vegas._
import vegas.render.WindowRenderer._

val chart = Vegas("Country Pop").
  withData(
    Map("country" -> "USA", "population" -> 314),
    Map("country" -> "UK", "population" -> 64),
    Map("country" -> "DK", "population" -> 80)
  ).
  encodeX("country", Nominal).
  encodeY("population", Quantitative).
  mark(Bar)

chart.show

"Readme Chart 1"

See further examples here

Rendering

Vegas provides a number of options for rendering charts out to. The primary focus is using Vegas within interactive notebook environments, such as Jupyter and Zeppelin.

Notebooks

Jupyter - Scala

If you're using jupyter-scala, then you must incldue the following in your notebook before using Vegas.

classpath.add("com.github.aishfenton" %% "vegas" % "{vegas-version}")
import vegas._
import vegas.render.HTMLRenderer._
implicit val displayer: String => Unit = display.html(_)

Jupyter - Apache Toree

And if you're using Apache Toree, then this:

%AddDeps com.github.aishfenton vegas_{scala-version} {vegas-version} --transitive
import vegas._
import vegas.render.HTMLRenderer._
implicit val displayer: String => Unit = { s => kernel.display.content("text/html", s) }

Zeppelin

And lastly if you're using Apache Zeppelin Zeppelin then use the following to initialize the notebook.

%dep
z.load("com.github.aishfenton:vegas_{scala-version}:{vegas-version}")
import vegas._
import vegas.render.HTMLRenderer._
implicit val displayer: String => Unit = { s => print("%html " + s) }

The last line in each of the above is required to connect Vegas to the notebook's HTML renderer (so that the returned HTML is rendered instead of displayed as a string).

See a comprehensive list example notebook of plots here

Standalone

Vegas can also be used to produce standalone HTML or even render plots within a built-in display app (useful if you wanted to display charts for a command-line-app).

HTML or JSON

The following renders the plot as both HTML (which is printed to the console), and as JSON containing the Vega-lite spec, which can copy-and-pasted into the Vega-lite editor.

import vegas._
import vegas.render.HTMLRenderer._

val chart = Vegas("Country Pop").
  withData(
    Map("country" -> "USA", "population" -> 314),
    Map("country" -> "UK", "population" -> 64),
    Map("country" -> "DK", "population" -> 80)
  ).
  encodeX("country", Nominal).
  encodeY("population", Quantitative).
  mark(Bar)

println(chart.pageHTML())
println(chart.spec.toJson())

Window

Vegas also contains a self-contained display app for displaying plots (internally JavaFX's HTML renderer is used). The following demonstrates this and can be used from the command line.

import vegas._
import vegas.render.WindowRenderer._

val chart = Vegas("Country Pop").
  withData(
    Map("country" -> "USA", "population" -> 314),
    Map("country" -> "UK", "population" -> 64),
    Map("country" -> "DK", "population" -> 80)
  ).
  encodeX("country", Nominal).
  encodeY("population", Quantitative).
  mark(Bar)

chart.show

Spark integration

Vegas comes with an optional extension package that makes it easier to work with Spark DataFrames and RDDs. First you'll need an extra import

libraryDependencies += "com.github.aishfenton" % "vegas-spark_{scala-version}" % "{vegas-version}"
import vegas.spark.Spark._

This adds the following new methods:

  • withDataFrame(df: DataFrame)
  • withRDD(rdd: RDD[Product])

In the first case, each DataFrame column is exposed as a field keyed using the column's name. In the second case, an RDD of case classes is expected, and reflection is used to map the case class's fields to fields within Vegas.

Flink integration


Vegas also comes with an optional extension package that makes it easier to work with Flink DataSets. You'll also need to import:

libraryDependencies += "com.github.aishfenton % "vegas-flink_{scala-version} % "{vegas-version}"

To use:

import vegas.flink.Flink._

This adds the following method:

* withData[T <: Product](ds: DataSet[T])

Similarly, to the RDD concept in Spark, a DataSet of case classes or tuples is expected and reflection is used to map the case class' fields to fields within Vegas. In the case of tuples you can encode the fields using "_1", "_2" and so on.

Plot Options

TODO

Contributing

See here for more information on contributing bug fixes and features.

vegas's People

Contributors

aishfenton avatar dbtsai avatar rogermenezes avatar asragab avatar daniperez avatar

Watchers

James Cloos avatar Matt Everson 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.