/
style.go
94 lines (81 loc) · 2.05 KB
/
style.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
// Copyright ©2017 The go-hep Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package hplot
import (
"github.com/golang/freetype/truetype"
"gonum.org/v1/plot"
"gonum.org/v1/plot/vg"
"gonum.org/v1/plot/vg/draw"
"gonum.org/v1/plot/vg/fonts"
)
var (
// DefaultStyle is the default style used for hplot plots.
DefaultStyle Style
)
// Style stores a given plot style.
type Style struct {
Fonts struct {
Name string // font name of this style
Title vg.Font // font used for the plot title
Label vg.Font // font used for the plot labels
Legend vg.Font // font used for the plot legend
Tick vg.Font // font used for the plot's axes' ticks
}
}
// Apply setups the plot p with the current style.
func (s *Style) Apply(p *Plot) {
p.Plot.Title.TextStyle.Font = s.Fonts.Title
p.Plot.X.Label.TextStyle.Font = s.Fonts.Label
p.Plot.Y.Label.TextStyle.Font = s.Fonts.Label
p.Plot.X.Tick.Label.Font = s.Fonts.Tick
p.Plot.Y.Tick.Label.Font = s.Fonts.Tick
p.Plot.Legend.TextStyle.Font = s.Fonts.Legend
p.Plot.Legend.YPosition = draw.PosCenter
}
func (s *Style) reset(name string) {
plot.DefaultFont = name
}
// NewStyle creates a new style with the given truetype font.
func NewStyle(name string, ft *truetype.Font) (Style, error) {
var sty Style
err := sty.init(name, ft)
if err != nil {
return sty, err
}
return sty, nil
}
func (sty *Style) init(name string, ft *truetype.Font) error {
sty.Fonts.Name = name
vg.AddFont(name, ft)
for _, t := range []struct {
ft *vg.Font
size vg.Length
}{
{&sty.Fonts.Title, 12},
{&sty.Fonts.Label, 12},
{&sty.Fonts.Legend, 12},
{&sty.Fonts.Tick, 10},
} {
ft, err := vg.MakeFont(sty.Fonts.Name, t.size)
if err != nil {
return err
}
*t.ft = ft
}
return nil
}
func init() {
vgfonts, err := fonts.Asset("LiberationSans-Regular.ttf")
if err != nil {
panic(err)
}
ft, err := truetype.Parse(vgfonts)
if err != nil {
panic(err)
}
err = DefaultStyle.init("Helvetica", ft)
if err != nil {
panic(err)
}
}