1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
|
// Copyright 2019-2022 Graham Clark. All rights reserved. Use of this source
// code is governed by the MIT license that can be found in the LICENSE
// file.
// Package bargraph provides a simple plotting widget.
package bargraph
import (
"github.com/gcla/gowid"
"github.com/gcla/gowid/widgets/columns"
"github.com/gcla/gowid/widgets/fill"
"github.com/gcla/gowid/widgets/overlay"
"github.com/gcla/gowid/widgets/pile"
)
//======================================================================
type IBarGraph interface {
GetData() [][]int
GetAttrs() []gowid.IColor
GetMax() int
}
type IWidget interface {
gowid.IWidget
IBarGraph
}
type Widget struct {
Data [][]int
Max int
Attrs []gowid.IColor
gowid.RejectUserInput
gowid.NotSelectable
}
func New(atts []gowid.IColor) *Widget {
res := &Widget{}
res.Data = make([][]int, 0)
res.Max = 0
res.Attrs = atts
var _ gowid.IWidget = res
return res
}
func (w *Widget) String() string {
return "bargraph"
}
func (w *Widget) GetData() [][]int {
return w.Data
}
func (w *Widget) SetData(l [][]int, max int, app gowid.IApp) {
w.Data = l
w.Max = max
}
func (w *Widget) GetAttrs() []gowid.IColor {
return w.Attrs
}
func (w *Widget) GetMax() int {
return w.Max
}
func (w *Widget) RenderSize(size gowid.IRenderSize, focus gowid.Selector, app gowid.IApp) gowid.IRenderBox {
return RenderSize(w, size, focus, app)
}
func (w *Widget) Render(size gowid.IRenderSize, focus gowid.Selector, app gowid.IApp) gowid.ICanvas {
return Render(w, size, focus, app)
}
//''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
func RenderSize(w gowid.IWidget, size gowid.IRenderSize, focus gowid.Selector, app gowid.IApp) gowid.IRenderBox {
return gowid.CalculateRenderSizeFallback(w, size, focus, app)
}
func Render(w IBarGraph, size gowid.IRenderSize, focus gowid.Selector, app gowid.IApp) gowid.ICanvas {
_, ok := size.(gowid.IRenderBox)
if !ok {
panic(gowid.WidgetSizeError{Widget: w, Size: size, Required: "gowid.IRenderBox"})
}
weight1 := gowid.RenderWithWeight{1}
bgTCellColor := gowid.IColorToTCell(w.GetAttrs()[0], gowid.ColorDefault, app.GetColorMode())
// TODO - check case when data is empty
dataIdxLimit := 0
if len(w.GetData()) > 0 {
dataIdxLimit = len(w.GetData()[0])
}
dataWidgets := make([]*columns.Widget, dataIdxLimit)
for dataIdx := 0; dataIdx < dataIdxLimit; dataIdx++ {
cols := make([]gowid.IContainerWidget, len(w.GetData()))
for i, d := range w.GetData() {
datum := d[dataIdx]
dataTCellColor := gowid.IColorToTCell(w.GetAttrs()[(i%(len(w.GetAttrs())-1))+1], gowid.ColorDefault, app.GetColorMode())
bar := pile.New([]gowid.IContainerWidget{
&gowid.ContainerWidget{
fill.NewSolidFromCell(
gowid.Cell{},
),
gowid.RenderWithWeight{w.GetMax() - datum},
},
// Use fg as background because I'm using spaces
&gowid.ContainerWidget{
fill.NewSolidFromCell(
gowid.MakeCell(
' ',
gowid.ColorNone,
dataTCellColor,
gowid.StyleNone),
),
gowid.RenderWithWeight{datum}},
})
cols[i] = &gowid.ContainerWidget{bar, weight1}
}
dataWidgets[dataIdx] = columns.New(cols)
}
var res gowid.IWidget = fill.NewSolidFromCell(
gowid.MakeCell(
' ',
gowid.ColorNone,
bgTCellColor,
gowid.StyleNone),
)
for _, dataWidget := range dataWidgets {
res = overlay.New(
dataWidget,
res,
gowid.VAlignMiddle{}, gowid.RenderWithRatio{R: 1.0},
gowid.HAlignMiddle{}, gowid.RenderWithRatio{R: 1.0},
)
}
return res.Render(size, focus, app)
}
//======================================================================
// Local Variables:
// mode: Go
// fill-column: 110
// End:
|