small fixes for legend + background padding.

This commit is contained in:
Will Charczuk 2016-07-17 16:58:52 -07:00
parent eb61fbfd0f
commit 4f8680a2b1
2 changed files with 47 additions and 15 deletions

View file

@ -277,7 +277,12 @@ func CreateLegend(c *Chart, userDefaults ...Style) Renderable {
}
// DEFAULTS
legendPadding := 5
legendPadding := Box{
Top: 5,
Left: 5,
Right: 5,
Bottom: 5,
}
lineTextGap := 5
lineLengthMinimum := 25
@ -293,13 +298,16 @@ func CreateLegend(c *Chart, userDefaults ...Style) Renderable {
}
legend := Box{
Top: cb.Top, //padding
Top: cb.Top,
Left: cb.Left,
// bottom and right will be sized by the legend content + relevant padding.
}
legendContent := Box{
Top: legend.Top + legendPadding,
Left: legend.Left + legendPadding,
Top: legend.Top + legendPadding.Top,
Left: legend.Left + legendPadding.Left,
Right: legend.Left + legendPadding.Left,
Bottom: legend.Top + legendPadding.Top,
}
r.SetFont(legendStyle.GetFont())
@ -307,35 +315,46 @@ func CreateLegend(c *Chart, userDefaults ...Style) Renderable {
r.SetFontSize(legendStyle.GetFontSize())
// measure
labelCount := 0
for x := 0; x < len(labels); x++ {
if len(labels[x]) > 0 {
tb := r.MeasureText(labels[x])
legendContent.Bottom += (tb.Height() + DefaultMinimumTickVerticalSpacing)
rowRight := tb.Width() + legendContent.Left + lineLengthMinimum + lineTextGap
legendContent.Right = MaxInt(legendContent.Right, rowRight)
if labelCount > 0 {
legendContent.Bottom += DefaultMinimumTickVerticalSpacing
}
legendContent.Bottom += tb.Height()
right := legendContent.Left + tb.Width() + lineTextGap + lineLengthMinimum
legendContent.Right = MaxInt(legendContent.Right, right)
labelCount++
}
}
legend = legend.Grow(legendContent)
DrawBox(r, legend, legendStyle)
legend.Right = legendContent.Right + legendPadding.Right
legend.Bottom = legendContent.Bottom + legendPadding.Bottom
legendContent.Right = legend.Right - legendPadding
legendContent.Bottom = legend.Bottom - legendPadding
DrawBox(r, legend, legendStyle)
ycursor := legendContent.Top
tx := legendContent.Left
legendCount := 0
for x := 0; x < len(labels); x++ {
if len(labels[x]) > 0 {
if legendCount > 0 {
ycursor += DefaultMinimumTickVerticalSpacing
}
tb := r.MeasureText(labels[x])
ycursor += tb.Height()
ty := ycursor + tb.Height()
r.Text(labels[x], tx, ty)
r.Text(labels[x], tx, ycursor)
th2 := tb.Height() >> 1
lx := tx + tb.Width() + lineTextGap
ly := ycursor - th2
lx2 := legendContent.Right - legendPadding
ly := ty - th2
lx2 := legendContent.Right - legendPadding.Right
r.SetStrokeColor(lines[x].GetStrokeColor())
r.SetStrokeWidth(lines[x].GetStrokeWidth())
@ -345,7 +364,8 @@ func CreateLegend(c *Chart, userDefaults ...Style) Renderable {
r.LineTo(lx2, ly)
r.Stroke()
ycursor += DefaultMinimumTickVerticalSpacing
ycursor += tb.Height()
legendCount++
}
}
}

View file

@ -15,6 +15,18 @@ func drawChart(res http.ResponseWriter, req *http.Request) {
*/
graph := chart.Chart{
XAxis: chart.XAxis{
Style: chart.Style{Show: true},
},
YAxis: chart.YAxis{
Style: chart.Style{Show: true},
},
Background: chart.Style{
Padding: chart.Box{
Top: 20,
Left: 20,
},
},
Series: []chart.Series{
chart.ContinuousSeries{
Name: "A test series",