|
| 1 | +package gofpdf |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "math" |
| 6 | + |
| 7 | + "github.com/johnfercher/maroto/v2/pkg/props" |
| 8 | + |
| 9 | + "github.com/johnfercher/maroto/v2/internal/providers/gofpdf/gofpdfwrapper" |
| 10 | + "github.com/johnfercher/maroto/v2/pkg/core" |
| 11 | + "github.com/johnfercher/maroto/v2/pkg/core/entity" |
| 12 | +) |
| 13 | + |
| 14 | +var ErrOutOfRange = errors.New("out of range") |
| 15 | + |
| 16 | +type heatMap struct { |
| 17 | + pdf gofpdfwrapper.Fpdf |
| 18 | + defaultFillColor *props.Color |
| 19 | + chart core.Chart |
| 20 | + padding float64 |
| 21 | +} |
| 22 | + |
| 23 | +func NewHeatMap(pdf gofpdfwrapper.Fpdf, chart core.Chart) *heatMap { |
| 24 | + return &heatMap{ |
| 25 | + pdf: pdf, |
| 26 | + chart: chart, |
| 27 | + defaultFillColor: &props.WhiteColor, |
| 28 | + padding: 0, |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +func (s heatMap) Add(heatMap [][]int, cell *entity.Cell, margins *entity.Margins, prop *props.HeatMap) { |
| 33 | + if heatMap == nil || len(heatMap) == 0 || len(heatMap[0]) == 0 { |
| 34 | + return |
| 35 | + } |
| 36 | + |
| 37 | + max := s.getMax(heatMap) |
| 38 | + transparent := s.getTransparent(prop) |
| 39 | + stepX, stepY := s.getSteps(heatMap, cell, prop) |
| 40 | + |
| 41 | + for i := 0; i < len(heatMap)-1; i++ { |
| 42 | + for j := 0; j < len(heatMap[i])-1; j++ { |
| 43 | + if !transparent[heatMap[i][j]] { |
| 44 | + r, g, b := GetHeatColor(heatMap[i][j], max, prop.InvertScale, prop.HalfColor) |
| 45 | + |
| 46 | + x := float64(i)*stepX + cell.X + margins.Left |
| 47 | + y := float64(j) * stepY |
| 48 | + |
| 49 | + // Invert to draw from bottom to up |
| 50 | + y = cell.Height + margins.Top + cell.Y - y - stepY |
| 51 | + |
| 52 | + s.pdf.SetFillColor(r, g, b) |
| 53 | + s.pdf.Rect(x, y, stepX, stepY, "F") |
| 54 | + s.pdf.SetFillColor(s.defaultFillColor.Red, s.defaultFillColor.Green, s.defaultFillColor.Blue) |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + if prop.Chart != nil { |
| 60 | + s.chart.Add(cell, margins, prop.Chart) |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +func (s heatMap) getSteps(heatMap [][]int, cell *entity.Cell, prop *props.HeatMap) (float64, float64) { |
| 65 | + xSize := len(heatMap) |
| 66 | + stepX := (cell.Width) / float64(xSize-1) |
| 67 | + |
| 68 | + ySize := len(heatMap[0]) |
| 69 | + stepY := (cell.Height) / float64(ySize-1) |
| 70 | + |
| 71 | + return stepX, stepY |
| 72 | +} |
| 73 | + |
| 74 | +func GetHeatColor(i int, total int, invertScale bool, halfColor bool) (int, int, int) { |
| 75 | + offset := 360.0 / 7.0 / 2.0 |
| 76 | + iStep := GetStepWithOffset(360, float64(total), float64(i), -offset) |
| 77 | + |
| 78 | + if iStep < 0 { |
| 79 | + iStep = 360 + iStep |
| 80 | + } |
| 81 | + |
| 82 | + r, g, b, _ := HSVToRGB(iStep, 1.0, 1.0) |
| 83 | + return int(r), int(g), int(b) |
| 84 | +} |
| 85 | + |
| 86 | +func (s heatMap) getMax(matrix [][]int) int { |
| 87 | + var max = 0 |
| 88 | + for _, row := range matrix { |
| 89 | + for _, cell := range row { |
| 90 | + if cell > max { |
| 91 | + max = cell |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + return max |
| 97 | +} |
| 98 | + |
| 99 | +func (s heatMap) getTransparent(p *props.HeatMap) map[int]bool { |
| 100 | + m := make(map[int]bool) |
| 101 | + for _, t := range p.TransparentValues { |
| 102 | + m[t] = true |
| 103 | + } |
| 104 | + return m |
| 105 | +} |
| 106 | + |
| 107 | +// HSVToRGB converts an HSV triple to an RGB triple. |
| 108 | +// Source: https://github.com/Crazy3lf/colorconv/blob/master/colorconv.go |
| 109 | +func HSVToRGB(h, s, v float64) (r, g, b uint8, err error) { |
| 110 | + if h < 0 || h >= 360 || |
| 111 | + s < 0 || s > 1 || |
| 112 | + v < 0 || v > 1 { |
| 113 | + return 0, 0, 0, ErrOutOfRange |
| 114 | + } |
| 115 | + // When 0 ≤ h < 360, 0 ≤ s ≤ 1 and 0 ≤ v ≤ 1: |
| 116 | + C := v * s |
| 117 | + X := C * (1 - math.Abs(math.Mod(h/60, 2)-1)) |
| 118 | + m := v - C |
| 119 | + var Rnot, Gnot, Bnot float64 |
| 120 | + switch { |
| 121 | + case 0 <= h && h < 60: |
| 122 | + Rnot, Gnot, Bnot = C, X, 0 |
| 123 | + case 60 <= h && h < 120: |
| 124 | + Rnot, Gnot, Bnot = X, C, 0 |
| 125 | + case 120 <= h && h < 180: |
| 126 | + Rnot, Gnot, Bnot = 0, C, X |
| 127 | + case 180 <= h && h < 240: |
| 128 | + Rnot, Gnot, Bnot = 0, X, C |
| 129 | + case 240 <= h && h < 300: |
| 130 | + Rnot, Gnot, Bnot = X, 0, C |
| 131 | + case 300 <= h && h < 360: |
| 132 | + Rnot, Gnot, Bnot = C, 0, X |
| 133 | + } |
| 134 | + r = uint8(math.Round((Rnot + m) * 255)) |
| 135 | + g = uint8(math.Round((Gnot + m) * 255)) |
| 136 | + b = uint8(math.Round((Bnot + m) * 255)) |
| 137 | + return r, g, b, nil |
| 138 | +} |
| 139 | + |
| 140 | +func GetStep(scaleMax float64, valueMax float64) float64 { |
| 141 | + return scaleMax / valueMax |
| 142 | +} |
| 143 | + |
| 144 | +func GetStepWithOffset(scaleMax float64, valueMax float64, i float64, offset float64) float64 { |
| 145 | + scaleStep := GetStep(scaleMax, valueMax) |
| 146 | + iStep := i * scaleStep |
| 147 | + return iStep + offset |
| 148 | +} |
0 commit comments