Styling

But, for now, having access to the *Line object allows us to play around with the styling a bit more. To set the right mood with the rather gloomy nature of this chapter, I have chosen a stark black line (in fact, I have grown rather fond of the stark black line charts and have started using them in my daily plots as well). A point to note is that I did this:

l.LineStyle.Color = color.RGBA{A: 255}

l.LineStyle.Color takes color.Colorcolor.RGBA is a struct found in the color library in the standard library. It's a struct that has four fields representing a color, such as Red, Green, Blue, and Alpha. Here I take advantage of Go's default values—0s. But having an Alpha value of 0 would mean that it's invisible. Hence, I only set the A field to 255—the rest of the fields are defaulted to 0, which gives it a stark black color.

After we set the line style, we add the line to the plot with p.Add(l). Because we're not using plotutil.AddLines, which abstracts away some of the manual work, we may find that if we run the function there isn't a legend in the plot. A plot without legends is generally useless. So, we also need to add a legend by using p.Legend.Add(seriesName, l).

Aside from color, width, and the like, I also want to set a more brutal feel to the plots I make for this chapter—after all, this chapter is rather doom and gloom. I feel that the default font, which is Times New Roman is a little too humanist. So, we'd need to change fonts. Luckily, the extended Go standard library comes with a font-processing library. While usually I'd choose to go with slab serif style fonts for the brutal look, Go itself comes with a font that works well—the Go family of fonts.

How do we change fonts in a *plot.Plot? Most components of *plot.Plot take a draw.TextStyle, which is a data structure that configures the styling of text, including fonts. So, we can set those fields to indicate we want to use the font we chose.

As I mentioned, in the extended standard library, Go comes with fonts and font-processing utilities. We'll be using it here. First, we'd have to install the packages: go get -u golang.org/x/image/font/gofont/gomono and go get -u github.com/golang/freetype/truetype. The former is the official Monospace Type of the Go family of typefaces. The latter is a library to handle TrueType fonts.

Here, a caveat must be mentioned—while draw.TextStyle does allow for the configuration of fonts, the fonts are in a vg.Font type, which wraps a *truetype.Font type. If we use truetype.Parse(gomono.TTF), we will get *truetype.Font. The vg package provides a function to make those fonts—vg.MakeFont. The reason why this is necessary instead of just using *truetype.Font is because vg has plenty of backends—some that could render fonts would require information about the font size.

So, to avoid having many calls to parse the font and making a vg.Font type, we can safely put it in a global variable, given we've already decided ahead that all fonts will be of the same brutal style:

var defaultFont vg.Font

func init() {
font, err := truetype.Parse(gomono.TTF)
if err != nil {
panic(err)
}
vg.AddFont("gomono", font)
defaultFont, err = vg.MakeFont("gomono", 12)
if err != nil {
panic(err)
}
}

Once that's done, we can set all draw.TextStyle.Font to be defaultFont. Setting a default font size of 12 does not, however, mean that you're stuck with the size for everything. Because vg.Font is a struct, not a pointer to a struct, once set in an object, you are free to change the font size of that particular field, as I have shown in the following two lines:

  p.Title.Font = defaultFont
p.Title.Font.Size = 16

With our main function we can execute the following code:

func main() {
dateStrings, co2s := parse(readFromFile)
dates := parseDates(dateStrings)
plt := newTSPlot(dates, co2s, "CO2 Level")
plt.X.Label.Text = "Time"
plt.Y.Label.Text = "CO2 in the atmosphere (ppm)"
plt.Title.Text = "CO2 in the atmosphere (ppm) over time Taken over the Mauna-Loa observatory"
dieIfErr(plt.Save(25*vg.Centimeter, 25*vg.Centimeter, "Moana-Loa.png"))
}

The result is stark , as shown in the following screenshot:

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.119.111.179