Initial commit of details_box.go and example

This commit is contained in:
Ana Cruz 2019-10-24 18:45:37 -05:00
parent 3a7bc55431
commit b47f91880a
3 changed files with 146 additions and 0 deletions

74
details_box.go Normal file
View file

@ -0,0 +1,74 @@
package chart
import (
"github.com/wcharczuk/go-chart/drawing"
)
// DetailsBox adds a box with additional text
func DetailsBox(c *Chart, text []string, userDefaults ...Style) Renderable {
return func(r Renderer, box Box, chartDefaults Style) {
// default style
defaults := Style{
FillColor: drawing.ColorWhite,
FontColor: DefaultTextColor,
FontSize: 8.0,
StrokeColor: DefaultAxisColor,
StrokeWidth: DefaultAxisLineWidth,
}
var style Style
if len(userDefaults) > 0 {
style = userDefaults[0].InheritFrom(chartDefaults.InheritFrom(defaults))
} else {
style = chartDefaults.InheritFrom(defaults)
}
contentPadding := Box{
Top: box.Height(),
Left: 5,
Right: 5,
Bottom: box.Height(),
}
contentBox := Box{
Bottom: box.Height(),
Left: 5,
}
content := Box{
Top: contentBox.Bottom - 5,
Left: contentBox.Left + contentPadding.Left,
Right: contentBox.Left + contentPadding.Left,
Bottom: contentBox.Bottom - 5,
}
style.GetTextOptions().WriteToRenderer(r)
// measure and add size of text to box height and width
for _, t := range text {
textbox := r.MeasureText(t)
content.Top -= textbox.Height()
right := content.Left + textbox.Width()
content.Right = MaxInt(content.Right, right)
}
contentBox = contentBox.Grow(content)
contentBox.Right = content.Right + contentPadding.Right
contentBox.Top = content.Top - 5
// draw the box
Draw.Box(r, contentBox, style)
style.GetTextOptions().WriteToRenderer(r)
// add the text
ycursor := content.Top
x := content.Left
for _, t := range text {
textbox := r.MeasureText(t)
y := ycursor + textbox.Height()
r.Text(t, x, y)
ycursor += textbox.Height()
}
}
}

View file

@ -0,0 +1,72 @@
package main
import (
"math/rand"
"os"
"time"
chart "github.com/wcharczuk/go-chart"
)
func random(min, max float64) float64 {
return rand.Float64()*(max-min) + min
}
func main() {
/*
This is an example of using `DetailsBox` to add a box with additional
information about the graph.
*/
xValues := make([]time.Time, 0)
yValues := make([][]float64, 3)
for i := 0; i < 50; i++ {
xValues = append(xValues, time.Now().Add(time.Duration(i)*time.Minute))
for j := 0; j < 3; j++ {
yValues[j] = append(yValues[j], random(float64(0), float64(20)))
}
}
seriesOne := chart.TimeSeries{
Name: "Series One",
XValues: xValues,
YValues: yValues[0],
}
seriesTwo := chart.TimeSeries{
Name: "Series Two",
XValues: xValues,
YValues: yValues[1],
}
seriesThree := chart.TimeSeries{
Name: "Series Three",
XValues: xValues,
YValues: yValues[2],
}
graph := chart.Chart{
Series: []chart.Series{
seriesOne,
seriesTwo,
seriesThree,
},
Background: chart.Style{
Padding: chart.Box{
Left: 120,
},
},
}
graph.Elements = []chart.Renderable{
chart.LegendLeft(&graph),
chart.DetailsBox(&graph, []string{
"M-F 8:00 am - 6:00 pm",
"Poll for data every 9 min.",
}),
}
f, _ := os.Create("output.png")
defer f.Close()
graph.Render(chart.PNG, f)
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 107 KiB