gopherdata / gopherdata/gophernotes

Directly support display MIME types for Vega and Vega-lite JSON rendering?

Offen
#203 4 Kommentare 1 Reaktion 0 zugewiesene Personen Auf GitHub ansehen
enhancement
Vorherrschende Sprache
Go
Sterne
4k
Forks
264
Ø Merge
7 T. 21 Std.
Gemergte PRs (30 T.)
1

Beschreibung

Hi, I've finally worked out how to use gophernotes to display [Vega](https://vega.github.io/vega/) / [VegaLite](https://vega.github.io/vega-lite/) SVG graphics, (crudely) similar to what the [Altair Python package](https://altair-viz.github.io/index.html) can do. This works "out of the box" in Nteract and JupyterLab. In classic Jupyter notebooks I believe it requires installing a Jupyter extension to support Vega(lite).

Anyway, thanks to the [excellent example documentation](https://github.com/gopherdata/gophernotes/blob/master/examples/Display.ipynb) for the Gophernotes `display` support package, I was able to figure out how to make this work:

```golang
// Support for Vega...
type Vega struct {
Json map[string]interface{}
}

func (v Vega) SimpleRender() display.MIMEMap {
return display.MIMEMap{
`application/vnd.vega.v5+json`: v.Json,
`text/plain`: "",
}
}

// ...and VegaLite
type VegaLite struct {
Json map[string]interface{}
}

func (vl VegaLite) SimpleRender() display.MIMEMap {
return display.MIMEMap{
`application/vnd.vegalite.v4+json`: vl.Json,
`text/plain`: "",
}
}
```

To use, simply create and populate one of the above types:

```golang
import "encoding/json"

g := []byte(`
{
"$schema": "https://vega.github.io/schema/vega-lite/v2.0.json",
"description": "A simple bar chart with embedded data.",
"data": {
"values": [
{"a": "A", "b": 28},
{"a": "B", "b": 55},
{"a": "C", "b": 43},
{"a": "D", "b": 91},
{"a": "E", "b": 81},
{"a": "F", "b": 53},
{"a": "G", "b": 19},
{"a": "H", "b": 87},
{"a": "I", "b": 52}
]
},
"mark": "bar",
"encoding": {
"x": {"field": "a", "type": "ordinal"},
"y": {"field": "b", "type": "quantitative"}
}
}`)

vl := VegaLite{}
err := json.Unmarshal(g, &vl.Json)
vl
```

![visualization](https://user-images.githubusercontent.com/428642/79538802-e7e11300-8039-11ea-8ca1-b1049ad38e5b.png)

It would be cool if this was actually built into Gophernotes, or at least documented, since Vega(Lite) are pretty world class declarative plotting libraries and this opens them up for use with gophernotes!

Beitragsleitfaden

Für dieses Repository ist kein Beitragsleitfaden indexiert

Bewertung

Dieses Issue wurde noch nicht bewertet.

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.