diff --git a/CHANGELOG.md b/CHANGELOG.md index 2fd6f43a..9829865b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Generalised mouse button FSM for use in widgets that need to track mouse button clicks. +======= +## [0.6.1] - 12-Feb-2019 + +### Fixes + +- The LineChart widget now correctly places custom labels. + ## [0.6.0] - 07-Feb-2019 ### Changed @@ -92,7 +99,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - The Gauge widget. - The Text widget. -[Unreleased]: https://github.com/mum4k/termdash/compare/v0.6.0...devel +[Unreleased]: https://github.com/mum4k/termdash/compare/v0.6.1...devel +[0.6.1]: https://github.com/mum4k/termdash/compare/v0.6.0...v0.6.1 [0.6.0]: https://github.com/mum4k/termdash/compare/v0.5.0...v0.6.0 [0.5.0]: https://github.com/mum4k/termdash/compare/v0.4.0...v0.5.0 [0.4.0]: https://github.com/mum4k/termdash/compare/v0.3.0...v0.4.0 diff --git a/attrrange/attrrange.go b/attrrange/attrrange.go index 736cafec..94e7c383 100644 --- a/attrrange/attrrange.go +++ b/attrrange/attrrange.go @@ -14,7 +14,7 @@ // Package attrrange simplifies tracking of attributes that apply to a range of // items. -// Refer to the examples if the test file for details on usage. +// Refer to the examples in the test file for details on usage. package attrrange import ( diff --git a/widgets/linechart/axes/label.go b/widgets/linechart/axes/label.go index cca6a1e7..ce2a22bd 100644 --- a/widgets/linechart/axes/label.go +++ b/widgets/linechart/axes/label.go @@ -169,7 +169,7 @@ func xLabels(scale *XScale, graphZero image.Point, customLabels map[int]string) next := 0 for haveLabels := 0; haveLabels <= int(scale.Max.Value); haveLabels = len(res) { - label, err := colLabel(scale, space, next, customLabels) + label, err := colLabel(scale, space, customLabels) if err != nil { return nil, err } @@ -202,23 +202,21 @@ func xLabels(scale *XScale, graphZero image.Point, customLabels map[int]string) return res, nil } -// colLabel returns a label placed either at the beginning of the space. +// colLabel returns a label placed at the beginning of the space. // The space is adjusted according to how much space was taken by the label. // Returns nil, nil if the label doesn't fit in the space. -func colLabel(scale *XScale, space *xSpace, labelNum int, customLabels map[int]string) (*Label, error) { - var val *Value - if custom, ok := customLabels[labelNum]; ok { - val = NewTextValue(custom) - } else { - pos := space.Relative() - v, err := scale.CellLabel(pos.X) - if err != nil { - return nil, fmt.Errorf("unable to determine label value for column %d: %v", pos.X, err) - } - val = v +func colLabel(scale *XScale, space *xSpace, customLabels map[int]string) (*Label, error) { + pos := space.Relative() + label, err := scale.CellLabel(pos.X) + if err != nil { + return nil, fmt.Errorf("unable to determine label value for column %d: %v", pos.X, err) + } + + if custom, ok := customLabels[int(label.Value)]; ok { + label = NewTextValue(custom) } - labelLen := len(val.Text()) + labelLen := len(label.Text()) if labelLen > space.Remaining() { return nil, nil } @@ -229,7 +227,7 @@ func colLabel(scale *XScale, space *xSpace, labelNum int, customLabels map[int]s } return &Label{ - Value: val, + Value: label, Pos: abs, }, nil } diff --git a/widgets/linechart/axes/label_test.go b/widgets/linechart/axes/label_test.go index 7ea97e57..d1fc746a 100644 --- a/widgets/linechart/axes/label_test.go +++ b/widgets/linechart/axes/label_test.go @@ -265,6 +265,26 @@ func TestXLabels(t *testing.T) { {NewTextValue("d"), image.Point{94, 3}}, }, }, + { + desc: "custom labels provided, but only some fit, regression for #117", + numPoints: 8, + graphWidth: 5, + graphZero: image.Point{0, 1}, + customLabels: map[int]string{ + 0: "a", + 1: "b", + 2: "c", + 3: "d", + 4: "e", + 5: "f", + 6: "g", + 7: "h", + }, + want: []*Label{ + {NewTextValue("a"), image.Point{0, 3}}, + {NewTextValue("g"), image.Point{4, 3}}, + }, + }, { desc: "only some custom labels provided", numPoints: 4,