-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
78 lines (66 loc) · 1.62 KB
/
main.go
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
package main
import (
"image/color"
"github.com/fogleman/gg"
"github.com/warmans/gochart"
)
const numPoints = 8
func main() {
// setup the canvas size and background color
canvas := gg.NewContext(800, 400)
canvas.SetColor(color.RGBA{255, 255, 255, 255})
canvas.DrawRectangle(0, 0, float64(canvas.Width()), float64(canvas.Height()))
canvas.Fill()
// generate some test data
series := gochart.NewXYSeries(gochart.GenTestTextLabels(numPoints), gochart.GenSinWave(numPoints))
// use a 10 tick vertical scale
yScale := gochart.NewYScale(10, series)
// offset the x scale by 10 to prevent the bars from overlapping the sides of the chart
// the offset depends on the bar width. So changing it to 50 means the offset needs to
// be 25.
xScale := gochart.NewXScale(series, 25)
grid := gochart.New12ColGridLayout(
gochart.GridRow{
HeightPercent: 0.95,
Columns: []gochart.GridColumn{
{
ColSpan: 1,
El: gochart.NewYAxis(
yScale,
),
},
{
ColSpan: 11,
El: gochart.NewCompositePlot(
// add a background grid along the same scale as the chart.
gochart.NewYGrid(yScale),
// add the bars plot
gochart.NewBarsPlot(
yScale,
xScale,
series,
gochart.PlotBarMaxWidth(50),
),
),
},
},
},
gochart.GridRow{
HeightPercent: 0.05,
Columns: []gochart.GridColumn{
{ColSpan: 1},
{
ColSpan: 11,
El: gochart.NewXAxis(
series,
xScale,
),
},
},
},
)
grid.Render(canvas, gochart.BoundingBoxFromCanvas(canvas))
if err := canvas.SavePNG("./example.png"); err != nil {
panic(err)
}
}