@@ -42,8 +42,10 @@ func New(spec data.Spec, dp *data.Points, width, height int) chart.Chart {
42
42
}
43
43
44
44
func newChart (series []chart.Series , markers []chart.GridLine , width , height int ) chart.Chart {
45
+ var min , max float64 = math .MaxFloat64 , - math .MaxFloat64
45
46
for i , s := range series {
46
47
if s , ok := s .(chart.ContinuousSeries ); ok {
48
+ min , max = minMax (s .YValues , min , max )
47
49
s .XValues = seq .Range (0 , float64 (len (s .YValues )- 1 ))
48
50
c := chart .GetAlternateColor (i + 4 )
49
51
s .Style = chart.Style {
@@ -74,6 +76,15 @@ func newChart(series []chart.Series, markers []chart.GridLine, width, height int
74
76
},
75
77
Series : series ,
76
78
}
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
+ }
77
88
if len (markers ) > 0 {
78
89
graph .Background .Padding .Bottom = 0 // compensate transparent tick space
79
90
graph .XAxis = chart.XAxis {
@@ -101,6 +112,19 @@ func newChart(series []chart.Series, markers []chart.GridLine, width, height int
101
112
return graph
102
113
}
103
114
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
+
104
128
func siValueFormater (v interface {}) string {
105
129
value , prefix := humanize .ComputeSI (v .(float64 ))
106
130
value = float64 (int (value * 100 )) / 100
0 commit comments