...

Source file src/gonum.org/v1/plot/plotter/rotation_example_test.go

Documentation: gonum.org/v1/plot/plotter

     1  // Copyright ©2016 The Gonum Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package plotter_test
     6  
     7  import (
     8  	"image/color"
     9  	"log"
    10  	"math"
    11  
    12  	"gonum.org/v1/plot"
    13  	"gonum.org/v1/plot/plotter"
    14  	"gonum.org/v1/plot/vg"
    15  	"gonum.org/v1/plot/vg/draw"
    16  )
    17  
    18  // Example_rotation gives some examples of rotating text.
    19  func Example_rotation() {
    20  	n := 100
    21  	xmax := 2 * math.Pi
    22  
    23  	// Sin creates a sine curve.
    24  	sin := func(n int, xmax float64) plotter.XYs {
    25  		xy := make(plotter.XYs, n)
    26  		for i := 0; i < n; i++ {
    27  			xy[i].X = xmax / float64(n) * float64(i)
    28  			xy[i].Y = math.Sin(xy[i].X) * 100
    29  		}
    30  		return xy
    31  	}
    32  
    33  	// These points will make up our sine curve.
    34  	linePoints := sin(n, xmax)
    35  
    36  	// These points are our label locations.
    37  	labelPoints := sin(8, xmax)
    38  
    39  	p := plot.New()
    40  	p.Title.Text = "Rotation Example"
    41  	p.X.Label.Text = "X"
    42  	p.Y.Label.Text = "100 × Sine X"
    43  
    44  	l, err := plotter.NewLine(linePoints)
    45  	if err != nil {
    46  		log.Panic(err)
    47  	}
    48  	l.LineStyle.Width = vg.Points(1)
    49  	l.LineStyle.Color = color.RGBA{B: 255, A: 255}
    50  
    51  	labelData := plotter.XYLabels{
    52  		XYs:    labelPoints,
    53  		Labels: []string{"0", "pi/4", "pi/2", "3pi/4", "pi", "5pi/4", "3pi/2", "7pi/4", "2pi"},
    54  	}
    55  
    56  	labels, err := plotter.NewLabels(labelData)
    57  	if err != nil {
    58  		log.Panic(err)
    59  	}
    60  
    61  	for i := range labels.TextStyle {
    62  		x := labels.XYs[i].X
    63  
    64  		// Set the label rotation to the slope of the line, so the label is
    65  		// parallel with the line.
    66  		labels.TextStyle[i].Rotation = math.Atan(math.Cos(x))
    67  		labels.TextStyle[i].XAlign = draw.XCenter
    68  		labels.TextStyle[i].YAlign = draw.YCenter
    69  		// Move the labels away from the line so they're more easily readable.
    70  		if x >= math.Pi {
    71  			labels.TextStyle[i].YAlign = draw.YTop
    72  		} else {
    73  			labels.TextStyle[i].YAlign = draw.YBottom
    74  		}
    75  	}
    76  
    77  	p.Add(l, labels)
    78  
    79  	// Add boundary boxes for debugging.
    80  	p.Add(plotter.NewGlyphBoxes())
    81  
    82  	p.NominalX("0", "The number 1", "Number 2", "The number 3", "Number 4",
    83  		"The number 5", "Number 6")
    84  
    85  	// Change the rotation of the X tick labels to make them fit better.
    86  	p.X.Tick.Label.Rotation = math.Pi / 5
    87  	p.X.Tick.Label.YAlign = draw.YCenter
    88  	p.X.Tick.Label.XAlign = draw.XRight
    89  
    90  	// Also change the rotation of the Y tick labels.
    91  	p.Y.Tick.Label.Rotation = math.Pi / 2
    92  	p.Y.Tick.Label.XAlign = draw.XCenter
    93  	p.Y.Tick.Label.YAlign = draw.YBottom
    94  
    95  	err = p.Save(200, 150, "testdata/rotation.png")
    96  	if err != nil {
    97  		log.Panic(err)
    98  	}
    99  }
   100  

View as plain text