forked from plandem/xlsx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
databar.go
106 lines (91 loc) · 2.31 KB
/
databar.go
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// Copyright (c) 2017 Andrey Gayvoronsky <plandem@gmail.com>
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package rule
import (
"fmt"
"github.com/miscoler/xlsx/internal/color"
"github.com/miscoler/xlsx/internal/ml"
"github.com/miscoler/xlsx/internal/ml/primitives"
)
type dataBarRule struct {
baseRule
}
//DataBar is helper object to set specific options for rule
var DataBar dataBarRule
func (x dataBarRule) initIfRequired(r *Info) {
if !r.initialized {
r.initialized = true
r.validator = DataBar
r.rule = &ml.ConditionalRule{
Type: primitives.ConditionTypeDataBar,
DataBar: &ml.DataBar{
Values: []*ml.ConditionValue{
{
Type: ValueTypeLowest,
},
{
Type: ValueTypeHighest,
},
},
Color: color.New("#638EC6"),
MinLength: 10,
MaxLength: 90,
},
}
}
}
func (x dataBarRule) Default(r *Info) {
x.initIfRequired(r)
}
func (x dataBarRule) setValue(r *Info, idx int, value string, settings []interface{}) {
x.initIfRequired(r)
r.rule.DataBar.Values[idx].Value = value
for _, p := range settings {
switch pv := p.(type) {
case primitives.ConditionValueType:
r.rule.DataBar.Values[idx].Type = pv
}
}
}
func (x dataBarRule) Min(value string, settings ...interface{}) Option {
return func(r *Info) {
x.setValue(r, 0, value, settings)
}
}
func (x dataBarRule) Max(value string, settings ...interface{}) Option {
return func(r *Info) {
x.setValue(r, 1, value, settings)
}
}
func (x dataBarRule) Color(rgb string) Option {
return func(r *Info) {
x.initIfRequired(r)
r.rule.DataBar.Color = color.New(rgb)
}
}
func (x dataBarRule) BarOnly(r *Info) {
x.initIfRequired(r)
r.rule.DataBar.ShowValue = primitives.OptionalBool(false)
}
func (x dataBarRule) Validate(r *Info) error {
if !r.rule.DataBar.Values[0].Type.IsAllowed(
ValueTypeLowest,
ValueTypeNumber,
ValueTypePercent,
ValueTypeFormula,
ValueTypePercentile,
) {
return fmt.Errorf("dataBar: Not allowed type '%s' for min value", r.rule.DataBar.Values[0].Type)
}
if !r.rule.DataBar.Values[1].Type.IsAllowed(
ValueTypeNumber,
ValueTypePercent,
ValueTypeFormula,
ValueTypePercentile,
ValueTypeHighest,
) {
return fmt.Errorf("dataBar: Not allowed type '%s' for max value", r.rule.DataBar.Values[1].Type)
}
return nil
}