diff options
| author | Kurt <kurt.w.jung@gmail.com> | 2018-04-03 14:13:52 -0400 | 
|---|---|---|
| committer | Kurt <kurt.w.jung@gmail.com> | 2018-04-03 14:13:52 -0400 | 
| commit | 0e1803180e2d0e5dab7f67ed5592974ccc2de7fa (patch) | |
| tree | cdc55dd891aba55f803ac1c032e761a0319278e2 | |
| parent | bc3ff903af0b59e0a5598ced64dfd8713d7959e2 (diff) | |
Add option to rotate X-axis labels; demonstrate in grid example
| -rw-r--r-- | fpdf_test.go | 7 | ||||
| -rw-r--r-- | grid.go | 45 | 
2 files changed, 33 insertions, 19 deletions
| diff --git a/fpdf_test.go b/fpdf_test.go index 26aae6d..bbeb5b0 100644 --- a/fpdf_test.go +++ b/fpdf_test.go @@ -2105,6 +2105,7 @@ func ExampleNewGrid() {  	gr.Grid(pdf)  	gr = gofpdf.NewGrid(13, 154, 187, 128) +	gr.XLabelRotate = true  	gr.TickmarksExtentX(0, 1, 12)  	gr.XDiv = 5  	gr.TickmarksContainY(0, 1.1) @@ -2121,12 +2122,14 @@ func ExampleNewGrid() {  	for month, val := range pts {  		dot(float64(month)+0.5, val)  	} -	pdf.SetDrawColor(0, 0, 0) -	pdf.SetLineWidth(0.1) +	pdf.SetDrawColor(255, 64, 64) +	pdf.SetAlpha(0.5, "Normal") +	pdf.SetLineWidth(1.2)  	gr.Plot(pdf, 0.5, 11.5, 50, func(x float64) float64 {  		// http://www.xuru.org/rt/PR.asp  		return 0.227 * math.Exp(-0.0373*x*x+0.471*x)  	}) +	pdf.SetAlpha(1.0, "Normal")  	pdf.SetXY(gr.X(0.5), gr.Y(1.35))  	pdf.SetFontSize(14)  	pdf.Write(0, "Solar energy (MWh) per month, 2016") @@ -75,7 +75,9 @@ type GridType struct {  	// Tickmarks  	xTicks, yTicks []float64  	// Labels are inside of graph boundary -	XInside, YInside bool +	XLabelIn, YLabelIn bool +	// Labels on X-axis should be rotated +	XLabelRotate bool  	// Formatters; use nil to eliminate labels  	XTickStr, YTickStr TickFormatFncType  	// Subdivisions between tickmarks @@ -133,8 +135,9 @@ 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.XLabelIn = false +	grid.YLabelIn = false +	grid.XLabelRotate = false  	grid.XDiv = 10  	grid.YDiv = 10  	grid.ClrText = RGBAType{R: 0, G: 0, B: 0, Alpha: 1} @@ -258,17 +261,11 @@ func lineAttr(pdf *Fpdf, clr RGBAType, lineWd float64) {  // Grid generates a graph-paperlike set of grid lines on the current page.  func (g GridType) Grid(pdf *Fpdf) {  	var st StateType -	// const textSz = 8 -	// var halfTextSz = g.TextSize / 2  	var yLen, xLen int  	var textSz, halfTextSz, yMin, yMax, xMin, xMax, yDiv, xDiv float64  	var str string  	var strOfs, strWd, tp, bt, lf, rt, drawX, drawY float64 -	textSz = pdf.PointToUnitConvert(g.TextSize) -	halfTextSz = textSz / 2 -	strOfs = pdf.GetStringWidth("I") -  	xLen = len(g.xTicks)  	yLen = len(g.yTicks)  	if xLen > 1 && yLen > 1 { @@ -284,8 +281,12 @@ func (g GridType) Grid(pdf *Fpdf) {  			pdf.Line(x1, y1, x2, y2)  		} +		textSz = pdf.PointToUnitConvert(g.TextSize) +		halfTextSz = textSz / 2 +  		pdf.SetAutoPageBreak(false, 0)  		pdf.SetFontUnitSize(textSz) +		strOfs = pdf.GetStringWidth("0")  		pdf.SetFillColor(255, 255, 255)  		pdf.SetCellMargin(0) @@ -341,15 +342,25 @@ func (g GridType) Grid(pdf *Fpdf) {  				str = g.XTickStr(x, g.xPrecision)  				strWd = pdf.GetStringWidth(str)  				drawX = g.X(x) -				pdf.TransformBegin() -				pdf.TransformRotate(90, drawX, drawY) -				if g.XInside { -					pdf.SetXY(drawX+strOfs, drawY-halfTextSz) +				if g.XLabelRotate { +					pdf.TransformBegin() +					pdf.TransformRotate(90, drawX, drawY) +					if g.XLabelIn { +						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()  				} else { -					pdf.SetXY(drawX-strOfs-strWd, drawY-halfTextSz) +					drawX -= strWd / 2.0 +					if g.XLabelIn { +						pdf.SetXY(drawX, drawY-textSz-strOfs) +					} else { +						pdf.SetXY(drawX, drawY+strOfs) +					} +					pdf.CellFormat(strWd, textSz, str, "", 0, "L", true, 0, "")  				} -				pdf.CellFormat(strWd, textSz, str, "", 0, "L", true, 0, "") -				pdf.TransformEnd()  			}  		} @@ -360,7 +371,7 @@ func (g GridType) Grid(pdf *Fpdf) {  				// str = strconv.FormatFloat(y, 'f', g.yPrecision, 64)  				str = g.YTickStr(y, g.yPrecision)  				strWd = pdf.GetStringWidth(str) -				if g.YInside { +				if g.YLabelIn {  					pdf.SetXY(drawX+strOfs, g.Y(y)-halfTextSz)  				} else {  					pdf.SetXY(lf-strOfs-strWd, g.Y(y)-halfTextSz) | 
