Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement tooling to generate multiple plotly schemas #18

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
7b2a11a
update to plotly version 2.29.0
Feb 9, 2024
29541a0
fix typos
Feb 9, 2024
7cdb932
update to go 1.22, fix review comments
Apr 13, 2024
0571e9c
reduce number of changes
MetalBlueberry Apr 14, 2024
612c969
add downloader script, improve readme
Apr 20, 2024
78a13f9
run go generate
MetalBlueberry May 1, 2024
ee89afd
create definition to support multiple versions of plotly
May 2, 2024
2293e99
update dependencies
May 11, 2024
e60c99f
complete full generation for each version separately
May 11, 2024
2a3e744
update notes
May 11, 2024
da906d6
enable offline plots
May 12, 2024
6aa6e62
allow arrays when the schema definition states 'arrayOK'
May 12, 2024
1492cb7
allow usage of frames
May 12, 2024
1640d4c
allow redirecting outputstream of create plot
May 12, 2024
3b1fe76
keep the offline folder, generate for each version
May 18, 2024
c85ba65
remove array enabling
May 18, 2024
ef4579f
Merge branch 'master' into feature/plotly_v2.29.0
PatrickVienne May 19, 2024
c3272da
fix typo
MetalBlueberry May 22, 2024
e9d5fa0
Refactor
MetalBlueberry May 22, 2024
b452fc1
Test the packages build
MetalBlueberry May 22, 2024
9411066
remove frames
May 24, 2024
49edb2b
remove change on offline package
MetalBlueberry May 25, 2024
1cd3708
cleanup readme
MetalBlueberry May 25, 2024
92f38a9
fix template syntax
MetalBlueberry May 25, 2024
2682f35
regenerate
MetalBlueberry May 25, 2024
8277e26
fix build issue with import path
MetalBlueberry May 25, 2024
f737784
fix CDN
MetalBlueberry May 25, 2024
bfe1851
fix go mod
MetalBlueberry May 25, 2024
4ced1cd
fix examples output
MetalBlueberry May 25, 2024
b55db8a
clarify the purpose of the schema
MetalBlueberry May 25, 2024
b95b94d
fix loading the schema from embed
MetalBlueberry May 25, 2024
b0c12cd
remove unused code
MetalBlueberry May 25, 2024
fd02e0d
readme update
MetalBlueberry May 25, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading