2024-09-29 18:27:50 +00:00
|
|
|
//go:build js && wasm
|
|
|
|
// +build js,wasm
|
|
|
|
|
|
|
|
package src
|
|
|
|
|
|
|
|
import (
|
|
|
|
"image/color"
|
|
|
|
|
|
|
|
"syscall/js"
|
|
|
|
|
|
|
|
"github.com/gonum/stat/distuv"
|
|
|
|
"gonum.org/v1/plot"
|
|
|
|
"gonum.org/v1/plot/plotter"
|
|
|
|
"gonum.org/v1/plot/vg"
|
|
|
|
)
|
|
|
|
|
|
|
|
func HistPlot(this js.Value, args []js.Value) interface{} {
|
|
|
|
var (
|
2024-09-30 09:55:13 +00:00
|
|
|
values = args[0]
|
|
|
|
bins = args[1]
|
|
|
|
opts js.Value
|
|
|
|
text = "Histogram"
|
|
|
|
lineColor = color.RGBA{R: 255, G: 255, B: 255, A: 255}
|
|
|
|
fillColor = color.RGBA{R: 50, G: 50, B: 50, A: 255}
|
|
|
|
normColor = color.RGBA{R: 255, G: 0, B: 0, A: 255}
|
2024-09-29 18:27:50 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
if len(args) == 3 {
|
|
|
|
opts = args[2]
|
|
|
|
if !opts.Get("title").IsUndefined() {
|
|
|
|
text = opts.Get("title").String()
|
|
|
|
}
|
2024-09-30 09:55:13 +00:00
|
|
|
if !opts.Get("lineColor").IsUndefined() {
|
|
|
|
lineColor = HexToRGBA(opts.Get("lineColor").String())
|
|
|
|
}
|
|
|
|
if !opts.Get("fillColor").IsUndefined() {
|
|
|
|
fillColor = HexToRGBA(opts.Get("fillColor").String())
|
|
|
|
}
|
|
|
|
if !opts.Get("normColor").IsUndefined() {
|
|
|
|
normColor = HexToRGBA(opts.Get("normColor").String())
|
|
|
|
}
|
2024-09-29 18:27:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
v := make(plotter.Values, values.Length())
|
|
|
|
for i := range v {
|
|
|
|
v[i] = values.Index(i).Float()
|
|
|
|
}
|
|
|
|
p := plot.New()
|
|
|
|
p.Title.Text = text
|
|
|
|
h, err := plotter.NewHist(v, bins.Int())
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2024-09-30 09:55:13 +00:00
|
|
|
h.LineStyle.Color = lineColor
|
|
|
|
h.FillColor = fillColor
|
2024-09-29 18:27:50 +00:00
|
|
|
h.Normalize(1)
|
|
|
|
p.Add(h)
|
|
|
|
norm := plotter.NewFunction(distuv.UnitNormal.Prob)
|
2024-09-30 09:55:13 +00:00
|
|
|
norm.Color = normColor
|
2024-09-29 18:27:50 +00:00
|
|
|
norm.Width = vg.Points(2)
|
|
|
|
p.Add(norm)
|
|
|
|
writer, err := p.WriterTo(6*vg.Inch, 4*vg.Inch, "png")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
b64string := WriterToBase64String(writer)
|
|
|
|
return b64string
|
|
|
|
}
|