Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
189 changes: 189 additions & 0 deletions cmd/heatmap/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
package main

import (
"github.com/johnfercher/maroto/v2"
"github.com/johnfercher/maroto/v2/pkg/components/chart"
"github.com/johnfercher/maroto/v2/pkg/components/row"
"github.com/johnfercher/maroto/v2/pkg/components/text"
"github.com/johnfercher/maroto/v2/pkg/config"
"github.com/johnfercher/maroto/v2/pkg/consts/pagesize"
"github.com/johnfercher/maroto/v2/pkg/props"
"log"
"math"
)

func main() {
//heat := buildHeat(50, 50)
//circleHeat := buildCircleHeat(50, 50)

cfg := config.NewBuilder().
//WithDebug(true).
WithPageSize(pagesize.A4).
Build()

m := maroto.New(cfg)

m.AddRows(text.NewRow(5, "HeatMap"))

m.AddRows(
row.New(70).Add(
chart.NewHeatMapCol(12, "Efficiency", buildCircleHeat(100, 100), props.HeatMap{
Chart: props.Chart{
Scale: props.ChartScale{
X: []float64{0, 25, 50, 75, 100},
Y: []float64{0, 25, 50, 75, 100},
},
Title: props.ChartTitle{
Text: "HeatMap",
},
},
}),
),
)

/*m.AddRows(
row.New(70).Add(
chart.NewHeatMapCol(5, "Efficiency", buildHeat(100, 100)),
col.New(2),
chart.NewHeatMapCol(5, "Efficiency", buildCircleHeat(100, 100), props.HeatMap{
Chart: props.Chart{
Scale: props.ChartScale{
X: []float64{0, 25, 50, 75, 100},
Y: []float64{0, 25, 50, 75, 100},
},
Title: props.ChartTitle{
Text: "HeatMap",
},
},
}),
),
)

m.AddRows(row.New(10))

m.AddRows(
row.New(35).Add(
chart.NewHeatMapCol(2, "Efficiency", heat, props.HeatMap{
HSVScale: props.HSVScale{
Begin: hsv.Pink,
End: hsv.Purple,
},
}),
chart.NewHeatMapCol(2, "Efficiency", heat, props.HeatMap{
HSVScale: props.HSVScale{
Begin: hsv.Pink,
End: hsv.Blue,
},
}),
chart.NewHeatMapCol(2, "Efficiency", heat, props.HeatMap{
HSVScale: props.HSVScale{
Begin: hsv.Pink,
End: hsv.Cyan,
},
}),
chart.NewHeatMapCol(2, "Efficiency", heat, props.HeatMap{
HSVScale: props.HSVScale{
Begin: hsv.Pink,
End: hsv.Green,
},
}),
chart.NewHeatMapCol(2, "Efficiency", heat, props.HeatMap{
HSVScale: props.HSVScale{
Begin: hsv.Pink,
End: hsv.Yellow,
},
}),
chart.NewHeatMapCol(2, "Efficiency", heat, props.HeatMap{
HSVScale: props.HSVScale{
Begin: hsv.Pink,
End: hsv.Orange,
},
}),
),
)

m.AddRows(row.New(5))

m.AddRows(
row.New(35).Add(
chart.NewHeatMapCol(2, "Efficiency", heat, props.HeatMap{
HSVScale: props.HSVScale{
Begin: hsv.Pink,
End: hsv.Red,
},
}),
chart.NewHeatMapCol(2, "Efficiency", circleHeat, props.HeatMap{
HSVScale: props.HSVScale{
Begin: hsv.Blue,
End: hsv.Cyan,
},
}),
chart.NewHeatMapCol(2, "Efficiency", circleHeat, props.HeatMap{
HSVScale: props.HSVScale{
Begin: hsv.Blue,
End: hsv.Green,
},
}),
chart.NewHeatMapCol(2, "Efficiency", circleHeat, props.HeatMap{
HSVScale: props.HSVScale{
Begin: hsv.Blue,
End: hsv.Yellow,
},
}),
chart.NewHeatMapCol(2, "Efficiency", circleHeat, props.HeatMap{
HSVScale: props.HSVScale{
Begin: hsv.Blue,
End: hsv.Orange,
},
}),
chart.NewHeatMapCol(2, "Efficiency", circleHeat, props.HeatMap{
HSVScale: props.HSVScale{
Begin: hsv.Blue,
End: hsv.Red,
},
}),
),
)*/

document, err := m.Generate()
if err != nil {
log.Fatal(err.Error())
}

err = document.Save("docs/assets/pdf/heatmap.pdf")
if err != nil {
log.Fatal(err.Error())
}
}

func buildHeat(x, y int) [][]int {
var heat [][]int
for i := 0; i < x; i++ {
var line []int
for j := 0; j < y; j++ {
w := i + j
wp := w
line = append(line, wp)
}
heat = append(heat, line)
}
return heat
}

func buildCircleHeat(x, y int) [][]int {
var heat [][]int

for i := 0; i < x; i++ {
var line []int
for j := 0; j < y; j++ {
iRad := float64(i) * math.Pi / 180.0
jRad := float64(j) * math.Pi / 180.0
x := math.Cos(iRad) * 360
y := math.Sin(jRad) * 360
line = append(line, int(y)+int(x))
}
heat = append(heat, line)
}

return heat
}
118 changes: 118 additions & 0 deletions cmd/timeseries/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package main

import (
"github.com/johnfercher/maroto/v2"
"github.com/johnfercher/maroto/v2/pkg/components/chart"
"github.com/johnfercher/maroto/v2/pkg/components/row"
"github.com/johnfercher/maroto/v2/pkg/config"
"github.com/johnfercher/maroto/v2/pkg/consts/fontfamily"
"github.com/johnfercher/maroto/v2/pkg/consts/fontstyle"
"github.com/johnfercher/maroto/v2/pkg/consts/pagesize"
"github.com/johnfercher/maroto/v2/pkg/core/entity"
"github.com/johnfercher/maroto/v2/pkg/props"
"log"
"math"
)

func main() {
width := 30.0
step := 0.1
timeSeriesList := SinCos(width, step)
timeSeriesList = append(timeSeriesList, Log(width, step)...)

prop := props.Chart{
Scale: props.ChartScale{
X: []float64{0, 10, 20, 30},
Y: []float64{0, 1, 2, 3},
Font: props.Font{
Family: fontfamily.Arial,
Style: fontstyle.Normal,
Size: 7,
},
},
Title: props.ChartTitle{
Text: "Time Series",
Font: props.Font{
Family: fontfamily.Arial,
Style: fontstyle.Normal,
Size: 9,
},
},
}

cfg := config.NewBuilder().
WithDebug(true).
WithPageSize(pagesize.A4).
Build()

m := maroto.New(cfg)

m.AddRows(
row.New(100).Add(
chart.NewTimeSeriesCol(12, timeSeriesList, prop),
),
)

document, err := m.Generate()
if err != nil {
log.Fatal(err.Error())
}

err = document.Save("docs/assets/pdf/timeseries.pdf")
if err != nil {
log.Fatal(err.Error())
}
}

func SinCos(width float64, step float64) []entity.TimeSeries {
timeSeries := []entity.TimeSeries{}

var maxSin entity.Point
sin := []entity.Point{}
for i := 0.0; i < width; i += step {
y := math.Sin(i) + 1 + (i / 25)
point := entity.NewPoint(i, y)
if y > maxSin.Y {
maxSin = point
}
sin = append(sin, point)
}
timeSeries = append(timeSeries, entity.NewTimeSeries(props.RedColor, sin, entity.NewLabel("Max", maxSin)))

var maxCos entity.Point
cos := []entity.Point{}
for i := 0.0; i < width; i += step {
y := math.Cos(i) + 1 + (i / 30)
point := entity.NewPoint(i, y)
if y > maxCos.Y {
maxCos = point
}
cos = append(cos, point)
}
timeSeries = append(timeSeries, entity.NewTimeSeries(props.BlueColor, cos, entity.NewLabel("Max", maxCos)))

return timeSeries
}

func Log(width float64, step float64) []entity.TimeSeries {
timeSeries := []entity.TimeSeries{}

var logMax entity.Point
sin := []entity.Point{}
for i := 0.0; i < width; i += step {
v := math.Log(i)
point := entity.NewPoint(i, v)
if v > logMax.Y {
logMax = point
}
if v > 0 {
sin = append(sin, point)
}
}
timeSeries = append(timeSeries, entity.NewTimeSeries(props.Color{
Red: 150,
Blue: 150,
}, sin, entity.NewLabel("Max", logMax)))

return timeSeries
}
3 changes: 2 additions & 1 deletion docs/assets/examples/textgrid/v2/main.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package main

import (
"github.com/johnfercher/maroto/v2/pkg/consts/fontstyle"
"log"

"github.com/johnfercher/maroto/v2/pkg/consts/fontstyle"

"github.com/johnfercher/maroto/v2/pkg/core"

"github.com/johnfercher/maroto/v2"
Expand Down
Binary file added docs/assets/pdf/heatmap.pdf
Binary file not shown.
Binary file added docs/assets/pdf/timeseries.pdf
Binary file not shown.
Binary file modified docs/assets/pdf/v2.pdf
Binary file not shown.
3 changes: 3 additions & 0 deletions docs/assets/text/heatmap.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
generate -> avg: 128.18ms, executions: [128.18ms]
add_rows -> avg: 5083.50ns, executions: [10.00μs, 0.17μs]
file_size -> 6.96Mb
10 changes: 5 additions & 5 deletions docs/assets/text/v2.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
generate -> avg: 18.06ms, executions: [18.06ms]
header -> avg: 356.00ns, executions: [356.00ns]
footer -> avg: 71.00ns, executions: [71.00ns]
add_row -> avg: 112.55ns, executions: [0.11μs, 0.15μs, 0.05μs, 0.08μs, 0.02μs, 0.02μs, 0.53μs, 0.06μs, 0.02μs, 0.07μs, 0.02μs, 0.02μs, 0.02μs, 0.06μs, 0.02μs, 0.02μs, 0.03μs, 1.41μs, 0.06μs, 0.02μs, 0.06μs, 0.02μs, 0.02μs, 0.01μs, 0.07μs, 0.02μs, 0.02μs, 0.03μs, 0.29μs, 1.74μs, 0.02μs, 0.07μs, 0.02μs, 0.02μs, 0.02μs, 0.06μs, 0.02μs, 0.02μs, 0.02μs, 0.24μs, 0.05μs, 0.02μs, 0.07μs, 0.02μs, 0.01μs, 0.02μs, 0.06μs, 0.03μs, 0.01μs, 0.01μs, 0.24μs, 0.05μs, 0.01μs, 0.05μs, 0.02μs]
file_size -> 267.93Kb
generate -> avg: 26.67ms, executions: [26.67ms]
header -> avg: 21.00μs, executions: [21.00μs]
footer -> avg: 333.00ns, executions: [333.00ns]
add_rows -> avg: 157.56ns, executions: [0.29μs, 0.50μs, 0.29μs, 0.50μs, 0.08μs, 0.12μs, 1.21μs, 0.17μs, 0.04μs, 0.08μs, 0.04μs, 0.04μs, 0.04μs, 0.21μs, 0.04μs, 0.04μs, 0.04μs, 0.67μs, 0.17μs, 0.04μs, 0.12μs, 0.04μs, 0.04μs, 0.04μs, 0.17μs, 0.04μs, 0.04μs, 0.04μs, 0.50μs, 0.12μs, 0.04μs, 0.12μs, 0.04μs, 0.21μs, 0.04μs, 0.12μs, 0.04μs, 0.04μs, 0.08μs, 0.50μs, 0.17μs, 0.04μs, 0.08μs, 0.04μs, 0.04μs, 0.04μs, 0.33μs, 0.04μs, 0.04μs, 0.04μs, 0.46μs, 0.08μs, 0.04μs, 0.12μs, 0.04μs]
file_size -> 282.23Kb
8 changes: 8 additions & 0 deletions internal/providers/gofpdf/builder.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package gofpdf

import (
"github.com/johnfercher/maroto/v2/internal/providers/gofpdf/chart"
"github.com/jung-kurt/gofpdf"

"github.com/johnfercher/maroto/v2/internal/cache"
Expand All @@ -20,6 +21,8 @@ type Dependencies struct {
Code core.Code
Image core.Image
Line core.Line
HeatMap core.HeatMap
TimeSeries core.TimeSeries
Cache cache.Cache
CellWriter cellwriter.CellWriter
Cfg *entity.Config
Expand Down Expand Up @@ -68,6 +71,9 @@ func (b *builder) Build(cfg *entity.Config, cache cache.Cache) *Dependencies {
text := NewText(fpdf, math, font)
image := NewImage(fpdf, math)
line := NewLine(fpdf)
baseChart := chart.NewChart(fpdf, font, text)
heatMap := chart.NewHeatMap(fpdf, baseChart, font)
timeSeries := chart.NewTimeSeries(fpdf, baseChart, font)
cellWriter := cellwriter.NewBuilder().
Build(fpdf)

Expand All @@ -78,6 +84,8 @@ func (b *builder) Build(cfg *entity.Config, cache cache.Cache) *Dependencies {
Code: code,
Image: image,
Line: line,
HeatMap: heatMap,
TimeSeries: timeSeries,
CellWriter: cellWriter,
Cfg: cfg,
Cache: cache,
Expand Down
Loading
Loading