diff --git a/plot/mod.wasm b/plot/mod.wasm index 2db6f8c..27176c5 100755 Binary files a/plot/mod.wasm and b/plot/mod.wasm differ diff --git a/plot/src/Hist.go b/plot/src/Hist.go index dad660a..8206ad3 100644 --- a/plot/src/Hist.go +++ b/plot/src/Hist.go @@ -16,10 +16,13 @@ import ( func HistPlot(this js.Value, args []js.Value) interface{} { var ( - values = args[0] - bins = args[1] - opts js.Value - text = "Histogram" + 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} ) if len(args) == 3 { @@ -27,6 +30,15 @@ func HistPlot(this js.Value, args []js.Value) interface{} { if !opts.Get("title").IsUndefined() { text = opts.Get("title").String() } + 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()) + } } v := make(plotter.Values, values.Length()) @@ -39,10 +51,12 @@ func HistPlot(this js.Value, args []js.Value) interface{} { if err != nil { panic(err) } + h.LineStyle.Color = lineColor + h.FillColor = fillColor h.Normalize(1) p.Add(h) norm := plotter.NewFunction(distuv.UnitNormal.Prob) - norm.Color = color.RGBA{R: 255, A: 255} + norm.Color = normColor norm.Width = vg.Points(2) p.Add(norm) writer, err := p.WriterTo(6*vg.Inch, 4*vg.Inch, "png") diff --git a/plot/src/utils.go b/plot/src/utils.go index 570a9d8..bc24cb0 100644 --- a/plot/src/utils.go +++ b/plot/src/utils.go @@ -6,6 +6,8 @@ package src import ( "bytes" "encoding/base64" + "fmt" + "image/color" "io" ) @@ -14,3 +16,23 @@ func WriterToBase64String(writer io.WriterTo) string { writer.WriteTo(&buf) return base64.StdEncoding.EncodeToString(buf.Bytes()) } + +func HexToRGBA(hex string) color.RGBA { + var c color.RGBA + switch len(hex) { + case 4: + fmt.Sscanf(hex, "#%1x%1x%1x%1x", &c.R, &c.G, &c.B, &c.A) + c.R *= 17 + c.G *= 17 + c.B *= 17 + c.A = 255 + case 7: + fmt.Sscanf(hex, "#%02x%02x%02x", &c.R, &c.G, &c.B) + c.A = 255 + case 9: + fmt.Sscanf(hex, "#%02x%02x%02x%02x", &c.R, &c.G, &c.B, &c.A) + default: + fmt.Println("Invalid hex color:", hex) + } + return c +}