|
| 1 | +package gofpdf |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "fmt" |
| 6 | + "math" |
| 7 | + |
| 8 | + "github.com/johnfercher/maroto/v2/pkg/props" |
| 9 | + |
| 10 | + "github.com/johnfercher/maroto/v2/internal/providers/gofpdf/gofpdfwrapper" |
| 11 | + "github.com/johnfercher/maroto/v2/pkg/core" |
| 12 | + "github.com/johnfercher/maroto/v2/pkg/core/entity" |
| 13 | +) |
| 14 | + |
| 15 | +var ErrOutOfRange = errors.New("out of range") |
| 16 | + |
| 17 | +type heatMap struct { |
| 18 | + pdf gofpdfwrapper.Fpdf |
| 19 | + math core.Math |
| 20 | + defaultFillColor *props.Color |
| 21 | +} |
| 22 | + |
| 23 | +func NewHeatMap(pdf gofpdfwrapper.Fpdf, math core.Math) *heatMap { |
| 24 | + return &heatMap{ |
| 25 | + pdf: pdf, |
| 26 | + math: math, |
| 27 | + defaultFillColor: &props.WhiteColor, |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +func (s heatMap) Add(heatMap [][]int, max int, cell *entity.Cell, margins *entity.Margins) { |
| 32 | + if heatMap == nil { |
| 33 | + return |
| 34 | + } |
| 35 | + |
| 36 | + ySize := len(heatMap) |
| 37 | + if ySize == 0 { |
| 38 | + return |
| 39 | + } |
| 40 | + stepY := (cell.Height) / float64(ySize-1) |
| 41 | + |
| 42 | + xSize := len(heatMap[0]) |
| 43 | + if xSize == 0 { |
| 44 | + return |
| 45 | + } |
| 46 | + stepX := (cell.Width) / float64(xSize-1) |
| 47 | + |
| 48 | + for i := 0; i < len(heatMap)-1; i++ { |
| 49 | + for j := 0; j < len(heatMap[i])-1; j++ { |
| 50 | + r, g, b := s.GetHeatColor(heatMap[i][j], max) |
| 51 | + |
| 52 | + x := float64(i)*stepX + cell.X + margins.Left |
| 53 | + y := float64(j)*stepY + cell.Y + margins.Top |
| 54 | + width := stepX |
| 55 | + height := stepY |
| 56 | + |
| 57 | + fmt.Println(x, y, width, height) |
| 58 | + |
| 59 | + s.pdf.SetFillColor(r, g, b) |
| 60 | + s.pdf.Rect(x, y, width, height, "F") |
| 61 | + s.pdf.SetFillColor(s.defaultFillColor.Red, s.defaultFillColor.Green, s.defaultFillColor.Blue) |
| 62 | + } |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +func (s heatMap) GetHeatColor(i int, total int) (int, int, int) { |
| 67 | + hueMax := 160.0 |
| 68 | + step := hueMax / float64(total) |
| 69 | + iStep := step * float64(i) |
| 70 | + |
| 71 | + r, g, b, _ := HSVToRGB(iStep, 1.0, 1.0) |
| 72 | + return int(r), int(g), int(b) |
| 73 | +} |
| 74 | + |
| 75 | +// HSVToRGB converts an HSV triple to an RGB triple. |
| 76 | +// Source: https://github.com/Crazy3lf/colorconv/blob/master/colorconv.go |
| 77 | +func HSVToRGB(h, s, v float64) (r, g, b uint8, err error) { |
| 78 | + if h < 0 || h >= 360 || |
| 79 | + s < 0 || s > 1 || |
| 80 | + v < 0 || v > 1 { |
| 81 | + return 0, 0, 0, ErrOutOfRange |
| 82 | + } |
| 83 | + // When 0 ≤ h < 360, 0 ≤ s ≤ 1 and 0 ≤ v ≤ 1: |
| 84 | + C := v * s |
| 85 | + X := C * (1 - math.Abs(math.Mod(h/60, 2)-1)) |
| 86 | + m := v - C |
| 87 | + var Rnot, Gnot, Bnot float64 |
| 88 | + switch { |
| 89 | + case 0 <= h && h < 60: |
| 90 | + Rnot, Gnot, Bnot = C, X, 0 |
| 91 | + case 60 <= h && h < 120: |
| 92 | + Rnot, Gnot, Bnot = X, C, 0 |
| 93 | + case 120 <= h && h < 180: |
| 94 | + Rnot, Gnot, Bnot = 0, C, X |
| 95 | + case 180 <= h && h < 240: |
| 96 | + Rnot, Gnot, Bnot = 0, X, C |
| 97 | + case 240 <= h && h < 300: |
| 98 | + Rnot, Gnot, Bnot = X, 0, C |
| 99 | + case 300 <= h && h < 360: |
| 100 | + Rnot, Gnot, Bnot = C, 0, X |
| 101 | + } |
| 102 | + r = uint8(math.Round((Rnot + m) * 255)) |
| 103 | + g = uint8(math.Round((Gnot + m) * 255)) |
| 104 | + b = uint8(math.Round((Bnot + m) * 255)) |
| 105 | + return r, g, b, nil |
| 106 | +} |
0 commit comments