diff --git a/screenplot.go b/screenplot.go index 4aacfd67a05ad2e26b764f656a8c01e6118e8f34..aa61dc3c9e641c777e6e83953173d069711c94aa 100644 --- a/screenplot.go +++ b/screenplot.go @@ -4,79 +4,64 @@ package main import ( "fmt" - "image" - "image/color" - "image/draw" + "math" ui "github.com/gizak/termui/v3" "github.com/gizak/termui/v3/widgets" ) func breaker(x interface{}) {} -func scrnPlt(pi, stdErr []float32) error { - nrmlStyle := ui.NewStyle (ui.ColorBlack, ui.ColorClear) -// invisStyle := ui.NewStyle (ui.ColorClear, ui.ColorClear) - thm := ui.Theme - thm.Block.Title = nrmlStyle - thm.Plot.Axes = ui.ColorBlack - - thm.Default = ui.NewStyle(ui.ColorBlack, ui.ColorClear) - thm.Block.Border = nrmlStyle - - ui.Theme = thm - - ptxt := widgets.NewParagraph() - - for i := 0; i < 24; i++ { - for j := 0; j < 800; j++ { - ptxt.Text += "." - } - ptxt.Text += " \n" +func copyToData (y []float32) ([][]float64){ + data := make([]float64, len(y)) + for i := range y { + data[i] = float64(y[i]) } + data[0] = data[1] + return [][]float64{data} +} + + +func scrnPlt(pi, stdErr []float32) error { + fmt.Println() + marker := widgets.MarkerDot + dotMarkerRune := '.' - ptxt.SetRect(0, 0, 80, 24) - ptxt.TextStyle = ui.NewStyle(ui.ColorWhite, ui.ColorWhite) - p1 := widgets.NewPlot() + p1 := widgets.NewPlot() p1.Title = "π" + p1.Data = copyToData (pi) + p1.Marker = marker + p1.DotMarkerRune = dotMarkerRune + p1.PlotType = widgets.ScatterPlot + p1.Border = true + + p2 := widgets.NewPlot() + p2.Title = "standard error" + p2.Data = copyToData (stdErr) + p2.DotMarkerRune = dotMarkerRune + p2.Marker = marker + + p3 := widgets.NewPlot() + p3.Title = "abs error" { - data := make([][]float64, 2) - data[0] = make([]float64, len(pi)) - data[1] = make([]float64, len(pi)) - for i := range pi { - data[0][i] = float64(i + 1) - data[1][i] = float64(pi[i]) + d := make ([]float64, len(pi)) + for i, piV := range pi { + d[i] = math.Pi - float64(piV) } - p1.Data = data + p3.Data = [][]float64{d} } - p1.SetRect(2, 2, 80, 24) - p1.Block.TitleStyle = nrmlStyle - p1.AxesColor = ui.ColorBlack -// p1.LineColors = []ui.Color{ui.ColorBlack, ui.ColorBlack} - p1.SetRect (0, 0, 80, 24) - p1.Marker = widgets.MarkerBraille - p1.PlotType = widgets.ScatterPlot - p1.Border = false - - breaker(thm) - - m := image.NewRGBA(image.Rect(0, 0, 80, 24)) - - // draw.Draw(m, m.Bounds(), &image.Uniform{color.White}, image.ZP, draw.Src) - draw.Draw(m, m.Bounds(), &image.Uniform{color.White}, image.ZP, draw.Src) - img := widgets.NewImage(m) - img.SetRect(0, 0, 100, 50) - defer fmt.Println ("m bounds", m.Bounds()) if err := ui.Init(); err != nil { return err } - - defer ui.Close() - - ui.Render(img, ptxt, p1) + grid := ui.NewGrid() + termWidth, termHeight := ui.TerminalDimensions() + grid.SetRect(0, 0, termWidth, termHeight) + grid.Set (ui.NewRow(1./3, p1), ui.NewRow(1./3, p2), ui.NewRow(1./3, p3)) + + render(grid) for e := range ui.PollEvents() { if e.Type == ui.KeyboardEvent { break @@ -84,3 +69,14 @@ func scrnPlt(pi, stdErr []float32) error { } return nil } + +// This was how I wanted to do it, but the library does +// not seem to respect background colours. +// ui.Theme = thm +// nrmlStyle := ui.NewStyle (ui.ColorWhite, ui.ColorBlack) +// thm := ui.Theme +// thm.Block.Title = nrmlStyle +// thm.Plot.Axes = ui.ColorWhite +// thm.Plot.Lines = []ui.Color{ui.ColorWhite} +// thm.Default = ui.NewStyle(ui.ColorWhite, ui.ColorBlack) +// thm.Block.Border = nrmlStyle