MCPcopy Index your code
hub / github.com/gonum/plot / Example_rotation

Function Example_rotation

plotter/rotation_example_test.go:20–100  ·  view source on GitHub ↗

Example_rotation gives some examples of rotating text.

()

Source from the content-addressed store, hash-verified

18
19// Example_rotation gives some examples of rotating text.
20func Example_rotation() {
21 n := 100
22 xmax := 2 * math.Pi
23
24 // Sin creates a sine curve.
25 sin := func(n int, xmax float64) plotter.XYs {
26 xy := make(plotter.XYs, n)
27 for i := range n {
28 xy[i].X = xmax / float64(n) * float64(i)
29 xy[i].Y = math.Sin(xy[i].X) * 100
30 }
31 return xy
32 }
33
34 // These points will make up our sine curve.
35 linePoints := sin(n, xmax)
36
37 // These points are our label locations.
38 labelPoints := sin(8, xmax)
39
40 p := plot.New()
41 p.Title.Text = "Rotation Example"
42 p.X.Label.Text = "X"
43 p.Y.Label.Text = "100 × Sine X"
44
45 l, err := plotter.NewLine(linePoints)
46 if err != nil {
47 log.Panic(err)
48 }
49 l.LineStyle.Width = vg.Points(1)
50 l.LineStyle.Color = color.RGBA{B: 255, A: 255}
51
52 labelData := plotter.XYLabels{
53 XYs: labelPoints,
54 Labels: []string{"0", "pi/4", "pi/2", "3pi/4", "pi", "5pi/4", "3pi/2", "7pi/4", "2pi"},
55 }
56
57 labels, err := plotter.NewLabels(labelData)
58 if err != nil {
59 log.Panic(err)
60 }
61
62 for i := range labels.TextStyle {
63 x := labels.XYs[i].X
64
65 // Set the label rotation to the slope of the line, so the label is
66 // parallel with the line.
67 labels.TextStyle[i].Rotation = math.Atan(math.Cos(x))
68 labels.TextStyle[i].XAlign = draw.XCenter
69 labels.TextStyle[i].YAlign = draw.YCenter
70 // Move the labels away from the line so they're more easily readable.
71 if x >= math.Pi {
72 labels.TextStyle[i].YAlign = draw.YTop
73 } else {
74 labels.TextStyle[i].YAlign = draw.YBottom
75 }
76 }
77

Callers

nothing calls this directly

Calls 8

NewFunction · 0.92
NewLineFunction · 0.92
PointsFunction · 0.92
NewLabelsFunction · 0.92
NewGlyphBoxesFunction · 0.92
NominalXMethod · 0.80
SaveMethod · 0.80
AddMethod · 0.45

Tested by

no test coverage detected