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 DataArray type #30

Merged
merged 2 commits into from
Aug 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
6 changes: 3 additions & 3 deletions examples/animation/animation.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func main() {
frames = append(frames, grob.Frame{
Data: []types.Trace{
&grob.Scatter{
Y: []float64{rand.Float64(), rand.Float64(), rand.Float64()},
Y: types.DataArray([]float64{rand.Float64(), rand.Float64(), rand.Float64()}),
},
},
})
Expand All @@ -46,8 +46,8 @@ func main() {
fig := &grob.Fig{
Data: []types.Trace{
&grob.Scatter{
X: []float64{1, 2, 3},
Y: []float64{0, 0.5, 1},
X: types.DataArray([]float64{1, 2, 3}),
Y: types.DataArray([]float64{0, 0.5, 1}),
Line: &grob.ScatterLine{
Simplify: types.False,
},
Expand Down
4 changes: 2 additions & 2 deletions examples/bar.html
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@

<head>
<script src="https://cdn.plot.ly/plotly-2.31.1.min.js"></script>
<script src="https://cdn.plot.ly/plotly-2.34.0.min.js"></script>
</head>
<body>
<div id="plot"></div>
<script>
data = JSON.parse(atob('eyJkYXRhIjpbeyJ0eXBlIjoiYmFyIiwiYmFzZSI6bnVsbCwiaG92ZXJpbmZvIjpudWxsLCJob3ZlcnRlbXBsYXRlIjpudWxsLCJob3ZlcnRleHQiOm51bGwsIm1ldGEiOm51bGwsIm9mZnNldCI6bnVsbCwidGV4dCI6bnVsbCwidGV4dHBvc2l0aW9uIjpudWxsLCJ0ZXh0dGVtcGxhdGUiOm51bGwsIndpZHRoIjpudWxsLCJ4IjpbMSwyLDNdLCJ5IjpbMSwyLDNdfV0sImxheW91dCI6eyJtZXRhIjpudWxsLCJ0aXRsZSI6eyJ0ZXh0IjoiQSBGaWd1cmUgJ1NwZWNpZmllZCcgQnkgR28gU3RydWN0In19fQ=='))
data = JSON.parse(atob('eyJkYXRhIjpbeyJ0eXBlIjoiYmFyIiwieCI6WzEsMiwzXSwieSI6WzEsMiwzXX1dLCJsYXlvdXQiOnsidGl0bGUiOnsic3VidGl0bGUiOnsidGV4dCI6IkFtYXppbmcifSwidGV4dCI6IkEgRmlndXJlIFNwZWNpZmllZCBCeSBHbyBTdHJ1Y3QifX19'))
Plotly.newPlot('plot', data);
</script>
</body>
Expand Down
4 changes: 2 additions & 2 deletions examples/bar/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ func main() {
fig := &grob.Fig{
Data: []types.Trace{
&grob.Bar{
X: []float64{1, 2, 3},
Y: []float64{1, 2, 3},
X: types.DataArray([]float64{1, 2, 3}),
Y: types.DataArray([]float64{1, 2, 3}),
},
},
Layout: &grob.Layout{
Expand Down
4 changes: 2 additions & 2 deletions examples/bar_custom/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ func main() {
}

trace1 := &grob.Bar{
X: xValue,
Y: yValue,
X: types.DataArray(xValue),
Y: types.DataArray(yValue),
Text: types.ArrayOKArray(toString(yValue)...),
Textposition: types.ArrayOKValue(grob.BarTextpositionAuto),
Hoverinfo: types.ArrayOKValue(grob.BarHoverinfoNone),
Expand Down
6 changes: 3 additions & 3 deletions examples/colorscale/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ func main() {
fig := &grob.Fig{
Data: []types.Trace{
&grob.Scatter3d{
X: x,
Y: y,
Z: z,
X: types.DataArray(x),
Y: types.DataArray(y),
Z: types.DataArray(z),
Mode: grob.Scatter3dModeMarkers,
Marker: &grob.Scatter3dMarker{
Autocolorscale: types.False,
Expand Down
9 changes: 9 additions & 0 deletions examples/numpy_bdata/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
FROM python:3.9-slim

WORKDIR /app

RUN pip install numpy

COPY compute.py .

CMD ["python", "compute.py"]
14 changes: 14 additions & 0 deletions examples/numpy_bdata/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
IMAGE_NAME = plotly-data-generator

all: build run

build:
@docker build -t $(IMAGE_NAME) .

run:
@docker run --rm $(IMAGE_NAME)



clean:
@docker rmi $(IMAGE_NAME)
33 changes: 33 additions & 0 deletions examples/numpy_bdata/compute.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import numpy as np
import json
import base64

# Generate Z data using NumPy
# Let's create a similar 3x5 matrix with some values
z = np.array([
[1, np.nan, 30, 50, 1],
[20, 1, 60, 80, 30],
[30, 60, 1, -10, 20]
])



dtype = z.dtype
bdata = z.tobytes() # Binary data representation
shape = z.shape

# Encode the binary data as a base64 string
z_bdata_base64 = base64.b64encode(bdata).decode('utf-8')

# Create the dictionary that represents the JSON object
z_data = {
'dtype': str(dtype),
'shape': shape,
'bdata': z_bdata_base64
}

# Convert the dictionary to a JSON object
json_data = json.dumps(z_data, indent=2)

# Output the JSON object
print(json_data)
52 changes: 52 additions & 0 deletions examples/numpy_bdata/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package main

import (
"encoding/json"
"os/exec"

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

func main() {
// https://plotly.com/javascript/heatmaps/
// var data = [
// {
// z: [[1, null, 30, 50, 1], [20, 1, 60, 80, 30], [30, 60, 1, -10, 20]],
// x: ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday'],
// y: ['Morning', 'Afternoon', 'Evening'],
// type: 'heatmap',
// hoverongaps: false
// }
// ];
zJSON, err := exec.Command("make").Output()
if err != nil {
panic(err)
}
type bdata struct {
DType types.DType
Shape []int
BData []byte
}

z := &bdata{}
err = json.Unmarshal(zJSON, z)
if err != nil {
panic(err)
}

fig := &grob.Fig{
Data: []types.Trace{
&grob.Heatmap{
X: types.DataArray([]string{"Monday", "Tuesday", "Wednesday", "Thursday", "Friday"}),
Y: types.DataArray([]string{"Morning", "Afternoon", "Evening"}),
Z: types.BDataArray(z.DType, z.BData, z.Shape),
Hoverongaps: types.False,
},
},
}

offline.Show(fig)

}
4 changes: 2 additions & 2 deletions examples/range_slider/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ func main() {

fig := &grob.Fig{}
fig.Data = append(fig.Data, &grob.Scatter{
X: df.Col("Date").Records(),
Y: df.Col("High").Float(),
X: types.DataArray(df.Col("Date").Records()),
Y: types.DataArray(df.Col("High").Float()),
})

fig.Layout = &grob.Layout{
Expand Down
4 changes: 2 additions & 2 deletions examples/responsive/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ func main() {
fig := &grob.Fig{
Data: []types.Trace{
&grob.Bar{
X: []float64{1, 2, 3},
Y: []float64{1, 2, 3},
X: types.DataArray([]float64{1, 2, 3}),
Y: types.DataArray([]float64{1, 2, 3}),
},
},
Layout: &grob.Layout{
Expand Down
4 changes: 2 additions & 2 deletions examples/scatter/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ func main() {
fig := &grob.Fig{
Data: []types.Trace{
&grob.Scatter{
X: t,
Y: y,
X: types.DataArray(t),
Y: types.DataArray(y),
Mode: grob.ScatterModeMarkers,
},
},
Expand Down
6 changes: 3 additions & 3 deletions examples/scatter3d/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ func main() {
fig := &grob.Fig{
Data: []types.Trace{
&grob.Scatter3d{
X: x,
Y: y,
Z: z,
X: types.DataArray(x),
Y: types.DataArray(y),
Z: types.DataArray(z),
Mode: grob.Scatter3dModeMarkers,
},
},
Expand Down
4 changes: 2 additions & 2 deletions examples/stargazers/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ func main() {
fig := &grob.Fig{
Data: []types.Trace{
&grob.Scatter{
X: x,
Y: y,
X: types.DataArray(x),
Y: types.DataArray(y),
Text: types.ArrayOKArray(text...),
Meta: types.ArrayOKArray(link...),
Mode: grob.ScatterModeLines + "+" + grob.ScatterModeMarkers,
Expand Down
4 changes: 2 additions & 2 deletions examples/static_image/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ func main() {
fig := &grob.Fig{
Data: []types.Trace{
&grob.Bar{
X: []float64{1, 2, 3},
Y: []float64{1, 2, 3},
X: types.DataArray([]float64{1, 2, 3}),
Y: types.DataArray([]float64{1, 2, 3}),
},
},
Layout: &grob.Layout{
Expand Down
16 changes: 8 additions & 8 deletions examples/subplots_share_axes/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,24 +56,24 @@ func main() {
fig := &grob.Fig{
Data: []types.Trace{
&grob.Scatter{
X: []float64{1, 2, 3},
Y: []float64{2, 3, 4},
X: types.DataArray([]float64{1, 2, 3}),
Y: types.DataArray([]float64{2, 3, 4}),
},
&grob.Scatter{
X: []float64{20, 30, 40},
Y: []float64{5, 5, 5},
X: types.DataArray([]float64{20, 30, 40}),
Y: types.DataArray([]float64{5, 5, 5}),
Xaxis: "x2",
Yaxis: "y",
},
&grob.Scatter{
X: []float64{2, 3, 4},
Y: []float64{600, 700, 800},
X: types.DataArray([]float64{2, 3, 4}),
Y: types.DataArray([]float64{600, 700, 800}),
Xaxis: "x",
Yaxis: "y3",
},
&grob.Scatter{
X: []float64{4000, 5000, 6000},
Y: []float64{7000, 8000, 9000},
X: types.DataArray([]float64{4000, 5000, 6000}),
Y: types.DataArray([]float64{7000, 8000, 9000}),
Xaxis: "x4",
Yaxis: "y4",
},
Expand Down
4 changes: 2 additions & 2 deletions examples/transforms/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ func main() {
fig := &grob.Fig{
Data: []types.Trace{
&grob.Bar{
X: []float64{1, 2, 3},
Y: []float64{1, 2, 3},
X: types.DataArray([]float64{1, 2, 3}),
Y: types.DataArray([]float64{1, 2, 3}),
},
},
Layout: &grob.Layout{
Expand Down
4 changes: 2 additions & 2 deletions examples/unmarshal/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ func main() {
fig := &grob.Fig{
Data: []types.Trace{
&grob.Scatter{
X: t,
Y: y,
X: types.DataArray(t),
Y: types.DataArray(y),
Mode: grob.ScatterModeMarkers,
},
},
Expand Down
16 changes: 8 additions & 8 deletions examples/waterfall_bar_chart/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,8 @@ func main() {
// Base

trace1 := &grob.Bar{
X: xData,
Y: []int{0, 430, 0, 570, 370, 370, 0},
X: types.DataArray(xData),
Y: types.DataArray([]int{0, 430, 0, 570, 370, 370, 0}),
Marker: &grob.BarMarker{
Color: types.ArrayOKValue(types.UseColor("rgba(1,1,1,0.0)")),
},
Expand All @@ -132,8 +132,8 @@ func main() {
// Revenue

trace2 := &grob.Bar{
X: xData,
Y: []int{430, 260, 690, 0, 0, 0, 0},
X: types.DataArray(xData),
Y: types.DataArray([]int{430, 260, 690, 0, 0, 0, 0}),
Marker: &grob.BarMarker{
Color: types.ArrayOKValue(types.UseColor("rgba(55,128,191,0.7)")),
Line: &grob.BarMarkerLine{
Expand All @@ -146,8 +146,8 @@ func main() {
// Cost

trace3 := &grob.Bar{
X: xData,
Y: []int{0, 0, 0, 120, 200, 320, 0},
X: types.DataArray(xData),
Y: types.DataArray([]int{0, 0, 0, 120, 200, 320, 0}),
Marker: &grob.BarMarker{
Color: types.ArrayOKValue(types.UseColor("rgba(219, 64, 82, 0.7)")),
Line: &grob.BarMarkerLine{
Expand All @@ -160,8 +160,8 @@ func main() {
// Profit

trace4 := &grob.Bar{
X: xData,
Y: []int{0, 0, 0, 0, 0, 0, 370},
X: types.DataArray(xData),
Y: types.DataArray([]int{0, 0, 0, 0, 0, 0, 370}),
Marker: &grob.BarMarker{
Color: types.ArrayOKValue(types.UseColor("rgba(50,171, 96, 0.7)")),
Line: &grob.BarMarkerLine{
Expand Down
Loading
Loading