png plots template

This commit is contained in:
Anton Nesterov 2024-09-30 11:55:13 +02:00
parent 1d9cee5ee1
commit 46fa140d01
No known key found for this signature in database
GPG key ID: 59121E8AE2851FB5
3 changed files with 41 additions and 5 deletions

Binary file not shown.

View file

@ -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")

View file

@ -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
}