-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathchart_type.go
More file actions
50 lines (45 loc) · 1.14 KB
/
chart_type.go
File metadata and controls
50 lines (45 loc) · 1.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package main
import (
"fmt"
"github.com/marianogappa/chart/format"
)
func resolveChartType(ct chartType, lf format.LineFormat, datasetLength int) (chartType, error) {
ct = _resolveChartType(ct, lf)
return ct, assertChartable(ct, lf, datasetLength)
}
func _resolveChartType(ct chartType, f format.LineFormat) chartType {
if ct != undefinedChartType {
return ct
}
switch {
case !f.HasFloats:
return pie
case f.FloatCount >= 2 && !f.HasStrings:
return scatter
case f.FloatCount > 1:
return line
default:
return pie
}
}
func assertChartable(ct chartType, f format.LineFormat, datasetLength int) error {
if datasetLength == 0 {
return fmt.Errorf("empty dataset; nothing to plot here")
}
var errIncompatibleFormat = fmt.Errorf("I don't know how to plot a dataset with this line format")
switch ct {
case pie, bar:
if !f.HasFloats || (f.FloatCount == 1 && !f.HasStrings && !f.HasDateTimes) {
return errIncompatibleFormat
}
case line:
if !f.HasFloats || (f.FloatCount < 2 && !f.HasStrings && !f.HasDateTimes) {
return errIncompatibleFormat
}
case scatter:
if !f.HasFloats {
return errIncompatibleFormat
}
}
return nil
}