Skip to content

Commit 640ceff

Browse files
committed
feature: refactor guides
1 parent 66b8255 commit 640ceff

File tree

5 files changed

+90
-23
lines changed

5 files changed

+90
-23
lines changed

cmd/timeseries/main.go

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@ func main() {
1818
width := 30.0
1919
step := 0.1
2020
timeSeriesList := SinCos(width, step)
21+
timeSeriesList = append(timeSeriesList, Log(width, step)...)
2122
prop := props.Chart{
2223
XLabels: []float64{0, 10, 20, 30},
23-
YLabels: []float64{0, 1, 2},
24+
YLabels: []float64{0, 1, 2, 3},
2425
Font: props.Font{
2526
Family: fontfamily.Arial,
2627
Style: fontstyle.Normal,
@@ -39,9 +40,6 @@ func main() {
3940
row.New(100).Add(
4041
chart.NewTimeSeriesCol(12, timeSeriesList, prop),
4142
),
42-
/*row.New(100).Add(
43-
chart.NewTimeSeriesCol(12, pointsMatrix),
44-
),*/
4543
)
4644

4745
document, err := m.Generate()
@@ -58,17 +56,52 @@ func main() {
5856
func SinCos(width float64, step float64) []entity.TimeSeries {
5957
timeSeries := []entity.TimeSeries{}
6058

59+
var maxSin entity.Point
6160
sin := []entity.Point{}
6261
for i := 0.0; i < width; i += step {
63-
sin = append(sin, entity.NewPoint(i, math.Sin(i)+1))
62+
y := math.Sin(i) + 1 + (i / 25)
63+
point := entity.NewPoint(i, y)
64+
if y > maxSin.Y {
65+
maxSin = point
66+
}
67+
sin = append(sin, point)
6468
}
65-
timeSeries = append(timeSeries, entity.NewTimeSeries(props.RedColor, sin...))
69+
timeSeries = append(timeSeries, entity.NewTimeSeries(props.RedColor, sin, entity.NewLabel("Max", maxSin)))
6670

71+
var maxCos entity.Point
6772
cos := []entity.Point{}
6873
for i := 0.0; i < width; i += step {
69-
cos = append(cos, entity.NewPoint(i, math.Cos(i)+1))
74+
y := math.Cos(i) + 1 + (i / 30)
75+
point := entity.NewPoint(i, y)
76+
if y > maxCos.Y {
77+
maxCos = point
78+
}
79+
cos = append(cos, point)
7080
}
71-
timeSeries = append(timeSeries, entity.NewTimeSeries(props.BlueColor, cos...))
81+
timeSeries = append(timeSeries, entity.NewTimeSeries(props.BlueColor, cos, entity.NewLabel("Max", maxCos)))
82+
83+
return timeSeries
84+
}
85+
86+
func Log(width float64, step float64) []entity.TimeSeries {
87+
timeSeries := []entity.TimeSeries{}
88+
89+
var logMax entity.Point
90+
sin := []entity.Point{}
91+
for i := 0.0; i < width; i += step {
92+
v := math.Log(i)
93+
point := entity.NewPoint(i, v)
94+
if v > logMax.Y {
95+
logMax = point
96+
}
97+
if v > 0 {
98+
sin = append(sin, point)
99+
}
100+
}
101+
timeSeries = append(timeSeries, entity.NewTimeSeries(props.Color{
102+
Red: 150,
103+
Blue: 150,
104+
}, sin, entity.NewLabel("Max", logMax)))
72105

73106
return timeSeries
74107
}

docs/assets/pdf/timeseries.pdf

11.1 KB
Binary file not shown.

internal/providers/gofpdf/builder.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ func (b *builder) Build(cfg *entity.Config, cache cache.Cache) *Dependencies {
7373
line := NewLine(fpdf)
7474
baseChart := chart.NewChart(fpdf, font)
7575
heatMap := chart.NewHeatMap(fpdf, baseChart)
76-
timeSeries := chart.NewTimeSeries(fpdf, baseChart)
76+
timeSeries := chart.NewTimeSeries(fpdf, baseChart, font)
7777
cellWriter := cellwriter.NewBuilder().
7878
Build(fpdf)
7979

internal/providers/gofpdf/chart/timeseries.go

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package chart
22

33
import (
4+
"fmt"
45
"github.com/johnfercher/maroto/v2/pkg/consts/linestyle"
56
"github.com/johnfercher/maroto/v2/pkg/props"
67

@@ -14,13 +15,15 @@ type timeSeries struct {
1415
defaultFillColor *props.Color
1516
defaultDrawColor *props.Color
1617
chart core.Chart
18+
font core.Font
1719
defaultLineWidth float64
1820
}
1921

20-
func NewTimeSeries(pdf gofpdfwrapper.Fpdf, chart core.Chart) *timeSeries {
22+
func NewTimeSeries(pdf gofpdfwrapper.Fpdf, chart core.Chart, font core.Font) *timeSeries {
2123
return &timeSeries{
2224
pdf: pdf,
2325
chart: chart,
26+
font: font,
2427
defaultFillColor: &props.WhiteColor,
2528
defaultDrawColor: &props.BlueColor,
2629
defaultLineWidth: linestyle.DefaultLineThickness,
@@ -38,20 +41,37 @@ func (s timeSeries) Add(timeSeriesList []entity.TimeSeries, cell *entity.Cell, m
3841

3942
for _, timeSeries := range timeSeriesList {
4043
s.pdf.SetDrawColor(timeSeries.Color.Red, timeSeries.Color.Green, timeSeries.Color.Blue)
41-
for i := 0; i < len(timeSeries.Values)-1; i++ {
4244

43-
aX := timeSeries.Values[i].X*stepX + margins.Left + cell.X
45+
for i := 0; i < len(timeSeries.Points)-1; i++ {
46+
aX := timeSeries.Points[i].X*stepX + margins.Left + cell.X
4447

45-
aY := timeSeries.Values[i].Y * stepY
48+
aY := timeSeries.Points[i].Y * stepY
4649
aY = cell.Height + margins.Top + cell.Y - aY
4750

48-
bX := timeSeries.Values[i+1].X*stepX + margins.Left + cell.X
51+
bX := timeSeries.Points[i+1].X*stepX + margins.Left + cell.X
4952

50-
bY := timeSeries.Values[i+1].Y * stepY
53+
bY := timeSeries.Points[i+1].Y * stepY
5154
bY = cell.Height + margins.Top + cell.Y - bY
5255

5356
s.pdf.Line(aX, aY, bX, bY)
5457
}
58+
59+
for i := 0; i < len(timeSeries.Labels); i++ {
60+
aX := timeSeries.Labels[i].Point.X*stepX + margins.Left + cell.X
61+
62+
aY := timeSeries.Labels[i].Point.Y * stepY
63+
aY = cell.Height + margins.Top + cell.Y - aY
64+
65+
stringLabel := fmt.Sprintf("%s(%.2f)", timeSeries.Labels[i].Value, timeSeries.Labels[i].Point.Y)
66+
stringWidth := s.pdf.GetStringWidth(stringLabel)
67+
68+
fontFamily, fontType, fontSize := s.font.GetFont()
69+
stringHeight := s.font.GetHeight(fontFamily, fontType, fontSize)
70+
71+
s.pdf.Circle(aX, aY, 0.6, "D")
72+
s.pdf.Text(aX-(stringWidth/2), aY-(stringHeight/1.5), stringLabel)
73+
}
74+
5575
s.pdf.SetDrawColor(s.defaultDrawColor.Red, s.defaultDrawColor.Green, s.defaultDrawColor.Blue)
5676
}
5777

@@ -63,12 +83,12 @@ func (s timeSeries) getSizes(timeSeriesList []entity.TimeSeries) (float64, float
6383
height := 0.0
6484

6585
for _, timeSeries := range timeSeriesList {
66-
for i := 0; i < len(timeSeries.Values); i++ {
67-
if timeSeries.Values[i].X > width {
68-
width = timeSeries.Values[i].X
86+
for i := 0; i < len(timeSeries.Points); i++ {
87+
if timeSeries.Points[i].X > width {
88+
width = timeSeries.Points[i].X
6989
}
70-
if timeSeries.Values[i].Y > height {
71-
height = timeSeries.Values[i].Y
90+
if timeSeries.Points[i].Y > height {
91+
height = timeSeries.Points[i].Y
7292
}
7393
}
7494
}

pkg/core/entity/point.go

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,28 @@ func NewPoint(x, y float64) Point {
1414
}
1515
}
1616

17+
type Label struct {
18+
Value string
19+
Point Point
20+
}
21+
22+
func NewLabel(value string, point Point) Label {
23+
return Label{
24+
Value: value,
25+
Point: point,
26+
}
27+
}
28+
1729
type TimeSeries struct {
18-
Values []Point
30+
Points []Point
31+
Labels []Label
1932
Color props.Color
2033
}
2134

22-
func NewTimeSeries(color props.Color, values ...Point) TimeSeries {
35+
func NewTimeSeries(color props.Color, points []Point, labels ...Label) TimeSeries {
2336
return TimeSeries{
24-
Values: values,
37+
Points: points,
2538
Color: color,
39+
Labels: labels,
2640
}
2741
}

0 commit comments

Comments
 (0)