medium support for all axis names.

This commit is contained in:
Will Charczuk 2016-08-06 22:27:26 -07:00
parent f7c53080bd
commit a846d2f4e8
3 changed files with 35 additions and 5 deletions

View file

@ -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() {

View file

@ -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) {

View file

@ -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)