@@ -42,8 +42,10 @@ func New(spec data.Spec, dp *data.Points, width, height int) chart.Chart {
4242}
4343
4444func 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+
104128func siValueFormater (v interface {}) string {
105129 value , prefix := humanize .ComputeSI (v .(float64 ))
106130 value = float64 (int (value * 100 )) / 100
0 commit comments