fixing fit issues on the xaxis labels for stacked bar.

This commit is contained in:
Will Charczuk 2016-07-30 09:35:44 -07:00
parent 9b4307e186
commit 9c96af2a22
6 changed files with 42 additions and 12 deletions

View file

@ -41,9 +41,7 @@ func (d draw) LineSeries(r Renderer, canvasBox Box, xrange, yrange Range, style
r.Fill() r.Fill()
} }
r.SetStrokeColor(style.GetStrokeColor()) style.GetStrokeOptions().WriteToRenderer(r)
r.SetStrokeDashArray(style.GetStrokeDashArray())
r.SetStrokeWidth(style.GetStrokeWidth())
r.MoveTo(x0, y0) r.MoveTo(x0, y0)
for i := 1; i < vs.Len(); i++ { for i := 1; i < vs.Len(); i++ {

View file

@ -10,9 +10,6 @@ import (
func drawChart(res http.ResponseWriter, req *http.Request) { func drawChart(res http.ResponseWriter, req *http.Request) {
sbc := chart.StackedBarChart{ sbc := chart.StackedBarChart{
Background: chart.Style{
Padding: chart.Box{Top: 50, Left: 50, Right: 50, Bottom: 50},
},
XAxis: chart.Style{ XAxis: chart.Style{
Show: true, Show: true,
}, },
@ -21,7 +18,7 @@ func drawChart(res http.ResponseWriter, req *http.Request) {
}, },
Bars: []chart.StackedBar{ Bars: []chart.StackedBar{
{ {
Name: "Katrina like animals that are very fat and furry.", Name: "This is a very long string to test word break wrapping.",
Values: []chart.Value{ Values: []chart.Value{
{Value: 5, Label: "Blue"}, {Value: 5, Label: "Blue"},
{Value: 5, Label: "Green"}, {Value: 5, Label: "Green"},

View file

@ -111,7 +111,7 @@ func (sbc StackedBarChart) Render(rp RendererProvider, w io.Writer) error {
} }
r.SetDPI(sbc.GetDPI(DefaultDPI)) r.SetDPI(sbc.GetDPI(DefaultDPI))
canvasBox := sbc.getAdjustedCanvasBox(sbc.getDefaultCanvasBox()) canvasBox := sbc.getAdjustedCanvasBox(r, sbc.getDefaultCanvasBox())
sbc.drawBars(r, canvasBox) sbc.drawBars(r, canvasBox)
sbc.drawXAxis(r, canvasBox) sbc.drawXAxis(r, canvasBox)
sbc.drawYAxis(r, canvasBox) sbc.drawYAxis(r, canvasBox)
@ -228,18 +228,47 @@ func (sbc StackedBarChart) getDefaultCanvasBox() Box {
return sbc.Box() return sbc.Box()
} }
func (sbc StackedBarChart) getAdjustedCanvasBox(canvasBox Box) Box { func (sbc StackedBarChart) getAdjustedCanvasBox(r Renderer, canvasBox Box) Box {
var totalWidth int var totalWidth int
for _, bar := range sbc.Bars { for _, bar := range sbc.Bars {
totalWidth += bar.GetWidth() + sbc.GetBarSpacing() totalWidth += bar.GetWidth() + sbc.GetBarSpacing()
} }
if sbc.XAxis.Show {
xaxisHeight := DefaultVerticalTickHeight
axisStyle := sbc.XAxis.InheritFrom(sbc.styleDefaultsAxes())
axisStyle.WriteToRenderer(r)
cursor := canvasBox.Left
for _, bar := range sbc.Bars {
if len(bar.Name) > 0 {
barLabelBox := Box{
Top: canvasBox.Bottom + DefaultXAxisMargin,
Left: cursor,
Right: cursor + bar.GetWidth() + sbc.GetBarSpacing(),
Bottom: sbc.GetHeight(),
}
lines := Text.WrapFit(r, bar.Name, barLabelBox.Width(), axisStyle)
linesBox := Text.MeasureLines(r, lines, axisStyle)
xaxisHeight = Math.MaxInt(linesBox.Height()+(2*DefaultXAxisMargin), xaxisHeight)
}
}
return Box{
Top: canvasBox.Top,
Left: canvasBox.Left,
Right: canvasBox.Left + totalWidth,
Bottom: sbc.GetHeight() - xaxisHeight,
}
}
return Box{ return Box{
Top: canvasBox.Top, Top: canvasBox.Top,
Left: canvasBox.Left, Left: canvasBox.Left,
Right: canvasBox.Left + totalWidth, Right: canvasBox.Left + totalWidth,
Bottom: canvasBox.Bottom, Bottom: canvasBox.Bottom,
} }
} }
// Box returns the chart bounds as a box. // Box returns the chart bounds as a box.

View file

@ -32,7 +32,7 @@ func (t Ticks) Less(i, j int) bool {
} }
// GenerateContinuousTicksWithStep generates a set of ticks. // GenerateContinuousTicksWithStep generates a set of ticks.
func GenerateContinuousTicksWithStep(ra Range, step float64, vf ValueFormatter) []Tick { func GenerateContinuousTicksWithStep(ra Range, step float64, vf ValueFormatter, includeMax bool) []Tick {
var ticks []Tick var ticks []Tick
min, max := ra.GetMin(), ra.GetMax() min, max := ra.GetMin(), ra.GetMax()
for cursor := min; cursor <= max; cursor += step { for cursor := min; cursor <= max; cursor += step {
@ -46,6 +46,12 @@ func GenerateContinuousTicksWithStep(ra Range, step float64, vf ValueFormatter)
return ticks return ticks
} }
} }
if includeMax {
ticks = append(ticks, Tick{
Value: ra.GetMax(),
Label: vf(ra.GetMax()),
})
}
return ticks return ticks
} }

View file

@ -54,7 +54,7 @@ func (xa XAxis) GetTicks(r Renderer, ra Range, defaults Style, vf ValueFormatter
return tp.GetTicks(vf) return tp.GetTicks(vf)
} }
step := CalculateContinuousTickStep(r, ra, false, xa.Style.InheritFrom(defaults), vf) step := CalculateContinuousTickStep(r, ra, false, xa.Style.InheritFrom(defaults), vf)
return GenerateContinuousTicksWithStep(ra, step, vf) return GenerateContinuousTicksWithStep(ra, step, vf, xa.TickPosition == TickPositionBetweenTicks)
} }
// GetGridLines returns the gridlines for the axis. // GetGridLines returns the gridlines for the axis.

View file

@ -48,7 +48,7 @@ func (ya YAxis) GetTicks(r Renderer, ra Range, defaults Style, vf ValueFormatter
return tp.GetTicks(vf) return tp.GetTicks(vf)
} }
step := CalculateContinuousTickStep(r, ra, true, ya.Style.InheritFrom(defaults), vf) step := CalculateContinuousTickStep(r, ra, true, ya.Style.InheritFrom(defaults), vf)
return GenerateContinuousTicksWithStep(ra, step, vf) return GenerateContinuousTicksWithStep(ra, step, vf, true)
} }
// GetGridLines returns the gridlines for the axis. // GetGridLines returns the gridlines for the axis.