Skip to content

Commit

Permalink
Add option to place chart labels outside of grid boundary. Make this …
Browse files Browse the repository at this point in the history
…the default
  • Loading branch information
jung-kurt committed Apr 3, 2018
1 parent 160e539 commit 5a1fdf3
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 10 deletions.
6 changes: 3 additions & 3 deletions fpdf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2099,12 +2099,12 @@ func ExampleNewGrid() {
pdf.SetFont("Arial", "", 12)
pdf.AddPage()

gr := gofpdf.NewGrid(10, 10, 190, 133)
gr := gofpdf.NewGrid(13, 10, 187, 130)
gr.TickmarksExtentX(0, 10, 4)
gr.TickmarksExtentY(0, 10, 3)
gr.Grid(pdf)

gr = gofpdf.NewGrid(10, 154, 190, 133)
gr = gofpdf.NewGrid(13, 154, 187, 128)
gr.TickmarksExtentX(0, 1, 12)
gr.XDiv = 5
gr.TickmarksContainY(0, 1.1)
Expand Down Expand Up @@ -2132,7 +2132,7 @@ func ExampleNewGrid() {
pdf.Write(0, "Solar energy (MWh) per month, 2016")
pdf.AddPage()

gr = gofpdf.NewGrid(10, 10, 190, 277)
gr = gofpdf.NewGrid(13, 10, 187, 274)
gr.TickmarksContainX(2.3, 3.4)
gr.TickmarksContainY(10.4, 56.8)
gr.Grid(pdf)
Expand Down
25 changes: 18 additions & 7 deletions grid.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ type GridType struct {
xm, xb, ym, yb float64
// Tickmarks
xTicks, yTicks []float64
// Labels are inside of graph boundary
XInside, YInside bool
// Formatters; use nil to eliminate labels
XTickStr, YTickStr TickFormatFncType
// Subdivisions between tickmarks
Expand Down Expand Up @@ -131,6 +133,8 @@ func NewGrid(x, y, w, h float64) (grid GridType) {
grid.TextSize = 7 // Points
grid.TickmarksExtentX(0, 1, 1)
grid.TickmarksExtentY(0, 1, 1)
grid.XInside = false
grid.YInside = false
grid.XDiv = 10
grid.YDiv = 10
grid.ClrText = RGBAType{R: 0, G: 0, B: 0, Alpha: 1}
Expand Down Expand Up @@ -262,9 +266,8 @@ func (g GridType) Grid(pdf *Fpdf) {
var strOfs, strWd, tp, bt, lf, rt, drawX, drawY float64

textSz = pdf.PointToUnitConvert(g.TextSize)
halfTextSz = textSz / 2
halfTextSz = textSz / 4
strOfs = pdf.GetStringWidth("I")
unused(strOfs)

xLen = len(g.xTicks)
yLen = len(g.yTicks)
Expand Down Expand Up @@ -333,27 +336,35 @@ func (g GridType) Grid(pdf *Fpdf) {

// X labels
if g.XTickStr != nil {
drawY = bt // g.Y(yMin)
drawY = bt
for _, x := range g.xTicks {
str = g.XTickStr(x, g.xPrecision) // strconv.FormatFloat(x, 'f', g.xPrecision, 64)
str = g.XTickStr(x, g.xPrecision)
strWd = pdf.GetStringWidth(str)
drawX = g.X(x)
pdf.TransformBegin()
pdf.TransformRotate(90, drawX, drawY)
pdf.SetXY(drawX+strOfs, drawY-halfTextSz)
if g.XInside {
pdf.SetXY(drawX+strOfs, drawY-halfTextSz)
} else {
pdf.SetXY(drawX-strOfs-strWd, drawY-halfTextSz)
}
pdf.CellFormat(strWd, textSz, str, "", 0, "L", true, 0, "")
pdf.TransformEnd()
}
}

// Y labels
if g.YTickStr != nil {
drawX = lf + strOfs // g.X(xMin) + strOfs
drawX = lf
for _, y := range g.yTicks {
// str = strconv.FormatFloat(y, 'f', g.yPrecision, 64)
str = g.YTickStr(y, g.yPrecision)
strWd = pdf.GetStringWidth(str)
pdf.SetXY(drawX, g.Y(y)-halfTextSz)
if g.YInside {
pdf.SetXY(drawX+strOfs, g.Y(y)-halfTextSz)
} else {
pdf.SetXY(lf-strOfs-strWd, g.Y(y)-halfTextSz)
}
pdf.CellFormat(strWd, textSz, str, "", 0, "L", true, 0, "")
}
}
Expand Down

0 comments on commit 5a1fdf3

Please sign in to comment.