Skip to content

Commit 5563bf4

Browse files
committed
Fix graph not rendering flat lines
1 parent da0cc8e commit 5563bf4

File tree

4 files changed

+33
-5
lines changed

4 files changed

+33
-5
lines changed

data/point.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ func (p *Points) push(name string, value float64, counter bool) {
5858
d := p.getLocked(name)
5959
if counter {
6060
var diff float64
61-
if last := p.last[name]; last > 0 {
61+
if last := p.last[name]; last > 0 && last < diff {
6262
diff = value - last
6363
}
6464
p.last[name] = value

graph/dash.go

+5-3
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ type Dash struct {
1616
}
1717

1818
// Render generates a PNG with all graphs stacked.
19-
func (d Dash) Render(w io.Writer, width, height int) {
19+
func (d Dash) Render(w io.Writer, width, height int) error {
2020
graphs := make([]chart.Chart, 0, len(d.Specs))
2121
for _, spec := range d.Specs {
2222
graphs = append(graphs, New(spec, d.Data, width, height/len(d.Specs)))
@@ -25,11 +25,13 @@ func (d Dash) Render(w io.Writer, width, height int) {
2525
var top int
2626
for _, graph := range graphs {
2727
iw := &chart.ImageWriter{}
28-
graph.Render(chart.PNG, iw)
28+
if err := graph.Render(chart.PNG, iw); err != nil {
29+
return err
30+
}
2931
img, _ := iw.Image()
3032
r := image.Rectangle{image.Point{0, top}, image.Point{width, top + graph.Height}}
3133
top += graph.Height
3234
draw.Draw(canvas, r, img, image.Point{0, 0}, draw.Src)
3335
}
34-
png.Encode(w, canvas)
36+
return png.Encode(w, canvas)
3537
}

graph/graph.go

+24
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,10 @@ func New(spec data.Spec, dp *data.Points, width, height int) chart.Chart {
4242
}
4343

4444
func newChart(series []chart.Series, markers []chart.GridLine, width, height int) chart.Chart {
45+
var min, max float64 = math.MaxFloat64, -math.MaxFloat64
4546
for i, s := range series {
4647
if s, ok := s.(chart.ContinuousSeries); ok {
48+
min, max = minMax(s.YValues, min, max)
4749
s.XValues = seq.Range(0, float64(len(s.YValues)-1))
4850
c := chart.GetAlternateColor(i + 4)
4951
s.Style = chart.Style{
@@ -74,6 +76,15 @@ func newChart(series []chart.Series, markers []chart.GridLine, width, height int
7476
},
7577
Series: series,
7678
}
79+
if min == max {
80+
// By default, go-chart will fail to render a flat line as the range will be NaN.
81+
// Define a manual range in such case.
82+
// See https://github.com/wcharczuk/go-chart/issues/31
83+
graph.YAxis.Range = &chart.ContinuousRange{
84+
Min: min - 0.05,
85+
Max: max + 0.05,
86+
}
87+
}
7788
if len(markers) > 0 {
7889
graph.Background.Padding.Bottom = 0 // compensate transparent tick space
7990
graph.XAxis = chart.XAxis{
@@ -101,6 +112,19 @@ func newChart(series []chart.Series, markers []chart.GridLine, width, height int
101112
return graph
102113
}
103114

115+
func minMax(values []float64, curMin, curMax float64) (min, max float64) {
116+
min, max = curMin, curMax
117+
for _, value := range values {
118+
if value < min {
119+
min = value
120+
}
121+
if value > max {
122+
max = value
123+
}
124+
}
125+
return
126+
}
127+
104128
func siValueFormater(v interface{}) string {
105129
value, prefix := humanize.ComputeSI(v.(float64))
106130
value = float64(int(value*100)) / 100

main.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -151,5 +151,7 @@ func render(dash graph.Dash, rows int) {
151151
// Use iTerm2 image display feature.
152152
term := &osc.ImageWriter{}
153153
defer term.Close()
154-
dash.Render(term, width, height)
154+
if err := dash.Render(term, width, height); err != nil {
155+
fatal(fmt.Sprintf("cannot render graph: %v", err.Error()))
156+
}
155157
}

0 commit comments

Comments
 (0)