Skip to content

Commit

Permalink
Don't panic on empty plot data (#1090)
Browse files Browse the repository at this point in the history
  • Loading branch information
rohansingh authored Sep 19, 2024
1 parent bdff0bb commit 29745c1
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 19 deletions.
44 changes: 25 additions & 19 deletions render/plot.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,25 +29,27 @@ var FillDampFactor uint8 = 0x55
//
// EXAMPLE BEGIN
// render.Plot(
// data = [
// (0, 3.35),
// (1, 2.15),
// (2, 2.37),
// (3, -0.31),
// (4, -3.53),
// (5, 1.31),
// (6, -1.3),
// (7, 4.60),
// (8, 3.33),
// (9, 5.92),
// ],
// width = 64,
// height = 32,
// color = "#0f0",
// color_inverted = "#f00",
// x_lim = (0, 9),
// y_lim = (-5, 7),
// fill = True,
//
// data = [
// (0, 3.35),
// (1, 2.15),
// (2, 2.37),
// (3, -0.31),
// (4, -3.53),
// (5, 1.31),
// (6, -1.3),
// (7, 4.60),
// (8, 3.33),
// (9, 5.92),
// ],
// width = 64,
// height = 32,
// color = "#0f0",
// color_inverted = "#f00",
// x_lim = (0, 9),
// y_lim = (-5, 7),
// fill = True,
//
// ),
// EXAMPLE END
type Plot struct {
Expand Down Expand Up @@ -95,6 +97,10 @@ func (p *Plot) computeLimits() (float64, float64, float64, float64) {
}

// Otherwise we'll need min/max of X and Y
if len(p.Data) == 0 {
return 0, 1, 0, 1
}

pt := p.Data[0]
minX, maxX, minY, maxY := pt[0], pt[0], pt[1], pt[1]
for i := 1; i < len(p.Data); i++ {
Expand Down
31 changes: 31 additions & 0 deletions render/plot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,37 @@ func TestPlotJaggedLine(t *testing.T) {

}

func TestPlotEmpty(t *testing.T) {
ic := ImageChecker{
Palette: map[string]color.RGBA{
"1": {0xff, 0xff, 0xff, 0xff},
".": {0, 0, 0, 0},
},
}

p := Plot{
Width: 10,
Height: 10,
Data: [][2]float64{},
XLim: Empty,
YLim: Empty,
}

assert.Equal(t, nil, ic.Check([]string{
"..........",
"..........",
"..........",
"..........",
"..........",
"..........",
"..........",
"..........",
"..........",
"..........",
}, PaintWidget(p, image.Rect(0, 0, 100, 100), 0)))

}

func TestPlotFewPoints(t *testing.T) {
ic := ImageChecker{
Palette: map[string]color.RGBA{
Expand Down

0 comments on commit 29745c1

Please sign in to comment.