forked from gonum/plot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrotation_example_test.go
More file actions
100 lines (81 loc) · 2.43 KB
/
rotation_example_test.go
File metadata and controls
100 lines (81 loc) · 2.43 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
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
// Copyright ©2016 The Gonum 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 plotter_test
import (
"image/color"
"log"
"math"
"runtime"
"gonum.org/v1/plot"
"gonum.org/v1/plot/plotter"
"gonum.org/v1/plot/vg"
"gonum.org/v1/plot/vg/draw"
)
// Example_rotation gives some examples of rotating text.
func Example_rotation() {
n := 100
xmax := 2 * math.Pi
// Sin creates a sine curve.
sin := func(n int, xmax float64) plotter.XYs {
xy := make(plotter.XYs, n)
for i := range n {
xy[i].X = xmax / float64(n) * float64(i)
xy[i].Y = math.Sin(xy[i].X) * 100
}
return xy
}
// These points will make up our sine curve.
linePoints := sin(n, xmax)
// These points are our label locations.
labelPoints := sin(8, xmax)
p := plot.New()
p.Title.Text = "Rotation Example"
p.X.Label.Text = "X"
p.Y.Label.Text = "100 × Sine X"
l, err := plotter.NewLine(linePoints)
if err != nil {
log.Panic(err)
}
l.LineStyle.Width = vg.Points(1)
l.LineStyle.Color = color.RGBA{B: 255, A: 255}
labelData := plotter.XYLabels{
XYs: labelPoints,
Labels: []string{"0", "pi/4", "pi/2", "3pi/4", "pi", "5pi/4", "3pi/2", "7pi/4", "2pi"},
}
labels, err := plotter.NewLabels(labelData)
if err != nil {
log.Panic(err)
}
for i := range labels.TextStyle {
x := labels.XYs[i].X
// Set the label rotation to the slope of the line, so the label is
// parallel with the line.
labels.TextStyle[i].Rotation = math.Atan(math.Cos(x))
labels.TextStyle[i].XAlign = draw.XCenter
labels.TextStyle[i].YAlign = draw.YCenter
// Move the labels away from the line so they're more easily readable.
if x >= math.Pi {
labels.TextStyle[i].YAlign = draw.YTop
} else {
labels.TextStyle[i].YAlign = draw.YBottom
}
}
p.Add(l, labels)
// Add boundary boxes for debugging.
p.Add(plotter.NewGlyphBoxes())
p.NominalX("0", "The number 1", "Number 2", "The number 3", "Number 4",
"The number 5", "Number 6")
// Change the rotation of the X tick labels to make them fit better.
p.X.Tick.Label.Rotation = math.Pi / 5
p.X.Tick.Label.YAlign = draw.YCenter
p.X.Tick.Label.XAlign = draw.XRight
// Also change the rotation of the Y tick labels.
p.Y.Tick.Label.Rotation = math.Pi / 2
p.Y.Tick.Label.XAlign = draw.XCenter
p.Y.Tick.Label.YAlign = draw.YBottom
err = p.Save(200, 150, "testdata/rotation_"+runtime.GOARCH+".png")
if err != nil {
log.Panic(err)
}
}