Skip to content

Commit 505d87c

Browse files
PatrickViennePatrickVienneJohann EspenMetalBlueberrypafo
authored
Implement tooling to generate multiple plotly schemas (#18)
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>
1 parent d03294c commit 505d87c

File tree

205 files changed

+165635
-76886
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

205 files changed

+165635
-76886
lines changed

.github/workflows/test.yaml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,13 @@ jobs:
1818
with:
1919
go-version: "1.22"
2020

21-
- name: Test
22-
run: go test -v ./...
23-
2421
- name: Check Generated Schema
2522
run: |
2623
go generate ./...
2724
git diff --exit-code
25+
26+
- name: Test
27+
run: go test -v ./...
28+
29+
- name: Build all packages
30+
run: go build -v ./...

README.md

Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ The good thing about this package is that it's **automatically generated** based
2323
package main
2424

2525
import (
26-
grob "github.com/MetalBlueberry/go-plotly/graph_objects"
26+
grob "github.com/MetalBlueberry/go-plotly/generated/v2.31.1/graph_objects"
2727
"github.com/MetalBlueberry/go-plotly/offline"
2828
)
2929

@@ -57,6 +57,10 @@ See the examples dir for more examples.
5757

5858
## Structure
5959

60+
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.
61+
62+
Updates to the package will affect all schemas. This will be done as we improve the generator.
63+
6064
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/).
6165

6266
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.
@@ -113,6 +117,63 @@ For strings... This is a little bit more complicated, In AWS package they are us
113117

114118
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.
115119

120+
### Go Plotly Update to any json schema version
121+
122+
#### Update the config to add a new version
123+
124+
To add a new version, add a new entry in: [schemas.yaml](schemas.yaml)
125+
126+
The documentation for each field can be found in [schema.go](generator%2Fschema.go)
127+
128+
Example entry:
129+
```yaml
130+
- Name: Plotly 2.31.1
131+
Tag: v2.31.1
132+
URL: https://raw.githubusercontent.com/plotly/plotly.js/v2.31.1/test/plot-schema.json
133+
Path: schemas/v2.31.1/plot-schema.json
134+
Generated: generated/v2.31.1
135+
CDN: https://cdn.plot.ly/plotly-2.31.1.min.js
136+
```
137+
138+
The local paths are relative to the project root.
139+
140+
#### run download and regeneration
141+
142+
> [!TIP]
143+
> Use this script for easier download of plotly json schemas.
144+
> ```shell
145+
> go run generator/cmd/downloader/main.go --config="schemas.yaml"
146+
> ```
147+
> 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**
148+
> ```shell
149+
> go run generator/cmd/generator/main.go --config="schemas.yaml"
150+
> ```
151+
> Alternatively, you can also generate the go package using following command from the project root:
152+
> ```shell
153+
> go generate ./...
154+
> ```
155+
156+
#### Missing Files?
157+
158+
if in doubt whether all types and traces have been generated, you can use the jsonviewer tool to introspect the json:
159+
https://jsonviewer.stack.hu/
160+
161+
Or use `jq` tool for quick introspection into the json files.
162+
Example:
163+
Display all traces in the schema.json file.
164+
```shell
165+
jq '.schema.traces | keys' schema.json --sort-keys | less
166+
```
167+
168+
More on the `jq` tool on: https://jqlang.github.io/jq/manual/
169+
170+
### plotly online editor sandbox
171+
http://plotly-json-editor.getforge.io/
172+
116173
## Star History
117174

118-
[![Star History Chart](https://api.star-history.com/svg?repos=Metalblueberry/go-plotly&type=Date)](https://star-history.com/#Metalblueberry/go-plotly&Date)
175+
[![Star History Chart](https://api.star-history.com/svg?repos=Metalblueberry/go-plotly&type=Date)](https://star-history.com/#Metalblueberry/go-plotly&Date)
176+
177+
178+
## Official Plotly Release Notes
179+
For detailed changes please follow the release notes of the original JS repo: https://github.com/plotly/plotly.js/releases

examples/bar/bar.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11

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

examples/bar/main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package main
22

33
import (
4-
grob "github.com/MetalBlueberry/go-plotly/graph_objects"
5-
"github.com/MetalBlueberry/go-plotly/offline"
4+
grob "github.com/MetalBlueberry/go-plotly/generated/v2.31.1/graph_objects"
5+
"github.com/MetalBlueberry/go-plotly/generated/v2.31.1/offline"
66
)
77

88
func main() {

examples/bar_custom/bar_custom.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11

22
<head>
3-
<script src="https://cdn.plot.ly/plotly-1.58.4.min.js"></script>
3+
<script src="https://cdn.plot.ly/plotly-2.31.1.min.js"></script>
44
</head>
5-
</body>
5+
<body>
66
<div id="plot"></div>
77
<script>
88
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"}}}')
99
Plotly.newPlot('plot', data);
1010
</script>
11-
<body>
11+
</body>
1212

examples/bar_custom/main.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ import (
44
"image/color"
55
"strconv"
66

7-
grob "github.com/MetalBlueberry/go-plotly/graph_objects"
8-
"github.com/MetalBlueberry/go-plotly/offline"
97
"github.com/lucasb-eyer/go-colorful"
8+
9+
grob "github.com/MetalBlueberry/go-plotly/generated/v2.31.1/graph_objects"
10+
"github.com/MetalBlueberry/go-plotly/generated/v2.31.1/offline"
1011
)
1112

1213
func main() {

examples/colorscale/main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import (
44
"encoding/json"
55
"math"
66

7-
grob "github.com/MetalBlueberry/go-plotly/graph_objects"
8-
"github.com/MetalBlueberry/go-plotly/offline"
7+
grob "github.com/MetalBlueberry/go-plotly/generated/v2.31.1/graph_objects"
8+
"github.com/MetalBlueberry/go-plotly/generated/v2.31.1/offline"
99
)
1010

1111
type ColorScale struct {

examples/go.mod

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
module github.com/metalblueberry/plotly/examples
22

3-
go 1.14
3+
go 1.22
44

55
require (
6-
github.com/MetalBlueberry/go-plotly v0.0.0-20200503142240-1276ab260dcb
7-
github.com/go-gota/gota v0.10.1
6+
github.com/MetalBlueberry/go-plotly v0.4.0
7+
github.com/go-gota/gota v0.12.0
88
github.com/lucasb-eyer/go-colorful v1.2.0
9-
github.com/pkg/browser v0.0.0-20180916011732-0a3d74bf9ce4
10-
gonum.org/v1/gonum v0.7.0 // indirect
9+
github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c
10+
)
11+
12+
require (
13+
golang.org/x/net v0.25.0 // indirect
14+
golang.org/x/sys v0.20.0 // indirect
15+
gonum.org/v1/gonum v0.15.0 // indirect
1116
)
1217

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

0 commit comments

Comments
 (0)