1
0
mirror of https://github.com/gizak/termui.git synced 2025-04-24 13:48:50 +08:00

Implement GetMinFloat64From2dSlice

This commit is contained in:
keaysma 2024-10-20 15:16:23 -04:00
parent 8da7bb6861
commit d372d9e539
2 changed files with 20 additions and 1 deletions

View File

@ -93,6 +93,22 @@ func GetMaxFloat64FromSlice(slice []float64) (float64, error) {
return max, nil
}
func GetMinFloat64From2dSlice(slices [][]float64) (float64, error) {
if len(slices) == 0 {
return 0, fmt.Errorf("cannot get min value from empty slice")
}
var min float64
for _, slice := range slices {
for _, val := range slice {
if val < min {
min = val
}
}
}
return min, nil
}
func GetMaxFloat64From2dSlice(slices [][]float64) (float64, error) {
if len(slices) == 0 {
return 0, fmt.Errorf("cannot get max value from empty slice")

View File

@ -86,7 +86,7 @@ func (self *Plot) renderBraille(buf *Buffer, drawArea image.Rectangle, minVal fl
case ScatterPlot:
for i, line := range self.Data {
for j, val := range line {
height := int(((val-minVal)/r)*float64(drawArea.Dy()-1))
height := int(((val - minVal) / r) * float64(drawArea.Dy()-1))
canvas.SetPoint(
image.Pt(
(drawArea.Min.X+(j*self.HorizontalScale))*2,
@ -208,6 +208,9 @@ func (self *Plot) Draw(buf *Buffer) {
if maxVal == 0 {
maxVal, _ = GetMaxFloat64From2dSlice(self.Data)
}
if minVal == 0 {
minVal, _ = GetMinFloat64From2dSlice(self.Data)
}
if self.ShowAxes {
self.plotAxes(buf, minVal, maxVal)