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