From 25e9984ad80090b61e60f2b03bde7bb37fc3b80a Mon Sep 17 00:00:00 2001 From: vicanso Date: Sat, 1 Jan 2022 20:11:51 +0800 Subject: [PATCH] feat: support customize title --- charts.go | 5 ++- echarts_test.go | 12 ++++++ examples/charts/main.go | 2 +- legend.go | 6 +-- legend_test.go | 35 +++++++++++++++++ range_test.go | 23 +++++++++++ title.go | 6 ++- title_test.go | 85 +++++++++++++++++++++++++++++++++++++++++ 8 files changed, 167 insertions(+), 7 deletions(-) create mode 100644 title_test.go diff --git a/charts.go b/charts.go index 9216606..595f569 100644 --- a/charts.go +++ b/charts.go @@ -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, diff --git a/echarts_test.go b/echarts_test.go index 0702591..6dcc4d0 100644 --- a/echarts_test.go +++ b/echarts_test.go @@ -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(`{ diff --git a/examples/charts/main.go b/examples/charts/main.go index fde8f5f..1828a52 100644 --- a/examples/charts/main.go +++ b/examples/charts/main.go @@ -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() diff --git a/legend.go b/legend.go index bc9cd39..c85564f 100644 --- a/legend.go +++ b/legend.go @@ -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) } diff --git a/legend_test.go b/legend_test.go index 66d3e47..8f21210 100644 --- a/legend_test.go +++ b/legend_test.go @@ -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", + })) + +} diff --git a/range_test.go b/range_test.go index 3e80568..33937bf 100644 --- a/range_test.go +++ b/range_test.go @@ -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()) +} diff --git a/title.go b/title.go index 654711c..3d75b5d 100644 --- a/title.go +++ b/title.go @@ -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) diff --git a/title_test.go b/title_test.go new file mode 100644 index 0000000..0fe8256 --- /dev/null +++ b/title_test.go @@ -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: "\\nHello World!", + }, + // 多行标题,靠右 + { + title: Title{ + Text: "Hello World!\nHello World", + Style: chart.Style{ + FontColor: drawing.ColorBlack, + }, + Left: "right", + }, + svg: "\\nHello World!Hello World", + }, + // 标题居中 + { + title: Title{ + Text: "Hello World!", + Style: chart.Style{ + FontColor: drawing.ColorBlack, + }, + Left: "center", + }, + svg: "\\nHello World!", + }, + } + 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()) + } +}