From 6eff5912ca3e96b979e59cb56a2702d87e87604d Mon Sep 17 00:00:00 2001 From: Rico Date: Tue, 8 Feb 2022 04:02:16 +0100 Subject: [PATCH] fix: introduce individual spacing constant for legend Legends need to use their individual text spacing - not use the Tick spacing. For vertical lists it looks a lot better to use a spacing of 10 instead of 20. Prior to this commit this couldn't be set in the defaults without also manipulating the axis tick spacing. Using their own constant, this can be now easily configured. --- defaults.go | 5 +++++ legend.go | 14 +++++++------- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/defaults.go b/defaults.go index d74330f..ceb9645 100644 --- a/defaults.go +++ b/defaults.go @@ -58,6 +58,11 @@ const ( // DefaultMinimumTickVerticalSpacing is the minimum distance between vertical ticks. DefaultMinimumTickVerticalSpacing = 20 + // DefaultLegendHorizontalSpacing is the minimum distance between two horizontal elements in the legend. + DefaultLegendHorizontalSpacing = 20 + // DefaultLegendVerticalSpacing is the minimum distance between two vertical elements in the legend. + DefaultLegendVerticalSpacing = 10 + // DefaultDateFormat is the default date format. DefaultDateFormat = "2006-01-02" // DefaultDateHourFormat is the date format for hour timestamp formats. diff --git a/legend.go b/legend.go index 8998edb..7f1c13f 100644 --- a/legend.go +++ b/legend.go @@ -64,7 +64,7 @@ func Legend(c *Chart, userDefaults ...Style) Renderable { if len(labels[x]) > 0 { tb := r.MeasureText(labels[x]) if labelCount > 0 { - legendContent.Bottom += DefaultMinimumTickVerticalSpacing + legendContent.Bottom += DefaultLegendVerticalSpacing } legendContent.Bottom += tb.Height() right := legendContent.Left + tb.Width() + lineTextGap + lineLengthMinimum @@ -89,7 +89,7 @@ func Legend(c *Chart, userDefaults ...Style) Renderable { label = labels[x] if len(label) > 0 { if legendCount > 0 { - ycursor += DefaultMinimumTickVerticalSpacing + ycursor += DefaultLegendVerticalSpacing } tb := r.MeasureText(label) @@ -179,7 +179,7 @@ func LegendLineLeft(c *Chart, userDefaults ...Style) Renderable { if len(labels[x]) > 0 { tb := r.MeasureText(labels[x]) if labelCount > 0 { - legendContent.Bottom += DefaultMinimumTickVerticalSpacing + legendContent.Bottom += DefaultLegendVerticalSpacing } legendContent.Bottom += tb.Height() right := legendContent.Left + tb.Width() + lineTextGap + lineLengthMinimum @@ -204,7 +204,7 @@ func LegendLineLeft(c *Chart, userDefaults ...Style) Renderable { label = labels[x] if len(label) > 0 { if legendCount > 0 { - ycursor += DefaultMinimumTickVerticalSpacing + ycursor += DefaultLegendVerticalSpacing } // Calculate text dimensions @@ -329,7 +329,7 @@ func LegendThin(c *Chart, userDefaults ...Style) Renderable { r.LineTo(lx+lineLengthMinimum, ly) r.Stroke() - tx += textBox.Width() + DefaultMinimumTickHorizontalSpacing + lineTextGap + lineLengthMinimum + tx += textBox.Width() + DefaultLegendHorizontalSpacing + lineTextGap + lineLengthMinimum } } } @@ -395,7 +395,7 @@ func LegendLeft(c *Chart, userDefaults ...Style) Renderable { if len(labels[x]) > 0 { tb := r.MeasureText(labels[x]) if labelCount > 0 { - legendContent.Bottom += DefaultMinimumTickVerticalSpacing + legendContent.Bottom += DefaultLegendVerticalSpacing } legendContent.Bottom += tb.Height() right := legendContent.Left + tb.Width() + lineTextGap + lineLengthMinimum @@ -420,7 +420,7 @@ func LegendLeft(c *Chart, userDefaults ...Style) Renderable { label = labels[x] if len(label) > 0 { if legendCount > 0 { - ycursor += DefaultMinimumTickVerticalSpacing + ycursor += DefaultLegendVerticalSpacing } tb := r.MeasureText(label)