feat: support customize title

This commit is contained in:
vicanso 2022-01-01 20:11:51 +08:00
parent 06c326bdc3
commit 25e9984ad8
8 changed files with 167 additions and 7 deletions

View file

@ -194,7 +194,10 @@ func newPieChart(opt Options) *chart.PieChart {
if opt.Title.Left == "" {
opt.Title.Left = "center"
}
titleRender := newTitleRenderable(opt.Title, p.GetFont(), p.GetColorPalette().TextColor())
themeColorPalette := &ThemeColorPalette{
Theme: opt.Theme,
}
titleRender := newTitleRenderable(opt.Title, p.GetFont(), themeColorPalette.TextColor())
if titleRender != nil {
p.Elements = []chart.Renderable{
titleRender,

View file

@ -398,6 +398,18 @@ func TestParseECharsOptions(t *testing.T) {
}, options)
}
func TestUnmarshalJSON(t *testing.T) {
assert := assert.New(t)
var lp Position
err := lp.UnmarshalJSON([]byte("123"))
assert.Nil(err)
assert.Equal("123", string(lp))
err = lp.UnmarshalJSON([]byte(`"234"`))
assert.Nil(err)
assert.Equal("234", string(lp))
}
func BenchmarkEChartsRenderPNG(b *testing.B) {
for i := 0; i < b.N; i++ {
_, err := RenderEChartsToPNG(`{

View file

@ -363,7 +363,7 @@ func render(opts renderOptions) ([]byte, error) {
}
func indexHandler(w http.ResponseWriter, r *http.Request) {
if r.RequestURI != "/" {
if r.URL.Path != "/" {
return
}
query := r.URL.Query()

View file

@ -72,7 +72,7 @@ func DefaultLegendIconDraw(r chart.Renderer, opt LegendIconDrawOption) {
r.FillStroke()
}
func covertPercent(value string) float64 {
func convertPercent(value string) float64 {
if !strings.HasSuffix(value, "%") {
return -1
}
@ -104,7 +104,7 @@ func getLegendLeft(canvasWidth, legendBoxWidth int, opt LegendOption) int {
return left
}
if leftValue != "" {
percent := covertPercent(leftValue)
percent := convertPercent(leftValue)
if percent >= 0 {
return int(float64(canvasWidth) * percent)
}
@ -112,7 +112,7 @@ func getLegendLeft(canvasWidth, legendBoxWidth int, opt LegendOption) int {
return v
}
if rightValue != "" {
percent := covertPercent(rightValue)
percent := convertPercent(rightValue)
if percent >= 0 {
return canvasWidth - legendBoxWidth - int(float64(canvasWidth)*percent)
}

View file

@ -76,3 +76,38 @@ func TestNewLegendCustomize(t *testing.T) {
assert.Equal(tt.svg, buf.String())
}
}
func TestConvertPercent(t *testing.T) {
assert := assert.New(t)
assert.Equal(-1.0, convertPercent("12"))
assert.Equal(0.12, convertPercent("12%"))
}
func TestGetLegendLeft(t *testing.T) {
assert := assert.New(t)
assert.Equal(150, getLegendLeft(500, 200, LegendOption{}))
assert.Equal(0, getLegendLeft(500, 200, LegendOption{
Left: "left",
}))
assert.Equal(100, getLegendLeft(500, 200, LegendOption{
Left: "20%",
}))
assert.Equal(20, getLegendLeft(500, 200, LegendOption{
Left: "20",
}))
assert.Equal(300, getLegendLeft(500, 200, LegendOption{
Right: "right",
}))
assert.Equal(200, getLegendLeft(500, 200, LegendOption{
Right: "20%",
}))
assert.Equal(280, getLegendLeft(500, 200, LegendOption{
Right: "20",
}))
}

View file

@ -23,6 +23,7 @@
package charts
import (
"math"
"testing"
"github.com/stretchr/testify/assert"
@ -52,3 +53,25 @@ func TestHiddenRange(t *testing.T) {
assert.Equal(float64(0), r.GetDelta())
}
func TestYContinuousRange(t *testing.T) {
assert := assert.New(t)
r := YContinuousRange{}
r.Min = -math.MaxFloat64
r.Max = math.MaxFloat64
assert.True(r.IsZero())
r.SetMin(1.0)
assert.Equal(1.0, r.GetMin())
// 再次设置无效
r.SetMin(2.0)
assert.Equal(1.0, r.GetMin())
r.SetMax(5.0)
// *1.2
assert.Equal(6.0, r.GetMax())
// 再次设置无效
r.SetMax(10.0)
assert.Equal(6.0, r.GetMax())
}

View file

@ -40,9 +40,11 @@ func NewTitleCustomize(title Title) chart.Renderable {
if len(title.Text) == 0 || title.Style.Hidden {
return
}
if title.Font != nil {
r.SetFont(title.Font)
font := title.Font
if font == nil {
font, _ = chart.GetDefaultFont()
}
r.SetFont(font)
r.SetFontColor(title.Style.FontColor)
titleFontSize := title.Style.GetFontSize(chart.DefaultTitleFontSize)
r.SetFontSize(titleFontSize)

85
title_test.go Normal file
View file

@ -0,0 +1,85 @@
// MIT License
// Copyright (c) 2021 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 (
"bytes"
"testing"
"github.com/stretchr/testify/assert"
"github.com/wcharczuk/go-chart/v2"
"github.com/wcharczuk/go-chart/v2/drawing"
)
func TestTitleCustomize(t *testing.T) {
assert := assert.New(t)
tests := []struct {
title Title
svg string
}{
// 单行标题
{
title: Title{
Text: "Hello World!",
Style: chart.Style{
FontColor: drawing.ColorBlack,
},
},
svg: "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"800\" height=\"600\">\\n<text x=\"50\" y=\"71\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,0,1.0);font-size:23.0px;font-family:'Roboto Medium',sans-serif\">Hello World!</text></svg>",
},
// 多行标题,靠右
{
title: Title{
Text: "Hello World!\nHello World",
Style: chart.Style{
FontColor: drawing.ColorBlack,
},
Left: "right",
},
svg: "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"800\" height=\"600\">\\n<text x=\"474\" y=\"71\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,0,1.0);font-size:23.0px;font-family:'Roboto Medium',sans-serif\">Hello World!</text><text x=\"477\" y=\"94\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,0,1.0);font-size:23.0px;font-family:'Roboto Medium',sans-serif\">Hello World</text></svg>",
},
// 标题居中
{
title: Title{
Text: "Hello World!",
Style: chart.Style{
FontColor: drawing.ColorBlack,
},
Left: "center",
},
svg: "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"800\" height=\"600\">\\n<text x=\"262\" y=\"71\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,0,1.0);font-size:23.0px;font-family:'Roboto Medium',sans-serif\">Hello World!</text></svg>",
},
}
for _, tt := range tests {
r, err := chart.SVG(800, 600)
assert.Nil(err)
fn := NewTitleCustomize(tt.title)
fn(r, chart.NewBox(50, 50, 600, 400), chart.Style{
Font: chart.StyleTextDefaults().Font,
})
buf := bytes.Buffer{}
err = r.Save(&buf)
assert.Nil(err)
assert.Equal(tt.svg, buf.String())
}
}