From a846d2f4e87b3b75b28c55383573b57d07d529ec Mon Sep 17 00:00:00 2001 From: Will Charczuk Date: Sat, 6 Aug 2016 22:27:26 -0700 Subject: [PATCH] medium support for all axis names. --- _examples/min_max/main.go | 8 +++++--- xaxis.go | 23 ++++++++++++++++++++++- yaxis.go | 9 ++++++++- 3 files changed, 35 insertions(+), 5 deletions(-) diff --git a/_examples/min_max/main.go b/_examples/min_max/main.go index 2376efe..05c1413 100644 --- a/_examples/min_max/main.go +++ b/_examples/min_max/main.go @@ -44,7 +44,9 @@ func drawChart(res http.ResponseWriter, req *http.Request) { }, }, XAxis: chart.XAxis{ - Style: chart.StyleShow(), + Name: "Random Other Values", + NameStyle: chart.StyleShow(), + Style: chart.StyleShow(), }, Series: []chart.Series{ mainSeries, @@ -57,8 +59,8 @@ func drawChart(res http.ResponseWriter, req *http.Request) { graph.Elements = []chart.Renderable{chart.Legend(&graph)} - res.Header().Set("Content-Type", "image/svg+xml") - graph.Render(chart.SVG, res) + res.Header().Set("Content-Type", "image/png") + graph.Render(chart.PNG, res) } func main() { diff --git a/xaxis.go b/xaxis.go index 7e7a6cf..8cbf788 100644 --- a/xaxis.go +++ b/xaxis.go @@ -8,6 +8,7 @@ import ( // XAxis represents the horizontal axis. type XAxis struct { Name string + NameStyle Style Style Style ValueFormatter ValueFormatter Range Range @@ -99,6 +100,11 @@ func (xa XAxis) Measure(r Renderer, canvasBox Box, ra Range, defaults Style, tic bottom = Math.MaxInt(bottom, ty) } + if xa.NameStyle.Show && len(xa.Name) > 0 { + tb := r.MeasureText(xa.Name) + bottom += DefaultXAxisMargin + tb.Height() + } + return Box{ Top: canvasBox.Bottom, Left: left, @@ -121,6 +127,7 @@ func (xa XAxis) Render(r Renderer, canvasBox Box, ra Range, defaults Style, tick tp := xa.GetTickPosition() var tx, ty int + var maxTextHeight int for index, t := range ticks { v := t.Value lx := ra.Translate(v) @@ -139,22 +146,36 @@ func (xa XAxis) Render(r Renderer, canvasBox Box, ra Range, defaults Style, tick case TickPositionUnderTick, TickPositionUnset: ty = canvasBox.Bottom + DefaultXAxisMargin + tb.Height() r.Text(t.Label, tx-tb.Width()>>1, ty) + maxTextHeight = Math.MaxInt(maxTextHeight, tb.Height()) break case TickPositionBetweenTicks: if index > 0 { llx := ra.Translate(ticks[index-1].Value) ltx := canvasBox.Left + llx + finalTickStyle := tickStyle.InheritFrom(Style{TextHorizontalAlign: TextHorizontalAlignCenter}) Draw.TextWithin(r, t.Label, Box{ Left: ltx, Right: tx, Top: canvasBox.Bottom + DefaultXAxisMargin, Bottom: canvasBox.Bottom + DefaultXAxisMargin + tb.Height(), - }, tickStyle.InheritFrom(Style{TextHorizontalAlign: TextHorizontalAlignCenter})) + }, finalTickStyle) + + ftb := Text.MeasureLines(r, Text.WrapFit(r, t.Label, tx-ltx, finalTickStyle), finalTickStyle) + maxTextHeight = Math.MaxInt(maxTextHeight, ftb.Height()) } break } } + nameStyle := xa.NameStyle.InheritFrom(defaults) + if xa.NameStyle.Show && len(xa.Name) > 0 { + nameStyle.GetTextOptions().WriteToRenderer(r) + tb := r.MeasureText(xa.Name) + tx := canvasBox.Right - (canvasBox.Width()>>1 + tb.Width()>>1) + ty := canvasBox.Bottom + DefaultXAxisMargin + maxTextHeight + DefaultXAxisMargin + tb.Height() + r.Text(xa.Name, tx, ty) + } + if xa.GridMajorStyle.Show || xa.GridMinorStyle.Show { for _, gl := range xa.GetGridLines(ticks) { if (gl.IsMinor && xa.GridMinorStyle.Show) || (!gl.IsMinor && xa.GridMajorStyle.Show) { diff --git a/yaxis.go b/yaxis.go index 5e8a2c6..abc9f5e 100644 --- a/yaxis.go +++ b/yaxis.go @@ -175,7 +175,14 @@ func (ya YAxis) Render(r Renderer, canvasBox Box, ra Range, defaults Style, tick r.SetTextRotation(Math.DegreesToRadians(90)) tb := r.MeasureText(ya.Name) - tx := canvasBox.Right + int(sw) + DefaultYAxisMargin + maxTextWidth + DefaultYAxisMargin + + var tx int + if ya.AxisType == YAxisPrimary { + tx = canvasBox.Right + int(sw) + DefaultYAxisMargin + maxTextWidth + DefaultYAxisMargin + } else if ya.AxisType == YAxisSecondary { + tx = canvasBox.Left - (DefaultYAxisMargin + int(sw) + maxTextWidth + DefaultYAxisMargin) + } + ty := canvasBox.Bottom - (canvasBox.Height()>>1 + tb.Width()>>1) r.Text(ya.Name, tx, ty)