Skip to content

Commit

Permalink
make vega view title optional and add options for window size
Browse files Browse the repository at this point in the history
  • Loading branch information
arnolddevos committed Jul 12, 2024
1 parent 0cef8df commit 317f333
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 17 deletions.
47 changes: 32 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,25 +35,20 @@ For example, the following nushell script will visualize the `b` column of the t

```nushell
use vega.nu
let title = "My Example"
let spec = vega bar b
let data = open example.json
$data | vega view $title $spec
$data | vega view $spec
```

This produces a single bar with height representing the sum of the `b` column in the data. A more interesting bar graph would divide the data up into categories. Identifying the category fields and putting it as a one-liner:

```nushell
open example.json | vega view "Stacked Bar Example" (vega bar b --category=t --subcategory=a)
open example.json | vega view (vega bar b --category=t --subcategory=a)
```

With nushell's excellent data handling abilities you can equally easily visualize CSV, SQLite, JSON and other data sources.
With nushell's excellent data handling abilities you can equally easily visualize CSV, SQLite, JSON and other data sources. You can write your own specification or use one of the built in ones below.

## Visualizations

You can write your own specification or use one of the built in ones below.

### Bar Graph
## Bar Graph

A vega-lite specification for a bar graph.

Expand All @@ -77,10 +72,10 @@ Parameters:
Example:

```nushell
open example.json | vega view "Stacked Bar Example" (vega bar b --category=t --subcategory=a)
open example.json | vega view --title "Stacked Bar Example" (vega bar b --category=t --subcategory=a)
```

### Time Series Plot
## Time Series Plot

A vega-lite specification for a time series plot.

Expand All @@ -105,10 +100,10 @@ Parameters:

Example:
```nushell
open example.json | vega view "Time Series Example" (vega series b t --category a)
open example.json | vega view --title "Time Series Example" (vega series b t --category a)
```

### Scatter Plot
## Scatter Plot

A vega-lite specification for a scatter plot.

Expand All @@ -133,13 +128,35 @@ Parameters:
Example:

```nushell
open example.json | vega view "Scatter Plot Example" (vega scatter b t --category a)
open example.json | vega view --title "Scatter Plot Example" (vega scatter b t --category a)
```

### Writing a Specification
## Writing a Specification

The [vega-lite gallery](https://vega.github.io/vega-lite/examples/) is a good place to start when developing a visualization. You can adapt one of these specifications by changing the field names to match your data. You can omit the `data` section of the specification.

## View Options

The `vega view` nushell command takes several options controlling how it displaya a Vega visualization of the input data in a window. (We have seen the `--title` option in the examples above.)

Usage:
```
> view {flags} <spec>
```

Flags:
```
--title <String> - title for the window (default: 'Vega View')
--width <Number> - width of the window (default: 1000)
--height <Number> - height of the window (default: 800)
-h, --help - Display the help message for this command
```

Parameters:
```
spec <record>: a vega-lite specification
```

## The `vega-view` Executable

The `vega-view` executable creates and controls the webview. The `vega view` nushell command wraps the executable and takes care of conversions from nushell tables. The wrapper locates the executable via the environment variable `$env.vega_view_bin`.
Expand Down
10 changes: 8 additions & 2 deletions vega.nu
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,14 @@ export-env {
$env.vega_view_bin = ($env.FILE_PWD | path join "target" "release" "vega-view")
}

export def view [title: string spec: record] {
to json | ^$env.vega_view_bin --title $title ($spec | upsert data { url: "/data"} | to json)
# Display a Vega visualization of the input data in a new window.
export def view [
spec: record # a vega-lite specification
--title: string = "Vega View" # title for the window
--width: number = 1000 # width of the window
--height: number = 800 # height of the window
] {
to json | ^$env.vega_view_bin --title $title --width $width --height $height ($spec | upsert data { url: "/data"} | to json)
}

# vega-lite specification for a bar graph
Expand Down

0 comments on commit 317f333

Please sign in to comment.