How to Get Data into Plotrb

Acknowledgment: This tutorial is based on Vega's documentation.


The very essence of a plot is the data behind it, and thus the information it is trying to convey. In this tutorial, I'm going to talk about how to get the data into Plotrb.

A Data class does not have many attributes (and it's not supposed to). The goal is to let you import the data as effortlessly as possible.

name

The name of the data set must be unique, so that you may reference it at other places of Plotrb.

source

Use an existing data set as source of the new data set.

You can pass in the name of the other data.

data.source('some_other_data')

You can also pass in other data object directly.

some_data = Plotrb::Data.new.name('some_data')
new_data = Plotrb::Data.new.name('new_data').source(some_data)

url

The actual data can come from a url, for example a json file from some web service.

data.url('http://some/web/service/data.json')

file

Similar to url, the data may also come from a local file, for example a csv file in another directory.

data.file('./some/other/dir/data.csv')

values

The data do not necessarily have to come from external sources. Instead of referencing the data source, you can also pass in the actual data values directly to Plotrb.

Currently, Plotrb supports several value types. You can pass in the values as an array.

data.values([1,2,3,4,5])

You can also pass in a hash.

data.values(foo: 1, bar: 2, baz: 3)

Or you can pass in JSON as string directly.

data.values('{"foo":1, "bar":{"baz":2}}')

transform

Specify the transforms to be performed on the data.

format

As of now, Plotrb supports five types of data formats, namely csv, tsv, json, topojson, and treejson.

To assign specific attribtues for a format, you have to define the format first.

data.format(:csv)

You can also use the following grammar.

data.as_csv

(Manipulating attributes for a format must be done inside a block, as you will see in the following examples.)


comments powered by Disqus