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{ XAxis: chart.XAxis{
Style: chart.StyleShow(), Name: "Random Other Values",
NameStyle: chart.StyleShow(),
Style: chart.StyleShow(),
}, },
Series: []chart.Series{ Series: []chart.Series{
mainSeries, mainSeries,
@ -57,8 +59,8 @@ func drawChart(res http.ResponseWriter, req *http.Request) {
graph.Elements = []chart.Renderable{chart.Legend(&graph)} graph.Elements = []chart.Renderable{chart.Legend(&graph)}
res.Header().Set("Content-Type", "image/svg+xml") res.Header().Set("Content-Type", "image/png")
graph.Render(chart.SVG, res) graph.Render(chart.PNG, res)
} }
func main() { func main() {

View file

@ -8,6 +8,7 @@ import (
// XAxis represents the horizontal axis. // XAxis represents the horizontal axis.
type XAxis struct { type XAxis struct {
Name string Name string
NameStyle Style
Style Style Style Style
ValueFormatter ValueFormatter ValueFormatter ValueFormatter
Range Range Range Range
@ -99,6 +100,11 @@ func (xa XAxis) Measure(r Renderer, canvasBox Box, ra Range, defaults Style, tic
bottom = Math.MaxInt(bottom, ty) bottom = Math.MaxInt(bottom, ty)
} }
if xa.NameStyle.Show && len(xa.Name) > 0 {
tb := r.MeasureText(xa.Name)
bottom += DefaultXAxisMargin + tb.Height()
}
return Box{ return Box{
Top: canvasBox.Bottom, Top: canvasBox.Bottom,
Left: left, Left: left,
@ -121,6 +127,7 @@ func (xa XAxis) Render(r Renderer, canvasBox Box, ra Range, defaults Style, tick
tp := xa.GetTickPosition() tp := xa.GetTickPosition()
var tx, ty int var tx, ty int
var maxTextHeight int
for index, t := range ticks { for index, t := range ticks {
v := t.Value v := t.Value
lx := ra.Translate(v) lx := ra.Translate(v)
@ -139,22 +146,36 @@ func (xa XAxis) Render(r Renderer, canvasBox Box, ra Range, defaults Style, tick
case TickPositionUnderTick, TickPositionUnset: case TickPositionUnderTick, TickPositionUnset:
ty = canvasBox.Bottom + DefaultXAxisMargin + tb.Height() ty = canvasBox.Bottom + DefaultXAxisMargin + tb.Height()
r.Text(t.Label, tx-tb.Width()>>1, ty) r.Text(t.Label, tx-tb.Width()>>1, ty)
maxTextHeight = Math.MaxInt(maxTextHeight, tb.Height())
break break
case TickPositionBetweenTicks: case TickPositionBetweenTicks:
if index > 0 { if index > 0 {
llx := ra.Translate(ticks[index-1].Value) llx := ra.Translate(ticks[index-1].Value)
ltx := canvasBox.Left + llx ltx := canvasBox.Left + llx
finalTickStyle := tickStyle.InheritFrom(Style{TextHorizontalAlign: TextHorizontalAlignCenter})
Draw.TextWithin(r, t.Label, Box{ Draw.TextWithin(r, t.Label, Box{
Left: ltx, Left: ltx,
Right: tx, Right: tx,
Top: canvasBox.Bottom + DefaultXAxisMargin, Top: canvasBox.Bottom + DefaultXAxisMargin,
Bottom: canvasBox.Bottom + DefaultXAxisMargin + tb.Height(), 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 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 { if xa.GridMajorStyle.Show || xa.GridMinorStyle.Show {
for _, gl := range xa.GetGridLines(ticks) { for _, gl := range xa.GetGridLines(ticks) {
if (gl.IsMinor && xa.GridMinorStyle.Show) || (!gl.IsMinor && xa.GridMajorStyle.Show) { 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)) r.SetTextRotation(Math.DegreesToRadians(90))
tb := r.MeasureText(ya.Name) 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) ty := canvasBox.Bottom - (canvasBox.Height()>>1 + tb.Width()>>1)
r.Text(ya.Name, tx, ty) r.Text(ya.Name, tx, ty)