diff --git a/title.go b/title.go
index cd8f6ad..a5ba6b3 100644
--- a/title.go
+++ b/title.go
@@ -30,12 +30,21 @@ import (
)
type TitleOption struct {
- Text string
- Subtext string
- Style chart.Style
+ // Title text, support \n for new line
+ Text string
+ // Subtitle text, support \n for new line
+ Subtext string
+ // Title style
+ Style chart.Style
+ // Subtitle style
SubtextStyle chart.Style
- Left string
- Top string
+ // Distance between title component and the left side of the container.
+ // It can be pixel value: 20, percentage value: 20%,
+ // or position value: right, center.
+ Left string
+ // Distance between title component and the top side of the container.
+ // It can be pixel value: 20.
+ Top string
}
type titleMeasureOption struct {
width int
@@ -138,7 +147,7 @@ func drawTitle(p *Draw, opt *TitleOption) (chart.Box, error) {
}
height := titleY + padding.Top + padding.Bottom
box := padding.Clone()
- box.Right = box.Left + width
+ box.Right = box.Left + titleX + width
box.Bottom = box.Top + height
return box, nil
diff --git a/title_test.go b/title_test.go
new file mode 100644
index 0000000..b5928eb
--- /dev/null
+++ b/title_test.go
@@ -0,0 +1,128 @@
+// MIT License
+
+// Copyright (c) 2022 Tree Xie
+
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to deal
+// in the Software without restriction, including without limitation the rights
+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
+
+// The above copyright notice and this permission notice shall be included in all
+// copies or substantial portions of the Software.
+
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+// SOFTWARE.
+
+package charts
+
+import (
+ "testing"
+
+ "github.com/stretchr/testify/assert"
+ "github.com/wcharczuk/go-chart/v2"
+ "github.com/wcharczuk/go-chart/v2/drawing"
+)
+
+func TestSplitTitleText(t *testing.T) {
+ assert := assert.New(t)
+
+ assert.Equal([]string{
+ "a",
+ "b",
+ }, splitTitleText("a\nb"))
+ assert.Equal([]string{
+ "a",
+ }, splitTitleText("a\n "))
+}
+
+func TestDrawTitle(t *testing.T) {
+ assert := assert.New(t)
+
+ newOption := func() *TitleOption {
+ f, _ := chart.GetDefaultFont()
+ return &TitleOption{
+ Text: "title\nHello",
+ Subtext: "subtitle\nWorld!",
+ Style: chart.Style{
+ FontSize: 14,
+ Font: f,
+ FontColor: drawing.ColorBlack,
+ },
+ SubtextStyle: chart.Style{
+ FontSize: 10,
+ Font: f,
+ FontColor: drawing.ColorBlue,
+ },
+ }
+ }
+ newDraw := func() *Draw {
+ d, _ := NewDraw(DrawOption{
+ Width: 400,
+ Height: 300,
+ })
+ return d
+ }
+
+ tests := []struct {
+ newDraw func() *Draw
+ newOption func() *TitleOption
+ result string
+ box chart.Box
+ }{
+ {
+ newDraw: newDraw,
+ newOption: newOption,
+ result: "",
+ box: chart.Box{
+ Right: 43,
+ Bottom: 58,
+ },
+ },
+ {
+ newDraw: newDraw,
+ newOption: func() *TitleOption {
+ opt := newOption()
+ opt.Left = PositionRight
+ opt.Top = "50"
+ return opt
+ },
+ result: "",
+ box: chart.Box{
+ Right: 400,
+ Bottom: 108,
+ },
+ },
+ {
+ newDraw: newDraw,
+ newOption: func() *TitleOption {
+ opt := newOption()
+ opt.Left = PositionCenter
+ opt.Top = "10"
+ return opt
+ },
+ result: "",
+ box: chart.Box{
+ Right: 222,
+ Bottom: 68,
+ },
+ },
+ }
+ for _, tt := range tests {
+ d := tt.newDraw()
+ o := tt.newOption()
+ b, err := drawTitle(d, o)
+ assert.Nil(err)
+ assert.Equal(tt.box, b)
+ data, err := d.Bytes()
+ assert.Nil(err)
+ assert.NotEmpty(data)
+ assert.Equal(tt.result, string(data))
+ }
+}