Skip to content

Commit 29745c1

Browse files
authored
Don't panic on empty plot data (#1090)
1 parent bdff0bb commit 29745c1

File tree

2 files changed

+56
-19
lines changed

2 files changed

+56
-19
lines changed

render/plot.go

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -29,25 +29,27 @@ var FillDampFactor uint8 = 0x55
2929
//
3030
// EXAMPLE BEGIN
3131
// render.Plot(
32-
// data = [
33-
// (0, 3.35),
34-
// (1, 2.15),
35-
// (2, 2.37),
36-
// (3, -0.31),
37-
// (4, -3.53),
38-
// (5, 1.31),
39-
// (6, -1.3),
40-
// (7, 4.60),
41-
// (8, 3.33),
42-
// (9, 5.92),
43-
// ],
44-
// width = 64,
45-
// height = 32,
46-
// color = "#0f0",
47-
// color_inverted = "#f00",
48-
// x_lim = (0, 9),
49-
// y_lim = (-5, 7),
50-
// fill = True,
32+
//
33+
// data = [
34+
// (0, 3.35),
35+
// (1, 2.15),
36+
// (2, 2.37),
37+
// (3, -0.31),
38+
// (4, -3.53),
39+
// (5, 1.31),
40+
// (6, -1.3),
41+
// (7, 4.60),
42+
// (8, 3.33),
43+
// (9, 5.92),
44+
// ],
45+
// width = 64,
46+
// height = 32,
47+
// color = "#0f0",
48+
// color_inverted = "#f00",
49+
// x_lim = (0, 9),
50+
// y_lim = (-5, 7),
51+
// fill = True,
52+
//
5153
// ),
5254
// EXAMPLE END
5355
type Plot struct {
@@ -95,6 +97,10 @@ func (p *Plot) computeLimits() (float64, float64, float64, float64) {
9597
}
9698

9799
// Otherwise we'll need min/max of X and Y
100+
if len(p.Data) == 0 {
101+
return 0, 1, 0, 1
102+
}
103+
98104
pt := p.Data[0]
99105
minX, maxX, minY, maxY := pt[0], pt[0], pt[1], pt[1]
100106
for i := 1; i < len(p.Data); i++ {

render/plot_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,37 @@ func TestPlotJaggedLine(t *testing.T) {
301301

302302
}
303303

304+
func TestPlotEmpty(t *testing.T) {
305+
ic := ImageChecker{
306+
Palette: map[string]color.RGBA{
307+
"1": {0xff, 0xff, 0xff, 0xff},
308+
".": {0, 0, 0, 0},
309+
},
310+
}
311+
312+
p := Plot{
313+
Width: 10,
314+
Height: 10,
315+
Data: [][2]float64{},
316+
XLim: Empty,
317+
YLim: Empty,
318+
}
319+
320+
assert.Equal(t, nil, ic.Check([]string{
321+
"..........",
322+
"..........",
323+
"..........",
324+
"..........",
325+
"..........",
326+
"..........",
327+
"..........",
328+
"..........",
329+
"..........",
330+
"..........",
331+
}, PaintWidget(p, image.Rect(0, 0, 100, 100), 0)))
332+
333+
}
334+
304335
func TestPlotFewPoints(t *testing.T) {
305336
ic := ImageChecker{
306337
Palette: map[string]color.RGBA{

0 commit comments

Comments
 (0)