Skip to content

Commit

Permalink
Implement tooling to generate multiple plotly schemas (#18)
Browse files Browse the repository at this point in the history
Implement tooling to generate multiple plotly schemas 

---------

Co-authored-by: PatrickVienne <patrick.fodor.sc@gmail.com>
Co-authored-by: Johann Espen <johann.espen.dev@gmail.com>
Co-authored-by: metalblueberry <metalblueberry@gmail.com>
Co-authored-by: pafo <patrick.fodor@e-star.com>
  • Loading branch information
5 people authored May 25, 2024
1 parent d03294c commit 505d87c
Show file tree
Hide file tree
Showing 205 changed files with 165,635 additions and 76,886 deletions.
9 changes: 6 additions & 3 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,13 @@ jobs:
with:
go-version: "1.22"

- name: Test
run: go test -v ./...

- name: Check Generated Schema
run: |
go generate ./...
git diff --exit-code
- name: Test
run: go test -v ./...

- name: Build all packages
run: go build -v ./...
65 changes: 63 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ The good thing about this package is that it's **automatically generated** based
package main

import (
grob "github.com/MetalBlueberry/go-plotly/graph_objects"
grob "github.com/MetalBlueberry/go-plotly/generated/v2.31.1/graph_objects"
"github.com/MetalBlueberry/go-plotly/offline"
)

Expand Down Expand Up @@ -57,6 +57,10 @@ See the examples dir for more examples.

## Structure

To keep the plotly.js independent of the version of this package, the generated directory contains a directory per plotly version supported. The plan is to support all minor releases and update as patches are released. But because I can not do it myself, I will accept PRs if anyone wants any specific version.

Updates to the package will affect all schemas. This will be done as we improve the generator.

Each trace type has its own file on **graph_objecs (grob)** package. The file contains the main structure and all the needed nested objects. Files ending with **_gen** are automatically generated files running `go generate`. This is executing the code in **generator** package to generate the structures from the plotly schema. The types are documented, but you can find more examples and extended documentation at [Plotly's documentation](https://plotly.com/python/).

The values that can hold single values or arrays are defined as custom types that are a type definition of `interfaces{}`. Most common case are X and Y values. You can pass any number slice and it will work (`[]float64`,`[]int`,`[]int64`...). In case of Hovertext, you can provide a `[]string` to display a text for each point, a `string` to display the same for all or `[]int` to display a number.
Expand Down Expand Up @@ -113,6 +117,63 @@ For strings... This is a little bit more complicated, In AWS package they are us

For numbers... It's similar to strings, Right now you cannot create plots with integer/float numbers with 0 value. I've only encounter problems when trying to remove the margin and can be workaround with an small value like `0.001`. I would like to avoid using interface{} or defining types again to keep the package interface as simple as possible.

### Go Plotly Update to any json schema version

#### Update the config to add a new version

To add a new version, add a new entry in: [schemas.yaml](schemas.yaml)

The documentation for each field can be found in [schema.go](generator%2Fschema.go)

Example entry:
```yaml
- Name: Plotly 2.31.1
Tag: v2.31.1
URL: https://raw.githubusercontent.com/plotly/plotly.js/v2.31.1/test/plot-schema.json
Path: schemas/v2.31.1/plot-schema.json
Generated: generated/v2.31.1
CDN: https://cdn.plot.ly/plotly-2.31.1.min.js
```
The local paths are relative to the project root.
#### run download and regeneration
> [!TIP]
> Use this script for easier download of plotly json schemas.
> ```shell
> go run generator/cmd/downloader/main.go --config="schemas.yaml"
> ```
> Then run the generator, which will clean up the generated files in **graph_objects** folder of each version and regenerate the new graph objects. DO NOT REMOVE **graph_objects/plotly.go**
> ```shell
> go run generator/cmd/generator/main.go --config="schemas.yaml"
> ```
> Alternatively, you can also generate the go package using following command from the project root:
> ```shell
> go generate ./...
> ```

#### Missing Files?

if in doubt whether all types and traces have been generated, you can use the jsonviewer tool to introspect the json:
https://jsonviewer.stack.hu/

Or use `jq` tool for quick introspection into the json files.
Example:
Display all traces in the schema.json file.
```shell
jq '.schema.traces | keys' schema.json --sort-keys | less
```

More on the `jq` tool on: https://jqlang.github.io/jq/manual/

### plotly online editor sandbox
http://plotly-json-editor.getforge.io/

## Star History

[![Star History Chart](https://api.star-history.com/svg?repos=Metalblueberry/go-plotly&type=Date)](https://star-history.com/#Metalblueberry/go-plotly&Date)
[![Star History Chart](https://api.star-history.com/svg?repos=Metalblueberry/go-plotly&type=Date)](https://star-history.com/#Metalblueberry/go-plotly&Date)


## Official Plotly Release Notes
For detailed changes please follow the release notes of the original JS repo: https://github.com/plotly/plotly.js/releases
6 changes: 3 additions & 3 deletions examples/bar/bar.html
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@

<head>
<script src="https://cdn.plot.ly/plotly-1.58.4.min.js"></script>
<script src="https://cdn.plot.ly/plotly-2.31.1.min.js"></script>
</head>
</body>
<body>
<div id="plot"></div>
<script>
data = JSON.parse('{"data":[{"type":"bar","x":[1,2,3],"y":[1,2,3]}],"layout":{"title":{"text":"A Figure Specified By Go Struct"}}}')
Plotly.newPlot('plot', data);
</script>
<body>
</body>

4 changes: 2 additions & 2 deletions examples/bar/main.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package main

import (
grob "github.com/MetalBlueberry/go-plotly/graph_objects"
"github.com/MetalBlueberry/go-plotly/offline"
grob "github.com/MetalBlueberry/go-plotly/generated/v2.31.1/graph_objects"
"github.com/MetalBlueberry/go-plotly/generated/v2.31.1/offline"
)

func main() {
Expand Down
6 changes: 3 additions & 3 deletions examples/bar_custom/bar_custom.html
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@

<head>
<script src="https://cdn.plot.ly/plotly-1.58.4.min.js"></script>
<script src="https://cdn.plot.ly/plotly-2.31.1.min.js"></script>
</head>
</body>
<body>
<div id="plot"></div>
<script>
data = JSON.parse('{"data":[{"type":"bar","hoverinfo":"none","marker":{"color":"#9ec9e0","line":{"color":"rgb(8,48,107)","width":1.5},"opacity":0.6},"text":["20","14","23"],"textposition":"auto","x":["Product A","Product B","Product C"],"y":[20,14,23]}],"layout":{"title":{"text":"A Figure Specified By Go Struct"}}}')
Plotly.newPlot('plot', data);
</script>
<body>
</body>

5 changes: 3 additions & 2 deletions examples/bar_custom/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import (
"image/color"
"strconv"

grob "github.com/MetalBlueberry/go-plotly/graph_objects"
"github.com/MetalBlueberry/go-plotly/offline"
"github.com/lucasb-eyer/go-colorful"

grob "github.com/MetalBlueberry/go-plotly/generated/v2.31.1/graph_objects"
"github.com/MetalBlueberry/go-plotly/generated/v2.31.1/offline"
)

func main() {
Expand Down
4 changes: 2 additions & 2 deletions examples/colorscale/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"encoding/json"
"math"

grob "github.com/MetalBlueberry/go-plotly/graph_objects"
"github.com/MetalBlueberry/go-plotly/offline"
grob "github.com/MetalBlueberry/go-plotly/generated/v2.31.1/graph_objects"
"github.com/MetalBlueberry/go-plotly/generated/v2.31.1/offline"
)

type ColorScale struct {
Expand Down
15 changes: 10 additions & 5 deletions examples/go.mod
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
module github.com/metalblueberry/plotly/examples

go 1.14
go 1.22

require (
github.com/MetalBlueberry/go-plotly v0.0.0-20200503142240-1276ab260dcb
github.com/go-gota/gota v0.10.1
github.com/MetalBlueberry/go-plotly v0.4.0
github.com/go-gota/gota v0.12.0
github.com/lucasb-eyer/go-colorful v1.2.0
github.com/pkg/browser v0.0.0-20180916011732-0a3d74bf9ce4
gonum.org/v1/gonum v0.7.0 // indirect
github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c
)

require (
golang.org/x/net v0.25.0 // indirect
golang.org/x/sys v0.20.0 // indirect
gonum.org/v1/gonum v0.15.0 // indirect
)

replace github.com/MetalBlueberry/go-plotly => ./../
Loading

0 comments on commit 505d87c

Please sign in to comment.