Compare commits

..

No commits in common. "main" and "v1" have entirely different histories.
main ... v1

79 changed files with 5883 additions and 10494 deletions

View file

@ -14,12 +14,12 @@ jobs:
strategy: strategy:
matrix: matrix:
go: go:
- '1.22'
- '1.21'
- '1.20'
- '1.19'
- '1.18' - '1.18'
- '1.17' - '1.17'
- '1.16'
- '1.15'
- '1.14'
- '1.13'
steps: steps:
- name: Go ${{ matrix.go }} test - name: Go ${{ matrix.go }} test

2
.gitignore vendored
View file

@ -16,5 +16,3 @@
*.png *.png
*.svg *.svg
tmp tmp
NotoSansSC.ttf
.vscode

483
README.md
View file

@ -1,13 +1,11 @@
# go-charts # go-charts
Clone from https://github.com/vicanso/go-charts
[![license](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/vicanso/go-charts/blob/master/LICENSE) [![license](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/vicanso/go-charts/blob/master/LICENSE)
[![Build Status](https://github.com/vicanso/go-charts/workflows/Test/badge.svg)](https://github.com/vicanso/go-charts/actions) [![Build Status](https://github.com/vicanso/go-charts/workflows/Test/badge.svg)](https://github.com/vicanso/go-charts/actions)
[中文](./README_zh.md) [中文](./README_zh.md)
`go-charts` base on [go-chart](https://github.com/wcharczuk/go-chart)it is simpler way for generating charts, which supports `svg` and `png` format and themes: `light`, `dark`, `grafana` and `ant`. The default format is `png` and the default theme is `light`. `go-charts` base on [go-chart](https://github.com/wcharczuk/go-chart)it is simpler way for generating charts, which supports `svg` and `png` format and themes: `light`, `dark`, `grafana` and `ant`.
`Apache ECharts` is popular among Front-end developers, so `go-charts` supports the option of `Apache ECharts`. Developers can generate charts almost the same as `Apache ECharts`. `Apache ECharts` is popular among Front-end developers, so `go-charts` supports the option of `Apache ECharts`. Developers can generate charts almost the same as `Apache ECharts`.
@ -17,55 +15,59 @@ Screenshot of common charts, the left part is light theme, the right part is gra
<img src="./assets/go-charts.png" alt="go-charts"> <img src="./assets/go-charts.png" alt="go-charts">
</p> </p>
<p align="center">
<img src="./assets/go-table.png" alt="go-table">
</p>
## Chart Type ## Chart Type
These chart types are supported: `line`, `bar`, `horizontal bar`, `pie`, `radar` or `funnel` and `table`. These chart types are supported: `line`, `bar`, `pie`, `radar` or `funnel`.
## Example ## Example
More examples can be found in the [./examples/](./examples/) directory. The example is for `golang option` and `echarts option`, more examples can be found in the `./examples/` directory.
### Line Chart
```go ```go
package main package main
import ( import (
charts "git.smarteching.com/zeni/go-charts/v2" "os"
"path/filepath"
charts "github.com/vicanso/go-charts"
) )
func main() { func writeFile(file string, buf []byte) error {
values := [][]float64{ tmpPath := "./tmp"
{ err := os.MkdirAll(tmpPath, 0700)
120, if err != nil {
132, return err
101,
134,
90,
230,
210,
},
{
// snip...
},
{
// snip...
},
{
// snip...
},
{
// snip...
},
} }
p, err := charts.LineRender(
values, file = filepath.Join(tmpPath, file)
charts.TitleTextOptionFunc("Line"), err = os.WriteFile(file, buf, 0600)
charts.XAxisDataOptionFunc([]string{ if err != nil {
return err
}
return nil
}
func chartsRender() ([]byte, error) {
d, err := charts.LineRender([][]float64{
{
150,
230,
224,
218,
135,
147,
260,
},
},
// output type
charts.PNGTypeOption(),
// title
charts.TitleOptionFunc(charts.TitleOption{
Text: "Line",
}),
// x axis
charts.XAxisOptionFunc(charts.NewXAxisOption([]string{
"Mon", "Mon",
"Tue", "Tue",
"Wed", "Wed",
@ -73,389 +75,16 @@ func main() {
"Fri", "Fri",
"Sat", "Sat",
"Sun", "Sun",
}), })),
charts.LegendLabelsOptionFunc([]string{
"Email",
"Union Ads",
"Video Ads",
"Direct",
"Search Engine",
}, charts.PositionCenter),
)
if err != nil {
panic(err)
}
buf, err := p.Bytes()
if err != nil {
panic(err)
}
// snip...
}
```
### Bar Chart
```go
package main
import (
"git.smarteching.com/zeni/go-charts/v2"
)
func main() {
values := [][]float64{
{
2.0,
4.9,
7.0,
23.2,
25.6,
76.7,
135.6,
162.2,
32.6,
20.0,
6.4,
3.3,
},
{
// snip...
},
}
p, err := charts.BarRender(
values,
charts.XAxisDataOptionFunc([]string{
"Jan",
"Feb",
"Mar",
"Apr",
"May",
"Jun",
"Jul",
"Aug",
"Sep",
"Oct",
"Nov",
"Dec",
}),
charts.LegendLabelsOptionFunc([]string{
"Rainfall",
"Evaporation",
}, charts.PositionRight),
charts.MarkLineOptionFunc(0, charts.SeriesMarkDataTypeAverage),
charts.MarkPointOptionFunc(0, charts.SeriesMarkDataTypeMax,
charts.SeriesMarkDataTypeMin),
// custom option func
func(opt *charts.ChartOption) {
opt.SeriesList[1].MarkPoint = charts.NewMarkPoint(
charts.SeriesMarkDataTypeMax,
charts.SeriesMarkDataTypeMin,
)
opt.SeriesList[1].MarkLine = charts.NewMarkLine(
charts.SeriesMarkDataTypeAverage,
)
},
) )
if err != nil { if err != nil {
panic(err) return nil, err
} }
return d.Bytes()
buf, err := p.Bytes()
if err != nil {
panic(err)
}
// snip...
} }
```
### Horizontal Bar Chart func echartsRender() ([]byte, error) {
return charts.RenderEChartsToPNG(`{
```go
package main
import (
"git.smarteching.com/zeni/go-charts/v2"
)
func main() {
values := [][]float64{
{
18203,
23489,
29034,
104970,
131744,
630230,
},
{
// snip...
},
}
p, err := charts.HorizontalBarRender(
values,
charts.TitleTextOptionFunc("World Population"),
charts.PaddingOptionFunc(charts.Box{
Top: 20,
Right: 40,
Bottom: 20,
Left: 20,
}),
charts.LegendLabelsOptionFunc([]string{
"2011",
"2012",
}),
charts.YAxisDataOptionFunc([]string{
"Brazil",
"Indonesia",
"USA",
"India",
"China",
"World",
}),
)
if err != nil {
panic(err)
}
buf, err := p.Bytes()
if err != nil {
panic(err)
}
// snip...
}
```
### Pie Chart
```go
package main
import (
"git.smarteching.com/zeni/go-charts/v2"
)
func main() {
values := []float64{
1048,
735,
580,
484,
300,
}
p, err := charts.PieRender(
values,
charts.TitleOptionFunc(charts.TitleOption{
Text: "Rainfall vs Evaporation",
Subtext: "Fake Data",
Left: charts.PositionCenter,
}),
charts.PaddingOptionFunc(charts.Box{
Top: 20,
Right: 20,
Bottom: 20,
Left: 20,
}),
charts.LegendOptionFunc(charts.LegendOption{
Orient: charts.OrientVertical,
Data: []string{
"Search Engine",
"Direct",
"Email",
"Union Ads",
"Video Ads",
},
Left: charts.PositionLeft,
}),
charts.PieSeriesShowLabel(),
)
if err != nil {
panic(err)
}
buf, err := p.Bytes()
if err != nil {
panic(err)
}
// snip...
}
```
### Radar Chart
```go
package main
import (
"git.smarteching.com/zeni/go-charts/v2"
)
func main() {
values := [][]float64{
{
4200,
3000,
20000,
35000,
50000,
18000,
},
{
// snip...
},
}
p, err := charts.RadarRender(
values,
charts.TitleTextOptionFunc("Basic Radar Chart"),
charts.LegendLabelsOptionFunc([]string{
"Allocated Budget",
"Actual Spending",
}),
charts.RadarIndicatorOptionFunc([]string{
"Sales",
"Administration",
"Information Technology",
"Customer Support",
"Development",
"Marketing",
}, []float64{
6500,
16000,
30000,
38000,
52000,
25000,
}),
)
if err != nil {
panic(err)
}
buf, err := p.Bytes()
if err != nil {
panic(err)
}
// snip...
}
```
### Funnel Chart
```go
package main
import (
"git.smarteching.com/zeni/go-charts/v2"
)
func main() {
values := []float64{
100,
80,
60,
40,
20,
}
p, err := charts.FunnelRender(
values,
charts.TitleTextOptionFunc("Funnel"),
charts.LegendLabelsOptionFunc([]string{
"Show",
"Click",
"Visit",
"Inquiry",
"Order",
}),
)
if err != nil {
panic(err)
}
buf, err := p.Bytes()
if err != nil {
panic(err)
}
// snip...
}
```
### Table
```go
package main
import (
"git.smarteching.com/zeni/go-charts/v2"
)
func main() {
header := []string{
"Name",
"Age",
"Address",
"Tag",
"Action",
}
data := [][]string{
{
"John Brown",
"32",
"New York No. 1 Lake Park",
"nice, developer",
"Send Mail",
},
{
"Jim Green ",
"42",
"London No. 1 Lake Park",
"wow",
"Send Mail",
},
{
"Joe Black ",
"32",
"Sidney No. 1 Lake Park",
"cool, teacher",
"Send Mail",
},
}
spans := map[int]int{
0: 2,
1: 1,
// 设置第三列的span
2: 3,
3: 2,
4: 2,
}
p, err := charts.TableRender(
header,
data,
spans,
)
if err != nil {
panic(err)
}
buf, err := p.Bytes()
if err != nil {
panic(err)
}
// snip...
}
```
### ECharts Render
```go
package main
import (
"git.smarteching.com/zeni/go-charts/v2"
)
func main() {
buf, err := charts.RenderEChartsToPNG(`{
"title": { "title": {
"text": "Line" "text": "Line"
}, },
@ -468,7 +97,25 @@ func main() {
} }
] ]
}`) }`)
// snip... }
type Render func() ([]byte, error)
func main() {
m := map[string]Render{
"charts-line.png": chartsRender,
"echarts-line.png": echartsRender,
}
for name, fn := range m {
buf, err := fn()
if err != nil {
panic(err)
}
err = writeFile(name, buf)
if err != nil {
panic(err)
}
}
} }
``` ```

View file

@ -3,7 +3,7 @@
[![license](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/vicanso/go-charts/blob/master/LICENSE) [![license](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/vicanso/go-charts/blob/master/LICENSE)
[![Build Status](https://github.com/vicanso/go-charts/workflows/Test/badge.svg)](https://github.com/vicanso/go-charts/actions) [![Build Status](https://github.com/vicanso/go-charts/workflows/Test/badge.svg)](https://github.com/vicanso/go-charts/actions)
`go-charts`基于[go-chart](https://github.com/wcharczuk/go-chart),更简单方便的形式生成数据图表,支持`svg``png`两种方式的输出,支持主题`light`, `dark`, `grafana`以及`ant`默认的输入格式为`png`,默认主题为`light` `go-charts`基于[go-chart](https://github.com/wcharczuk/go-chart),更简单方便的形式生成数据图表,支持`svg``png`两种方式的输出,支持主题`light`, `dark`, `grafana`以及`ant`
`Apache ECharts`在前端开发中得到众多开发者的认可,因此`go-charts`提供了兼容`Apache ECharts`的配置参数,简单快捷的生成相似的图表(`svg``png`)方便插入至Email或分享使用。下面为常用的图表截图(主题为light与grafana) `Apache ECharts`在前端开发中得到众多开发者的认可,因此`go-charts`提供了兼容`Apache ECharts`的配置参数,简单快捷的生成相似的图表(`svg``png`)方便插入至Email或分享使用。下面为常用的图表截图(主题为light与grafana)
@ -12,57 +12,62 @@
<img src="./assets/go-charts.png" alt="go-charts"> <img src="./assets/go-charts.png" alt="go-charts">
</p> </p>
<p align="center">
<img src="./assets/go-table.png" alt="go-table">
</p
## 支持图表类型 ## 支持图表类型
支持以下的图表类型:`line`, `bar`, `horizontal bar`, `pie`, `radar`, `funnel` 以及 `table` 支持以下的图表类型:`line`, `bar`, `pie`, `radar` 以及 `funnel`
## 示例 ## 示例
下面的示例为`go-charts`两种方式的参数配置golang的参数配置、echarts的JSON配置输出相同的折线图。 下面的示例为`go-charts`两种方式的参数配置golang的参数配置、echarts的JSON配置输出相同的折线图。
更多的示例参考:[./examples/](./examples/)目录 更多的示例参考:`./examples/`目录
### Line Chart
```go ```go
package main package main
import ( import (
charts "git.smarteching.com/zeni/go-charts/v2" "os"
"path/filepath"
charts "github.com/vicanso/go-charts"
) )
func main() { func writeFile(file string, buf []byte) error {
values := [][]float64{ tmpPath := "./tmp"
{ err := os.MkdirAll(tmpPath, 0700)
120, if err != nil {
132, return err
101,
134,
90,
230,
210,
},
{
// snip...
},
{
// snip...
},
{
// snip...
},
{
// snip...
},
} }
p, err := charts.LineRender(
values, file = filepath.Join(tmpPath, file)
charts.TitleTextOptionFunc("Line"), err = os.WriteFile(file, buf, 0600)
charts.XAxisDataOptionFunc([]string{ if err != nil {
return err
}
return nil
}
func chartsRender() ([]byte, error) {
d, err := charts.LineRender([][]float64{
{
150,
230,
224,
218,
135,
147,
260,
},
},
// output type
charts.PNGTypeOption(),
// title
charts.TitleOptionFunc(charts.TitleOption{
Text: "Line",
}),
// x axis
charts.XAxisOptionFunc(charts.NewXAxisOption([]string{
"Mon", "Mon",
"Tue", "Tue",
"Wed", "Wed",
@ -70,388 +75,16 @@ func main() {
"Fri", "Fri",
"Sat", "Sat",
"Sun", "Sun",
}), })),
charts.LegendLabelsOptionFunc([]string{
"Email",
"Union Ads",
"Video Ads",
"Direct",
"Search Engine",
}, charts.PositionCenter),
)
if err != nil {
panic(err)
}
buf, err := p.Bytes()
if err != nil {
panic(err)
}
// snip...
}
```
### Bar Chart
```go
package main
import (
"git.smarteching.com/zeni/go-charts/v2"
)
func main() {
values := [][]float64{
{
2.0,
4.9,
7.0,
23.2,
25.6,
76.7,
135.6,
162.2,
32.6,
20.0,
6.4,
3.3,
},
{
// snip...
},
}
p, err := charts.BarRender(
values,
charts.XAxisDataOptionFunc([]string{
"Jan",
"Feb",
"Mar",
"Apr",
"May",
"Jun",
"Jul",
"Aug",
"Sep",
"Oct",
"Nov",
"Dec",
}),
charts.LegendLabelsOptionFunc([]string{
"Rainfall",
"Evaporation",
}, charts.PositionRight),
charts.MarkLineOptionFunc(0, charts.SeriesMarkDataTypeAverage),
charts.MarkPointOptionFunc(0, charts.SeriesMarkDataTypeMax,
charts.SeriesMarkDataTypeMin),
// custom option func
func(opt *charts.ChartOption) {
opt.SeriesList[1].MarkPoint = charts.NewMarkPoint(
charts.SeriesMarkDataTypeMax,
charts.SeriesMarkDataTypeMin,
)
opt.SeriesList[1].MarkLine = charts.NewMarkLine(
charts.SeriesMarkDataTypeAverage,
)
},
) )
if err != nil { if err != nil {
panic(err) return nil, err
} }
return d.Bytes()
buf, err := p.Bytes()
if err != nil {
panic(err)
}
// snip...
} }
```
### Horizontal Bar Chart func echartsRender() ([]byte, error) {
return charts.RenderEChartsToPNG(`{
```go
package main
import (
"git.smarteching.com/zeni/go-charts/v2"
)
func main() {
values := [][]float64{
{
18203,
23489,
29034,
104970,
131744,
630230,
},
{
// snip...
},
}
p, err := charts.HorizontalBarRender(
values,
charts.TitleTextOptionFunc("World Population"),
charts.PaddingOptionFunc(charts.Box{
Top: 20,
Right: 40,
Bottom: 20,
Left: 20,
}),
charts.LegendLabelsOptionFunc([]string{
"2011",
"2012",
}),
charts.YAxisDataOptionFunc([]string{
"Brazil",
"Indonesia",
"USA",
"India",
"China",
"World",
}),
)
if err != nil {
panic(err)
}
buf, err := p.Bytes()
if err != nil {
panic(err)
}
// snip...
}
```
### Pie Chart
```go
package main
import (
"git.smarteching.com/zeni/go-charts/v2"
)
func main() {
values := []float64{
1048,
735,
580,
484,
300,
}
p, err := charts.PieRender(
values,
charts.TitleOptionFunc(charts.TitleOption{
Text: "Rainfall vs Evaporation",
Subtext: "Fake Data",
Left: charts.PositionCenter,
}),
charts.PaddingOptionFunc(charts.Box{
Top: 20,
Right: 20,
Bottom: 20,
Left: 20,
}),
charts.LegendOptionFunc(charts.LegendOption{
Orient: charts.OrientVertical,
Data: []string{
"Search Engine",
"Direct",
"Email",
"Union Ads",
"Video Ads",
},
Left: charts.PositionLeft,
}),
charts.PieSeriesShowLabel(),
)
if err != nil {
panic(err)
}
buf, err := p.Bytes()
if err != nil {
panic(err)
}
// snip...
}
```
### Radar Chart
```go
package main
import (
"git.smarteching.com/zeni/go-charts/v2"
)
func main() {
values := [][]float64{
{
4200,
3000,
20000,
35000,
50000,
18000,
},
{
// snip...
},
}
p, err := charts.RadarRender(
values,
charts.TitleTextOptionFunc("Basic Radar Chart"),
charts.LegendLabelsOptionFunc([]string{
"Allocated Budget",
"Actual Spending",
}),
charts.RadarIndicatorOptionFunc([]string{
"Sales",
"Administration",
"Information Technology",
"Customer Support",
"Development",
"Marketing",
}, []float64{
6500,
16000,
30000,
38000,
52000,
25000,
}),
)
if err != nil {
panic(err)
}
buf, err := p.Bytes()
if err != nil {
panic(err)
}
// snip...
}
```
### Funnel Chart
```go
package main
import (
"git.smarteching.com/zeni/go-charts/v2"
)
func main() {
values := []float64{
100,
80,
60,
40,
20,
}
p, err := charts.FunnelRender(
values,
charts.TitleTextOptionFunc("Funnel"),
charts.LegendLabelsOptionFunc([]string{
"Show",
"Click",
"Visit",
"Inquiry",
"Order",
}),
)
if err != nil {
panic(err)
}
buf, err := p.Bytes()
if err != nil {
panic(err)
}
// snip...
}
```
### Table
```go
package main
import (
"git.smarteching.com/zeni/go-charts/v2"
)
func main() {
header := []string{
"Name",
"Age",
"Address",
"Tag",
"Action",
}
data := [][]string{
{
"John Brown",
"32",
"New York No. 1 Lake Park",
"nice, developer",
"Send Mail",
},
{
"Jim Green ",
"42",
"London No. 1 Lake Park",
"wow",
"Send Mail",
},
{
"Joe Black ",
"32",
"Sidney No. 1 Lake Park",
"cool, teacher",
"Send Mail",
},
}
spans := map[int]int{
0: 2,
1: 1,
// 设置第三列的span
2: 3,
3: 2,
4: 2,
}
p, err := charts.TableRender(
header,
data,
spans,
)
if err != nil {
panic(err)
}
buf, err := p.Bytes()
if err != nil {
panic(err)
}
// snip...
}
```
### ECharts Render
```go
package main
import (
"git.smarteching.com/zeni/go-charts/v2"
)
func main() {
buf, err := charts.RenderEChartsToPNG(`{
"title": { "title": {
"text": "Line" "text": "Line"
}, },
@ -464,7 +97,25 @@ func main() {
} }
] ]
}`) }`)
// snip... }
type Render func() ([]byte, error)
func main() {
m := map[string]Render{
"charts-line.png": chartsRender,
"echarts-line.png": echartsRender,
}
for name, fn := range m {
buf, err := fn()
if err != nil {
panic(err)
}
err = writeFile(name, buf)
if err != nil {
panic(err)
}
}
} }
``` ```
@ -569,8 +220,6 @@ BenchmarkMultiChartSVGRender-8 367 3356325 ns/op
默认使用的字符为`roboto`为英文字体库,因此如果需要显示中文字符需要增加中文字体库,`InstallFont`函数可添加对应的字体库,成功添加之后则指定`title.textStyle.fontFamily`即可。 默认使用的字符为`roboto`为英文字体库,因此如果需要显示中文字符需要增加中文字体库,`InstallFont`函数可添加对应的字体库,成功添加之后则指定`title.textStyle.fontFamily`即可。
在浏览器中使用`svg`时,如果指定的`fontFamily`不支持中文字符,展示的中文并不会乱码,但是会导致在计算字符宽度等错误。 在浏览器中使用`svg`时,如果指定的`fontFamily`不支持中文字符,展示的中文并不会乱码,但是会导致在计算字符宽度等错误。
字体文件可以在[中文字库noto-cjk](https://github.com/googlefonts/noto-cjk)下载,注意下载时选择字体格式为 `ttf` 格式,如果选用 `otf` 格式可能会加载失败字体尽量选择Bold类型否则生成的图片会有点模糊。 字体文件可以在[中文字库noto-cjk](https://github.com/googlefonts/noto-cjk)下载,注意下载时选择字体格式为 `ttf` 格式,如果选用 `otf` 格式可能会加载失败。
示例见 [examples/chinese/main.go](examples/chinese/main.go)
示例见 [examples/basic/chinese.go](examples/basic/chinese.go)

Binary file not shown.

Before

Width:  |  Height:  |  Size: 332 KiB

After

Width:  |  Height:  |  Size: 364 KiB

Before After
Before After

Binary file not shown.

Before

Width:  |  Height:  |  Size: 30 KiB

617
axis.go
View file

@ -23,31 +23,14 @@
package charts package charts
import ( import (
"strings" "math"
"github.com/golang/freetype/truetype" "github.com/golang/freetype/truetype"
"git.smarteching.com/zeni/go-chart/v2" "github.com/wcharczuk/go-chart/v2"
"github.com/wcharczuk/go-chart/v2/drawing"
) )
type axisPainter struct {
p *Painter
opt *AxisOption
}
func NewAxisPainter(p *Painter, opt AxisOption) *axisPainter {
return &axisPainter{
p: p,
opt: &opt,
}
}
type AxisOption struct { type AxisOption struct {
// The theme of chart
Theme ColorPalette
// Formatter for y axis text value
Formatter string
// The label of axis
Data []string
// The boundary gap on both sides of a coordinate axis. // The boundary gap on both sides of a coordinate axis.
// Nil or *true means the center part of two axis ticks // Nil or *true means the center part of two axis ticks
BoundaryGap *bool BoundaryGap *bool
@ -57,14 +40,15 @@ type AxisOption struct {
Position string Position string
// Number of segments that the axis is split into. Note that this number serves only as a recommendation. // Number of segments that the axis is split into. Note that this number serves only as a recommendation.
SplitNumber int SplitNumber int
ClassName string
// The line color of axis // The line color of axis
StrokeColor Color StrokeColor drawing.Color
// The line width // The line width
StrokeWidth float64 StrokeWidth float64
// The length of the axis tick // The length of the axis tick
TickLength int TickLength int
// The first axis // The flag for show axis tick, set this to *false will hide axis tick
FirstAxis int TickShow *bool
// The margin value of label // The margin value of label
LabelMargin int LabelMargin int
// The font size of label // The font size of label
@ -72,268 +56,413 @@ type AxisOption struct {
// The font of label // The font of label
Font *truetype.Font Font *truetype.Font
// The color of label // The color of label
FontColor Color FontColor drawing.Color
// The flag for show axis split line, set this to true will show axis split line // The flag for show axis split line, set this to true will show axis split line
SplitLineShow bool SplitLineShow bool
// The color of split line // The color of split line
SplitLineColor Color SplitLineColor drawing.Color
// The text rotation of label
TextRotation float64
// The offset of label
LabelOffset Box
Unit int
} }
func (a *axisPainter) Render() (Box, error) { type axis struct {
opt := a.opt d *Draw
top := a.p data *AxisDataList
theme := opt.Theme option *AxisOption
if theme == nil { }
theme = top.theme type axisMeasurement struct {
} Width int
if isFalse(opt.Show) { Height int
return BoxZero, nil }
// NewAxis creates a new axis with data and style options
func NewAxis(d *Draw, data AxisDataList, option AxisOption) *axis {
return &axis{
d: d,
data: &data,
option: &option,
} }
strokeWidth := opt.StrokeWidth }
if strokeWidth == 0 {
strokeWidth = 1
}
font := opt.Font // GetLabelMargin returns the label margin value
if font == nil { func (as *AxisOption) GetLabelMargin() int {
font = a.p.font return getDefaultInt(as.LabelMargin, 8)
} }
if font == nil {
font = theme.GetFont()
}
fontColor := opt.FontColor
if fontColor.IsZero() {
fontColor = theme.GetTextColor()
}
fontSize := opt.FontSize
if fontSize == 0 {
fontSize = theme.GetFontSize()
}
strokeColor := opt.StrokeColor
if strokeColor.IsZero() {
strokeColor = theme.GetAxisStrokeColor()
}
data := opt.Data // GetTickLength returns the tick length value
formatter := opt.Formatter func (as *AxisOption) GetTickLength() int {
if len(formatter) != 0 { return getDefaultInt(as.TickLength, 5)
for index, text := range data { }
data[index] = strings.ReplaceAll(formatter, "{value}", text)
// Style returns the style of axis
func (as *AxisOption) Style(f *truetype.Font) chart.Style {
s := chart.Style{
ClassName: as.ClassName,
StrokeColor: as.StrokeColor,
StrokeWidth: as.StrokeWidth,
FontSize: as.FontSize,
FontColor: as.FontColor,
Font: as.Font,
}
if s.FontSize == 0 {
s.FontSize = chart.DefaultFontSize
}
if s.Font == nil {
s.Font = f
}
return s
}
type AxisData struct {
// The text value of axis
Text string
}
type AxisDataList []AxisData
// TextList returns the text list of axis data
func (l AxisDataList) TextList() []string {
textList := make([]string, len(l))
for index, item := range l {
textList[index] = item.Text
}
return textList
}
type axisRenderOption struct {
textMaxWith int
textMaxHeight int
boundaryGap bool
unitCount int
modValue int
}
// NewAxisDataListFromStringList creates a new axis data list from string list
func NewAxisDataListFromStringList(textList []string) AxisDataList {
list := make(AxisDataList, len(textList))
for index, text := range textList {
list[index] = AxisData{
Text: text,
} }
} }
dataCount := len(data) return list
tickCount := dataCount }
boundaryGap := true func (a *axis) axisLabel(renderOpt *axisRenderOption) {
if isFalse(opt.BoundaryGap) { option := a.option
boundaryGap = false data := *a.data
d := a.d
if option.FontColor.IsZero() || len(data) == 0 {
return
} }
isVertical := opt.Position == PositionLeft || r := d.Render
opt.Position == PositionRight
labelPosition := "" s := option.Style(d.Font)
s.GetTextOptions().WriteTextOptionsToRenderer(r)
width := d.Box.Width()
height := d.Box.Height()
textList := data.TextList()
count := len(textList)
boundaryGap := renderOpt.boundaryGap
if !boundaryGap { if !boundaryGap {
tickCount-- count--
labelPosition = PositionLeft
}
if isVertical && boundaryGap {
labelPosition = PositionCenter
} }
// 如果小于0则表示不处理 unitCount := renderOpt.unitCount
tickLength := getDefaultInt(opt.TickLength, 5) modValue := renderOpt.modValue
labelMargin := getDefaultInt(opt.LabelMargin, 5) labelMargin := option.GetLabelMargin()
style := Style{ // 轴线
StrokeColor: strokeColor, labelHeight := labelMargin + renderOpt.textMaxHeight
StrokeWidth: strokeWidth, labelWidth := labelMargin + renderOpt.textMaxWith
Font: font,
FontColor: fontColor,
FontSize: fontSize,
}
top.SetDrawingStyle(style).OverrideTextStyle(style)
isTextRotation := opt.TextRotation != 0 // 坐标轴文本
position := option.Position
if isTextRotation { switch position {
top.SetTextRotation(opt.TextRotation) case PositionLeft:
} fallthrough
textMaxWidth, textMaxHeight := top.MeasureTextMaxWidthHeight(data) case PositionRight:
if isTextRotation { values := autoDivide(height, count)
top.ClearTextRotation() textList := data.TextList()
} // 由下往上
reverseIntSlice(values)
// 增加30px来计算文本展示区域 for index, text := range textList {
textFillWidth := float64(textMaxWidth + 20) y := values[index] - 2
// 根据文本宽度计算较为符合的展示项 b := r.MeasureText(text)
fitTextCount := ceilFloatToInt(float64(top.Width()) / textFillWidth) if boundaryGap {
height := y - values[index+1]
unit := opt.Unit y -= (height - b.Height()) >> 1
if unit <= 0 { } else {
y += b.Height() >> 1
unit = ceilFloatToInt(float64(dataCount) / float64(fitTextCount)) }
unit = chart.MaxInt(unit, opt.SplitNumber) // 左右位置的x不一样
// 偶数 x := width - renderOpt.textMaxWith
if unit%2 == 0 && dataCount%(unit+1) == 0 { if position == PositionLeft {
unit++ x = labelWidth - b.Width() - 1
}
d.text(text, x, y)
}
default:
// 定位bottom重新计算y0的定位
y0 := height - labelMargin
if position == PositionTop {
y0 = labelHeight - labelMargin
}
values := autoDivide(width, count)
for index, text := range data.TextList() {
if unitCount != 0 && index%unitCount != modValue {
continue
}
x := values[index]
leftOffset := 0
b := r.MeasureText(text)
if boundaryGap {
width := values[index+1] - x
leftOffset = (width - b.Width()) >> 1
} else {
// 左移文本长度
leftOffset = -b.Width() >> 1
}
d.text(text, x+leftOffset, y0)
} }
} }
}
width := 0 func (a *axis) axisLine(renderOpt *axisRenderOption) {
height := 0 d := a.d
// 垂直 r := d.Render
if isVertical { option := a.option
width = textMaxWidth + tickLength<<1 s := option.Style(d.Font)
height = top.Height() s.GetStrokeOptions().WriteDrawingOptionsToRenderer(r)
} else {
width = top.Width()
height = tickLength<<1 + textMaxHeight
}
padding := Box{}
switch opt.Position {
case PositionTop:
padding.Top = top.Height() - height
case PositionLeft:
padding.Right = top.Width() - width
case PositionRight:
padding.Left = top.Width() - width
default:
padding.Top = top.Height() - defaultXAxisHeight
}
p := top.Child(PainterPaddingOption(padding))
x0 := 0 x0 := 0
y0 := 0 y0 := 0
x1 := 0 x1 := 0
y1 := 0 y1 := 0
ticksPaddingTop := 0 width := d.Box.Width()
ticksPaddingLeft := 0 height := d.Box.Height()
labelPaddingTop := 0 labelMargin := option.GetLabelMargin()
labelPaddingLeft := 0
labelPaddingRight := 0
orient := ""
textAlign := ""
switch opt.Position { // 轴线
case PositionTop: labelHeight := labelMargin + renderOpt.textMaxHeight
labelPaddingTop = 0 labelWidth := labelMargin + renderOpt.textMaxWith
x1 = p.Width() tickLength := option.GetTickLength()
y0 = labelMargin + int(opt.FontSize) switch option.Position {
ticksPaddingTop = int(opt.FontSize)
y1 = y0
orient = OrientHorizontal
case PositionLeft: case PositionLeft:
x0 = p.Width() x0 = tickLength + labelWidth
x1 = x0
y0 = 0 y0 = 0
x1 = p.Width() y1 = height
y1 = p.Height()
orient = OrientVertical
textAlign = AlignRight
ticksPaddingLeft = textMaxWidth + tickLength
labelPaddingRight = width - textMaxWidth
case PositionRight: case PositionRight:
orient = OrientVertical x0 = width - labelWidth
y1 = p.Height() x1 = x0
labelPaddingLeft = width - textMaxWidth y0 = 0
y1 = height
case PositionTop:
x0 = 0
x1 = width
y0 = labelHeight
y1 = y0
// bottom
default: default:
labelPaddingTop = height x0 = 0
x1 = p.Width() x1 = width
orient = OrientHorizontal y0 = height - tickLength - labelHeight
y1 = y0
} }
if strokeWidth > 0 { d.moveTo(x0, y0)
p.Child(PainterPaddingOption(Box{ d.lineTo(x1, y1)
Top: ticksPaddingTop, r.FillStroke()
Left: ticksPaddingLeft, }
})).Ticks(TicksOption{
Count: tickCount,
Length: tickLength,
Unit: unit,
Orient: orient,
First: opt.FirstAxis,
})
p.LineStroke([]Point{
{
X: x0,
Y: y0,
},
{
X: x1,
Y: y1,
},
})
}
p.Child(PainterPaddingOption(Box{ func (a *axis) axisTick(renderOpt *axisRenderOption) {
Left: labelPaddingLeft, d := a.d
Top: labelPaddingTop, r := d.Render
Right: labelPaddingRight,
})).MultiText(MultiTextOption{ option := a.option
First: opt.FirstAxis, s := option.Style(d.Font)
Align: textAlign, s.GetStrokeOptions().WriteDrawingOptionsToRenderer(r)
TextList: data,
Orient: orient, width := d.Box.Width()
Unit: unit, height := d.Box.Height()
Position: labelPosition, data := *a.data
TextRotation: opt.TextRotation, tickCount := len(data)
Offset: opt.LabelOffset, if tickCount == 0 {
}) return
// 显示辅助线 }
if opt.SplitLineShow { if !renderOpt.boundaryGap {
style.StrokeColor = opt.SplitLineColor tickCount--
style.StrokeWidth = 1 }
top.OverrideDrawingStyle(style) labelMargin := option.GetLabelMargin()
if isVertical { tickShow := true
x0 := p.Width() if isFalse(option.TickShow) {
x1 := top.Width() tickShow = false
if opt.Position == PositionRight { }
unitCount := renderOpt.unitCount
tickLengthValue := option.GetTickLength()
labelHeight := labelMargin + renderOpt.textMaxHeight
labelWidth := labelMargin + renderOpt.textMaxWith
position := option.Position
switch position {
case PositionLeft:
fallthrough
case PositionRight:
values := autoDivide(height, tickCount)
// 左右仅是x0的位置不一样
x0 := width - labelWidth
if option.Position == PositionLeft {
x0 = labelWidth
}
if tickShow {
for _, v := range values {
x := x0
y := v
d.moveTo(x, y)
d.lineTo(x+tickLengthValue, y)
r.Stroke()
}
}
// 辅助线
if option.SplitLineShow && !option.SplitLineColor.IsZero() {
r.SetStrokeColor(option.SplitLineColor)
splitLineWidth := width - labelWidth - tickLengthValue
x0 = labelWidth + tickLengthValue
if position == PositionRight {
x0 = 0 x0 = 0
x1 = top.Width() - p.Width() splitLineWidth = width - labelWidth - 1
} }
yValues := autoDivide(height, tickCount) for _, v := range values[0 : len(values)-1] {
yValues = yValues[0 : len(yValues)-1] x := x0
for _, y := range yValues { y := v
top.LineStroke([]Point{ d.moveTo(x, y)
{ d.lineTo(x+splitLineWidth, y)
X: x0, r.Stroke()
Y: y,
},
{
X: x1,
Y: y,
},
})
} }
} else { }
y0 := p.Height() - defaultXAxisHeight default:
y1 := top.Height() - defaultXAxisHeight values := autoDivide(width, tickCount)
for index, x := range autoDivide(width, tickCount) { // 上下仅是y0的位置不一样
if index == 0 { y0 := height - labelHeight
if position == PositionTop {
y0 = labelHeight
}
if tickShow {
for index, v := range values {
if index%unitCount != 0 {
continue continue
} }
top.LineStroke([]Point{ x := v
{ y := y0
X: x, d.moveTo(x, y-tickLengthValue)
Y: y0, d.lineTo(x, y)
}, r.Stroke()
{ }
X: x, }
Y: y1, // 辅助线
}, if option.SplitLineShow && !option.SplitLineColor.IsZero() {
}) r.SetStrokeColor(option.SplitLineColor)
y0 = 0
splitLineHeight := height - labelHeight - tickLengthValue
if position == PositionTop {
y0 = labelHeight
splitLineHeight = height - labelHeight
}
for index, v := range values {
if index%unitCount != 0 {
continue
}
x := v
y := y0
d.moveTo(x, y)
d.lineTo(x, y0+splitLineHeight)
r.Stroke()
} }
} }
} }
}
return Box{
Bottom: height, func (a *axis) measureTextMaxWidthHeight() (int, int) {
Right: width, d := a.d
}, nil r := d.Render
s := a.option.Style(d.Font)
data := a.data
s.GetStrokeOptions().WriteDrawingOptionsToRenderer(r)
s.GetTextOptions().WriteTextOptionsToRenderer(r)
return measureTextMaxWidthHeight(data.TextList(), r)
}
// measure returns the measurement of axis.
// Width will be textMaxWidth + labelMargin + tickLength for position left or right.
// Height will be textMaxHeight + labelMargin + tickLength for position top or bottom.
func (a *axis) measure() axisMeasurement {
option := a.option
value := option.GetLabelMargin() + option.GetTickLength()
textMaxWidth, textMaxHeight := a.measureTextMaxWidthHeight()
info := axisMeasurement{}
if option.Position == PositionLeft ||
option.Position == PositionRight {
info.Width = textMaxWidth + value
} else {
info.Height = textMaxHeight + value
}
return info
}
// Render renders the axis for chart
func (a *axis) Render() {
option := a.option
if isFalse(option.Show) {
return
}
textMaxWidth, textMaxHeight := a.measureTextMaxWidthHeight()
opt := &axisRenderOption{
textMaxWith: textMaxWidth,
textMaxHeight: textMaxHeight,
boundaryGap: true,
}
if isFalse(option.BoundaryGap) {
opt.boundaryGap = false
}
unitCount := chart.MaxInt(option.SplitNumber, 1)
width := a.d.Box.Width()
textList := a.data.TextList()
count := len(textList)
position := option.Position
switch position {
case PositionLeft:
fallthrough
case PositionRight:
default:
maxCount := width / (opt.textMaxWith + 10)
// 可以显示所有
if maxCount >= count {
unitCount = 1
} else if maxCount < count/unitCount {
unitCount = int(math.Ceil(float64(count) / float64(maxCount)))
}
}
boundaryGap := opt.boundaryGap
modValue := 0
if boundaryGap && unitCount > 1 {
// 如果是居中unit count需要设置为奇数
if unitCount%2 == 0 {
unitCount++
}
modValue = unitCount / 2
}
opt.modValue = modValue
opt.unitCount = unitCount
// 坐标轴线
a.axisLine(opt)
a.axisTick(opt)
// 坐标文本
a.axisLabel(opt)
} }

View file

@ -25,149 +25,235 @@ package charts
import ( import (
"testing" "testing"
"github.com/golang/freetype/truetype"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"git.smarteching.com/zeni/go-chart/v2/drawing" "github.com/wcharczuk/go-chart/v2"
"github.com/wcharczuk/go-chart/v2/drawing"
) )
func TestAxisOption(t *testing.T) {
assert := assert.New(t)
as := AxisOption{}
assert.Equal(8, as.GetLabelMargin())
as.LabelMargin = 10
assert.Equal(10, as.GetLabelMargin())
assert.Equal(5, as.GetTickLength())
as.TickLength = 6
assert.Equal(6, as.GetTickLength())
f := &truetype.Font{}
style := as.Style(f)
assert.Equal(float64(10), style.FontSize)
assert.Equal(f, style.Font)
}
func TestAxisDataList(t *testing.T) {
assert := assert.New(t)
textList := []string{
"a",
"b",
}
data := NewAxisDataListFromStringList(textList)
assert.Equal(textList, data.TextList())
}
func TestAxis(t *testing.T) { func TestAxis(t *testing.T) {
assert := assert.New(t) assert := assert.New(t)
axisData := NewAxisDataListFromStringList([]string{
"Mon",
"Tue",
"Wed",
"Thu",
"Fri",
"Sat",
"Sun",
})
getDefaultOption := func() AxisOption {
return AxisOption{
StrokeColor: drawing.ColorBlack,
StrokeWidth: 1,
FontColor: drawing.ColorBlack,
Show: TrueFlag(),
TickShow: TrueFlag(),
SplitLineShow: true,
SplitLineColor: drawing.ColorBlack.WithAlpha(60),
}
}
tests := []struct { tests := []struct {
render func(*Painter) ([]byte, error) newOption func() AxisOption
result string newData func() AxisDataList
result string
}{ }{
// 底部x轴 // 文本按起始位置展示
// axis位于bottom
{ {
render: func(p *Painter) ([]byte, error) { newOption: func() AxisOption {
_, _ = NewAxisPainter(p, AxisOption{ opt := getDefaultOption()
Data: []string{ opt.BoundaryGap = FalseFlag()
"Mon", return opt
"Tue",
"Wed",
"Thu",
"Fri",
"Sat",
"Sun",
},
SplitLineShow: true,
SplitLineColor: drawing.ColorBlack,
}).Render()
return p.Bytes()
}, },
result: "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"600\" height=\"400\">\\n<path d=\"M 0 375\nL 0 370\" style=\"stroke-width:1;stroke:rgba(110,112,121,1.0);fill:none\"/><path d=\"M 85 375\nL 85 370\" style=\"stroke-width:1;stroke:rgba(110,112,121,1.0);fill:none\"/><path d=\"M 171 375\nL 171 370\" style=\"stroke-width:1;stroke:rgba(110,112,121,1.0);fill:none\"/><path d=\"M 257 375\nL 257 370\" style=\"stroke-width:1;stroke:rgba(110,112,121,1.0);fill:none\"/><path d=\"M 342 375\nL 342 370\" style=\"stroke-width:1;stroke:rgba(110,112,121,1.0);fill:none\"/><path d=\"M 428 375\nL 428 370\" style=\"stroke-width:1;stroke:rgba(110,112,121,1.0);fill:none\"/><path d=\"M 514 375\nL 514 370\" style=\"stroke-width:1;stroke:rgba(110,112,121,1.0);fill:none\"/><path d=\"M 600 375\nL 600 370\" style=\"stroke-width:1;stroke:rgba(110,112,121,1.0);fill:none\"/><path d=\"M 0 370\nL 600 370\" style=\"stroke-width:1;stroke:rgba(110,112,121,1.0);fill:none\"/><text x=\"27\" y=\"395\" style=\"stroke-width:0;stroke:none;fill:rgba(70,70,70,1.0);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">Mon</text><text x=\"115\" y=\"395\" style=\"stroke-width:0;stroke:none;fill:rgba(70,70,70,1.0);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">Tue</text><text x=\"199\" y=\"395\" style=\"stroke-width:0;stroke:none;fill:rgba(70,70,70,1.0);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">Wed</text><text x=\"286\" y=\"395\" style=\"stroke-width:0;stroke:none;fill:rgba(70,70,70,1.0);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">Thu</text><text x=\"376\" y=\"395\" style=\"stroke-width:0;stroke:none;fill:rgba(70,70,70,1.0);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">Fri</text><text x=\"460\" y=\"395\" style=\"stroke-width:0;stroke:none;fill:rgba(70,70,70,1.0);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">Sat</text><text x=\"544\" y=\"395\" style=\"stroke-width:0;stroke:none;fill:rgba(70,70,70,1.0);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">Sun</text><path d=\"M 85 0\nL 85 370\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 171 0\nL 171 370\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 257 0\nL 257 370\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 342 0\nL 342 370\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 428 0\nL 428 370\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 514 0\nL 514 370\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 600 0\nL 600 370\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/></svg>", result: "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"400\" height=\"300\">\\n<path d=\"M 5 270\nL 395 270\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 5 270\nL 5 275\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 70 270\nL 70 275\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 135 270\nL 135 275\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 200 270\nL 200 275\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 265 270\nL 265 275\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 330 270\nL 330 275\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 395 270\nL 395 275\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 5 5\nL 5 270\" style=\"stroke-width:1;stroke:rgba(0,0,0,0.2);fill:none\"/><path d=\"M 70 5\nL 70 270\" style=\"stroke-width:1;stroke:rgba(0,0,0,0.2);fill:none\"/><path d=\"M 135 5\nL 135 270\" style=\"stroke-width:1;stroke:rgba(0,0,0,0.2);fill:none\"/><path d=\"M 200 5\nL 200 270\" style=\"stroke-width:1;stroke:rgba(0,0,0,0.2);fill:none\"/><path d=\"M 265 5\nL 265 270\" style=\"stroke-width:1;stroke:rgba(0,0,0,0.2);fill:none\"/><path d=\"M 330 5\nL 330 270\" style=\"stroke-width:1;stroke:rgba(0,0,0,0.2);fill:none\"/><path d=\"M 395 5\nL 395 270\" style=\"stroke-width:1;stroke:rgba(0,0,0,0.2);fill:none\"/><text x=\"-8\" y=\"287\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,0,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">Mon</text><text x=\"59\" y=\"287\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,0,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">Tue</text><text x=\"122\" y=\"287\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,0,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">Wed</text><text x=\"189\" y=\"287\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,0,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">Thu</text><text x=\"257\" y=\"287\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,0,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">Fri</text><text x=\"320\" y=\"287\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,0,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">Sat</text><text x=\"384\" y=\"287\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,0,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">Sun</text></svg>",
}, },
// 底部x轴文本居左 // 文本居中展示
// axis位于bottom
{ {
render: func(p *Painter) ([]byte, error) { newOption: func() AxisOption {
_, _ = NewAxisPainter(p, AxisOption{ opt := getDefaultOption()
Data: []string{ return opt
"Mon",
"Tue",
"Wed",
"Thu",
"Fri",
"Sat",
"Sun",
},
BoundaryGap: FalseFlag(),
}).Render()
return p.Bytes()
}, },
result: "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"600\" height=\"400\">\\n<path d=\"M 0 375\nL 0 370\" style=\"stroke-width:1;stroke:rgba(110,112,121,1.0);fill:none\"/><path d=\"M 100 375\nL 100 370\" style=\"stroke-width:1;stroke:rgba(110,112,121,1.0);fill:none\"/><path d=\"M 200 375\nL 200 370\" style=\"stroke-width:1;stroke:rgba(110,112,121,1.0);fill:none\"/><path d=\"M 300 375\nL 300 370\" style=\"stroke-width:1;stroke:rgba(110,112,121,1.0);fill:none\"/><path d=\"M 400 375\nL 400 370\" style=\"stroke-width:1;stroke:rgba(110,112,121,1.0);fill:none\"/><path d=\"M 500 375\nL 500 370\" style=\"stroke-width:1;stroke:rgba(110,112,121,1.0);fill:none\"/><path d=\"M 600 375\nL 600 370\" style=\"stroke-width:1;stroke:rgba(110,112,121,1.0);fill:none\"/><path d=\"M 0 370\nL 600 370\" style=\"stroke-width:1;stroke:rgba(110,112,121,1.0);fill:none\"/><text x=\"-15\" y=\"395\" style=\"stroke-width:0;stroke:none;fill:rgba(70,70,70,1.0);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">Mon</text><text x=\"87\" y=\"395\" style=\"stroke-width:0;stroke:none;fill:rgba(70,70,70,1.0);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">Tue</text><text x=\"185\" y=\"395\" style=\"stroke-width:0;stroke:none;fill:rgba(70,70,70,1.0);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">Wed</text><text x=\"287\" y=\"395\" style=\"stroke-width:0;stroke:none;fill:rgba(70,70,70,1.0);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">Thu</text><text x=\"391\" y=\"395\" style=\"stroke-width:0;stroke:none;fill:rgba(70,70,70,1.0);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">Fri</text><text x=\"489\" y=\"395\" style=\"stroke-width:0;stroke:none;fill:rgba(70,70,70,1.0);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">Sat</text><text x=\"587\" y=\"395\" style=\"stroke-width:0;stroke:none;fill:rgba(70,70,70,1.0);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">Sun</text></svg>", result: "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"400\" height=\"300\">\\n<path d=\"M 5 270\nL 395 270\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 5 270\nL 5 275\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 61 270\nL 61 275\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 117 270\nL 117 275\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 173 270\nL 173 275\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 229 270\nL 229 275\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 285 270\nL 285 275\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 340 270\nL 340 275\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 395 270\nL 395 275\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 5 5\nL 5 270\" style=\"stroke-width:1;stroke:rgba(0,0,0,0.2);fill:none\"/><path d=\"M 61 5\nL 61 270\" style=\"stroke-width:1;stroke:rgba(0,0,0,0.2);fill:none\"/><path d=\"M 117 5\nL 117 270\" style=\"stroke-width:1;stroke:rgba(0,0,0,0.2);fill:none\"/><path d=\"M 173 5\nL 173 270\" style=\"stroke-width:1;stroke:rgba(0,0,0,0.2);fill:none\"/><path d=\"M 229 5\nL 229 270\" style=\"stroke-width:1;stroke:rgba(0,0,0,0.2);fill:none\"/><path d=\"M 285 5\nL 285 270\" style=\"stroke-width:1;stroke:rgba(0,0,0,0.2);fill:none\"/><path d=\"M 340 5\nL 340 270\" style=\"stroke-width:1;stroke:rgba(0,0,0,0.2);fill:none\"/><path d=\"M 395 5\nL 395 270\" style=\"stroke-width:1;stroke:rgba(0,0,0,0.2);fill:none\"/><text x=\"20\" y=\"287\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,0,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">Mon</text><text x=\"78\" y=\"287\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,0,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">Tue</text><text x=\"132\" y=\"287\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,0,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">Wed</text><text x=\"190\" y=\"287\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,0,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">Thu</text><text x=\"249\" y=\"287\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,0,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">Fri</text><text x=\"303\" y=\"287\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,0,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">Sat</text><text x=\"356\" y=\"287\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,0,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">Sun</text></svg>",
}, },
// 左侧y轴 // 文本按起始位置展示
// axis位于top
{ {
render: func(p *Painter) ([]byte, error) { newOption: func() AxisOption {
_, _ = NewAxisPainter(p, AxisOption{ opt := getDefaultOption()
Data: []string{ opt.Position = PositionTop
"Mon", opt.BoundaryGap = FalseFlag()
"Tue", return opt
"Wed",
"Thu",
"Fri",
"Sat",
"Sun",
},
Position: PositionLeft,
}).Render()
return p.Bytes()
}, },
result: "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"600\" height=\"400\">\\n<path d=\"M 36 0\nL 41 0\" style=\"stroke-width:1;stroke:rgba(110,112,121,1.0);fill:none\"/><path d=\"M 36 57\nL 41 57\" style=\"stroke-width:1;stroke:rgba(110,112,121,1.0);fill:none\"/><path d=\"M 36 114\nL 41 114\" style=\"stroke-width:1;stroke:rgba(110,112,121,1.0);fill:none\"/><path d=\"M 36 171\nL 41 171\" style=\"stroke-width:1;stroke:rgba(110,112,121,1.0);fill:none\"/><path d=\"M 36 228\nL 41 228\" style=\"stroke-width:1;stroke:rgba(110,112,121,1.0);fill:none\"/><path d=\"M 36 285\nL 41 285\" style=\"stroke-width:1;stroke:rgba(110,112,121,1.0);fill:none\"/><path d=\"M 36 342\nL 41 342\" style=\"stroke-width:1;stroke:rgba(110,112,121,1.0);fill:none\"/><path d=\"M 36 400\nL 41 400\" style=\"stroke-width:1;stroke:rgba(110,112,121,1.0);fill:none\"/><path d=\"M 41 0\nL 41 400\" style=\"stroke-width:1;stroke:rgba(110,112,121,1.0);fill:none\"/><text x=\"0\" y=\"35\" style=\"stroke-width:0;stroke:none;fill:rgba(70,70,70,1.0);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">Mon</text><text x=\"4\" y=\"92\" style=\"stroke-width:0;stroke:none;fill:rgba(70,70,70,1.0);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">Tue</text><text x=\"0\" y=\"149\" style=\"stroke-width:0;stroke:none;fill:rgba(70,70,70,1.0);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">Wed</text><text x=\"4\" y=\"206\" style=\"stroke-width:0;stroke:none;fill:rgba(70,70,70,1.0);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">Thu</text><text x=\"13\" y=\"263\" style=\"stroke-width:0;stroke:none;fill:rgba(70,70,70,1.0);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">Fri</text><text x=\"8\" y=\"320\" style=\"stroke-width:0;stroke:none;fill:rgba(70,70,70,1.0);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">Sat</text><text x=\"4\" y=\"378\" style=\"stroke-width:0;stroke:none;fill:rgba(70,70,70,1.0);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">Sun</text></svg>", result: "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"400\" height=\"300\">\\n<path d=\"M 5 25\nL 395 25\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 5 20\nL 5 25\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 70 20\nL 70 25\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 135 20\nL 135 25\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 200 20\nL 200 25\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 265 20\nL 265 25\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 330 20\nL 330 25\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 395 20\nL 395 25\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 5 25\nL 5 295\" style=\"stroke-width:1;stroke:rgba(0,0,0,0.2);fill:none\"/><path d=\"M 70 25\nL 70 295\" style=\"stroke-width:1;stroke:rgba(0,0,0,0.2);fill:none\"/><path d=\"M 135 25\nL 135 295\" style=\"stroke-width:1;stroke:rgba(0,0,0,0.2);fill:none\"/><path d=\"M 200 25\nL 200 295\" style=\"stroke-width:1;stroke:rgba(0,0,0,0.2);fill:none\"/><path d=\"M 265 25\nL 265 295\" style=\"stroke-width:1;stroke:rgba(0,0,0,0.2);fill:none\"/><path d=\"M 330 25\nL 330 295\" style=\"stroke-width:1;stroke:rgba(0,0,0,0.2);fill:none\"/><path d=\"M 395 25\nL 395 295\" style=\"stroke-width:1;stroke:rgba(0,0,0,0.2);fill:none\"/><text x=\"-8\" y=\"17\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,0,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">Mon</text><text x=\"59\" y=\"17\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,0,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">Tue</text><text x=\"122\" y=\"17\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,0,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">Wed</text><text x=\"189\" y=\"17\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,0,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">Thu</text><text x=\"257\" y=\"17\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,0,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">Fri</text><text x=\"320\" y=\"17\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,0,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">Sat</text><text x=\"384\" y=\"17\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,0,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">Sun</text></svg>",
}, },
// 左侧y轴居中 // 文本居中展示
// axis位于top
{ {
render: func(p *Painter) ([]byte, error) { newOption: func() AxisOption {
_, _ = NewAxisPainter(p, AxisOption{ opt := getDefaultOption()
Data: []string{ opt.Position = PositionTop
"Mon", return opt
"Tue",
"Wed",
"Thu",
"Fri",
"Sat",
"Sun",
},
Position: PositionLeft,
BoundaryGap: FalseFlag(),
SplitLineShow: true,
SplitLineColor: drawing.ColorBlack,
}).Render()
return p.Bytes()
}, },
result: "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"600\" height=\"400\">\\n<path d=\"M 36 0\nL 41 0\" style=\"stroke-width:1;stroke:rgba(110,112,121,1.0);fill:none\"/><path d=\"M 36 66\nL 41 66\" style=\"stroke-width:1;stroke:rgba(110,112,121,1.0);fill:none\"/><path d=\"M 36 133\nL 41 133\" style=\"stroke-width:1;stroke:rgba(110,112,121,1.0);fill:none\"/><path d=\"M 36 200\nL 41 200\" style=\"stroke-width:1;stroke:rgba(110,112,121,1.0);fill:none\"/><path d=\"M 36 266\nL 41 266\" style=\"stroke-width:1;stroke:rgba(110,112,121,1.0);fill:none\"/><path d=\"M 36 333\nL 41 333\" style=\"stroke-width:1;stroke:rgba(110,112,121,1.0);fill:none\"/><path d=\"M 36 400\nL 41 400\" style=\"stroke-width:1;stroke:rgba(110,112,121,1.0);fill:none\"/><path d=\"M 41 0\nL 41 400\" style=\"stroke-width:1;stroke:rgba(110,112,121,1.0);fill:none\"/><text x=\"0\" y=\"7\" style=\"stroke-width:0;stroke:none;fill:rgba(70,70,70,1.0);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">Mon</text><text x=\"4\" y=\"73\" style=\"stroke-width:0;stroke:none;fill:rgba(70,70,70,1.0);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">Tue</text><text x=\"0\" y=\"140\" style=\"stroke-width:0;stroke:none;fill:rgba(70,70,70,1.0);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">Wed</text><text x=\"4\" y=\"207\" style=\"stroke-width:0;stroke:none;fill:rgba(70,70,70,1.0);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">Thu</text><text x=\"13\" y=\"273\" style=\"stroke-width:0;stroke:none;fill:rgba(70,70,70,1.0);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">Fri</text><text x=\"8\" y=\"340\" style=\"stroke-width:0;stroke:none;fill:rgba(70,70,70,1.0);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">Sat</text><text x=\"4\" y=\"407\" style=\"stroke-width:0;stroke:none;fill:rgba(70,70,70,1.0);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">Sun</text><path d=\"M 41 0\nL 600 0\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 41 66\nL 600 66\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 41 133\nL 600 133\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 41 200\nL 600 200\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 41 266\nL 600 266\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 41 333\nL 600 333\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/></svg>", result: "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"400\" height=\"300\">\\n<path d=\"M 5 25\nL 395 25\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 5 20\nL 5 25\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 61 20\nL 61 25\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 117 20\nL 117 25\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 173 20\nL 173 25\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 229 20\nL 229 25\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 285 20\nL 285 25\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 340 20\nL 340 25\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 395 20\nL 395 25\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 5 25\nL 5 295\" style=\"stroke-width:1;stroke:rgba(0,0,0,0.2);fill:none\"/><path d=\"M 61 25\nL 61 295\" style=\"stroke-width:1;stroke:rgba(0,0,0,0.2);fill:none\"/><path d=\"M 117 25\nL 117 295\" style=\"stroke-width:1;stroke:rgba(0,0,0,0.2);fill:none\"/><path d=\"M 173 25\nL 173 295\" style=\"stroke-width:1;stroke:rgba(0,0,0,0.2);fill:none\"/><path d=\"M 229 25\nL 229 295\" style=\"stroke-width:1;stroke:rgba(0,0,0,0.2);fill:none\"/><path d=\"M 285 25\nL 285 295\" style=\"stroke-width:1;stroke:rgba(0,0,0,0.2);fill:none\"/><path d=\"M 340 25\nL 340 295\" style=\"stroke-width:1;stroke:rgba(0,0,0,0.2);fill:none\"/><path d=\"M 395 25\nL 395 295\" style=\"stroke-width:1;stroke:rgba(0,0,0,0.2);fill:none\"/><text x=\"20\" y=\"17\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,0,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">Mon</text><text x=\"78\" y=\"17\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,0,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">Tue</text><text x=\"132\" y=\"17\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,0,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">Wed</text><text x=\"190\" y=\"17\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,0,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">Thu</text><text x=\"249\" y=\"17\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,0,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">Fri</text><text x=\"303\" y=\"17\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,0,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">Sat</text><text x=\"356\" y=\"17\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,0,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">Sun</text></svg>",
}, },
// 右侧 // 文本按起始位置展示
// axis位于left
{ {
render: func(p *Painter) ([]byte, error) { newOption: func() AxisOption {
_, _ = NewAxisPainter(p, AxisOption{ opt := getDefaultOption()
Data: []string{ opt.Position = PositionLeft
"Mon", opt.BoundaryGap = FalseFlag()
"Tue", return opt
"Wed",
"Thu",
"Fri",
"Sat",
"Sun",
},
Position: PositionRight,
BoundaryGap: FalseFlag(),
SplitLineShow: true,
SplitLineColor: drawing.ColorBlack,
}).Render()
return p.Bytes()
}, },
result: "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"600\" height=\"400\">\\n<path d=\"M 559 0\nL 564 0\" style=\"stroke-width:1;stroke:rgba(110,112,121,1.0);fill:none\"/><path d=\"M 559 66\nL 564 66\" style=\"stroke-width:1;stroke:rgba(110,112,121,1.0);fill:none\"/><path d=\"M 559 133\nL 564 133\" style=\"stroke-width:1;stroke:rgba(110,112,121,1.0);fill:none\"/><path d=\"M 559 200\nL 564 200\" style=\"stroke-width:1;stroke:rgba(110,112,121,1.0);fill:none\"/><path d=\"M 559 266\nL 564 266\" style=\"stroke-width:1;stroke:rgba(110,112,121,1.0);fill:none\"/><path d=\"M 559 333\nL 564 333\" style=\"stroke-width:1;stroke:rgba(110,112,121,1.0);fill:none\"/><path d=\"M 559 400\nL 564 400\" style=\"stroke-width:1;stroke:rgba(110,112,121,1.0);fill:none\"/><path d=\"M 559 0\nL 559 400\" style=\"stroke-width:1;stroke:rgba(110,112,121,1.0);fill:none\"/><text x=\"569\" y=\"7\" style=\"stroke-width:0;stroke:none;fill:rgba(70,70,70,1.0);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">Mon</text><text x=\"569\" y=\"73\" style=\"stroke-width:0;stroke:none;fill:rgba(70,70,70,1.0);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">Tue</text><text x=\"569\" y=\"140\" style=\"stroke-width:0;stroke:none;fill:rgba(70,70,70,1.0);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">Wed</text><text x=\"569\" y=\"207\" style=\"stroke-width:0;stroke:none;fill:rgba(70,70,70,1.0);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">Thu</text><text x=\"569\" y=\"273\" style=\"stroke-width:0;stroke:none;fill:rgba(70,70,70,1.0);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">Fri</text><text x=\"569\" y=\"340\" style=\"stroke-width:0;stroke:none;fill:rgba(70,70,70,1.0);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">Sat</text><text x=\"569\" y=\"407\" style=\"stroke-width:0;stroke:none;fill:rgba(70,70,70,1.0);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">Sun</text><path d=\"M 0 0\nL 559 0\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 0 66\nL 559 66\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 0 133\nL 559 133\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 0 200\nL 559 200\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 0 266\nL 559 266\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 0 333\nL 559 333\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/></svg>", result: "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"400\" height=\"300\">\\n<path d=\"M 44 5\nL 44 295\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 39 5\nL 44 5\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 39 54\nL 44 54\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 39 103\nL 44 103\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 39 151\nL 44 151\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 39 199\nL 44 199\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 39 247\nL 44 247\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 39 295\nL 44 295\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 44 5\nL 395 5\" style=\"stroke-width:1;stroke:rgba(0,0,0,0.2);fill:none\"/><path d=\"M 44 54\nL 395 54\" style=\"stroke-width:1;stroke:rgba(0,0,0,0.2);fill:none\"/><path d=\"M 44 103\nL 395 103\" style=\"stroke-width:1;stroke:rgba(0,0,0,0.2);fill:none\"/><path d=\"M 44 151\nL 395 151\" style=\"stroke-width:1;stroke:rgba(0,0,0,0.2);fill:none\"/><path d=\"M 44 199\nL 395 199\" style=\"stroke-width:1;stroke:rgba(0,0,0,0.2);fill:none\"/><path d=\"M 44 247\nL 395 247\" style=\"stroke-width:1;stroke:rgba(0,0,0,0.2);fill:none\"/><text x=\"12\" y=\"299\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,0,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">Mon</text><text x=\"16\" y=\"251\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,0,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">Tue</text><text x=\"12\" y=\"203\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,0,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">Wed</text><text x=\"16\" y=\"155\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,0,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">Thu</text><text x=\"23\" y=\"107\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,0,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">Fri</text><text x=\"19\" y=\"58\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,0,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">Sat</text><text x=\"16\" y=\"9\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,0,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">Sun</text></svg>",
}, },
// 顶部 // 文本居中展示
// axis位于left
{ {
render: func(p *Painter) ([]byte, error) { newOption: func() AxisOption {
_, _ = NewAxisPainter(p, AxisOption{ opt := getDefaultOption()
Data: []string{ opt.Position = PositionLeft
"Mon", return opt
"Tue",
"Wed",
"Thu",
"Fri",
"Sat",
"Sun",
},
Formatter: "{value} --",
Position: PositionTop,
}).Render()
return p.Bytes()
}, },
result: "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"600\" height=\"400\">\\n<path d=\"M 0 380\nL 0 375\" style=\"stroke-width:1;stroke:rgba(110,112,121,1.0);fill:none\"/><path d=\"M 85 380\nL 85 375\" style=\"stroke-width:1;stroke:rgba(110,112,121,1.0);fill:none\"/><path d=\"M 171 380\nL 171 375\" style=\"stroke-width:1;stroke:rgba(110,112,121,1.0);fill:none\"/><path d=\"M 257 380\nL 257 375\" style=\"stroke-width:1;stroke:rgba(110,112,121,1.0);fill:none\"/><path d=\"M 342 380\nL 342 375\" style=\"stroke-width:1;stroke:rgba(110,112,121,1.0);fill:none\"/><path d=\"M 428 380\nL 428 375\" style=\"stroke-width:1;stroke:rgba(110,112,121,1.0);fill:none\"/><path d=\"M 514 380\nL 514 375\" style=\"stroke-width:1;stroke:rgba(110,112,121,1.0);fill:none\"/><path d=\"M 600 380\nL 600 375\" style=\"stroke-width:1;stroke:rgba(110,112,121,1.0);fill:none\"/><path d=\"M 0 380\nL 600 380\" style=\"stroke-width:1;stroke:rgba(110,112,121,1.0);fill:none\"/><text x=\"20\" y=\"375\" style=\"stroke-width:0;stroke:none;fill:rgba(70,70,70,1.0);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">Mon --</text><text x=\"108\" y=\"375\" style=\"stroke-width:0;stroke:none;fill:rgba(70,70,70,1.0);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">Tue --</text><text x=\"192\" y=\"375\" style=\"stroke-width:0;stroke:none;fill:rgba(70,70,70,1.0);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">Wed --</text><text x=\"279\" y=\"375\" style=\"stroke-width:0;stroke:none;fill:rgba(70,70,70,1.0);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">Thu --</text><text x=\"369\" y=\"375\" style=\"stroke-width:0;stroke:none;fill:rgba(70,70,70,1.0);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">Fri --</text><text x=\"453\" y=\"375\" style=\"stroke-width:0;stroke:none;fill:rgba(70,70,70,1.0);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">Sat --</text><text x=\"537\" y=\"375\" style=\"stroke-width:0;stroke:none;fill:rgba(70,70,70,1.0);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">Sun --</text></svg>", result: "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"400\" height=\"300\">\\n<path d=\"M 44 5\nL 44 295\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 39 5\nL 44 5\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 39 47\nL 44 47\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 39 89\nL 44 89\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 39 131\nL 44 131\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 39 172\nL 44 172\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 39 213\nL 44 213\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 39 254\nL 44 254\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 39 295\nL 44 295\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 44 5\nL 395 5\" style=\"stroke-width:1;stroke:rgba(0,0,0,0.2);fill:none\"/><path d=\"M 44 47\nL 395 47\" style=\"stroke-width:1;stroke:rgba(0,0,0,0.2);fill:none\"/><path d=\"M 44 89\nL 395 89\" style=\"stroke-width:1;stroke:rgba(0,0,0,0.2);fill:none\"/><path d=\"M 44 131\nL 395 131\" style=\"stroke-width:1;stroke:rgba(0,0,0,0.2);fill:none\"/><path d=\"M 44 172\nL 395 172\" style=\"stroke-width:1;stroke:rgba(0,0,0,0.2);fill:none\"/><path d=\"M 44 213\nL 395 213\" style=\"stroke-width:1;stroke:rgba(0,0,0,0.2);fill:none\"/><path d=\"M 44 254\nL 395 254\" style=\"stroke-width:1;stroke:rgba(0,0,0,0.2);fill:none\"/><text x=\"12\" y=\"280\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,0,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">Mon</text><text x=\"16\" y=\"239\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,0,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">Tue</text><text x=\"12\" y=\"198\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,0,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">Wed</text><text x=\"16\" y=\"157\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,0,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">Thu</text><text x=\"23\" y=\"115\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,0,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">Fri</text><text x=\"19\" y=\"73\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,0,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">Sat</text><text x=\"16\" y=\"31\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,0,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">Sun</text></svg>",
},
// 文本按起始位置展示
// axis位于right
{
newOption: func() AxisOption {
opt := getDefaultOption()
opt.Position = PositionRight
opt.BoundaryGap = FalseFlag()
return opt
},
result: "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"400\" height=\"300\">\\n<path d=\"M 361 5\nL 361 295\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 361 5\nL 366 5\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 361 54\nL 366 54\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 361 103\nL 366 103\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 361 151\nL 366 151\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 361 199\nL 366 199\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 361 247\nL 366 247\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 361 295\nL 366 295\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 5 5\nL 360 5\" style=\"stroke-width:1;stroke:rgba(0,0,0,0.2);fill:none\"/><path d=\"M 5 54\nL 360 54\" style=\"stroke-width:1;stroke:rgba(0,0,0,0.2);fill:none\"/><path d=\"M 5 103\nL 360 103\" style=\"stroke-width:1;stroke:rgba(0,0,0,0.2);fill:none\"/><path d=\"M 5 151\nL 360 151\" style=\"stroke-width:1;stroke:rgba(0,0,0,0.2);fill:none\"/><path d=\"M 5 199\nL 360 199\" style=\"stroke-width:1;stroke:rgba(0,0,0,0.2);fill:none\"/><path d=\"M 5 247\nL 360 247\" style=\"stroke-width:1;stroke:rgba(0,0,0,0.2);fill:none\"/><text x=\"369\" y=\"299\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,0,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">Mon</text><text x=\"369\" y=\"251\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,0,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">Tue</text><text x=\"369\" y=\"203\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,0,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">Wed</text><text x=\"369\" y=\"155\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,0,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">Thu</text><text x=\"369\" y=\"107\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,0,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">Fri</text><text x=\"369\" y=\"58\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,0,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">Sat</text><text x=\"369\" y=\"9\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,0,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">Sun</text></svg>",
},
// 文本居中展示
// axis位于right
{
newOption: func() AxisOption {
opt := getDefaultOption()
opt.Position = PositionRight
return opt
},
result: "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"400\" height=\"300\">\\n<path d=\"M 361 5\nL 361 295\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 361 5\nL 366 5\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 361 47\nL 366 47\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 361 89\nL 366 89\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 361 131\nL 366 131\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 361 172\nL 366 172\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 361 213\nL 366 213\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 361 254\nL 366 254\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 361 295\nL 366 295\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 5 5\nL 360 5\" style=\"stroke-width:1;stroke:rgba(0,0,0,0.2);fill:none\"/><path d=\"M 5 47\nL 360 47\" style=\"stroke-width:1;stroke:rgba(0,0,0,0.2);fill:none\"/><path d=\"M 5 89\nL 360 89\" style=\"stroke-width:1;stroke:rgba(0,0,0,0.2);fill:none\"/><path d=\"M 5 131\nL 360 131\" style=\"stroke-width:1;stroke:rgba(0,0,0,0.2);fill:none\"/><path d=\"M 5 172\nL 360 172\" style=\"stroke-width:1;stroke:rgba(0,0,0,0.2);fill:none\"/><path d=\"M 5 213\nL 360 213\" style=\"stroke-width:1;stroke:rgba(0,0,0,0.2);fill:none\"/><path d=\"M 5 254\nL 360 254\" style=\"stroke-width:1;stroke:rgba(0,0,0,0.2);fill:none\"/><text x=\"369\" y=\"280\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,0,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">Mon</text><text x=\"369\" y=\"239\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,0,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">Tue</text><text x=\"369\" y=\"198\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,0,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">Wed</text><text x=\"369\" y=\"157\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,0,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">Thu</text><text x=\"369\" y=\"115\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,0,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">Fri</text><text x=\"369\" y=\"73\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,0,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">Sat</text><text x=\"369\" y=\"31\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,0,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">Sun</text></svg>",
},
// text较多仅展示部分
{
newOption: func() AxisOption {
opt := getDefaultOption()
opt.Position = PositionBottom
return opt
},
newData: func() AxisDataList {
return NewAxisDataListFromStringList([]string{
"01-01",
"01-02",
"01-03",
"01-04",
"01-05",
"01-06",
"01-07",
"01-08",
"01-09",
"01-10",
"01-11",
"01-12",
"01-13",
"01-14",
"01-15",
"01-16",
"01-17",
"01-18",
"01-19",
"01-20",
"01-21",
})
},
result: "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"400\" height=\"300\">\\n<path d=\"M 5 270\nL 395 270\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 5 270\nL 5 275\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 62 270\nL 62 275\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 119 270\nL 119 275\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 176 270\nL 176 275\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 233 270\nL 233 275\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 287 270\nL 287 275\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 341 270\nL 341 275\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 395 270\nL 395 275\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 5 5\nL 5 270\" style=\"stroke-width:1;stroke:rgba(0,0,0,0.2);fill:none\"/><path d=\"M 62 5\nL 62 270\" style=\"stroke-width:1;stroke:rgba(0,0,0,0.2);fill:none\"/><path d=\"M 119 5\nL 119 270\" style=\"stroke-width:1;stroke:rgba(0,0,0,0.2);fill:none\"/><path d=\"M 176 5\nL 176 270\" style=\"stroke-width:1;stroke:rgba(0,0,0,0.2);fill:none\"/><path d=\"M 233 5\nL 233 270\" style=\"stroke-width:1;stroke:rgba(0,0,0,0.2);fill:none\"/><path d=\"M 287 5\nL 287 270\" style=\"stroke-width:1;stroke:rgba(0,0,0,0.2);fill:none\"/><path d=\"M 341 5\nL 341 270\" style=\"stroke-width:1;stroke:rgba(0,0,0,0.2);fill:none\"/><path d=\"M 395 5\nL 395 270\" style=\"stroke-width:1;stroke:rgba(0,0,0,0.2);fill:none\"/><text x=\"16\" y=\"287\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,0,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">01-02</text><text x=\"73\" y=\"287\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,0,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">01-05</text><text x=\"130\" y=\"287\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,0,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">01-08</text><text x=\"187\" y=\"287\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,0,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">01-11</text><text x=\"243\" y=\"287\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,0,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">01-14</text><text x=\"297\" y=\"287\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,0,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">01-17</text><text x=\"351\" y=\"287\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,0,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">01-20</text></svg>",
}, },
} }
for _, tt := range tests { for _, tt := range tests {
p, err := NewPainter(PainterOptions{ d, err := NewDraw(DrawOption{
Type: ChartOutputSVG, Width: 400,
Width: 600, Height: 300,
Height: 400, }, PaddingOption(chart.Box{
}, PainterThemeOption(defaultTheme)) Left: 5,
Top: 5,
Right: 5,
Bottom: 5,
}))
assert.Nil(err) assert.Nil(err)
data, err := tt.render(p) style := tt.newOption()
data := axisData
if tt.newData != nil {
data = tt.newData()
}
NewAxis(d, data, style).Render()
result, err := d.Bytes()
assert.Nil(err) assert.Nil(err)
assert.Equal(tt.result, string(data)) assert.Equal(tt.result, string(result))
} }
} }
func TestMeasureAxis(t *testing.T) {
assert := assert.New(t)
d, err := NewDraw(DrawOption{
Width: 400,
Height: 300,
})
assert.Nil(err)
data := NewAxisDataListFromStringList([]string{
"Mon",
"Sun",
})
f, _ := chart.GetDefaultFont()
width := NewAxis(d, data, AxisOption{
FontSize: 12,
Font: f,
Position: PositionLeft,
}).measure().Width
assert.Equal(44, width)
height := NewAxis(d, data, AxisOption{
FontSize: 12,
Font: f,
Position: PositionTop,
}).measure().Height
assert.Equal(28, height)
}

View file

@ -23,51 +23,36 @@
package charts package charts
import ( import (
"git.smarteching.com/zeni/go-chart/v2" "github.com/wcharczuk/go-chart/v2"
"git.smarteching.com/zeni/go-chart/v2/drawing" "github.com/wcharczuk/go-chart/v2/drawing"
) )
type Box = chart.Box type BarStyle struct {
type Style = chart.Style ClassName string
type Color = drawing.Color StrokeDashArray []float64
FillColor drawing.Color
var BoxZero = chart.BoxZero
type Point struct {
X int
Y int
} }
const ( func (bs *BarStyle) Style() chart.Style {
ChartTypeLine = "line" return chart.Style{
ChartTypeBar = "bar" ClassName: bs.ClassName,
ChartTypePie = "pie" StrokeDashArray: bs.StrokeDashArray,
ChartTypeRadar = "radar" StrokeColor: bs.FillColor,
ChartTypeFunnel = "funnel" StrokeWidth: 1,
// horizontal bar FillColor: bs.FillColor,
ChartTypeHorizontalBar = "horizontalBar" }
) }
const ( // Bar renders bar for chart
ChartOutputSVG = "svg" func (d *Draw) Bar(b chart.Box, style BarStyle) {
ChartOutputPNG = "png" s := style.Style()
)
const ( r := d.Render
PositionLeft = "left" s.GetFillAndStrokeOptions().WriteToRenderer(r)
PositionRight = "right" d.moveTo(b.Left, b.Top)
PositionCenter = "center" d.lineTo(b.Right, b.Top)
PositionTop = "top" d.lineTo(b.Right, b.Bottom)
PositionBottom = "bottom" d.lineTo(b.Left, b.Bottom)
) d.lineTo(b.Left, b.Top)
d.Render.FillStroke()
const ( }
AlignLeft = "left"
AlignRight = "right"
AlignCenter = "center"
)
const (
OrientHorizontal = "horizontal"
OrientVertical = "vertical"
)

View file

@ -23,60 +23,31 @@
package charts package charts
import ( import (
"math"
"github.com/golang/freetype/truetype" "github.com/golang/freetype/truetype"
"git.smarteching.com/zeni/go-chart/v2" "github.com/wcharczuk/go-chart/v2"
) )
type barChart struct { type barChartOption struct {
p *Painter // The series list fo bar chart
opt *BarChartOption
}
// NewBarChart returns a bar chart renderer
func NewBarChart(p *Painter, opt BarChartOption) *barChart {
if opt.Theme == nil {
opt.Theme = defaultTheme
}
return &barChart{
p: p,
opt: &opt,
}
}
type BarChartOption struct {
// The theme
Theme ColorPalette
// The font size
Font *truetype.Font
// The data series list
SeriesList SeriesList SeriesList SeriesList
// The x axis option // The theme
XAxis XAxisOption Theme string
// The padding of line chart // The font
Padding Box Font *truetype.Font
// The y axis option
YAxisOptions []YAxisOption
// The option of title
Title TitleOption
// The legend option
Legend LegendOption
BarWidth int
// Margin of bar
BarMargin int
} }
func (b *barChart) render(result *defaultRenderResult, seriesList SeriesList) (Box, error) { func barChartRender(opt barChartOption, result *basicRenderResult) ([]markPointRenderOption, error) {
p := b.p d, err := NewDraw(DrawOption{
opt := b.opt Parent: result.d,
seriesPainter := result.seriesPainter }, PaddingOption(chart.Box{
Top: result.titleBox.Height(),
xRange := NewRange(AxisRangeOption{ // TODO 后续考虑是否需要根据左侧是否展示y轴再生成对应的left
Painter: b.p, Left: YAxisWidth,
DivideCount: len(opt.XAxis.Data), }))
Size: seriesPainter.Width(), if err != nil {
}) return nil, err
}
xRange := result.xRange
x0, x1 := xRange.GetRange(0) x0, x1 := xRange.GetRange(0)
width := int(x1 - x0) width := int(x1 - x0)
// 每一块之间的margin // 每一块之间的margin
@ -90,54 +61,50 @@ func (b *barChart) render(result *defaultRenderResult, seriesList SeriesList) (B
margin = 5 margin = 5
barMargin = 3 barMargin = 3
} }
if opt.BarMargin > 0 {
barMargin = opt.BarMargin seriesCount := len(opt.SeriesList)
}
seriesCount := len(seriesList)
// 总的宽度-两个margin-(总数-1)的barMargin // 总的宽度-两个margin-(总数-1)的barMargin
barWidth := (width - 2*margin - barMargin*(seriesCount-1)) / seriesCount barWidth := (width - 2*margin - barMargin*(seriesCount-1)) / len(opt.SeriesList)
if opt.BarWidth > 0 && opt.BarWidth < barWidth {
barWidth = opt.BarWidth
// 重新计算margin
margin = (width - seriesCount*barWidth - barMargin*(seriesCount-1)) / 2
}
barMaxHeight := seriesPainter.Height()
theme := opt.Theme
seriesNames := seriesList.Names()
markPointPainter := NewMarkPointPainter(seriesPainter) barMaxHeight := result.getYRange(0).Size
markLinePainter := NewMarkLinePainter(seriesPainter) theme := NewTheme(opt.Theme)
rendererList := []Renderer{
markPointPainter,
markLinePainter,
}
for index := range seriesList {
series := seriesList[index]
yRange := result.axisRanges[series.AxisIndex]
seriesColor := theme.GetSeriesColor(series.index)
divideValues := xRange.AutoDivide() seriesNames := opt.SeriesList.Names()
r := d.Render
markPointRenderOptions := make([]markPointRenderOption, 0)
for i, s := range opt.SeriesList {
// 由于series是for range为同一个数据因此需要clone
// 后续需要使用如mark point
series := s
yRange := result.getYRange(series.YAxisIndex)
points := make([]Point, len(series.Data)) points := make([]Point, len(series.Data))
var labelPainter *SeriesLabelPainter index := series.index
if series.Label.Show { if index == 0 {
labelPainter = NewSeriesLabelPainter(SeriesLabelPainterParams{ index = i
P: seriesPainter,
SeriesNames: seriesNames,
Label: series.Label,
Theme: opt.Theme,
Font: opt.Font,
})
rendererList = append(rendererList, labelPainter)
} }
seriesColor := theme.GetSeriesColor(index)
// mark line
markLineRender(markLineRenderOption{
Draw: d,
FillColor: seriesColor,
FontColor: theme.GetTextColor(),
StrokeColor: seriesColor,
Font: opt.Font,
Series: &series,
Range: yRange,
})
divideValues := xRange.AutoDivide()
for j, item := range series.Data { for j, item := range series.Data {
if j >= xRange.divideCount { if j >= xRange.divideCount {
continue continue
} }
x := divideValues[j] x := divideValues[j]
x += margin x += margin
if index != 0 { if i != 0 {
x += index * (barWidth + barMargin) x += i * (barWidth + barMargin)
} }
h := int(yRange.getHeight(item.Value)) h := int(yRange.getHeight(item.Value))
@ -146,32 +113,14 @@ func (b *barChart) render(result *defaultRenderResult, seriesList SeriesList) (B
fillColor = item.Style.FillColor fillColor = item.Style.FillColor
} }
top := barMaxHeight - h top := barMaxHeight - h
d.Bar(chart.Box{
if series.RoundRadius <= 0 { Top: top,
seriesPainter.OverrideDrawingStyle(Style{ Left: x,
FillColor: fillColor, Right: x + barWidth,
}).Rect(chart.Box{ Bottom: barMaxHeight - 1,
Top: top, }, BarStyle{
Left: x, FillColor: fillColor,
Right: x + barWidth, })
Bottom: barMaxHeight - 1,
})
} else {
seriesPainter.OverrideDrawingStyle(Style{
FillColor: fillColor,
}).RoundedRect(chart.Box{
Top: top,
Left: x,
Right: x + barWidth,
Bottom: barMaxHeight - 1,
}, series.RoundRadius)
}
// 用于生成marker point
points[j] = Point{
// 居中的位置
X: x + barWidth>>1,
Y: top,
}
// 用于生成marker point // 用于生成marker point
points[j] = Point{ points[j] = Point{
// 居中的位置 // 居中的位置
@ -179,75 +128,36 @@ func (b *barChart) render(result *defaultRenderResult, seriesList SeriesList) (B
Y: top, Y: top,
} }
// 如果label不需要展示则返回 // 如果label不需要展示则返回
if labelPainter == nil { if !series.Label.Show {
continue continue
} }
y := barMaxHeight - h distance := series.Label.Distance
radians := float64(0) if distance == 0 {
fontColor := series.Label.Color distance = 5
if series.Label.Position == PositionBottom {
y = barMaxHeight
radians = -math.Pi / 2
if fontColor.IsZero() {
if isLightColor(fillColor) {
fontColor = defaultLightFontColor
} else {
fontColor = defaultDarkFontColor
}
}
} }
labelPainter.Add(LabelValue{ text := NewValueLabelFormater(seriesNames, series.Label.Formatter)(i, item.Value, -1)
Index: index, labelStyle := chart.Style{
Value: item.Value, FontColor: theme.GetTextColor(),
X: x + barWidth>>1, FontSize: labelFontSize,
Y: y, Font: opt.Font,
// 旋转 }
Radians: radians, if !series.Label.Color.IsZero() {
FontColor: fontColor, labelStyle.FontColor = series.Label.Color
Offset: series.Label.Offset, }
FontSize: series.Label.FontSize, labelStyle.GetTextOptions().WriteToRenderer(r)
}) textBox := r.MeasureText(text)
d.text(text, x+(barWidth-textBox.Width())>>1, barMaxHeight-h-distance)
} }
markPointPainter.Add(markPointRenderOption{ // 生成mark point的参数
markPointRenderOptions = append(markPointRenderOptions, markPointRenderOption{
Draw: d,
FillColor: seriesColor, FillColor: seriesColor,
Font: opt.Font, Font: opt.Font,
Series: series,
Points: points, Points: points,
}) Series: &series,
markLinePainter.Add(markLineRenderOption{
FillColor: seriesColor,
FontColor: opt.Theme.GetTextColor(),
StrokeColor: seriesColor,
Font: opt.Font,
Series: series,
Range: yRange,
}) })
} }
// 最大、最小的mark point
err := doRender(rendererList...)
if err != nil {
return BoxZero, err
}
return p.box, nil return markPointRenderOptions, nil
}
func (b *barChart) Render() (Box, error) {
p := b.p
opt := b.opt
renderResult, err := defaultRender(p, defaultRenderOption{
Theme: opt.Theme,
Padding: opt.Padding,
SeriesList: opt.SeriesList,
XAxis: opt.XAxis,
YAxisOptions: opt.YAxisOptions,
TitleOption: opt.Title,
LegendOption: opt.Legend,
})
if err != nil {
return BoxZero, err
}
seriesList := opt.SeriesList.Filter(ChartTypeLine)
return b.render(renderResult, seriesList)
} }

File diff suppressed because one or more lines are too long

78
bar_test.go Normal file
View file

@ -0,0 +1,78 @@
// 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 TestBarStyle(t *testing.T) {
assert := assert.New(t)
bs := BarStyle{
ClassName: "test",
StrokeDashArray: []float64{
1.0,
},
FillColor: drawing.ColorBlack,
}
assert.Equal(chart.Style{
ClassName: "test",
StrokeDashArray: []float64{
1.0,
},
StrokeWidth: 1,
FillColor: drawing.ColorBlack,
StrokeColor: drawing.ColorBlack,
}, bs.Style())
}
func TestDrawBar(t *testing.T) {
assert := assert.New(t)
d, err := NewDraw(DrawOption{
Width: 400,
Height: 300,
}, PaddingOption(chart.Box{
Left: 10,
Top: 20,
Right: 30,
Bottom: 40,
}))
assert.Nil(err)
d.Bar(chart.Box{
Left: 0,
Top: 0,
Right: 20,
Bottom: 200,
}, BarStyle{
FillColor: drawing.ColorBlack,
})
data, err := d.Bytes()
assert.Nil(err)
assert.Equal("<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"400\" height=\"300\">\\n<path d=\"M 10 20\nL 30 20\nL 30 220\nL 10 220\nL 10 20\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:rgba(0,0,0,1.0)\"/></svg>", string(data))
}

502
chart.go Normal file
View file

@ -0,0 +1,502 @@
// 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 (
"errors"
"math"
"sort"
"strings"
"github.com/golang/freetype/truetype"
"github.com/wcharczuk/go-chart/v2"
"github.com/wcharczuk/go-chart/v2/drawing"
)
const (
ChartTypeLine = "line"
ChartTypeBar = "bar"
ChartTypePie = "pie"
ChartTypeRadar = "radar"
ChartTypeFunnel = "funnel"
)
const (
ChartOutputSVG = "svg"
ChartOutputPNG = "png"
)
type Point struct {
X int
Y int
}
const labelFontSize = 10
const defaultDotWidth = 2.0
const defaultStrokeWidth = 2.0
var defaultChartWidth = 600
var defaultChartHeight = 400
type ChartOption struct {
// The output type of chart, "svg" or "png", default value is "svg"
Type string
// The font family, which should be installed first
FontFamily string
// The font of chart, the default font is "roboto"
Font *truetype.Font
// The theme of chart, "light" and "dark".
// The default theme is "light"
Theme string
// The title option
Title TitleOption
// The legend option
Legend LegendOption
// The x axis option
XAxis XAxisOption
// The y axis option list
YAxisList []YAxisOption
// The width of chart, default width is 600
Width int
// The height of chart, default height is 400
Height int
Parent *Draw
// The padding for chart, default padding is [20, 10, 10, 10]
Padding chart.Box
// The canvas box for chart
Box chart.Box
// The series list
SeriesList SeriesList
// The radar indicator list
RadarIndicators []RadarIndicator
// The background color of chart
BackgroundColor drawing.Color
// The child charts
Children []ChartOption
}
// FillDefault fills the default value for chart option
func (o *ChartOption) FillDefault(theme string) {
t := NewTheme(theme)
// 如果为空,初始化
yAxisCount := 1
for _, series := range o.SeriesList {
if series.YAxisIndex >= yAxisCount {
yAxisCount++
}
}
yAxisList := make([]YAxisOption, yAxisCount)
copy(yAxisList, o.YAxisList)
o.YAxisList = yAxisList
if o.Font == nil {
o.Font, _ = chart.GetDefaultFont()
}
if o.BackgroundColor.IsZero() {
o.BackgroundColor = t.GetBackgroundColor()
}
if o.Padding.IsZero() {
o.Padding = chart.Box{
Top: 10,
Right: 10,
Bottom: 10,
Left: 10,
}
}
// 标题的默认值
if o.Title.Style.FontColor.IsZero() {
o.Title.Style.FontColor = t.GetTextColor()
}
if o.Title.Style.FontSize == 0 {
o.Title.Style.FontSize = 14
}
if o.Title.Style.Font == nil {
o.Title.Style.Font = o.Font
}
if o.Title.Style.Padding.IsZero() {
o.Title.Style.Padding = chart.Box{
Bottom: 10,
}
}
// 副标题
if o.Title.SubtextStyle.FontColor.IsZero() {
o.Title.SubtextStyle.FontColor = o.Title.Style.FontColor.WithAlpha(180)
}
if o.Title.SubtextStyle.FontSize == 0 {
o.Title.SubtextStyle.FontSize = labelFontSize
}
if o.Title.SubtextStyle.Font == nil {
o.Title.SubtextStyle.Font = o.Font
}
o.Legend.theme = theme
if o.Legend.Style.FontSize == 0 {
o.Legend.Style.FontSize = labelFontSize
}
if o.Legend.Left == "" {
o.Legend.Left = PositionCenter
}
// legend与series name的关联
if len(o.Legend.Data) == 0 {
o.Legend.Data = o.SeriesList.Names()
} else {
seriesCount := len(o.SeriesList)
for index, name := range o.Legend.Data {
if index < seriesCount &&
len(o.SeriesList[index].Name) == 0 {
o.SeriesList[index].Name = name
}
}
nameIndexDict := map[string]int{}
for index, name := range o.Legend.Data {
nameIndexDict[name] = index
}
// 保证series的顺序与legend一致
sort.Slice(o.SeriesList, func(i, j int) bool {
return nameIndexDict[o.SeriesList[i].Name] < nameIndexDict[o.SeriesList[j].Name]
})
}
// 如果无legend数据则隐藏
if len(strings.Join(o.Legend.Data, "")) == 0 {
o.Legend.Show = FalseFlag()
}
if o.Legend.Style.Font == nil {
o.Legend.Style.Font = o.Font
}
if o.Legend.Style.FontColor.IsZero() {
o.Legend.Style.FontColor = t.GetTextColor()
}
if o.XAxis.Theme == "" {
o.XAxis.Theme = theme
}
o.XAxis.Font = o.Font
}
func (o *ChartOption) getWidth() int {
if o.Width != 0 {
return o.Width
}
if o.Parent != nil {
return o.Parent.Box.Width()
}
return defaultChartWidth
}
func SetDefaultWidth(width int) {
if width > 0 {
defaultChartWidth = width
}
}
func SetDefaultHeight(height int) {
if height > 0 {
defaultChartHeight = height
}
}
func (o *ChartOption) getHeight() int {
if o.Height != 0 {
return o.Height
}
if o.Parent != nil {
return o.Parent.Box.Height()
}
return defaultChartHeight
}
func (o *ChartOption) newYRange(axisIndex int) Range {
min := math.MaxFloat64
max := -math.MaxFloat64
if axisIndex >= len(o.YAxisList) {
axisIndex = 0
}
yAxis := o.YAxisList[axisIndex]
for _, series := range o.SeriesList {
if series.YAxisIndex != axisIndex {
continue
}
for _, item := range series.Data {
if item.Value > max {
max = item.Value
}
if item.Value < min {
min = item.Value
}
}
}
min = min * 0.9
max = max * 1.1
if yAxis.Min != nil {
min = *yAxis.Min
}
if yAxis.Max != nil {
max = *yAxis.Max
}
divideCount := 6
// y轴分设置默认划分为6块
r := NewRange(min, max, divideCount)
// 由于NewRange会重新计算min max
if yAxis.Min != nil {
r.Min = min
}
if yAxis.Max != nil {
r.Max = max
}
return r
}
type basicRenderResult struct {
xRange *Range
yRangeList []*Range
d *Draw
titleBox chart.Box
}
func (r *basicRenderResult) getYRange(index int) *Range {
if index >= len(r.yRangeList) {
index = 0
}
return r.yRangeList[index]
}
// Render renders the chart by option
func Render(opt ChartOption, optFuncs ...OptionFunc) (*Draw, error) {
for _, optFunc := range optFuncs {
optFunc(&opt)
}
if len(opt.SeriesList) == 0 {
return nil, errors.New("series can not be nil")
}
if len(opt.FontFamily) != 0 {
f, err := GetFont(opt.FontFamily)
if err != nil {
return nil, err
}
opt.Font = f
}
opt.FillDefault(opt.Theme)
lineSeries := make([]Series, 0)
barSeries := make([]Series, 0)
isPieChart := false
isRadarChart := false
isFunnelChart := false
for index := range opt.SeriesList {
opt.SeriesList[index].index = index
item := opt.SeriesList[index]
switch item.Type {
case ChartTypePie:
isPieChart = true
case ChartTypeRadar:
isRadarChart = true
case ChartTypeFunnel:
isFunnelChart = true
case ChartTypeBar:
barSeries = append(barSeries, item)
default:
lineSeries = append(lineSeries, item)
}
}
// 如果指定了pie则以pie的形式处理pie不支持多类型图表
// pie不需要axis
// radar 同样处理
if isPieChart ||
isRadarChart ||
isFunnelChart {
opt.XAxis.Hidden = true
for index := range opt.YAxisList {
opt.YAxisList[index].Hidden = true
}
}
result, err := chartBasicRender(&opt)
if err != nil {
return nil, err
}
markPointRenderOptions := make([]markPointRenderOption, 0)
fns := []func() error{
// pie render
func() error {
if !isPieChart {
return nil
}
return pieChartRender(pieChartOption{
SeriesList: opt.SeriesList,
Theme: opt.Theme,
Font: opt.Font,
}, result)
},
// radar render
func() error {
if !isRadarChart {
return nil
}
return radarChartRender(radarChartOption{
SeriesList: opt.SeriesList,
Theme: opt.Theme,
Font: opt.Font,
Indicators: opt.RadarIndicators,
}, result)
},
// funnel render
func() error {
if !isFunnelChart {
return nil
}
return funnelChartRender(funnelChartOption{
SeriesList: opt.SeriesList,
Theme: opt.Theme,
Font: opt.Font,
}, result)
},
// bar render
func() error {
// 如果无bar类型的series
if len(barSeries) == 0 {
return nil
}
options, err := barChartRender(barChartOption{
SeriesList: barSeries,
Theme: opt.Theme,
Font: opt.Font,
}, result)
if err != nil {
return err
}
markPointRenderOptions = append(markPointRenderOptions, options...)
return nil
},
// line render
func() error {
// 如果无line类型的series
if len(lineSeries) == 0 {
return nil
}
options, err := lineChartRender(lineChartOption{
Theme: opt.Theme,
SeriesList: lineSeries,
Font: opt.Font,
}, result)
if err != nil {
return err
}
markPointRenderOptions = append(markPointRenderOptions, options...)
return nil
},
// legend需要在顶层因此此处render
func() error {
_, err := NewLegend(result.d, opt.Legend).Render()
return err
},
// mark point最后render
func() error {
// mark point render不会出错
for _, opt := range markPointRenderOptions {
markPointRender(opt)
}
return nil
},
}
for _, fn := range fns {
err = fn()
if err != nil {
return nil, err
}
}
for _, child := range opt.Children {
child.Parent = result.d
if len(child.Theme) == 0 {
child.Theme = opt.Theme
}
_, err = Render(child)
if err != nil {
return nil, err
}
}
return result.d, nil
}
func chartBasicRender(opt *ChartOption) (*basicRenderResult, error) {
d, err := NewDraw(
DrawOption{
Type: opt.Type,
Parent: opt.Parent,
Width: opt.getWidth(),
Height: opt.getHeight(),
},
BoxOption(opt.Box),
PaddingOption(opt.Padding),
)
if err != nil {
return nil, err
}
if len(opt.YAxisList) > 2 {
return nil, errors.New("y axis should not be gt 2")
}
if opt.Parent == nil {
d.setBackground(opt.getWidth(), opt.getHeight(), opt.BackgroundColor)
}
// 标题
titleBox, err := drawTitle(d, &opt.Title)
if err != nil {
return nil, err
}
xAxisHeight := 0
var xRange *Range
if !opt.XAxis.Hidden {
// xAxis
xAxisHeight, xRange, err = drawXAxis(d, &opt.XAxis, len(opt.YAxisList))
if err != nil {
return nil, err
}
}
yRangeList := make([]*Range, len(opt.YAxisList))
for index, yAxis := range opt.YAxisList {
var yRange *Range
if !yAxis.Hidden {
yRange, err = drawYAxis(d, opt, index, xAxisHeight, chart.Box{
Top: titleBox.Height(),
})
if err != nil {
return nil, err
}
yRangeList[index] = yRange
}
}
return &basicRenderResult{
xRange: xRange,
yRangeList: yRangeList,
d: d,
titleBox: titleBox,
}, nil
}

View file

@ -23,72 +23,13 @@
package charts package charts
import ( import (
"sort" "github.com/wcharczuk/go-chart/v2"
"github.com/wcharczuk/go-chart/v2/drawing"
"github.com/golang/freetype/truetype"
) )
type ChartOption struct {
theme ColorPalette
font *truetype.Font
// The output type of chart, "svg" or "png", default value is "svg"
Type string
// The font family, which should be installed first
FontFamily string
// The theme of chart, "light" and "dark".
// The default theme is "light"
Theme string
// The title option
Title TitleOption
// The legend option
Legend LegendOption
// The x axis option
XAxis XAxisOption
// The y axis option list
YAxisOptions []YAxisOption
// The width of chart, default width is 600
Width int
// The height of chart, default height is 400
Height int
Parent *Painter
// The padding for chart, default padding is [20, 10, 10, 10]
Padding Box
// The canvas box for chart
Box Box
// The series list
SeriesList SeriesList
// The radar indicator list
RadarIndicators []RadarIndicator
// The background color of chart
BackgroundColor Color
// The flag for show symbol of line, set this to *false will hide symbol
SymbolShow *bool
// The stroke width of line chart
LineStrokeWidth float64
// The bar with of bar chart
BarWidth int
// The margin of each bar
BarMargin int
// The bar height of horizontal bar chart
BarHeight int
// Fill the area of line chart
FillArea bool
// background fill (alpha) opacity
Opacity uint8
// The child charts
Children []ChartOption
// The value formatter
ValueFormatter ValueFormatter
}
// OptionFunc option function // OptionFunc option function
type OptionFunc func(opt *ChartOption) type OptionFunc func(opt *ChartOption)
// SVGTypeOption set svg type of chart's output
func SVGTypeOption() OptionFunc {
return TypeOptionFunc(ChartOutputSVG)
}
// PNGTypeOption set png type of chart's output // PNGTypeOption set png type of chart's output
func PNGTypeOption() OptionFunc { func PNGTypeOption() OptionFunc {
return TypeOptionFunc(ChartOutputPNG) return TypeOptionFunc(ChartOutputPNG)
@ -122,16 +63,6 @@ func TitleOptionFunc(title TitleOption) OptionFunc {
} }
} }
// TitleTextOptionFunc set title text of chart
func TitleTextOptionFunc(text string, subtext ...string) OptionFunc {
return func(opt *ChartOption) {
opt.Title.Text = text
if len(subtext) != 0 {
opt.Title.Subtext = subtext[0]
}
}
}
// LegendOptionFunc set legend of chart // LegendOptionFunc set legend of chart
func LegendOptionFunc(legend LegendOption) OptionFunc { func LegendOptionFunc(legend LegendOption) OptionFunc {
return func(opt *ChartOption) { return func(opt *ChartOption) {
@ -139,13 +70,6 @@ func LegendOptionFunc(legend LegendOption) OptionFunc {
} }
} }
// LegendLabelsOptionFunc set legend labels of chart
func LegendLabelsOptionFunc(labels []string, left ...string) OptionFunc {
return func(opt *ChartOption) {
opt.Legend = NewLegendOption(labels, left...)
}
}
// XAxisOptionFunc set x axis of chart // XAxisOptionFunc set x axis of chart
func XAxisOptionFunc(xAxisOption XAxisOption) OptionFunc { func XAxisOptionFunc(xAxisOption XAxisOption) OptionFunc {
return func(opt *ChartOption) { return func(opt *ChartOption) {
@ -153,24 +77,10 @@ func XAxisOptionFunc(xAxisOption XAxisOption) OptionFunc {
} }
} }
// XAxisDataOptionFunc set x axis data of chart
func XAxisDataOptionFunc(data []string, boundaryGap ...*bool) OptionFunc {
return func(opt *ChartOption) {
opt.XAxis = NewXAxisOption(data, boundaryGap...)
}
}
// YAxisOptionFunc set y axis of chart, support two y axis // YAxisOptionFunc set y axis of chart, support two y axis
func YAxisOptionFunc(yAxisOption ...YAxisOption) OptionFunc { func YAxisOptionFunc(yAxisOption ...YAxisOption) OptionFunc {
return func(opt *ChartOption) { return func(opt *ChartOption) {
opt.YAxisOptions = yAxisOption opt.YAxisList = yAxisOption
}
}
// YAxisDataOptionFunc set y axis data of chart
func YAxisDataOptionFunc(data []string) OptionFunc {
return func(opt *ChartOption) {
opt.YAxisOptions = NewYAxisOptions(data)
} }
} }
@ -189,28 +99,19 @@ func HeightOptionFunc(height int) OptionFunc {
} }
// PaddingOptionFunc set padding of chart // PaddingOptionFunc set padding of chart
func PaddingOptionFunc(padding Box) OptionFunc { func PaddingOptionFunc(padding chart.Box) OptionFunc {
return func(opt *ChartOption) { return func(opt *ChartOption) {
opt.Padding = padding opt.Padding = padding
} }
} }
// BoxOptionFunc set box of chart // BoxOptionFunc set box of chart
func BoxOptionFunc(box Box) OptionFunc { func BoxOptionFunc(box chart.Box) OptionFunc {
return func(opt *ChartOption) { return func(opt *ChartOption) {
opt.Box = box opt.Box = box
} }
} }
// PieSeriesShowLabel set pie series show label
func PieSeriesShowLabel() OptionFunc {
return func(opt *ChartOption) {
for index := range opt.SeriesList {
opt.SeriesList[index].Label.Show = true
}
}
}
// ChildOptionFunc add child chart // ChildOptionFunc add child chart
func ChildOptionFunc(child ...ChartOption) OptionFunc { func ChildOptionFunc(child ...ChartOption) OptionFunc {
return func(opt *ChartOption) { return func(opt *ChartOption) {
@ -222,205 +123,68 @@ func ChildOptionFunc(child ...ChartOption) OptionFunc {
} }
// RadarIndicatorOptionFunc set radar indicator of chart // RadarIndicatorOptionFunc set radar indicator of chart
func RadarIndicatorOptionFunc(names []string, values []float64) OptionFunc { func RadarIndicatorOptionFunc(radarIndicator ...RadarIndicator) OptionFunc {
return func(opt *ChartOption) { return func(opt *ChartOption) {
opt.RadarIndicators = NewRadarIndicators(names, values) opt.RadarIndicators = radarIndicator
} }
} }
// BackgroundColorOptionFunc set background color of chart // BackgroundColorOptionFunc set background color of chart
func BackgroundColorOptionFunc(color Color) OptionFunc { func BackgroundColorOptionFunc(color drawing.Color) OptionFunc {
return func(opt *ChartOption) { return func(opt *ChartOption) {
opt.BackgroundColor = color opt.BackgroundColor = color
} }
} }
// MarkLineOptionFunc set mark line for series of chart
func MarkLineOptionFunc(seriesIndex int, markLineTypes ...string) OptionFunc {
return func(opt *ChartOption) {
if len(opt.SeriesList) <= seriesIndex {
return
}
opt.SeriesList[seriesIndex].MarkLine = NewMarkLine(markLineTypes...)
}
}
// MarkPointOptionFunc set mark point for series of chart
func MarkPointOptionFunc(seriesIndex int, markPointTypes ...string) OptionFunc {
return func(opt *ChartOption) {
if len(opt.SeriesList) <= seriesIndex {
return
}
opt.SeriesList[seriesIndex].MarkPoint = NewMarkPoint(markPointTypes...)
}
}
func (o *ChartOption) fillDefault() {
t := NewTheme(o.Theme)
o.theme = t
// 如果为空,初始化
axisCount := 1
for _, series := range o.SeriesList {
if series.AxisIndex >= axisCount {
axisCount++
}
}
o.Width = getDefaultInt(o.Width, defaultChartWidth)
o.Height = getDefaultInt(o.Height, defaultChartHeight)
yAxisOptions := make([]YAxisOption, axisCount)
copy(yAxisOptions, o.YAxisOptions)
o.YAxisOptions = yAxisOptions
o.font, _ = GetFont(o.FontFamily)
if o.font == nil {
o.font, _ = GetDefaultFont()
} else {
// 如果指定了字体,则设置主题的字体
t.SetFont(o.font)
}
if o.BackgroundColor.IsZero() {
o.BackgroundColor = t.GetBackgroundColor()
}
if o.Padding.IsZero() {
o.Padding = Box{
Top: 20,
Right: 20,
Bottom: 20,
Left: 20,
}
}
// legend与series name的关联
if len(o.Legend.Data) == 0 {
o.Legend.Data = o.SeriesList.Names()
} else {
seriesCount := len(o.SeriesList)
for index, name := range o.Legend.Data {
if index < seriesCount &&
len(o.SeriesList[index].Name) == 0 {
o.SeriesList[index].Name = name
}
}
nameIndexDict := map[string]int{}
for index, name := range o.Legend.Data {
nameIndexDict[name] = index
}
// 保证series的顺序与legend一致
sort.Slice(o.SeriesList, func(i, j int) bool {
return nameIndexDict[o.SeriesList[i].Name] < nameIndexDict[o.SeriesList[j].Name]
})
}
}
// LineRender line chart render // LineRender line chart render
func LineRender(values [][]float64, opts ...OptionFunc) (*Painter, error) { func LineRender(values [][]float64, opts ...OptionFunc) (*Draw, error) {
seriesList := NewSeriesListDataFromValues(values, ChartTypeLine) seriesList := make(SeriesList, len(values))
for index, value := range values {
seriesList[index] = NewSeriesFromValues(value, ChartTypeLine)
}
return Render(ChartOption{ return Render(ChartOption{
SeriesList: seriesList, SeriesList: seriesList,
}, opts...) }, opts...)
} }
// BarRender bar chart render // BarRender bar chart render
func BarRender(values [][]float64, opts ...OptionFunc) (*Painter, error) { func BarRender(values [][]float64, opts ...OptionFunc) (*Draw, error) {
seriesList := NewSeriesListDataFromValues(values, ChartTypeBar) seriesList := make(SeriesList, len(values))
return Render(ChartOption{ for index, value := range values {
SeriesList: seriesList, seriesList[index] = NewSeriesFromValues(value, ChartTypeBar)
}, opts...) }
}
// HorizontalBarRender horizontal bar chart render
func HorizontalBarRender(values [][]float64, opts ...OptionFunc) (*Painter, error) {
seriesList := NewSeriesListDataFromValues(values, ChartTypeHorizontalBar)
return Render(ChartOption{ return Render(ChartOption{
SeriesList: seriesList, SeriesList: seriesList,
}, opts...) }, opts...)
} }
// PieRender pie chart render // PieRender pie chart render
func PieRender(values []float64, opts ...OptionFunc) (*Painter, error) { func PieRender(values []float64, opts ...OptionFunc) (*Draw, error) {
return Render(ChartOption{ return Render(ChartOption{
SeriesList: NewPieSeriesList(values), SeriesList: NewPieSeriesList(values),
}, opts...) }, opts...)
} }
// RadarRender radar chart render // RadarRender radar chart render
func RadarRender(values [][]float64, opts ...OptionFunc) (*Painter, error) { func RadarRender(values [][]float64, opts ...OptionFunc) (*Draw, error) {
seriesList := NewSeriesListDataFromValues(values, ChartTypeRadar) seriesList := make(SeriesList, len(values))
for index, value := range values {
seriesList[index] = NewSeriesFromValues(value, ChartTypeRadar)
}
return Render(ChartOption{ return Render(ChartOption{
SeriesList: seriesList, SeriesList: seriesList,
}, opts...) }, opts...)
} }
// FunnelRender funnel chart render // FunnelRender funnel chart render
func FunnelRender(values []float64, opts ...OptionFunc) (*Painter, error) { func FunnelRender(values []float64, opts ...OptionFunc) (*Draw, error) {
seriesList := NewFunnelSeriesList(values) seriesList := make(SeriesList, len(values))
for index, value := range values {
seriesList[index] = NewSeriesFromValues([]float64{
value,
}, ChartTypeFunnel)
}
return Render(ChartOption{ return Render(ChartOption{
SeriesList: seriesList, SeriesList: seriesList,
}, opts...) }, opts...)
} }
// TableRender table chart render
func TableRender(header []string, data [][]string, spanMaps ...map[int]int) (*Painter, error) {
opt := TableChartOption{
Header: header,
Data: data,
}
if len(spanMaps) != 0 {
spanMap := spanMaps[0]
spans := make([]int, len(opt.Header))
for index := range spans {
v, ok := spanMap[index]
if !ok {
v = 1
}
spans[index] = v
}
opt.Spans = spans
}
return TableOptionRender(opt)
}
// TableOptionRender table render with option
func TableOptionRender(opt TableChartOption) (*Painter, error) {
if opt.Type == "" {
opt.Type = ChartOutputPNG
}
if opt.Width <= 0 {
opt.Width = defaultChartWidth
}
if opt.FontFamily != "" {
opt.Font, _ = GetFont(opt.FontFamily)
}
if opt.Font == nil {
opt.Font, _ = GetDefaultFont()
}
p, err := NewPainter(PainterOptions{
Type: opt.Type,
Width: opt.Width,
// 仅用于计算表格高度,因此随便设置即可
Height: 100,
Font: opt.Font,
})
if err != nil {
return nil, err
}
info, err := NewTableChart(p, opt).render()
if err != nil {
return nil, err
}
p, err = NewPainter(PainterOptions{
Type: opt.Type,
Width: info.Width,
Height: info.Height,
Font: opt.Font,
})
if err != nil {
return nil, err
}
_, err = NewTableChart(p, opt).renderWithInfo(info)
if err != nil {
return nil, err
}
return p, nil
}

File diff suppressed because one or more lines are too long

567
chart_test.go Normal file

File diff suppressed because one or more lines are too long

473
charts.go
View file

@ -1,473 +0,0 @@
// 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 (
"errors"
"math"
"sort"
"git.smarteching.com/zeni/go-chart/v2"
)
const labelFontSize = 10
const smallLabelFontSize = 8
const defaultDotWidth = 2.0
const defaultStrokeWidth = 2.0
var defaultChartWidth = 600
var defaultChartHeight = 400
// SetDefaultWidth sets default width of chart
func SetDefaultWidth(width int) {
if width > 0 {
defaultChartWidth = width
}
}
// SetDefaultHeight sets default height of chart
func SetDefaultHeight(height int) {
if height > 0 {
defaultChartHeight = height
}
}
var nullValue = math.MaxFloat64
// SetNullValue sets the null value, default is MaxFloat64
func SetNullValue(v float64) {
nullValue = v
}
// GetNullValue gets the null value
func GetNullValue() float64 {
return nullValue
}
type Renderer interface {
Render() (Box, error)
}
type renderHandler struct {
list []func() error
}
func (rh *renderHandler) Add(fn func() error) {
list := rh.list
if len(list) == 0 {
list = make([]func() error, 0)
}
rh.list = append(list, fn)
}
func (rh *renderHandler) Do() error {
for _, fn := range rh.list {
err := fn()
if err != nil {
return err
}
}
return nil
}
type defaultRenderOption struct {
Theme ColorPalette
Padding Box
SeriesList SeriesList
// The y axis option
YAxisOptions []YAxisOption
// The x axis option
XAxis XAxisOption
// The title option
TitleOption TitleOption
// The legend option
LegendOption LegendOption
// background is filled
backgroundIsFilled bool
// x y axis is reversed
axisReversed bool
}
type defaultRenderResult struct {
axisRanges map[int]axisRange
// 图例区域
seriesPainter *Painter
}
func defaultRender(p *Painter, opt defaultRenderOption) (*defaultRenderResult, error) {
seriesList := opt.SeriesList
seriesList.init()
if !opt.backgroundIsFilled {
p.SetBackground(p.Width(), p.Height(), opt.Theme.GetBackgroundColor())
}
if !opt.Padding.IsZero() {
p = p.Child(PainterPaddingOption(opt.Padding))
}
legendHeight := 0
if len(opt.LegendOption.Data) != 0 {
if opt.LegendOption.Theme == nil {
opt.LegendOption.Theme = opt.Theme
}
legendResult, err := NewLegendPainter(p, opt.LegendOption).Render()
if err != nil {
return nil, err
}
legendHeight = legendResult.Height()
}
// 如果有标题
if opt.TitleOption.Text != "" {
if opt.TitleOption.Theme == nil {
opt.TitleOption.Theme = opt.Theme
}
titlePainter := NewTitlePainter(p, opt.TitleOption)
titleBox, err := titlePainter.Render()
if err != nil {
return nil, err
}
top := chart.MaxInt(legendHeight, titleBox.Height())
// 如果是垂直方式则不计算legend高度
if opt.LegendOption.Orient == OrientVertical {
top = titleBox.Height()
}
p = p.Child(PainterPaddingOption(Box{
// 标题下留白
Top: top + 20,
}))
}
result := defaultRenderResult{
axisRanges: make(map[int]axisRange),
}
// 计算图表对应的轴有哪些
axisIndexList := make([]int, 0)
for _, series := range opt.SeriesList {
if containsInt(axisIndexList, series.AxisIndex) {
continue
}
axisIndexList = append(axisIndexList, series.AxisIndex)
}
// 高度需要减去x轴的高度
rangeHeight := p.Height() - defaultXAxisHeight
rangeWidthLeft := 0
rangeWidthRight := 0
// 倒序
sort.Sort(sort.Reverse(sort.IntSlice(axisIndexList)))
// 计算对应的axis range
for _, index := range axisIndexList {
yAxisOption := YAxisOption{}
if len(opt.YAxisOptions) > index {
yAxisOption = opt.YAxisOptions[index]
}
divideCount := yAxisOption.DivideCount
if divideCount <= 0 {
divideCount = defaultAxisDivideCount
}
max, min := opt.SeriesList.GetMaxMin(index)
r := NewRange(AxisRangeOption{
Painter: p,
Min: min,
Max: max,
// 高度需要减去x轴的高度
Size: rangeHeight,
// 分隔数量
DivideCount: divideCount,
})
if yAxisOption.Min != nil && *yAxisOption.Min <= min {
r.min = *yAxisOption.Min
}
if yAxisOption.Max != nil && *yAxisOption.Max >= max {
r.max = *yAxisOption.Max
}
result.axisRanges[index] = r
if yAxisOption.Theme == nil {
yAxisOption.Theme = opt.Theme
}
if !opt.axisReversed {
yAxisOption.Data = r.Values()
} else {
yAxisOption.isCategoryAxis = true
// 由于x轴为value部分因此计算其label单独处理
opt.XAxis.Data = NewRange(AxisRangeOption{
Painter: p,
Min: min,
Max: max,
// 高度需要减去x轴的高度
Size: rangeHeight,
// 分隔数量
DivideCount: defaultAxisDivideCount,
}).Values()
opt.XAxis.isValueAxis = true
}
reverseStringSlice(yAxisOption.Data)
// TODO生成其它位置既yAxis
var yAxis *axisPainter
child := p.Child(PainterPaddingOption(Box{
Left: rangeWidthLeft,
Right: rangeWidthRight,
}))
if index == 0 {
yAxis = NewLeftYAxis(child, yAxisOption)
} else {
yAxis = NewRightYAxis(child, yAxisOption)
}
yAxisBox, err := yAxis.Render()
if err != nil {
return nil, err
}
if index == 0 {
rangeWidthLeft += yAxisBox.Width()
} else {
rangeWidthRight += yAxisBox.Width()
}
}
if opt.XAxis.Theme == nil {
opt.XAxis.Theme = opt.Theme
}
xAxis := NewBottomXAxis(p.Child(PainterPaddingOption(Box{
Left: rangeWidthLeft,
Right: rangeWidthRight,
})), opt.XAxis)
_, err := xAxis.Render()
if err != nil {
return nil, err
}
result.seriesPainter = p.Child(PainterPaddingOption(Box{
Bottom: defaultXAxisHeight,
Left: rangeWidthLeft,
Right: rangeWidthRight,
}))
return &result, nil
}
func doRender(renderers ...Renderer) error {
for _, r := range renderers {
_, err := r.Render()
if err != nil {
return err
}
}
return nil
}
func Render(opt ChartOption, opts ...OptionFunc) (*Painter, error) {
for _, fn := range opts {
fn(&opt)
}
opt.fillDefault()
isChild := true
if opt.Parent == nil {
isChild = false
p, err := NewPainter(PainterOptions{
Type: opt.Type,
Width: opt.Width,
Height: opt.Height,
Font: opt.font,
})
if err != nil {
return nil, err
}
opt.Parent = p
}
p := opt.Parent
if opt.ValueFormatter != nil {
p.valueFormatter = opt.ValueFormatter
}
if !opt.Box.IsZero() {
p = p.Child(PainterBoxOption(opt.Box))
}
if !isChild {
p.SetBackground(p.Width(), p.Height(), opt.BackgroundColor)
}
seriesList := opt.SeriesList
seriesList.init()
seriesCount := len(seriesList)
// line chart
lineSeriesList := seriesList.Filter(ChartTypeLine)
barSeriesList := seriesList.Filter(ChartTypeBar)
horizontalBarSeriesList := seriesList.Filter(ChartTypeHorizontalBar)
pieSeriesList := seriesList.Filter(ChartTypePie)
radarSeriesList := seriesList.Filter(ChartTypeRadar)
funnelSeriesList := seriesList.Filter(ChartTypeFunnel)
if len(horizontalBarSeriesList) != 0 && len(horizontalBarSeriesList) != seriesCount {
return nil, errors.New("Horizontal bar can not mix other charts")
}
if len(pieSeriesList) != 0 && len(pieSeriesList) != seriesCount {
return nil, errors.New("Pie can not mix other charts")
}
if len(radarSeriesList) != 0 && len(radarSeriesList) != seriesCount {
return nil, errors.New("Radar can not mix other charts")
}
if len(funnelSeriesList) != 0 && len(funnelSeriesList) != seriesCount {
return nil, errors.New("Funnel can not mix other charts")
}
axisReversed := len(horizontalBarSeriesList) != 0
renderOpt := defaultRenderOption{
Theme: opt.theme,
Padding: opt.Padding,
SeriesList: opt.SeriesList,
XAxis: opt.XAxis,
YAxisOptions: opt.YAxisOptions,
TitleOption: opt.Title,
LegendOption: opt.Legend,
axisReversed: axisReversed,
// 前置已设置背景色
backgroundIsFilled: true,
}
if len(pieSeriesList) != 0 ||
len(radarSeriesList) != 0 ||
len(funnelSeriesList) != 0 {
renderOpt.XAxis.Show = FalseFlag()
renderOpt.YAxisOptions = []YAxisOption{
{
Show: FalseFlag(),
},
}
}
if len(horizontalBarSeriesList) != 0 {
renderOpt.YAxisOptions[0].DivideCount = len(renderOpt.YAxisOptions[0].Data)
renderOpt.YAxisOptions[0].Unit = 1
}
renderResult, err := defaultRender(p, renderOpt)
if err != nil {
return nil, err
}
handler := renderHandler{}
// bar chart
if len(barSeriesList) != 0 {
handler.Add(func() error {
_, err := NewBarChart(p, BarChartOption{
Theme: opt.theme,
Font: opt.font,
XAxis: opt.XAxis,
BarWidth: opt.BarWidth,
BarMargin: opt.BarMargin,
}).render(renderResult, barSeriesList)
return err
})
}
// horizontal bar chart
if len(horizontalBarSeriesList) != 0 {
handler.Add(func() error {
_, err := NewHorizontalBarChart(p, HorizontalBarChartOption{
Theme: opt.theme,
Font: opt.font,
BarHeight: opt.BarHeight,
BarMargin: opt.BarMargin,
YAxisOptions: opt.YAxisOptions,
}).render(renderResult, horizontalBarSeriesList)
return err
})
}
// pie chart
if len(pieSeriesList) != 0 {
handler.Add(func() error {
_, err := NewPieChart(p, PieChartOption{
Theme: opt.theme,
Font: opt.font,
}).render(renderResult, pieSeriesList)
return err
})
}
// line chart
if len(lineSeriesList) != 0 {
handler.Add(func() error {
_, err := NewLineChart(p, LineChartOption{
Theme: opt.theme,
Font: opt.font,
XAxis: opt.XAxis,
SymbolShow: opt.SymbolShow,
StrokeWidth: opt.LineStrokeWidth,
FillArea: opt.FillArea,
Opacity: opt.Opacity,
}).render(renderResult, lineSeriesList)
return err
})
}
// radar chart
if len(radarSeriesList) != 0 {
handler.Add(func() error {
_, err := NewRadarChart(p, RadarChartOption{
Theme: opt.theme,
Font: opt.font,
// 相应值
RadarIndicators: opt.RadarIndicators,
}).render(renderResult, radarSeriesList)
return err
})
}
// funnel chart
if len(funnelSeriesList) != 0 {
handler.Add(func() error {
_, err := NewFunnelChart(p, FunnelChartOption{
Theme: opt.theme,
Font: opt.font,
}).render(renderResult, funnelSeriesList)
return err
})
}
err = handler.Do()
if err != nil {
return nil, err
}
for _, item := range opt.Children {
item.Parent = p
if item.Theme == "" {
item.Theme = opt.Theme
}
if item.FontFamily == "" {
item.FontFamily = opt.FontFamily
}
_, err = Render(item)
if err != nil {
return nil, err
}
}
return p, nil
}

View file

@ -1,255 +0,0 @@
// 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 (
"errors"
"testing"
"git.smarteching.com/zeni/go-chart/v2"
)
func BenchmarkMultiChartPNGRender(b *testing.B) {
for i := 0; i < b.N; i++ {
opt := ChartOption{
Type: ChartOutputPNG,
Legend: LegendOption{
Top: "-90",
Data: []string{
"Milk Tea",
"Matcha Latte",
"Cheese Cocoa",
"Walnut Brownie",
},
},
Padding: chart.Box{
Top: 100,
Right: 10,
Bottom: 10,
Left: 10,
},
XAxis: NewXAxisOption([]string{
"2012",
"2013",
"2014",
"2015",
"2016",
"2017",
}),
YAxisOptions: []YAxisOption{
{
Min: NewFloatPoint(0),
Max: NewFloatPoint(90),
},
},
SeriesList: []Series{
NewSeriesFromValues([]float64{
56.5,
82.1,
88.7,
70.1,
53.4,
85.1,
}),
NewSeriesFromValues([]float64{
51.1,
51.4,
55.1,
53.3,
73.8,
68.7,
}),
NewSeriesFromValues([]float64{
40.1,
62.2,
69.5,
36.4,
45.2,
32.5,
}, ChartTypeBar),
NewSeriesFromValues([]float64{
25.2,
37.1,
41.2,
18,
33.9,
49.1,
}, ChartTypeBar),
},
Children: []ChartOption{
{
Legend: LegendOption{
Show: FalseFlag(),
Data: []string{
"Milk Tea",
"Matcha Latte",
"Cheese Cocoa",
"Walnut Brownie",
},
},
Box: chart.Box{
Top: 20,
Left: 400,
Right: 500,
Bottom: 120,
},
SeriesList: NewPieSeriesList([]float64{
435.9,
354.3,
285.9,
204.5,
}, PieSeriesOption{
Label: SeriesLabel{
Show: true,
},
Radius: "35%",
}),
},
},
}
d, err := Render(opt)
if err != nil {
panic(err)
}
buf, err := d.Bytes()
if err != nil {
panic(err)
}
if len(buf) == 0 {
panic(errors.New("data is nil"))
}
}
}
func BenchmarkMultiChartSVGRender(b *testing.B) {
for i := 0; i < b.N; i++ {
opt := ChartOption{
Legend: LegendOption{
Top: "-90",
Data: []string{
"Milk Tea",
"Matcha Latte",
"Cheese Cocoa",
"Walnut Brownie",
},
},
Padding: chart.Box{
Top: 100,
Right: 10,
Bottom: 10,
Left: 10,
},
XAxis: NewXAxisOption([]string{
"2012",
"2013",
"2014",
"2015",
"2016",
"2017",
}),
YAxisOptions: []YAxisOption{
{
Min: NewFloatPoint(0),
Max: NewFloatPoint(90),
},
},
SeriesList: []Series{
NewSeriesFromValues([]float64{
56.5,
82.1,
88.7,
70.1,
53.4,
85.1,
}),
NewSeriesFromValues([]float64{
51.1,
51.4,
55.1,
53.3,
73.8,
68.7,
}),
NewSeriesFromValues([]float64{
40.1,
62.2,
69.5,
36.4,
45.2,
32.5,
}, ChartTypeBar),
NewSeriesFromValues([]float64{
25.2,
37.1,
41.2,
18,
33.9,
49.1,
}, ChartTypeBar),
},
Children: []ChartOption{
{
Legend: LegendOption{
Show: FalseFlag(),
Data: []string{
"Milk Tea",
"Matcha Latte",
"Cheese Cocoa",
"Walnut Brownie",
},
},
Box: chart.Box{
Top: 20,
Left: 400,
Right: 500,
Bottom: 120,
},
SeriesList: NewPieSeriesList([]float64{
435.9,
354.3,
285.9,
204.5,
}, PieSeriesOption{
Label: SeriesLabel{
Show: true,
},
Radius: "35%",
}),
},
},
}
d, err := Render(opt)
if err != nil {
panic(err)
}
buf, err := d.Bytes()
if err != nil {
panic(err)
}
if len(buf) == 0 {
panic(errors.New("data is nil"))
}
}
}

372
draw.go Normal file
View file

@ -0,0 +1,372 @@
// 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 (
"bytes"
"errors"
"math"
"github.com/golang/freetype/truetype"
"github.com/wcharczuk/go-chart/v2"
"github.com/wcharczuk/go-chart/v2/drawing"
)
const (
PositionLeft = "left"
PositionRight = "right"
PositionCenter = "center"
PositionTop = "top"
PositionBottom = "bottom"
)
const (
OrientHorizontal = "horizontal"
OrientVertical = "vertical"
)
type Draw struct {
// Render
Render chart.Renderer
// The canvas box
Box chart.Box
// The font for draw
Font *truetype.Font
// The parent of draw
parent *Draw
}
type DrawOption struct {
// Draw type, "svg" or "png", default type is "svg"
Type string
// Parent of draw
Parent *Draw
// The width of draw canvas
Width int
// The height of draw canvas
Height int
}
type Option func(*Draw) error
// PaddingOption sets the padding of draw canvas
func PaddingOption(padding chart.Box) Option {
return func(d *Draw) error {
d.Box.Left += padding.Left
d.Box.Top += padding.Top
d.Box.Right -= padding.Right
d.Box.Bottom -= padding.Bottom
return nil
}
}
// BoxOption set the box of draw canvas
func BoxOption(box chart.Box) Option {
return func(d *Draw) error {
if box.IsZero() {
return nil
}
d.Box = box
return nil
}
}
// NewDraw returns a new draw canvas
func NewDraw(opt DrawOption, opts ...Option) (*Draw, error) {
if opt.Parent == nil && (opt.Width <= 0 || opt.Height <= 0) {
return nil, errors.New("parent and width/height can not be nil")
}
font, _ := chart.GetDefaultFont()
d := &Draw{
Font: font,
}
width := opt.Width
height := opt.Height
if opt.Parent != nil {
d.parent = opt.Parent
d.Render = d.parent.Render
d.Box = opt.Parent.Box.Clone()
}
if width != 0 && height != 0 {
d.Box.Right = width + d.Box.Left
d.Box.Bottom = height + d.Box.Top
}
// 创建render
if d.parent == nil {
fn := chart.SVG
if opt.Type == ChartOutputPNG {
fn = chart.PNG
}
r, err := fn(d.Box.Right, d.Box.Bottom)
if err != nil {
return nil, err
}
d.Render = r
}
for _, o := range opts {
err := o(d)
if err != nil {
return nil, err
}
}
return d, nil
}
// Parent returns the parent of draw
func (d *Draw) Parent() *Draw {
return d.parent
}
// Top returns the top parent of draw
func (d *Draw) Top() *Draw {
if d.parent == nil {
return nil
}
t := d.parent
// 限制最多查询次数,避免嵌套引用
for i := 50; i > 0; i-- {
if t.parent == nil {
break
}
t = t.parent
}
return t
}
// Bytes returns the data of draw canvas
func (d *Draw) Bytes() ([]byte, error) {
buffer := bytes.Buffer{}
err := d.Render.Save(&buffer)
if err != nil {
return nil, err
}
return buffer.Bytes(), err
}
func (d *Draw) moveTo(x, y int) {
d.Render.MoveTo(x+d.Box.Left, y+d.Box.Top)
}
func (d *Draw) arcTo(cx, cy int, rx, ry, startAngle, delta float64) {
d.Render.ArcTo(cx+d.Box.Left, cy+d.Box.Top, rx, ry, startAngle, delta)
}
func (d *Draw) lineTo(x, y int) {
d.Render.LineTo(x+d.Box.Left, y+d.Box.Top)
}
func (d *Draw) pin(x, y, width int) {
r := float64(width) / 2
y -= width / 4
angle := chart.DegreesToRadians(15)
startAngle := math.Pi/2 + angle
delta := 2*math.Pi - 2*angle
d.arcTo(x, y, r, r, startAngle, delta)
d.lineTo(x, y)
d.Render.Close()
d.Render.FillStroke()
startX := x - int(r)
startY := y
endX := x + int(r)
endY := y
d.moveTo(startX, startY)
left := d.Box.Left
top := d.Box.Top
cx := x
cy := y + int(r*2.5)
d.Render.QuadCurveTo(cx+left, cy+top, endX+left, endY+top)
d.Render.Close()
d.Render.Fill()
}
func (d *Draw) arrowLeft(x, y, width, height int) {
d.arrow(x, y, width, height, PositionLeft)
}
func (d *Draw) arrowRight(x, y, width, height int) {
d.arrow(x, y, width, height, PositionRight)
}
func (d *Draw) arrowTop(x, y, width, height int) {
d.arrow(x, y, width, height, PositionTop)
}
func (d *Draw) arrowBottom(x, y, width, height int) {
d.arrow(x, y, width, height, PositionBottom)
}
func (d *Draw) arrow(x, y, width, height int, direction string) {
halfWidth := width >> 1
halfHeight := height >> 1
if direction == PositionTop || direction == PositionBottom {
x0 := x - halfWidth
x1 := x0 + width
dy := -height / 3
y0 := y
y1 := y0 - height
if direction == PositionBottom {
y0 = y - height
y1 = y
dy = 2 * dy
}
d.moveTo(x0, y0)
d.lineTo(x0+halfWidth, y1)
d.lineTo(x1, y0)
d.lineTo(x0+halfWidth, y+dy)
d.lineTo(x0, y0)
} else {
x0 := x + width
x1 := x0 - width
y0 := y - halfHeight
dx := -width / 3
if direction == PositionRight {
x0 = x - width
dx = -dx
x1 = x0 + width
}
d.moveTo(x0, y0)
d.lineTo(x1, y0+halfHeight)
d.lineTo(x0, y0+height)
d.lineTo(x0+dx, y0+halfHeight)
d.lineTo(x0, y0)
}
d.Render.FillStroke()
}
func (d *Draw) makeLine(x, y, width int) {
arrowWidth := 16
arrowHeight := 10
endX := x + width
d.circle(3, x, y)
d.Render.Fill()
d.moveTo(x+5, y)
d.lineTo(endX-arrowWidth, y)
d.Render.Stroke()
d.Render.SetStrokeDashArray([]float64{})
d.arrowRight(endX, y, arrowWidth, arrowHeight)
}
func (d *Draw) circle(radius float64, x, y int) {
d.Render.Circle(radius, x+d.Box.Left, y+d.Box.Top)
}
func (d *Draw) text(body string, x, y int) {
d.Render.Text(body, x+d.Box.Left, y+d.Box.Top)
}
func (d *Draw) textFit(body string, x, y, width int, style chart.Style) chart.Box {
style.TextWrap = chart.TextWrapWord
r := d.Render
lines := chart.Text.WrapFit(r, body, width, style)
style.WriteTextOptionsToRenderer(r)
var output chart.Box
for index, line := range lines {
x0 := x
y0 := y + output.Height()
d.text(line, x0, y0)
lineBox := r.MeasureText(line)
output.Right = chart.MaxInt(lineBox.Right, output.Right)
output.Bottom += lineBox.Height()
if index < len(lines)-1 {
output.Bottom += +style.GetTextLineSpacing()
}
}
return output
}
func (d *Draw) measureTextFit(body string, x, y, width int, style chart.Style) chart.Box {
style.TextWrap = chart.TextWrapWord
r := d.Render
lines := chart.Text.WrapFit(r, body, width, style)
style.WriteTextOptionsToRenderer(r)
return chart.Text.MeasureLines(r, lines, style)
}
func (d *Draw) lineStroke(points []Point, style LineStyle) {
s := style.Style()
if !s.ShouldDrawStroke() {
return
}
r := d.Render
s.GetStrokeOptions().WriteDrawingOptionsToRenderer(r)
for index, point := range points {
x := point.X
y := point.Y
if index == 0 {
d.moveTo(x, y)
} else {
d.lineTo(x, y)
}
}
r.Stroke()
}
func (d *Draw) setBackground(width, height int, color drawing.Color) {
r := d.Render
s := chart.Style{
FillColor: color,
}
s.WriteToRenderer(r)
r.MoveTo(0, 0)
r.LineTo(width, 0)
r.LineTo(width, height)
r.LineTo(0, height)
r.LineTo(0, 0)
r.FillStroke()
}
func (d *Draw) polygon(center Point, radius float64, sides int) {
points := getPolygonPoints(center, radius, sides)
for i, p := range points {
if i == 0 {
d.moveTo(p.X, p.Y)
} else {
d.lineTo(p.X, p.Y)
}
}
d.lineTo(points[0].X, points[0].Y)
d.Render.Stroke()
}
func (d *Draw) fill(points []Point, s chart.Style) {
if !s.ShouldDrawFill() {
return
}
r := d.Render
var x, y int
s.GetFillOptions().WriteDrawingOptionsToRenderer(r)
for index, point := range points {
x = point.X
y = point.Y
if index == 0 {
d.moveTo(x, y)
} else {
d.lineTo(x, y)
}
}
r.Fill()
}

View file

@ -26,85 +26,217 @@ import (
"math" "math"
"testing" "testing"
"github.com/golang/freetype/truetype"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"git.smarteching.com/zeni/go-chart/v2" "github.com/wcharczuk/go-chart/v2"
"git.smarteching.com/zeni/go-chart/v2/drawing" "github.com/wcharczuk/go-chart/v2/drawing"
) )
func TestPainterOption(t *testing.T) { func TestParentOption(t *testing.T) {
assert := assert.New(t)
p, err := NewDraw(DrawOption{
Width: 400,
Height: 300,
})
assert.Nil(err)
d, err := NewDraw(DrawOption{
Parent: p,
})
assert.Nil(err)
assert.Equal(p, d.parent)
}
func TestWidthHeightOption(t *testing.T) {
assert := assert.New(t) assert := assert.New(t)
font := &truetype.Font{} // no parent
d, err := NewPainter(PainterOptions{ width := 300
Width: 800, height := 200
Height: 600, d, err := NewDraw(DrawOption{
Type: ChartOutputSVG, Width: width,
}, Height: height,
PainterBoxOption(Box{ })
Right: 400, assert.Nil(err)
Bottom: 300, assert.Equal(chart.Box{
}), Top: 0,
PainterPaddingOption(Box{ Left: 0,
Right: width,
Bottom: height,
}, d.Box)
width = 500
height = 600
// with parent
p, err := NewDraw(
DrawOption{
Width: width,
Height: height,
},
PaddingOption(chart.NewBox(5, 5, 5, 5)),
)
assert.Nil(err)
d, err = NewDraw(
DrawOption{
Parent: p,
},
PaddingOption(chart.NewBox(1, 2, 3, 4)),
)
assert.Nil(err)
assert.Equal(chart.Box{
Top: 6,
Left: 7,
Right: 492,
Bottom: 591,
}, d.Box)
}
func TestBoxOption(t *testing.T) {
assert := assert.New(t)
d, err := NewDraw(DrawOption{
Width: 400,
Height: 300,
})
assert.Nil(err)
err = BoxOption(chart.Box{
Left: 10,
Top: 20,
Right: 50,
Bottom: 100,
})(d)
assert.Nil(err)
assert.Equal(chart.Box{
Left: 10,
Top: 20,
Right: 50,
Bottom: 100,
}, d.Box)
// zero box will be ignored
err = BoxOption(chart.Box{})(d)
assert.Nil(err)
assert.Equal(chart.Box{
Left: 10,
Top: 20,
Right: 50,
Bottom: 100,
}, d.Box)
}
func TestPaddingOption(t *testing.T) {
assert := assert.New(t)
d, err := NewDraw(DrawOption{
Width: 400,
Height: 300,
})
assert.Nil(err)
// 默认的box
assert.Equal(chart.Box{
Right: 400,
Bottom: 300,
}, d.Box)
// 设置padding之后的box
d, err = NewDraw(DrawOption{
Width: 400,
Height: 300,
}, PaddingOption(chart.Box{
Left: 1,
Top: 2,
Right: 3,
Bottom: 4,
}))
assert.Nil(err)
assert.Equal(chart.Box{
Top: 2,
Left: 1,
Right: 397,
Bottom: 296,
}, d.Box)
p := d
// 设置父元素之后的box
d, err = NewDraw(
DrawOption{
Parent: p,
},
PaddingOption(chart.Box{
Left: 1, Left: 1,
Top: 2, Top: 2,
Right: 3, Right: 3,
Bottom: 4, Bottom: 4,
}), }),
PainterFontOption(font),
PainterStyleOption(Style{
ClassName: "test",
}),
) )
assert.Nil(err) assert.Nil(err)
assert.Equal(Box{ assert.Equal(chart.Box{
Left: 1, Top: 4,
Top: 2, Left: 2,
Right: 397, Right: 394,
Bottom: 296, Bottom: 292,
}, d.box) }, d.Box)
assert.Equal(font, d.font)
assert.Equal("test", d.style.ClassName)
} }
func TestPainter(t *testing.T) { func TestParentTop(t *testing.T) {
assert := assert.New(t)
d1, err := NewDraw(DrawOption{
Width: 400,
Height: 300,
})
assert.Nil(err)
d2, err := NewDraw(DrawOption{
Parent: d1,
})
assert.Nil(err)
d3, err := NewDraw(DrawOption{
Parent: d2,
})
assert.Nil(err)
assert.Equal(d2, d3.Parent())
assert.Equal(d1, d2.Parent())
assert.Equal(d1, d3.Top())
assert.Equal(d1, d2.Top())
}
func TestDraw(t *testing.T) {
assert := assert.New(t) assert := assert.New(t)
tests := []struct { tests := []struct {
fn func(*Painter) fn func(d *Draw)
result string result string
}{ }{
// moveTo, lineTo // moveTo, lineTo
{ {
fn: func(p *Painter) { fn: func(d *Draw) {
p.MoveTo(1, 1) d.moveTo(1, 1)
p.LineTo(2, 2) d.lineTo(2, 2)
p.Stroke() d.Render.Stroke()
}, },
result: "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"400\" height=\"300\">\\n<path d=\"M 6 11\nL 7 12\" style=\"stroke-width:0;stroke:none;fill:none\"/></svg>", result: "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"400\" height=\"300\">\\n<path d=\"M 6 11\nL 7 12\" style=\"stroke-width:0;stroke:none;fill:none\"/></svg>",
}, },
// circle // circle
{ {
fn: func(p *Painter) { fn: func(d *Draw) {
p.Circle(5, 2, 3) d.circle(5, 2, 3)
}, },
result: "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"400\" height=\"300\">\\n<circle cx=\"7\" cy=\"13\" r=\"5\" style=\"stroke-width:0;stroke:none;fill:none\"/></svg>", result: "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"400\" height=\"300\">\\n<circle cx=\"7\" cy=\"13\" r=\"5\" style=\"stroke-width:0;stroke:none;fill:none\"/></svg>",
}, },
// text // text
{ {
fn: func(p *Painter) { fn: func(d *Draw) {
p.Text("hello world!", 3, 6) d.text("hello world!", 3, 6)
}, },
result: "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"400\" height=\"300\">\\n<text x=\"8\" y=\"16\" style=\"stroke-width:0;stroke:none;fill:none;font-family:'Roboto Medium',sans-serif\">hello world!</text></svg>", result: "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"400\" height=\"300\">\\n<text x=\"8\" y=\"16\" style=\"stroke-width:0;stroke:none;fill:none\">hello world!</text></svg>",
}, },
// line stroke // line stroke
{ {
fn: func(p *Painter) { fn: func(d *Draw) {
p.SetDrawingStyle(Style{ d.lineStroke([]Point{
StrokeColor: drawing.ColorBlack,
StrokeWidth: 1,
})
p.LineStroke([]Point{
{ {
X: 1, X: 1,
Y: 2, Y: 2,
@ -113,153 +245,156 @@ func TestPainter(t *testing.T) {
X: 3, X: 3,
Y: 4, Y: 4,
}, },
}, LineStyle{
StrokeColor: drawing.ColorBlack,
StrokeWidth: 1,
}) })
}, },
result: "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"400\" height=\"300\">\\n<path d=\"M 6 12\nL 8 14\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/></svg>", result: "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"400\" height=\"300\">\\n<path d=\"M 6 12\nL 8 14\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/></svg>",
}, },
// set background // set background
{ {
fn: func(p *Painter) { fn: func(d *Draw) {
p.SetBackground(400, 300, chart.ColorWhite) d.setBackground(400, 300, chart.ColorWhite)
}, },
result: "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"400\" height=\"300\">\\n<path d=\"M 0 0\nL 400 0\nL 400 300\nL 0 300\nL 0 0\" style=\"stroke-width:0;stroke:none;fill:rgba(255,255,255,1.0)\"/></svg>", result: "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"400\" height=\"300\">\\n<path d=\"M 0 0\nL 400 0\nL 400 300\nL 0 300\nL 0 0\" style=\"stroke-width:0;stroke:none;fill:rgba(255,255,255,1.0)\"/></svg>",
}, },
// arcTo // arcTo
{ {
fn: func(p *Painter) { fn: func(d *Draw) {
p.SetStyle(Style{ chart.Style{
StrokeWidth: 1, StrokeWidth: 1,
StrokeColor: drawing.ColorBlack, StrokeColor: drawing.ColorBlack,
FillColor: drawing.ColorBlue, FillColor: drawing.ColorBlue,
}) }.WriteToRenderer(d.Render)
p.ArcTo(100, 100, 100, 100, 0, math.Pi/2) d.arcTo(100, 100, 100, 100, 0, math.Pi/2)
p.Close() d.Render.Close()
p.FillStroke() d.Render.FillStroke()
}, },
result: "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"400\" height=\"300\">\\n<path d=\"M 205 110\nA 100 100 90.00 0 1 105 210\nZ\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:rgba(0,0,255,1.0)\"/></svg>", result: "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"400\" height=\"300\">\\n<path d=\"M 205 110\nA 100 100 90.00 0 1 105 210\nZ\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:rgba(0,0,255,1.0)\"/></svg>",
}, },
// pin // pin
{ {
fn: func(p *Painter) { fn: func(d *Draw) {
p.SetStyle(Style{ chart.Style{
StrokeWidth: 1, StrokeWidth: 1,
StrokeColor: Color{ StrokeColor: drawing.Color{
R: 84, R: 84,
G: 112, G: 112,
B: 198, B: 198,
A: 255, A: 255,
}, },
FillColor: Color{ FillColor: drawing.Color{
R: 84, R: 84,
G: 112, G: 112,
B: 198, B: 198,
A: 255, A: 255,
}, },
}) }.WriteToRenderer(d.Render)
p.Pin(30, 30, 30) d.pin(30, 30, 30)
}, },
result: "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"400\" height=\"300\">\\n<path d=\"M 32 47\nA 15 15 330.00 1 1 38 47\nL 35 33\nZ\" style=\"stroke-width:1;stroke:rgba(84,112,198,1.0);fill:rgba(84,112,198,1.0)\"/><path d=\"M 20 33\nQ35,70 50,33\nZ\" style=\"stroke-width:1;stroke:rgba(84,112,198,1.0);fill:rgba(84,112,198,1.0)\"/></svg>", result: "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"400\" height=\"300\">\\n<path d=\"M 32 47\nA 15 15 330.00 1 1 38 47\nL 35 33\nZ\" style=\"stroke-width:1;stroke:rgba(84,112,198,1.0);fill:rgba(84,112,198,1.0)\"/><path d=\"M 20 33\nQ35,70 50,33\nZ\" style=\"stroke-width:1;stroke:rgba(84,112,198,1.0);fill:rgba(84,112,198,1.0)\"/></svg>",
}, },
// arrow left // arrow left
{ {
fn: func(p *Painter) { fn: func(d *Draw) {
p.SetStyle(Style{ chart.Style{
StrokeWidth: 1, StrokeWidth: 1,
StrokeColor: Color{ StrokeColor: drawing.Color{
R: 84, R: 84,
G: 112, G: 112,
B: 198, B: 198,
A: 255, A: 255,
}, },
FillColor: Color{ FillColor: drawing.Color{
R: 84, R: 84,
G: 112, G: 112,
B: 198, B: 198,
A: 255, A: 255,
}, },
}) }.WriteToRenderer(d.Render)
p.ArrowLeft(30, 30, 16, 10) d.arrowLeft(30, 30, 16, 10)
}, },
result: "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"400\" height=\"300\">\\n<path d=\"M 51 35\nL 35 40\nL 51 45\nL 46 40\nL 51 35\" style=\"stroke-width:1;stroke:rgba(84,112,198,1.0);fill:rgba(84,112,198,1.0)\"/></svg>", result: "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"400\" height=\"300\">\\n<path d=\"M 51 35\nL 35 40\nL 51 45\nL 46 40\nL 51 35\" style=\"stroke-width:1;stroke:rgba(84,112,198,1.0);fill:rgba(84,112,198,1.0)\"/></svg>",
}, },
// arrow right // arrow right
{ {
fn: func(p *Painter) { fn: func(d *Draw) {
p.SetStyle(Style{ chart.Style{
StrokeWidth: 1, StrokeWidth: 1,
StrokeColor: Color{ StrokeColor: drawing.Color{
R: 84, R: 84,
G: 112, G: 112,
B: 198, B: 198,
A: 255, A: 255,
}, },
FillColor: Color{ FillColor: drawing.Color{
R: 84, R: 84,
G: 112, G: 112,
B: 198, B: 198,
A: 255, A: 255,
}, },
}) }.WriteToRenderer(d.Render)
p.ArrowRight(30, 30, 16, 10) d.arrowRight(30, 30, 16, 10)
}, },
result: "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"400\" height=\"300\">\\n<path d=\"M 19 35\nL 35 40\nL 19 45\nL 24 40\nL 19 35\" style=\"stroke-width:1;stroke:rgba(84,112,198,1.0);fill:rgba(84,112,198,1.0)\"/></svg>", result: "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"400\" height=\"300\">\\n<path d=\"M 19 35\nL 35 40\nL 19 45\nL 24 40\nL 19 35\" style=\"stroke-width:1;stroke:rgba(84,112,198,1.0);fill:rgba(84,112,198,1.0)\"/></svg>",
}, },
// arrow top // arrow top
{ {
fn: func(p *Painter) { fn: func(d *Draw) {
p.SetStyle(Style{ chart.Style{
StrokeWidth: 1, StrokeWidth: 1,
StrokeColor: Color{ StrokeColor: drawing.Color{
R: 84, R: 84,
G: 112, G: 112,
B: 198, B: 198,
A: 255, A: 255,
}, },
FillColor: Color{ FillColor: drawing.Color{
R: 84, R: 84,
G: 112, G: 112,
B: 198, B: 198,
A: 255, A: 255,
}, },
}) }.WriteToRenderer(d.Render)
p.ArrowTop(30, 30, 10, 16) d.arrowTop(30, 30, 10, 16)
}, },
result: "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"400\" height=\"300\">\\n<path d=\"M 30 40\nL 35 24\nL 40 40\nL 35 35\nL 30 40\" style=\"stroke-width:1;stroke:rgba(84,112,198,1.0);fill:rgba(84,112,198,1.0)\"/></svg>", result: "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"400\" height=\"300\">\\n<path d=\"M 30 40\nL 35 24\nL 40 40\nL 35 35\nL 30 40\" style=\"stroke-width:1;stroke:rgba(84,112,198,1.0);fill:rgba(84,112,198,1.0)\"/></svg>",
}, },
// arrow bottom // arrow bottom
{ {
fn: func(p *Painter) { fn: func(d *Draw) {
p.SetStyle(Style{ chart.Style{
StrokeWidth: 1, StrokeWidth: 1,
StrokeColor: Color{ StrokeColor: drawing.Color{
R: 84, R: 84,
G: 112, G: 112,
B: 198, B: 198,
A: 255, A: 255,
}, },
FillColor: Color{ FillColor: drawing.Color{
R: 84, R: 84,
G: 112, G: 112,
B: 198, B: 198,
A: 255, A: 255,
}, },
}) }.WriteToRenderer(d.Render)
p.ArrowBottom(30, 30, 10, 16) d.arrowBottom(30, 30, 10, 16)
}, },
result: "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"400\" height=\"300\">\\n<path d=\"M 30 24\nL 35 40\nL 40 24\nL 35 30\nL 30 24\" style=\"stroke-width:1;stroke:rgba(84,112,198,1.0);fill:rgba(84,112,198,1.0)\"/></svg>", result: "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"400\" height=\"300\">\\n<path d=\"M 30 24\nL 35 40\nL 40 24\nL 35 30\nL 30 24\" style=\"stroke-width:1;stroke:rgba(84,112,198,1.0);fill:rgba(84,112,198,1.0)\"/></svg>",
}, },
// mark line // mark line
{ {
fn: func(p *Painter) { fn: func(d *Draw) {
p.SetStyle(Style{ chart.Style{
StrokeWidth: 1, StrokeWidth: 1,
StrokeColor: Color{ StrokeColor: drawing.Color{
R: 84, R: 84,
G: 112, G: 112,
B: 198, B: 198,
A: 255, A: 255,
}, },
FillColor: Color{ FillColor: drawing.Color{
R: 84, R: 84,
G: 112, G: 112,
B: 198, B: 198,
@ -269,42 +404,34 @@ func TestPainter(t *testing.T) {
4, 4,
2, 2,
}, },
}) }.WriteToRenderer(d.Render)
p.MarkLine(0, 20, 300) d.makeLine(0, 20, 300)
}, },
result: "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"400\" height=\"300\">\\n<circle cx=\"8\" cy=\"30\" r=\"3\" style=\"stroke-width:1;stroke:rgba(84,112,198,1.0);fill:rgba(84,112,198,1.0)\"/><path stroke-dasharray=\"4.0, 2.0\" d=\"\" style=\"stroke-width:1;stroke:rgba(84,112,198,1.0);fill:rgba(84,112,198,1.0)\"/><path stroke-dasharray=\"4.0, 2.0\" d=\"M 14 30\nL 289 30\" style=\"stroke-width:1;stroke:rgba(84,112,198,1.0);fill:rgba(84,112,198,1.0)\"/><path stroke-dasharray=\"4.0, 2.0\" d=\"M 289 25\nL 305 30\nL 289 35\nL 294 30\nL 289 25\" style=\"stroke-width:1;stroke:rgba(84,112,198,1.0);fill:rgba(84,112,198,1.0)\"/></svg>", result: "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"400\" height=\"300\">\\n<circle cx=\"5\" cy=\"30\" r=\"3\" style=\"stroke-width:1;stroke:rgba(84,112,198,1.0);fill:rgba(84,112,198,1.0)\"/><path stroke-dasharray=\"4.0, 2.0\" d=\"\" style=\"stroke-width:1;stroke:rgba(84,112,198,1.0);fill:rgba(84,112,198,1.0)\"/><path stroke-dasharray=\"4.0, 2.0\" d=\"M 10 30\nL 289 30\" style=\"stroke-width:1;stroke:rgba(84,112,198,1.0);fill:rgba(84,112,198,1.0)\"/><path d=\"M 289 25\nL 305 30\nL 289 35\nL 294 30\nL 289 25\" style=\"stroke-width:1;stroke:rgba(84,112,198,1.0);fill:rgba(84,112,198,1.0)\"/></svg>",
}, },
// polygon // polygon
{ {
fn: func(p *Painter) { fn: func(d *Draw) {
p.SetStyle(Style{ chart.Style{
StrokeWidth: 1, StrokeWidth: 1,
StrokeColor: Color{ StrokeColor: drawing.Color{
R: 84, R: 84,
G: 112, G: 112,
B: 198, B: 198,
A: 255, A: 255,
}, },
}) }.WriteToRenderer(d.Render)
p.Polygon(Point{ d.polygon(Point{
X: 100, X: 100,
Y: 100, Y: 100,
}, 50, 6) }, 50, 6)
}, },
result: "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"400\" height=\"300\">\\n<path d=\"M 105 60\nL 148 85\nL 148 134\nL 105 160\nL 62 135\nL 62 86\nL 105 60\" style=\"stroke-width:1;stroke:rgba(84,112,198,1.0);fill:none\"/></svg>", result: "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"400\" height=\"300\">\\n<path d=\"M 105 60\nL 148 85\nL 148 134\nL 105 160\nL 62 135\nL 62 86\nL 105 60\" style=\"stroke-width:1;stroke:rgba(84,112,198,1.0);fill:none\"/></svg>",
}, },
// FillArea // fill
{ {
fn: func(p *Painter) { fn: func(d *Draw) {
p.SetDrawingStyle(Style{ d.fill([]Point{
FillColor: Color{
R: 84,
G: 112,
B: 198,
A: 255,
},
})
p.FillArea([]Point{
{ {
X: 0, X: 0,
Y: 0, Y: 0,
@ -321,17 +448,23 @@ func TestPainter(t *testing.T) {
X: 0, X: 0,
Y: 0, Y: 0,
}, },
}, chart.Style{
FillColor: drawing.Color{
R: 84,
G: 112,
B: 198,
A: 255,
},
}) })
}, },
result: "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"400\" height=\"300\">\\n<path d=\"M 5 10\nL 5 110\nL 105 110\nL 5 10\" style=\"stroke-width:0;stroke:none;fill:rgba(84,112,198,1.0)\"/></svg>", result: "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"400\" height=\"300\">\\n<path d=\"M 5 10\nL 5 110\nL 105 110\nL 5 10\" style=\"stroke-width:0;stroke:none;fill:rgba(84,112,198,1.0)\"/></svg>",
}, },
} }
for _, tt := range tests { for _, tt := range tests {
d, err := NewPainter(PainterOptions{ d, err := NewDraw(DrawOption{
Width: 400, Width: 400,
Height: 300, Height: 300,
Type: ChartOutputSVG, }, PaddingOption(chart.Box{
}, PainterPaddingOption(chart.Box{
Left: 5, Left: 5,
Top: 10, Top: 10,
})) }))
@ -343,57 +476,32 @@ func TestPainter(t *testing.T) {
} }
} }
func TestRoundedRect(t *testing.T) { func TestDrawTextFit(t *testing.T) {
assert := assert.New(t) assert := assert.New(t)
p, err := NewPainter(PainterOptions{ d, err := NewDraw(DrawOption{
Width: 400, Width: 400,
Height: 300, Height: 300,
Type: ChartOutputSVG,
}) })
assert.Nil(err) assert.Nil(err)
p.OverrideDrawingStyle(Style{ f, _ := chart.GetDefaultFont()
FillColor: drawing.ColorWhite, style := chart.Style{
StrokeWidth: 1,
StrokeColor: drawing.ColorWhite,
}).RoundedRect(Box{
Left: 10,
Right: 30,
Bottom: 150,
Top: 10,
}, 5)
buf, err := p.Bytes()
assert.Nil(err)
assert.Equal("<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"400\" height=\"300\">\\n<path d=\"M 15 10\nL 25 10\nL 25 10\nA 5 5 90.00 0 1 30 15\nL 30 145\nL 30 145\nA 5 5 90.00 0 1 25 150\nL 15 150\nL 15 150\nA 5 5 90.00 0 1 10 145\nL 10 15\nL 10 15\nA 5 5 90.00 0 1 15 10\nZ\" style=\"stroke-width:1;stroke:rgba(255,255,255,1.0);fill:rgba(255,255,255,1.0)\"/><path d=\"\" style=\"stroke-width:1;stroke:rgba(255,255,255,1.0);fill:rgba(255,255,255,1.0)\"/></svg>", string(buf))
}
func TestPainterTextFit(t *testing.T) {
assert := assert.New(t)
p, err := NewPainter(PainterOptions{
Width: 400,
Height: 300,
Type: ChartOutputSVG,
})
assert.Nil(err)
f, _ := GetDefaultFont()
style := Style{
FontSize: 12, FontSize: 12,
FontColor: chart.ColorBlack, FontColor: chart.ColorBlack,
Font: f, Font: f,
} }
p.SetStyle(style) box := d.textFit("Hello World!", 0, 20, 80, style)
box := p.TextFit("Hello World!", 0, 20, 80)
assert.Equal(chart.Box{ assert.Equal(chart.Box{
Right: 45, Right: 45,
Bottom: 35, Bottom: 35,
}, box) }, box)
box = p.TextFit("Hello World!", 0, 100, 200) box = d.textFit("Hello World!", 0, 100, 200, style)
assert.Equal(chart.Box{ assert.Equal(chart.Box{
Right: 84, Right: 84,
Bottom: 15, Bottom: 15,
}, box) }, box)
buf, err := p.Bytes() buf, err := d.Bytes()
assert.Nil(err) assert.Nil(err)
assert.Equal(`<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="400" height="300">\n<text x="0" y="20" style="stroke-width:0;stroke:none;fill:rgba(51,51,51,1.0);font-size:15.3px;font-family:'Roboto Medium',sans-serif">Hello</text><text x="0" y="40" style="stroke-width:0;stroke:none;fill:rgba(51,51,51,1.0);font-size:15.3px;font-family:'Roboto Medium',sans-serif">World!</text><text x="0" y="100" style="stroke-width:0;stroke:none;fill:rgba(51,51,51,1.0);font-size:15.3px;font-family:'Roboto Medium',sans-serif">Hello World!</text></svg>`, string(buf)) assert.Equal(`<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="400" height="300">\n<text x="0" y="20" style="stroke-width:0;stroke:none;fill:rgba(51,51,51,1.0);font-size:15.3px;font-family:'Roboto Medium',sans-serif">Hello</text><text x="0" y="40" style="stroke-width:0;stroke:none;fill:rgba(51,51,51,1.0);font-size:15.3px;font-family:'Roboto Medium',sans-serif">World!</text><text x="0" y="100" style="stroke-width:0;stroke:none;fill:rgba(51,51,51,1.0);font-size:15.3px;font-family:'Roboto Medium',sans-serif">Hello World!</text></svg>`, string(buf))
} }

View file

@ -29,7 +29,7 @@ import (
"regexp" "regexp"
"strconv" "strconv"
"git.smarteching.com/zeni/go-chart/v2" "github.com/wcharczuk/go-chart/v2"
) )
func convertToArray(data []byte) []byte { func convertToArray(data []byte) []byte {
@ -60,9 +60,9 @@ type EChartStyle struct {
Color string `json:"color"` Color string `json:"color"`
} }
func (es *EChartStyle) ToStyle() Style { func (es *EChartStyle) ToStyle() chart.Style {
color := parseColor(es.Color) color := parseColor(es.Color)
return Style{ return chart.Style{
FillColor: color, FillColor: color,
FontColor: color, FontColor: color,
StrokeColor: color, StrokeColor: color,
@ -130,7 +130,6 @@ type EChartsXAxisData struct {
BoundaryGap *bool `json:"boundaryGap"` BoundaryGap *bool `json:"boundaryGap"`
SplitNumber int `json:"splitNumber"` SplitNumber int `json:"splitNumber"`
Data []string `json:"data"` Data []string `json:"data"`
Type string `json:"type"`
} }
type EChartsXAxis struct { type EChartsXAxis struct {
Data []EChartsXAxisData Data []EChartsXAxisData
@ -156,7 +155,6 @@ type EChartsYAxisData struct {
Color string `json:"color"` Color string `json:"color"`
} `json:"lineStyle"` } `json:"lineStyle"`
} `json:"axisLine"` } `json:"axisLine"`
Data []string `json:"data"`
} }
type EChartsYAxis struct { type EChartsYAxis struct {
Data []EChartsYAxisData `json:"data"` Data []EChartsYAxisData `json:"data"`
@ -344,11 +342,6 @@ func (esList EChartsSeriesList) ToSeriesList() SeriesList {
Data: NewSeriesDataFromValues(dataItem.Value.values), Data: NewSeriesDataFromValues(dataItem.Value.values),
Max: item.Max, Max: item.Max,
Min: item.Min, Min: item.Min,
Label: SeriesLabel{
Color: parseColor(item.Label.Color),
Show: item.Label.Show,
Distance: item.Label.Distance,
},
}) })
} }
continue continue
@ -361,10 +354,10 @@ func (esList EChartsSeriesList) ToSeriesList() SeriesList {
} }
} }
seriesList = append(seriesList, Series{ seriesList = append(seriesList, Series{
Type: item.Type, Type: item.Type,
Data: data, Data: data,
AxisIndex: item.YAxisIndex, YAxisIndex: item.YAxisIndex,
Style: item.ItemStyle.ToStyle(), Style: item.ItemStyle.ToStyle(),
Label: SeriesLabel{ Label: SeriesLabel{
Color: parseColor(item.Label.Color), Color: parseColor(item.Label.Color),
Show: item.Label.Show, Show: item.Label.Show,
@ -426,32 +419,26 @@ func (eo *EChartsOption) ToOption() ChartOption {
if len(fontFamily) == 0 { if len(fontFamily) == 0 {
fontFamily = eo.Title.TextStyle.FontFamily fontFamily = eo.Title.TextStyle.FontFamily
} }
titleTextStyle := eo.Title.TextStyle.ToStyle()
titleSubtextStyle := eo.Title.SubtextStyle.ToStyle()
legendTextStyle := eo.Legend.TextStyle.ToStyle()
o := ChartOption{ o := ChartOption{
Type: eo.Type, Type: eo.Type,
FontFamily: fontFamily, FontFamily: fontFamily,
Theme: eo.Theme, Theme: eo.Theme,
Title: TitleOption{ Title: TitleOption{
Text: eo.Title.Text, Text: eo.Title.Text,
Subtext: eo.Title.Subtext, Subtext: eo.Title.Subtext,
FontColor: titleTextStyle.FontColor, Style: eo.Title.TextStyle.ToStyle(),
FontSize: titleTextStyle.FontSize, SubtextStyle: eo.Title.SubtextStyle.ToStyle(),
SubtextFontSize: titleSubtextStyle.FontSize, Left: string(eo.Title.Left),
SubtextFontColor: titleSubtextStyle.FontColor, Top: string(eo.Title.Top),
Left: string(eo.Title.Left),
Top: string(eo.Title.Top),
}, },
Legend: LegendOption{ Legend: LegendOption{
Show: eo.Legend.Show, Show: eo.Legend.Show,
FontSize: legendTextStyle.FontSize, Style: eo.Legend.TextStyle.ToStyle(),
FontColor: legendTextStyle.FontColor, Data: eo.Legend.Data,
Data: eo.Legend.Data, Left: string(eo.Legend.Left),
Left: string(eo.Legend.Left), Top: string(eo.Legend.Top),
Top: string(eo.Legend.Top), Align: eo.Legend.Align,
Align: eo.Legend.Align, Orient: eo.Legend.Orient,
Orient: eo.Legend.Orient,
}, },
RadarIndicators: eo.Radar.Indicator, RadarIndicators: eo.Radar.Indicator,
Width: eo.Width, Width: eo.Width,
@ -460,21 +447,6 @@ func (eo *EChartsOption) ToOption() ChartOption {
Box: eo.Box, Box: eo.Box,
SeriesList: eo.Series.ToSeriesList(), SeriesList: eo.Series.ToSeriesList(),
} }
isHorizontalChart := false
for _, item := range eo.XAxis.Data {
if item.Type == "value" {
isHorizontalChart = true
}
}
if isHorizontalChart {
for index := range o.SeriesList {
series := o.SeriesList[index]
if series.Type == ChartTypeBar {
o.SeriesList[index].Type = ChartTypeHorizontalBar
}
}
}
if len(eo.XAxis.Data) != 0 { if len(eo.XAxis.Data) != 0 {
xAxisData := eo.XAxis.Data[0] xAxisData := eo.XAxis.Data[0]
o.XAxis = XAxisOption{ o.XAxis = XAxisOption{
@ -490,10 +462,9 @@ func (eo *EChartsOption) ToOption() ChartOption {
Max: item.Max, Max: item.Max,
Formatter: item.AxisLabel.Formatter, Formatter: item.AxisLabel.Formatter,
Color: parseColor(item.AxisLine.LineStyle.Color), Color: parseColor(item.AxisLine.LineStyle.Color),
Data: item.Data,
} }
} }
o.YAxisOptions = yAxisOptions o.YAxisList = yAxisOptions
if len(eo.Children) != 0 { if len(eo.Children) != 0 {
o.Children = make([]ChartOption, len(eo.Children)) o.Children = make([]ChartOption, len(eo.Children))

File diff suppressed because one or more lines are too long

View file

@ -1,73 +0,0 @@
package main
import (
"os"
"path/filepath"
"git.smarteching.com/zeni/go-charts/v2"
)
func writeFile(buf []byte) error {
tmpPath := "./tmp"
err := os.MkdirAll(tmpPath, 0700)
if err != nil {
return err
}
file := filepath.Join(tmpPath, "area-line-chart.png")
err = os.WriteFile(file, buf, 0600)
if err != nil {
return err
}
return nil
}
func main() {
values := [][]float64{
{
120,
132,
101,
134,
90,
230,
210,
},
}
p, err := charts.LineRender(
values,
charts.TitleTextOptionFunc("Line"),
charts.XAxisDataOptionFunc([]string{
"Mon",
"Tue",
"Wed",
"Thu",
"Fri",
"Sat",
"Sun",
}),
charts.LegendLabelsOptionFunc([]string{
"Email",
}, "50"),
func(opt *charts.ChartOption) {
opt.Legend.Padding = charts.Box{
Top: 5,
Bottom: 10,
}
opt.FillArea = true
},
)
if err != nil {
panic(err)
}
buf, err := p.Bytes()
if err != nil {
panic(err)
}
err = writeFile(buf)
if err != nil {
panic(err)
}
}

View file

@ -1,102 +0,0 @@
package main
import (
"os"
"path/filepath"
"git.smarteching.com/zeni/go-charts/v2"
)
func writeFile(buf []byte) error {
tmpPath := "./tmp"
err := os.MkdirAll(tmpPath, 0700)
if err != nil {
return err
}
file := filepath.Join(tmpPath, "bar-chart.png")
err = os.WriteFile(file, buf, 0600)
if err != nil {
return err
}
return nil
}
func main() {
values := [][]float64{
{
2.0,
4.9,
7.0,
23.2,
25.6,
76.7,
135.6,
162.2,
32.6,
20.0,
6.4,
3.3,
},
{
2.6,
5.9,
9.0,
26.4,
28.7,
70.7,
175.6,
182.2,
48.7,
18.8,
6.0,
2.3,
},
}
p, err := charts.BarRender(
values,
charts.XAxisDataOptionFunc([]string{
"Jan",
"Feb",
"Mar",
"Apr",
"May",
"Jun",
"Jul",
"Aug",
"Sep",
"Oct",
"Nov",
"Dec",
}),
charts.LegendLabelsOptionFunc([]string{
"Rainfall",
"Evaporation",
}, charts.PositionRight),
charts.MarkLineOptionFunc(0, charts.SeriesMarkDataTypeAverage),
charts.MarkPointOptionFunc(0, charts.SeriesMarkDataTypeMax,
charts.SeriesMarkDataTypeMin),
// custom option func
func(opt *charts.ChartOption) {
opt.SeriesList[1].MarkPoint = charts.NewMarkPoint(
charts.SeriesMarkDataTypeMax,
charts.SeriesMarkDataTypeMin,
)
opt.SeriesList[1].MarkLine = charts.NewMarkLine(
charts.SeriesMarkDataTypeAverage,
)
},
)
if err != nil {
panic(err)
}
buf, err := p.Bytes()
if err != nil {
panic(err)
}
err = writeFile(buf)
if err != nil {
panic(err)
}
}

94
examples/basic/main.go Normal file
View file

@ -0,0 +1,94 @@
package main
import (
"io/ioutil"
"os"
"path/filepath"
charts "github.com/vicanso/go-charts"
)
func writeFile(file string, buf []byte) error {
tmpPath := "./tmp"
err := os.MkdirAll(tmpPath, 0700)
if err != nil {
return err
}
file = filepath.Join(tmpPath, file)
err = ioutil.WriteFile(file, buf, 0600)
if err != nil {
return err
}
return nil
}
func chartsRender() ([]byte, error) {
d, err := charts.LineRender([][]float64{
{
150,
230,
224,
218,
135,
147,
260,
},
},
// output type
charts.PNGTypeOption(),
// title
charts.TitleOptionFunc(charts.TitleOption{
Text: "Line",
}),
// x axis
charts.XAxisOptionFunc(charts.NewXAxisOption([]string{
"Mon",
"Tue",
"Wed",
"Thu",
"Fri",
"Sat",
"Sun",
})),
)
if err != nil {
return nil, err
}
return d.Bytes()
}
func echartsRender() ([]byte, error) {
return charts.RenderEChartsToPNG(`{
"title": {
"text": "Line"
},
"xAxis": {
"data": ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]
},
"series": [
{
"data": [150, 230, 224, 218, 135, 147, 260]
}
]
}`)
}
type Render func() ([]byte, error)
func main() {
m := map[string]Render{
"charts-line.png": chartsRender,
"echarts-line.png": echartsRender,
}
for name, fn := range m {
buf, err := fn()
if err != nil {
panic(err)
}
err = writeFile(name, buf)
if err != nil {
panic(err)
}
}
}

View file

@ -2,11 +2,12 @@ package main
import ( import (
"bytes" "bytes"
"fmt"
"net/http" "net/http"
"strconv" "strconv"
charts "git.smarteching.com/zeni/go-charts/v2" charts "github.com/vicanso/go-charts"
"github.com/wcharczuk/go-chart/v2"
"github.com/wcharczuk/go-chart/v2/drawing"
) )
var html = `<!DOCTYPE html> var html = `<!DOCTYPE html>
@ -74,7 +75,6 @@ func handler(w http.ResponseWriter, req *http.Request, chartOptions []charts.Cha
bytesList := make([][]byte, 0) bytesList := make([][]byte, 0)
for _, opt := range chartOptions { for _, opt := range chartOptions {
opt.Theme = theme opt.Theme = theme
opt.Type = charts.ChartOutputSVG
d, err := charts.Render(opt) d, err := charts.Render(opt)
if err != nil { if err != nil {
panic(err) panic(err)
@ -93,48 +93,6 @@ func handler(w http.ResponseWriter, req *http.Request, chartOptions []charts.Cha
bytesList = append(bytesList, buf) bytesList = append(bytesList, buf)
} }
p, err := charts.TableOptionRender(charts.TableChartOption{
Type: charts.ChartOutputSVG,
Header: []string{
"Name",
"Age",
"Address",
"Tag",
"Action",
},
Data: [][]string{
{
"John Brown",
"32",
"New York No. 1 Lake Park",
"nice, developer",
"Send Mail",
},
{
"Jim Green ",
"42",
"London No. 1 Lake Park",
"wow",
"Send Mail",
},
{
"Joe Black ",
"32",
"Sidney No. 1 Lake Park",
"cool, teacher",
"Send Mail",
},
},
})
if err != nil {
panic(err)
}
buf, err := p.Bytes()
if err != nil {
panic(err)
}
bytesList = append(bytesList, buf)
data := bytes.ReplaceAll([]byte(html), []byte("{{body}}"), bytes.Join(bytesList, []byte(""))) data := bytes.ReplaceAll([]byte(html), []byte("{{body}}"), bytes.Join(bytesList, []byte("")))
w.Header().Set("Content-Type", "text/html") w.Header().Set("Content-Type", "text/html")
w.Write(data) w.Write(data)
@ -142,6 +100,7 @@ func handler(w http.ResponseWriter, req *http.Request, chartOptions []charts.Cha
func indexHandler(w http.ResponseWriter, req *http.Request) { func indexHandler(w http.ResponseWriter, req *http.Request) {
chartOptions := []charts.ChartOption{ chartOptions := []charts.ChartOption{
// 普通折线图
{ {
Title: charts.TitleOption{ Title: charts.TitleOption{
Text: "Line", Text: "Line",
@ -215,7 +174,7 @@ func indexHandler(w http.ResponseWriter, req *http.Request) {
Title: charts.TitleOption{ Title: charts.TitleOption{
Text: "Temperature Change in the Coming Week", Text: "Temperature Change in the Coming Week",
}, },
Padding: charts.Box{ Padding: chart.Box{
Top: 20, Top: 20,
Left: 20, Left: 20,
Right: 30, Right: 30,
@ -262,35 +221,6 @@ func indexHandler(w http.ResponseWriter, req *http.Request) {
}, },
}, },
}, },
{
Title: charts.TitleOption{
Text: "Line Area",
},
Legend: charts.NewLegendOption([]string{
"Email",
}),
XAxis: charts.NewXAxisOption([]string{
"Mon",
"Tue",
"Wed",
"Thu",
"Fri",
"Sat",
"Sun",
}),
SeriesList: []charts.Series{
charts.NewSeriesFromValues([]float64{
120,
132,
101,
134,
90,
230,
210,
}),
},
FillArea: true,
},
// 柱状图 // 柱状图
{ {
Title: charts.TitleOption{ Title: charts.TitleOption{
@ -310,7 +240,7 @@ func indexHandler(w http.ResponseWriter, req *http.Request) {
"Rainfall", "Rainfall",
"Evaporation", "Evaporation",
}, },
Icon: charts.IconRect, Icon: charts.LegendIconRect,
}, },
SeriesList: []charts.Series{ SeriesList: []charts.Series{
charts.NewSeriesFromValues([]float64{ charts.NewSeriesFromValues([]float64{
@ -330,8 +260,8 @@ func indexHandler(w http.ResponseWriter, req *http.Request) {
}, },
{ {
Value: 190, Value: 190,
Style: charts.Style{ Style: chart.Style{
FillColor: charts.Color{ FillColor: drawing.Color{
R: 169, R: 169,
G: 0, G: 0,
B: 0, B: 0,
@ -355,68 +285,16 @@ func indexHandler(w http.ResponseWriter, req *http.Request) {
Value: 180, Value: 180,
}, },
}, },
Label: charts.SeriesLabel{
Show: true,
Position: charts.PositionBottom,
},
}, },
}, },
}, },
// 水平柱状图 // 柱状图+mark
{
Title: charts.TitleOption{
Text: "World Population",
},
Padding: charts.Box{
Top: 20,
Right: 40,
Bottom: 20,
Left: 20,
},
Legend: charts.NewLegendOption([]string{
"2011",
"2012",
}),
YAxisOptions: charts.NewYAxisOptions([]string{
"Brazil",
"Indonesia",
"USA",
"India",
"China",
"World",
}),
SeriesList: []charts.Series{
{
Type: charts.ChartTypeHorizontalBar,
Data: charts.NewSeriesDataFromValues([]float64{
18203,
23489,
29034,
104970,
131744,
630230,
}),
},
{
Type: charts.ChartTypeHorizontalBar,
Data: charts.NewSeriesDataFromValues([]float64{
19325,
23438,
31000,
121594,
134141,
681807,
}),
},
},
},
// 柱状图+标记
{ {
Title: charts.TitleOption{ Title: charts.TitleOption{
Text: "Rainfall vs Evaporation", Text: "Rainfall vs Evaporation",
Subtext: "Fake Data", Subtext: "Fake Data",
}, },
Padding: charts.Box{ Padding: chart.Box{
Top: 20, Top: 20,
Right: 20, Right: 20,
Bottom: 20, Bottom: 20,
@ -493,9 +371,6 @@ func indexHandler(w http.ResponseWriter, req *http.Request) {
}, },
// 双Y轴示例 // 双Y轴示例
{ {
Title: charts.TitleOption{
Text: "Temperature",
},
XAxis: charts.NewXAxisOption([]string{ XAxis: charts.NewXAxisOption([]string{
"Jan", "Jan",
"Feb", "Feb",
@ -515,22 +390,22 @@ func indexHandler(w http.ResponseWriter, req *http.Request) {
"Precipitation", "Precipitation",
"Temperature", "Temperature",
}), }),
YAxisOptions: []charts.YAxisOption{ YAxisList: []charts.YAxisOption{
{ {
Formatter: "{value}ml", Formatter: "{value}°C",
Color: charts.Color{ Color: drawing.Color{
R: 84, R: 250,
G: 112, G: 200,
B: 198, B: 88,
A: 255, A: 255,
}, },
}, },
{ {
Formatter: "{value}°C", Formatter: "{value}ml",
Color: charts.Color{ Color: drawing.Color{
R: 250, R: 84,
G: 200, G: 112,
B: 88, B: 198,
A: 255, A: 255,
}, },
}, },
@ -551,7 +426,9 @@ func indexHandler(w http.ResponseWriter, req *http.Request) {
20.0, 20.0,
6.4, 6.4,
3.3, 3.3,
10.2,
}), }),
YAxisIndex: 1,
}, },
{ {
Type: charts.ChartTypeBar, Type: charts.ChartTypeBar,
@ -568,7 +445,9 @@ func indexHandler(w http.ResponseWriter, req *http.Request) {
18.8, 18.8,
6.0, 6.0,
2.3, 2.3,
20.2,
}), }),
YAxisIndex: 1,
}, },
{ {
Data: charts.NewSeriesDataFromValues([]float64{ Data: charts.NewSeriesDataFromValues([]float64{
@ -584,8 +463,8 @@ func indexHandler(w http.ResponseWriter, req *http.Request) {
16.5, 16.5,
12.0, 12.0,
6.2, 6.2,
30.3,
}), }),
AxisIndex: 1,
}, },
}, },
}, },
@ -693,20 +572,6 @@ func indexHandler(w http.ResponseWriter, req *http.Request) {
"Order", "Order",
}), }),
SeriesList: []charts.Series{ SeriesList: []charts.Series{
{
Type: charts.ChartTypeFunnel,
Name: "Show",
Data: charts.NewSeriesDataFromValues([]float64{
100,
}),
},
{
Type: charts.ChartTypeFunnel,
Name: "Click",
Data: charts.NewSeriesDataFromValues([]float64{
80,
}),
},
{ {
Type: charts.ChartTypeFunnel, Type: charts.ChartTypeFunnel,
Name: "Visit", Name: "Visit",
@ -728,6 +593,20 @@ func indexHandler(w http.ResponseWriter, req *http.Request) {
20, 20,
}), }),
}, },
{
Type: charts.ChartTypeFunnel,
Name: "Click",
Data: charts.NewSeriesDataFromValues([]float64{
80,
}),
},
{
Type: charts.ChartTypeFunnel,
Name: "Show",
Data: charts.NewSeriesDataFromValues([]float64{
100,
}),
},
}, },
}, },
// 多图展示 // 多图展示
@ -741,7 +620,7 @@ func indexHandler(w http.ResponseWriter, req *http.Request) {
"Walnut Brownie", "Walnut Brownie",
}, },
}, },
Padding: charts.Box{ Padding: chart.Box{
Top: 100, Top: 100,
Right: 10, Right: 10,
Bottom: 10, Bottom: 10,
@ -755,7 +634,7 @@ func indexHandler(w http.ResponseWriter, req *http.Request) {
"2016", "2016",
"2017", "2017",
}), }),
YAxisOptions: []charts.YAxisOption{ YAxisList: []charts.YAxisOption{
{ {
Min: charts.NewFloatPoint(0), Min: charts.NewFloatPoint(0),
@ -807,7 +686,7 @@ func indexHandler(w http.ResponseWriter, req *http.Request) {
"Walnut Brownie", "Walnut Brownie",
}, },
}, },
Box: charts.Box{ Box: chart.Box{
Top: 20, Top: 20,
Left: 400, Left: 400,
Right: 500, Right: 500,
@ -1132,64 +1011,6 @@ func echartsHandler(w http.ResponseWriter, req *http.Request) {
} }
] ]
}`, }`,
`{
"title": {
"text": "World Population"
},
"tooltip": {
"trigger": "axis",
"axisPointer": {
"type": "shadow"
}
},
"legend": {},
"grid": {
"left": "3%",
"right": "4%",
"bottom": "3%",
"containLabel": true
},
"xAxis": {
"type": "value"
},
"yAxis": {
"type": "category",
"data": [
"Brazil",
"Indonesia",
"USA",
"India",
"China",
"World"
]
},
"series": [
{
"name": "2011",
"type": "bar",
"data": [
18203,
23489,
29034,
104970,
131744,
630230
]
},
{
"name": "2012",
"type": "bar",
"data": [
19325,
23438,
31000,
121594,
134141,
681807
]
}
]
}`,
`{ `{
"title": { "title": {
"text": "Rainfall vs Evaporation", "text": "Rainfall vs Evaporation",
@ -1351,7 +1172,12 @@ func echartsHandler(w http.ResponseWriter, req *http.Request) {
23.2, 23.2,
25.6, 25.6,
76.7, 76.7,
135.6 135.6,
162.2,
32.6,
20,
6.4,
3.3
] ]
}, },
{ {
@ -1365,7 +1191,12 @@ func echartsHandler(w http.ResponseWriter, req *http.Request) {
26.4, 26.4,
28.7, 28.7,
70.7, 70.7,
175.6 175.6,
182.2,
48.7,
18.8,
6,
2.3
] ]
}, },
{ {
@ -1380,7 +1211,12 @@ func echartsHandler(w http.ResponseWriter, req *http.Request) {
4.5, 4.5,
6.3, 6.3,
10.2, 10.2,
20.3 20.3,
23.4,
23,
16.5,
12,
6.2
] ]
} }
] ]
@ -1969,6 +1805,5 @@ func echartsHandler(w http.ResponseWriter, req *http.Request) {
func main() { func main() {
http.HandleFunc("/", indexHandler) http.HandleFunc("/", indexHandler)
http.HandleFunc("/echarts", echartsHandler) http.HandleFunc("/echarts", echartsHandler)
fmt.Println("http://127.0.0.1:3012/")
http.ListenAndServe(":3012", nil) http.ListenAndServe(":3012", nil)
} }

View file

@ -2,119 +2,49 @@ package main
import ( import (
"io/ioutil" "io/ioutil"
"os" "log"
"path/filepath"
"git.smarteching.com/zeni/go-charts/v2" charts "github.com/vicanso/go-charts"
) )
func writeFile(buf []byte) error { func echartsRender() ([]byte, error) {
tmpPath := "./tmp" return charts.RenderEChartsToPNG(`{
err := os.MkdirAll(tmpPath, 0700) "title": {
if err != nil { "text": "用户访问次数",
return err "textStyle": {
} "fontFamily": "chinese"
}
file := filepath.Join(tmpPath, "chinese-line-chart.png") },
err = os.WriteFile(file, buf, 0600) "xAxis": {
if err != nil { "data": ["周一", "周二", "周三", "周四", "周五", "周六", "周日"]
return err },
} "series": [
return nil {
"data": [150, 230, 224, 218, 135, 147, 260],
"label": {
"show": true
}
}
]
}`)
} }
func main() { func main() {
// 字体文件需要自行下载 fontData, err := ioutil.ReadFile("/Users/darcy/Downloads/NotoSansCJKsc-VF.ttf")
// https://github.com/googlefonts/noto-cjk
buf, err := ioutil.ReadFile("./NotoSansSC.ttf")
if err != nil { if err != nil {
panic(err) log.Fatalln("Error when reading font file:", err)
}
err = charts.InstallFont("noto", buf)
if err != nil {
panic(err)
}
font, _ := charts.GetFont("noto")
charts.SetDefaultFont(font)
values := [][]float64{
{
120,
132,
101,
134,
90,
230,
210,
},
{
220,
182,
191,
234,
290,
330,
310,
},
{
150,
232,
201,
154,
190,
330,
410,
},
{
320,
332,
301,
334,
390,
330,
320,
},
{
820,
932,
901,
934,
1290,
1330,
1320,
},
}
p, err := charts.LineRender(
values,
charts.TitleTextOptionFunc("测试"),
charts.XAxisDataOptionFunc([]string{
"星期一",
"星期二",
"星期三",
"星期四",
"星期五",
"星期六",
"星期日",
}),
charts.LegendLabelsOptionFunc([]string{
"邮件",
"广告",
"视频广告",
"直接访问",
"搜索引擎",
}, charts.PositionCenter),
)
if err != nil {
panic(err)
} }
buf, err = p.Bytes() if err := charts.InstallFont("chinese", fontData); err != nil {
if err != nil { log.Fatalln("Error when instaling font:", err)
panic(err)
} }
err = writeFile(buf)
fileData, err := echartsRender()
if err != nil { if err != nil {
panic(err) log.Fatalln("Error when rendering image:", err)
}
if err := ioutil.WriteFile("chinese.png", fileData, 0644); err != nil {
log.Fatalln("Error when save image to chinese.png:", err)
} }
} }

View file

@ -1,60 +0,0 @@
package main
import (
"os"
"path/filepath"
"git.smarteching.com/zeni/go-charts/v2"
)
func writeFile(buf []byte) error {
tmpPath := "./tmp"
err := os.MkdirAll(tmpPath, 0700)
if err != nil {
return err
}
file := filepath.Join(tmpPath, "funnel-chart.png")
err = os.WriteFile(file, buf, 0600)
if err != nil {
return err
}
return nil
}
func main() {
values := []float64{
100,
80,
60,
40,
20,
10,
0,
}
p, err := charts.FunnelRender(
values,
charts.TitleTextOptionFunc("Funnel"),
charts.LegendLabelsOptionFunc([]string{
"Show",
"Click",
"Visit",
"Inquiry",
"Order",
"Pay",
"Cancel",
}),
)
if err != nil {
panic(err)
}
buf, err := p.Bytes()
if err != nil {
panic(err)
}
err = writeFile(buf)
if err != nil {
panic(err)
}
}

View file

@ -1,84 +0,0 @@
package main
import (
"os"
"path/filepath"
"git.smarteching.com/zeni/go-charts/v2"
)
func writeFile(buf []byte) error {
tmpPath := "./tmp"
err := os.MkdirAll(tmpPath, 0700)
if err != nil {
return err
}
file := filepath.Join(tmpPath, "horizontal-bar-chart.png")
err = os.WriteFile(file, buf, 0600)
if err != nil {
return err
}
return nil
}
func main() {
values := [][]float64{
{
10,
30,
50,
70,
90,
110,
130,
},
{
20,
40,
60,
80,
100,
120,
140,
},
}
p, err := charts.HorizontalBarRender(
values,
charts.TitleTextOptionFunc("World Population"),
charts.PaddingOptionFunc(charts.Box{
Top: 20,
Right: 40,
Bottom: 20,
Left: 20,
}),
charts.LegendLabelsOptionFunc([]string{
"2011",
"2012",
}),
charts.YAxisDataOptionFunc([]string{
"UN",
"Brazil",
"Indonesia",
"USA",
"India",
"China",
"World",
}),
func(opt *charts.ChartOption) {
opt.SeriesList[0].RoundRadius = 5
},
)
if err != nil {
panic(err)
}
buf, err := p.Bytes()
if err != nil {
panic(err)
}
err = writeFile(buf)
if err != nil {
panic(err)
}
}

View file

@ -1,124 +0,0 @@
package main
import (
"fmt"
"os"
"path/filepath"
"git.smarteching.com/zeni/go-charts/v2"
)
func writeFile(buf []byte) error {
tmpPath := "./tmp"
err := os.MkdirAll(tmpPath, 0700)
if err != nil {
return err
}
file := filepath.Join(tmpPath, "line-chart.png")
err = os.WriteFile(file, buf, 0600)
if err != nil {
return err
}
return nil
}
func main() {
values := [][]float64{
{
120,
132,
101,
// 134,
charts.GetNullValue(),
90,
230,
210,
},
{
220,
182,
191,
234,
290,
330,
310,
},
{
150,
232,
201,
154,
190,
330,
410,
},
{
320,
332,
301,
334,
390,
330,
320,
},
{
820,
932,
901,
934,
1290,
1330,
1320,
},
}
p, err := charts.LineRender(
values,
charts.TitleTextOptionFunc("Line"),
charts.XAxisDataOptionFunc([]string{
"Mon",
"Tue",
"Wed",
"Thu",
"Fri",
"Sat",
"Sun",
}),
charts.LegendLabelsOptionFunc([]string{
"Email",
"Union Ads",
"Video Ads",
"Direct",
"Search Engine",
}, "50"),
func(opt *charts.ChartOption) {
opt.Legend.Padding = charts.Box{
Top: 5,
Bottom: 10,
}
opt.YAxisOptions = []charts.YAxisOption{
{
SplitLineShow: charts.FalseFlag(),
},
}
opt.SymbolShow = charts.FalseFlag()
opt.LineStrokeWidth = 1
opt.ValueFormatter = func(f float64) string {
return fmt.Sprintf("%.0f", f)
}
},
)
if err != nil {
panic(err)
}
buf, err := p.Bytes()
if err != nil {
panic(err)
}
err = writeFile(buf)
if err != nil {
panic(err)
}
}

View file

@ -1,607 +0,0 @@
package main
import (
"os"
"path/filepath"
charts "git.smarteching.com/zeni/go-charts/v2"
"git.smarteching.com/zeni/go-chart/v2/drawing"
)
func writeFile(buf []byte) error {
tmpPath := "./tmp"
err := os.MkdirAll(tmpPath, 0700)
if err != nil {
return err
}
file := filepath.Join(tmpPath, "painter.png")
err = os.WriteFile(file, buf, 0600)
if err != nil {
return err
}
return nil
}
func main() {
p, err := charts.NewPainter(charts.PainterOptions{
Width: 600,
Height: 2000,
Type: charts.ChartOutputPNG,
})
if err != nil {
panic(err)
}
// 背景色
p.SetBackground(p.Width(), p.Height(), drawing.ColorWhite)
top := 0
// 画线
p.SetDrawingStyle(charts.Style{
StrokeColor: drawing.ColorBlack,
FillColor: drawing.ColorBlack,
StrokeWidth: 1,
})
p.LineStroke([]charts.Point{
{
X: 0,
Y: 0,
},
{
X: 100,
Y: 10,
},
{
X: 200,
Y: 0,
},
{
X: 300,
Y: 10,
},
})
// 圆滑曲线
// top += 50
// p.Child(charts.PainterPaddingOption(charts.Box{
// Top: top,
// })).SetDrawingStyle(charts.Style{
// StrokeColor: drawing.ColorBlack,
// FillColor: drawing.ColorBlack,
// StrokeWidth: 1,
// }).SmoothLineStroke([]charts.Point{
// {
// X: 0,
// Y: 0,
// },
// {
// X: 100,
// Y: 10,
// },
// {
// X: 200,
// Y: 0,
// },
// {
// X: 300,
// Y: 10,
// },
// })
// 标线
top += 50
p.Child(charts.PainterPaddingOption(charts.Box{
Top: top,
})).SetDrawingStyle(charts.Style{
StrokeColor: drawing.ColorBlack,
FillColor: drawing.ColorBlack,
StrokeWidth: 1,
StrokeDashArray: []float64{
4,
2,
},
}).MarkLine(0, 0, p.Width())
top += 60
// Polygon
p.Child(charts.PainterBoxOption(charts.Box{
Top: top,
})).SetDrawingStyle(charts.Style{
StrokeColor: drawing.ColorBlack,
FillColor: drawing.ColorBlack,
StrokeWidth: 1,
}).Polygon(charts.Point{
X: 100,
Y: 0,
}, 50, 6)
// FillArea
top += 60
p.Child(charts.PainterPaddingOption(charts.Box{
Top: top,
})).SetDrawingStyle(charts.Style{
FillColor: drawing.ColorBlack,
}).FillArea([]charts.Point{
{
X: 0,
Y: 0,
},
{
X: 100,
Y: 0,
},
{
X: 150,
Y: 40,
},
{
X: 80,
Y: 30,
},
{
X: 0,
Y: 0,
},
})
// 坐标轴的点
top += 50
p.Child(
charts.PainterBoxOption(charts.Box{
Top: top,
Left: 1,
Right: p.Width() - 1,
Bottom: 20,
}),
).SetDrawingStyle(charts.Style{
StrokeColor: drawing.ColorBlack,
FillColor: drawing.ColorBlack,
StrokeWidth: 1,
}).Ticks(charts.TicksOption{
Count: 7,
Length: 5,
})
// 坐标轴的点每2格显示一个
top += 20
p.Child(
charts.PainterBoxOption(charts.Box{
Top: top,
Left: 1,
Right: p.Width() - 1,
Bottom: 20,
}),
).SetDrawingStyle(charts.Style{
StrokeColor: drawing.ColorBlack,
FillColor: drawing.ColorBlack,
StrokeWidth: 1,
}).Ticks(charts.TicksOption{
Unit: 2,
Count: 7,
Length: 5,
})
// 坐标轴的点,纵向
top += 20
p.Child(
charts.PainterBoxOption(charts.Box{
Top: top,
Left: 1,
Right: p.Width() - 1,
Bottom: top + 100,
}),
).SetDrawingStyle(charts.Style{
StrokeColor: drawing.ColorBlack,
FillColor: drawing.ColorBlack,
StrokeWidth: 1,
}).Ticks(charts.TicksOption{
Orient: charts.OrientVertical,
Count: 7,
Length: 5,
})
// 横向展示文本
top += 120
p.Child(
charts.PainterBoxOption(charts.Box{
Top: top,
Left: 1,
Right: p.Width() - 1,
Bottom: 20,
}),
).OverrideTextStyle(charts.Style{
FontColor: drawing.ColorBlack,
FontSize: 10,
}).MultiText(charts.MultiTextOption{
TextList: []string{
"Mon",
"Tue",
"Wed",
"Thu",
"Fri",
"Sat",
"Sun",
},
})
// 横向显示文本,靠左
top += 20
p.Child(
charts.PainterBoxOption(charts.Box{
Top: top,
Left: 1,
Right: p.Width() - 1,
Bottom: 20,
}),
).OverrideTextStyle(charts.Style{
FontColor: drawing.ColorBlack,
FontSize: 10,
}).MultiText(charts.MultiTextOption{
Position: charts.PositionLeft,
TextList: []string{
"Mon",
"Tue",
"Wed",
"Thu",
"Fri",
"Sat",
"Sun",
},
})
// 纵向显示文本
top += 20
p.Child(
charts.PainterBoxOption(charts.Box{
Top: top,
Left: 1,
Right: 50,
Bottom: top + 150,
}),
).OverrideTextStyle(charts.Style{
FontColor: drawing.ColorBlack,
FontSize: 10,
}).MultiText(charts.MultiTextOption{
Orient: charts.OrientVertical,
Align: charts.AlignRight,
TextList: []string{
"Mon",
"Tue",
"Wed",
"Thu",
"Fri",
"Sat",
"Sun",
},
})
// 纵向 文本居中
p.Child(
charts.PainterBoxOption(charts.Box{
Top: top,
Left: 50,
Right: 100,
Bottom: top + 150,
}),
).OverrideTextStyle(charts.Style{
FontColor: drawing.ColorBlack,
FontSize: 10,
}).MultiText(charts.MultiTextOption{
Orient: charts.OrientVertical,
Align: charts.AlignCenter,
TextList: []string{
"Mon",
"Tue",
"Wed",
"Thu",
"Fri",
"Sat",
"Sun",
},
})
// 纵向 文本置顶
p.Child(
charts.PainterBoxOption(charts.Box{
Top: top,
Left: 100,
Right: 150,
Bottom: top + 150,
}),
).OverrideTextStyle(charts.Style{
FontColor: drawing.ColorBlack,
FontSize: 10,
}).MultiText(charts.MultiTextOption{
Orient: charts.OrientVertical,
Position: charts.PositionTop,
Align: charts.AlignCenter,
TextList: []string{
"Mon",
"Tue",
"Wed",
"Thu",
"Fri",
"Sat",
"Sun",
},
})
// grid
top += 150
p.Child(
charts.PainterBoxOption(charts.Box{
Top: top,
Left: 1,
Right: p.Width() - 1,
Bottom: top + 100,
}),
).OverrideTextStyle(charts.Style{
FontColor: drawing.ColorBlack,
FontSize: 10,
}).Grid(charts.GridOption{
Column: 8,
IgnoreColumnLines: []int{0, 8},
Row: 8,
IgnoreRowLines: []int{0, 8},
})
// dots
top += 100
p.Child(
charts.PainterBoxOption(charts.Box{
Top: top,
Left: 1,
Right: p.Width() - 1,
Bottom: top + 20,
}),
).OverrideDrawingStyle(charts.Style{
FillColor: drawing.ColorWhite,
StrokeColor: drawing.ColorBlack,
StrokeWidth: 1,
}).Dots([]charts.Point{
{
X: 0,
Y: 0,
},
{
X: 50,
Y: 0,
},
{
X: 100,
Y: 10,
},
})
// rect
top += 30
p.Child(
charts.PainterBoxOption(charts.Box{
Top: top,
Left: 1,
Right: 200,
Bottom: top + 50,
}),
).OverrideDrawingStyle(charts.Style{
StrokeColor: drawing.ColorBlack,
FillColor: drawing.ColorBlack,
}).Rect(charts.Box{
Left: 10,
Top: 0,
Right: 110,
Bottom: 20,
})
// legend line dot
p.Child(
charts.PainterBoxOption(charts.Box{
Top: top,
Left: 200,
Right: p.Width() - 1,
Bottom: top + 50,
}),
).OverrideDrawingStyle(charts.Style{
StrokeColor: drawing.ColorBlack,
FillColor: drawing.ColorBlack,
}).LegendLineDot(charts.Box{
Left: 10,
Top: 0,
Right: 50,
Bottom: 20,
})
// grid
top += 50
charts.NewGridPainter(p.Child(charts.PainterBoxOption(charts.Box{
Top: top,
Left: 1,
Right: p.Width() - 1,
Bottom: top + 100,
})), charts.GridPainterOption{
Row: 5,
IgnoreFirstRow: true,
IgnoreLastRow: true,
StrokeColor: drawing.ColorBlue,
}).Render()
// legend
top += 100
charts.NewLegendPainter(p.Child(charts.PainterBoxOption(charts.Box{
Top: top,
Left: 1,
Right: p.Width() - 1,
Bottom: top + 30,
})), charts.LegendOption{
Left: "10",
Data: []string{
"Email",
"Union Ads",
"Video Ads",
"Direct",
},
FontSize: 12,
FontColor: drawing.ColorBlack,
}).Render()
// legend
top += 30
charts.NewLegendPainter(p.Child(charts.PainterBoxOption(charts.Box{
Top: top,
Left: 1,
Right: p.Width() - 1,
Bottom: top + 30,
})), charts.LegendOption{
Left: charts.PositionRight,
Data: []string{
"Email",
"Union Ads",
"Video Ads",
"Direct",
},
Align: charts.AlignRight,
FontSize: 16,
Icon: charts.IconRect,
FontColor: drawing.ColorBlack,
}).Render()
// legend
top += 30
charts.NewLegendPainter(p.Child(charts.PainterBoxOption(charts.Box{
Top: top,
Left: 1,
Right: p.Width() - 1,
Bottom: top + 100,
})), charts.LegendOption{
Top: "10",
Data: []string{
"Email",
"Union Ads",
"Video Ads",
"Direct",
},
Orient: charts.OrientVertical,
FontSize: 12,
FontColor: drawing.ColorBlack,
}).Render()
// axis bottom
top += 100
charts.NewAxisPainter(p.Child(charts.PainterBoxOption(charts.Box{
Top: top,
Left: 1,
Right: p.Width() - 1,
Bottom: top + 50,
})), charts.AxisOption{
Data: []string{
"Mon",
"Tue",
"Wed",
"Thu",
"Fri",
"Sat",
"Sun",
},
StrokeColor: drawing.ColorBlack,
FontSize: 12,
FontColor: drawing.ColorBlack,
}).Render()
// axis top
top += 50
charts.NewAxisPainter(p.Child(charts.PainterBoxOption(charts.Box{
Top: top,
Left: 1,
Right: p.Width() - 1,
Bottom: top + 50,
})), charts.AxisOption{
Position: charts.PositionTop,
BoundaryGap: charts.FalseFlag(),
Data: []string{
"Mon",
"Tue",
"Wed",
"Thu",
"Fri",
"Sat",
"Sun",
},
StrokeColor: drawing.ColorBlack,
FontSize: 12,
FontColor: drawing.ColorBlack,
}).Render()
// axis left
top += 50
charts.NewAxisPainter(p.Child(charts.PainterBoxOption(charts.Box{
Top: top,
Left: 10,
Right: 60,
Bottom: top + 200,
})), charts.AxisOption{
Position: charts.PositionLeft,
Data: []string{
"Mon",
"Tue",
"Wed",
"Thu",
"Fri",
"Sat",
"Sun",
},
StrokeColor: drawing.ColorBlack,
FontSize: 12,
FontColor: drawing.ColorBlack,
}).Render()
// axis right
charts.NewAxisPainter(p.Child(charts.PainterBoxOption(charts.Box{
Top: top,
Left: 100,
Right: 150,
Bottom: top + 200,
})), charts.AxisOption{
Position: charts.PositionRight,
Data: []string{
"Mon",
"Tue",
"Wed",
"Thu",
"Fri",
"Sat",
"Sun",
},
StrokeColor: drawing.ColorBlack,
FontSize: 12,
FontColor: drawing.ColorBlack,
}).Render()
// axis left no tick
charts.NewAxisPainter(p.Child(charts.PainterBoxOption(charts.Box{
Top: top,
Left: 150,
Right: 300,
Bottom: top + 200,
})), charts.AxisOption{
BoundaryGap: charts.FalseFlag(),
Position: charts.PositionLeft,
Data: []string{
"Mon",
"Tue",
"Wed",
"Thu",
"Fri",
"Sat",
"Sun",
},
FontSize: 12,
FontColor: drawing.ColorBlack,
SplitLineShow: true,
SplitLineColor: drawing.ColorBlack.WithAlpha(100),
}).Render()
buf, err := p.Bytes()
if err != nil {
panic(err)
}
err = writeFile(buf)
if err != nil {
panic(err)
}
}

View file

@ -1,71 +0,0 @@
package main
import (
"os"
"path/filepath"
"git.smarteching.com/zeni/go-charts/v2"
)
func writeFile(buf []byte) error {
tmpPath := "./tmp"
err := os.MkdirAll(tmpPath, 0700)
if err != nil {
return err
}
file := filepath.Join(tmpPath, "pie-chart.png")
err = os.WriteFile(file, buf, 0600)
if err != nil {
return err
}
return nil
}
func main() {
values := []float64{
1048,
735,
580,
484,
300,
}
p, err := charts.PieRender(
values,
charts.TitleOptionFunc(charts.TitleOption{
Text: "Rainfall vs Evaporation",
Subtext: "Fake Data",
Left: charts.PositionCenter,
}),
charts.PaddingOptionFunc(charts.Box{
Top: 20,
Right: 20,
Bottom: 20,
Left: 20,
}),
charts.LegendOptionFunc(charts.LegendOption{
Orient: charts.OrientVertical,
Data: []string{
"Search Engine",
"Direct",
"Email",
"Union Ads",
"Video Ads",
},
Left: charts.PositionLeft,
}),
charts.PieSeriesShowLabel(),
)
if err != nil {
panic(err)
}
buf, err := p.Bytes()
if err != nil {
panic(err)
}
err = writeFile(buf)
if err != nil {
panic(err)
}
}

View file

@ -1,79 +0,0 @@
package main
import (
"os"
"path/filepath"
"git.smarteching.com/zeni/go-charts/v2"
)
func writeFile(buf []byte) error {
tmpPath := "./tmp"
err := os.MkdirAll(tmpPath, 0700)
if err != nil {
return err
}
file := filepath.Join(tmpPath, "radar-chart.png")
err = os.WriteFile(file, buf, 0600)
if err != nil {
return err
}
return nil
}
func main() {
values := [][]float64{
{
4200,
3000,
20000,
35000,
50000,
18000,
},
{
5000,
14000,
28000,
26000,
42000,
21000,
},
}
p, err := charts.RadarRender(
values,
charts.TitleTextOptionFunc("Basic Radar Chart"),
charts.LegendLabelsOptionFunc([]string{
"Allocated Budget",
"Actual Spending",
}),
charts.RadarIndicatorOptionFunc([]string{
"Sales",
"Administration",
"Information Technology",
"Customer Support",
"Development",
"Marketing",
}, []float64{
6500,
16000,
30000,
38000,
52000,
25000,
}),
)
if err != nil {
panic(err)
}
buf, err := p.Bytes()
if err != nil {
panic(err)
}
err = writeFile(buf)
if err != nil {
panic(err)
}
}

View file

@ -1,178 +0,0 @@
package main
import (
"os"
"path/filepath"
"strconv"
"strings"
"git.smarteching.com/zeni/go-charts/v2"
"git.smarteching.com/zeni/go-chart/v2/drawing"
)
func writeFile(buf []byte, filename string) error {
tmpPath := "./tmp"
err := os.MkdirAll(tmpPath, 0700)
if err != nil {
return err
}
file := filepath.Join(tmpPath, filename)
err = os.WriteFile(file, buf, 0600)
if err != nil {
return err
}
return nil
}
func main() {
// charts.SetDefaultTableSetting(charts.TableDarkThemeSetting)
charts.SetDefaultWidth(810)
header := []string{
"Name",
"Age",
"Address",
"Tag",
"Action",
}
data := [][]string{
{
"John Brown",
"32",
"New York No. 1 Lake Park",
"nice, developer",
"Send Mail",
},
{
"Jim Green ",
"42",
"London No. 1 Lake Park",
"wow",
"Send Mail",
},
{
"Joe Black ",
"32",
"Sidney No. 1 Lake Park",
"cool, teacher",
"Send Mail",
},
}
spans := map[int]int{
0: 2,
1: 1,
// 设置第三列的span
2: 3,
3: 2,
4: 2,
}
p, err := charts.TableRender(
header,
data,
spans,
)
if err != nil {
panic(err)
}
buf, err := p.Bytes()
if err != nil {
panic(err)
}
err = writeFile(buf, "table.png")
if err != nil {
panic(err)
}
bgColor := charts.Color{
R: 16,
G: 22,
B: 30,
A: 255,
}
p, err = charts.TableOptionRender(charts.TableChartOption{
Header: []string{
"Name",
"Price",
"Change",
},
BackgroundColor: bgColor,
HeaderBackgroundColor: bgColor,
RowBackgroundColors: []charts.Color{
bgColor,
},
HeaderFontColor: drawing.ColorWhite,
FontColor: drawing.ColorWhite,
Padding: charts.Box{
Top: 15,
Right: 10,
Bottom: 15,
Left: 10,
},
Data: [][]string{
{
"Datadog Inc",
"97.32",
"-7.49%",
},
{
"Hashicorp Inc",
"28.66",
"-9.25%",
},
{
"Gitlab Inc",
"51.63",
"+4.32%",
},
},
TextAligns: []string{
"",
charts.AlignRight,
charts.AlignRight,
},
CellStyle: func(tc charts.TableCell) *charts.Style {
column := tc.Column
if column != 2 {
return nil
}
value, _ := strconv.ParseFloat(strings.Replace(tc.Text, "%", "", 1), 64)
if value == 0 {
return nil
}
style := charts.Style{
Padding: charts.Box{
Bottom: 5,
},
}
if value > 0 {
style.FillColor = charts.Color{
R: 179,
G: 53,
B: 20,
A: 255,
}
} else if value < 0 {
style.FillColor = charts.Color{
R: 33,
G: 124,
B: 50,
A: 255,
}
}
return &style
},
})
if err != nil {
panic(err)
}
buf, err = p.Bytes()
if err != nil {
panic(err)
}
err = writeFile(buf, "table-color.png")
if err != nil {
panic(err)
}
}

View file

@ -1,81 +0,0 @@
package main
import (
"crypto/rand"
"fmt"
"math/big"
"os"
"path/filepath"
"time"
"git.smarteching.com/zeni/go-charts/v2"
)
func writeFile(buf []byte) error {
tmpPath := "./tmp"
err := os.MkdirAll(tmpPath, 0700)
if err != nil {
return err
}
file := filepath.Join(tmpPath, "time-line-chart.png")
err = os.WriteFile(file, buf, 0600)
if err != nil {
return err
}
return nil
}
func main() {
xAxisValue := []string{}
values := []float64{}
now := time.Now()
firstAxis := 0
for i := 0; i < 300; i++ {
// 设置首个axis为xx:00的时间点
if firstAxis == 0 && now.Minute() == 0 {
firstAxis = i
}
xAxisValue = append(xAxisValue, now.Format("15:04"))
now = now.Add(time.Minute)
value, _ := rand.Int(rand.Reader, big.NewInt(100))
values = append(values, float64(value.Int64()))
}
p, err := charts.LineRender(
[][]float64{
values,
},
charts.TitleTextOptionFunc("Line"),
charts.XAxisDataOptionFunc(xAxisValue, charts.FalseFlag()),
charts.LegendLabelsOptionFunc([]string{
"Demo",
}, "50"),
func(opt *charts.ChartOption) {
opt.XAxis.FirstAxis = firstAxis
// 必须要比计算得来的最小值更大(每60分钟)
opt.XAxis.SplitNumber = 60
opt.Legend.Padding = charts.Box{
Top: 5,
Bottom: 10,
}
opt.SymbolShow = charts.FalseFlag()
opt.LineStrokeWidth = 1
opt.ValueFormatter = func(f float64) string {
return fmt.Sprintf("%.0f", f)
}
},
)
if err != nil {
panic(err)
}
buf, err := p.Bytes()
if err != nil {
panic(err)
}
err = writeFile(buf)
if err != nil {
panic(err)
}
}

21
font.go
View file

@ -27,18 +27,14 @@ import (
"sync" "sync"
"github.com/golang/freetype/truetype" "github.com/golang/freetype/truetype"
"git.smarteching.com/zeni/go-chart/v2/roboto" "github.com/wcharczuk/go-chart/v2/roboto"
) )
var fonts = sync.Map{} var fonts = sync.Map{}
var ErrFontNotExists = errors.New("font is not exists") var ErrFontNotExists = errors.New("font is not exists")
var defaultFontFamily = "defaultFontFamily"
func init() { func init() {
name := "roboto" _ = InstallFont("roboto", roboto.Roboto)
_ = InstallFont(name, roboto.Roboto)
font, _ := GetFont(name)
SetDefaultFont(font)
} }
// InstallFont installs the font for charts // InstallFont installs the font for charts
@ -51,19 +47,6 @@ func InstallFont(fontFamily string, data []byte) error {
return nil return nil
} }
// GetDefaultFont get default font
func GetDefaultFont() (*truetype.Font, error) {
return GetFont(defaultFontFamily)
}
// SetDefaultFont set default font
func SetDefaultFont(font *truetype.Font) {
if font == nil {
return
}
fonts.Store(defaultFontFamily, font)
}
// GetFont get the font by font family // GetFont get the font by font family
func GetFont(fontFamily string) (*truetype.Font, error) { func GetFont(fontFamily string) (*truetype.Font, error) {
value, ok := fonts.Load(fontFamily) value, ok := fonts.Load(fontFamily)

View file

@ -26,7 +26,7 @@ import (
"testing" "testing"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"git.smarteching.com/zeni/go-chart/v2/roboto" "github.com/wcharczuk/go-chart/v2/roboto"
) )
func TestInstallFont(t *testing.T) { func TestInstallFont(t *testing.T) {

View file

@ -23,54 +23,35 @@
package charts package charts
import ( import (
"fmt"
"sort"
"github.com/dustin/go-humanize"
"github.com/golang/freetype/truetype" "github.com/golang/freetype/truetype"
"github.com/wcharczuk/go-chart/v2"
) )
type funnelChart struct { type funnelChartOption struct {
p *Painter Theme string
opt *FunnelChartOption Font *truetype.Font
}
// NewFunnelSeriesList returns a series list for funnel
func NewFunnelSeriesList(values []float64) SeriesList {
seriesList := make(SeriesList, len(values))
for index, value := range values {
seriesList[index] = NewSeriesFromValues([]float64{
value,
}, ChartTypeFunnel)
}
return seriesList
}
// NewFunnelChart returns a funnel chart renderer
func NewFunnelChart(p *Painter, opt FunnelChartOption) *funnelChart {
if opt.Theme == nil {
opt.Theme = defaultTheme
}
return &funnelChart{
p: p,
opt: &opt,
}
}
type FunnelChartOption struct {
// The theme
Theme ColorPalette
// The font size
Font *truetype.Font
// The data series list
SeriesList SeriesList SeriesList SeriesList
// The padding of line chart
Padding Box
// The option of title
Title TitleOption
// The legend option
Legend LegendOption
} }
func (f *funnelChart) render(result *defaultRenderResult, seriesList SeriesList) (Box, error) { func funnelChartRender(opt funnelChartOption, result *basicRenderResult) error {
opt := f.opt d, err := NewDraw(DrawOption{
seriesPainter := result.seriesPainter Parent: result.d,
}, PaddingOption(chart.Box{
Top: result.titleBox.Height(),
}))
if err != nil {
return err
}
seriesList := make([]Series, len(opt.SeriesList))
copy(seriesList, opt.SeriesList)
sort.Slice(seriesList, func(i, j int) bool {
// 大的数据在前
return seriesList[i].Data[0].Value > seriesList[j].Data[0].Value
})
max := seriesList[0].Data[0].Value max := seriesList[0].Data[0].Value
min := float64(0) min := float64(0)
for _, item := range seriesList { for _, item := range seriesList {
@ -81,10 +62,11 @@ func (f *funnelChart) render(result *defaultRenderResult, seriesList SeriesList)
min = *item.Min min = *item.Min
} }
} }
theme := opt.Theme
theme := NewTheme(opt.Theme)
gap := 2 gap := 2
height := seriesPainter.Height() height := d.Box.Height()
width := seriesPainter.Width() width := d.Box.Width()
count := len(seriesList) count := len(seriesList)
h := (height - gap*(count-1)) / count h := (height - gap*(count-1)) / count
@ -92,23 +74,13 @@ func (f *funnelChart) render(result *defaultRenderResult, seriesList SeriesList)
y := 0 y := 0
widthList := make([]int, len(seriesList)) widthList := make([]int, len(seriesList))
textList := make([]string, len(seriesList)) textList := make([]string, len(seriesList))
seriesNames := seriesList.Names()
offset := max - min
for index, item := range seriesList { for index, item := range seriesList {
value := item.Data[0].Value value := item.Data[0].Value
// 最大最小值一致则为100% widthPercent := (value - min) / (max - min)
widthPercent := 100.0
if offset != 0 {
widthPercent = (value - min) / offset
}
w := int(widthPercent * float64(width)) w := int(widthPercent * float64(width))
widthList[index] = w widthList[index] = w
// 如果最大值为0则占比100% p := humanize.CommafWithDigits(value/max*100, 2) + "%"
percent := 1.0 textList[index] = fmt.Sprintf("%s(%s)", item.Name, p)
if max != 0 {
percent = value / max
}
textList[index] = NewFunnelLabelFormatter(seriesNames, item.Label.Formatter)(index, value, percent)
} }
for index, w := range widthList { for index, w := range widthList {
@ -144,49 +116,26 @@ func (f *funnelChart) render(result *defaultRenderResult, seriesList SeriesList)
}, },
} }
color := theme.GetSeriesColor(series.index) color := theme.GetSeriesColor(series.index)
d.fill(points, chart.Style{
seriesPainter.OverrideDrawingStyle(Style{
FillColor: color, FillColor: color,
}).FillArea(points) })
// 文本 // 文本
text := textList[index] text := textList[index]
seriesPainter.OverrideTextStyle(Style{ r := d.Render
textStyle := chart.Style{
FontColor: theme.GetTextColor(), FontColor: theme.GetTextColor(),
FontSize: labelFontSize, FontSize: labelFontSize,
Font: opt.Font, Font: opt.Font,
}) }
textBox := seriesPainter.MeasureText(text) textStyle.GetTextOptions().WriteToRenderer(r)
textBox := r.MeasureText(text)
textX := width>>1 - textBox.Width()>>1 textX := width>>1 - textBox.Width()>>1
textY := y + h>>1 textY := y + h>>1
seriesPainter.Text(text, textX, textY) d.text(text, textX, textY)
y += (h + gap) y += (h + gap)
} }
return f.p.box, nil return nil
}
func (f *funnelChart) Render() (Box, error) {
p := f.p
opt := f.opt
renderResult, err := defaultRender(p, defaultRenderOption{
Theme: opt.Theme,
Padding: opt.Padding,
SeriesList: opt.SeriesList,
XAxis: XAxisOption{
Show: FalseFlag(),
},
YAxisOptions: []YAxisOption{
{
Show: FalseFlag(),
},
},
TitleOption: opt.Title,
LegendOption: opt.Legend,
})
if err != nil {
return BoxZero, err
}
seriesList := opt.SeriesList.Filter(ChartTypeFunnel)
return f.render(renderResult, seriesList)
} }

View file

@ -1,79 +0,0 @@
// 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"
)
func TestFunnelChart(t *testing.T) {
assert := assert.New(t)
tests := []struct {
render func(*Painter) ([]byte, error)
result string
}{
{
render: func(p *Painter) ([]byte, error) {
_, err := NewFunnelChart(p, FunnelChartOption{
SeriesList: NewFunnelSeriesList([]float64{
100,
80,
60,
40,
20,
}),
Legend: NewLegendOption([]string{
"Show",
"Click",
"Visit",
"Inquiry",
"Order",
}),
Title: TitleOption{
Text: "Funnel",
},
}).Render()
if err != nil {
return nil, err
}
return p.Bytes()
},
result: "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"600\" height=\"400\">\\n<path d=\"M 0 0\nL 600 0\nL 600 400\nL 0 400\nL 0 0\" style=\"stroke-width:0;stroke:none;fill:rgba(255,255,255,1.0)\"/><path d=\"M 86 9\nL 116 9\" style=\"stroke-width:3;stroke:rgba(84,112,198,1.0);fill:rgba(84,112,198,1.0)\"/><circle cx=\"101\" cy=\"9\" r=\"5\" style=\"stroke-width:3;stroke:rgba(84,112,198,1.0);fill:rgba(84,112,198,1.0)\"/><path d=\"\" style=\"stroke-width:3;stroke:rgba(84,112,198,1.0);fill:rgba(84,112,198,1.0)\"/><text x=\"118\" y=\"15\" style=\"stroke-width:0;stroke:none;fill:rgba(70,70,70,1.0);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">Show</text><path d=\"M 176 9\nL 206 9\" style=\"stroke-width:3;stroke:rgba(145,204,117,1.0);fill:rgba(145,204,117,1.0)\"/><circle cx=\"191\" cy=\"9\" r=\"5\" style=\"stroke-width:3;stroke:rgba(145,204,117,1.0);fill:rgba(145,204,117,1.0)\"/><path d=\"\" style=\"stroke-width:3;stroke:rgba(145,204,117,1.0);fill:rgba(145,204,117,1.0)\"/><text x=\"208\" y=\"15\" style=\"stroke-width:0;stroke:none;fill:rgba(70,70,70,1.0);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">Click</text><path d=\"M 262 9\nL 292 9\" style=\"stroke-width:3;stroke:rgba(250,200,88,1.0);fill:rgba(250,200,88,1.0)\"/><circle cx=\"277\" cy=\"9\" r=\"5\" style=\"stroke-width:3;stroke:rgba(250,200,88,1.0);fill:rgba(250,200,88,1.0)\"/><path d=\"\" style=\"stroke-width:3;stroke:rgba(250,200,88,1.0);fill:rgba(250,200,88,1.0)\"/><text x=\"294\" y=\"15\" style=\"stroke-width:0;stroke:none;fill:rgba(70,70,70,1.0);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">Visit</text><path d=\"M 345 9\nL 375 9\" style=\"stroke-width:3;stroke:rgba(238,102,102,1.0);fill:rgba(238,102,102,1.0)\"/><circle cx=\"360\" cy=\"9\" r=\"5\" style=\"stroke-width:3;stroke:rgba(238,102,102,1.0);fill:rgba(238,102,102,1.0)\"/><path d=\"\" style=\"stroke-width:3;stroke:rgba(238,102,102,1.0);fill:rgba(238,102,102,1.0)\"/><text x=\"377\" y=\"15\" style=\"stroke-width:0;stroke:none;fill:rgba(70,70,70,1.0);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">Inquiry</text><path d=\"M 444 9\nL 474 9\" style=\"stroke-width:3;stroke:rgba(115,192,222,1.0);fill:rgba(115,192,222,1.0)\"/><circle cx=\"459\" cy=\"9\" r=\"5\" style=\"stroke-width:3;stroke:rgba(115,192,222,1.0);fill:rgba(115,192,222,1.0)\"/><path d=\"\" style=\"stroke-width:3;stroke:rgba(115,192,222,1.0);fill:rgba(115,192,222,1.0)\"/><text x=\"476\" y=\"15\" style=\"stroke-width:0;stroke:none;fill:rgba(70,70,70,1.0);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">Order</text><text x=\"0\" y=\"15\" style=\"stroke-width:0;stroke:none;fill:rgba(70,70,70,1.0);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">Funnel</text><path d=\"M 0 35\nL 600 35\nL 540 100\nL 60 100\nL 0 35\" style=\"stroke-width:0;stroke:none;fill:rgba(84,112,198,1.0)\"/><text x=\"280\" y=\"67\" style=\"stroke-width:0;stroke:none;fill:rgba(70,70,70,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">(100%)</text><path d=\"M 60 102\nL 540 102\nL 480 167\nL 120 167\nL 60 102\" style=\"stroke-width:0;stroke:none;fill:rgba(145,204,117,1.0)\"/><text x=\"284\" y=\"134\" style=\"stroke-width:0;stroke:none;fill:rgba(70,70,70,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">(80%)</text><path d=\"M 120 169\nL 480 169\nL 420 234\nL 180 234\nL 120 169\" style=\"stroke-width:0;stroke:none;fill:rgba(250,200,88,1.0)\"/><text x=\"284\" y=\"201\" style=\"stroke-width:0;stroke:none;fill:rgba(70,70,70,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">(60%)</text><path d=\"M 180 236\nL 420 236\nL 360 301\nL 240 301\nL 180 236\" style=\"stroke-width:0;stroke:none;fill:rgba(238,102,102,1.0)\"/><text x=\"284\" y=\"268\" style=\"stroke-width:0;stroke:none;fill:rgba(70,70,70,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">(40%)</text><path d=\"M 240 303\nL 360 303\nL 300 368\nL 300 368\nL 240 303\" style=\"stroke-width:0;stroke:none;fill:rgba(115,192,222,1.0)\"/><text x=\"284\" y=\"335\" style=\"stroke-width:0;stroke:none;fill:rgba(70,70,70,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">(20%)</text></svg>",
},
}
for _, tt := range tests {
p, err := NewPainter(PainterOptions{
Type: ChartOutputSVG,
Width: 600,
Height: 400,
}, PainterThemeOption(defaultTheme))
assert.Nil(err)
data, err := tt.render(p)
assert.Nil(err)
assert.Equal(tt.result, string(data))
}
}

91
funnel_test.go Normal file
View file

@ -0,0 +1,91 @@
// 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"
)
func TestFunnelChartRender(t *testing.T) {
assert := assert.New(t)
d, err := NewDraw(DrawOption{
Width: 250,
Height: 150,
})
assert.Nil(err)
f, _ := chart.GetDefaultFont()
err = funnelChartRender(funnelChartOption{
Font: f,
SeriesList: []Series{
{
Type: ChartTypeFunnel,
Name: "Visit",
Data: NewSeriesDataFromValues([]float64{
60,
}),
},
{
Type: ChartTypeFunnel,
Name: "Inquiry",
Data: NewSeriesDataFromValues([]float64{
40,
}),
index: 1,
},
{
Type: ChartTypeFunnel,
Name: "Order",
Data: NewSeriesDataFromValues([]float64{
20,
}),
index: 2,
},
{
Type: ChartTypeFunnel,
Name: "Click",
Data: NewSeriesDataFromValues([]float64{
80,
}),
index: 3,
},
{
Type: ChartTypeFunnel,
Name: "Show",
Data: NewSeriesDataFromValues([]float64{
100,
}),
index: 4,
},
},
}, &basicRenderResult{
d: d,
})
assert.Nil(err)
data, err := d.Bytes()
assert.Nil(err)
assert.Equal("<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"250\" height=\"150\">\\n<path d=\"M 0 0\nL 250 0\nL 225 28\nL 25 28\nL 0 0\" style=\"stroke-width:0;stroke:none;fill:rgba(115,192,222,1.0)\"/><text x=\"89\" y=\"14\" style=\"stroke-width:0;stroke:none;fill:rgba(70,70,70,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">Show(100%)</text><path d=\"M 25 30\nL 225 30\nL 200 58\nL 50 58\nL 25 30\" style=\"stroke-width:0;stroke:none;fill:rgba(238,102,102,1.0)\"/><text x=\"94\" y=\"44\" style=\"stroke-width:0;stroke:none;fill:rgba(70,70,70,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">Click(80%)</text><path d=\"M 50 60\nL 200 60\nL 175 88\nL 75 88\nL 50 60\" style=\"stroke-width:0;stroke:none;fill:rgba(84,112,198,1.0)\"/><text x=\"96\" y=\"74\" style=\"stroke-width:0;stroke:none;fill:rgba(70,70,70,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">Visit(60%)</text><path d=\"M 75 90\nL 175 90\nL 150 118\nL 100 118\nL 75 90\" style=\"stroke-width:0;stroke:none;fill:rgba(145,204,117,1.0)\"/><text x=\"89\" y=\"104\" style=\"stroke-width:0;stroke:none;fill:rgba(70,70,70,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">Inquiry(40%)</text><path d=\"M 100 120\nL 150 120\nL 125 148\nL 125 148\nL 100 120\" style=\"stroke-width:0;stroke:none;fill:rgba(250,200,88,1.0)\"/><text x=\"93\" y=\"134\" style=\"stroke-width:0;stroke:none;fill:rgba(70,70,70,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">Order(20%)</text></svg>", string(data))
}

14
go.mod
View file

@ -1,17 +1,17 @@
module git.smarteching.com/zeni/go-charts/v2 module github.com/vicanso/go-charts
go 1.24.1 go 1.17
require ( require (
git.smarteching.com/zeni/go-chart/v2 v2.1.4 github.com/dustin/go-humanize v1.0.0
github.com/dustin/go-humanize v1.0.1
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0
github.com/stretchr/testify v1.10.0 github.com/stretchr/testify v1.7.1
github.com/wcharczuk/go-chart/v2 v2.1.0
) )
require ( require (
github.com/davecgh/go-spew v1.1.1 // indirect github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect
golang.org/x/image v0.21.0 // indirect golang.org/x/image v0.0.0-20220413100746-70e8d0d3baa9 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
) )

27
go.sum
View file

@ -1,18 +1,25 @@
git.smarteching.com/zeni/go-chart/v2 v2.1.4 h1:pF06+F6eqJLIG8uMiTVPR5TygPGMjM/FHMzTxmu5V/Q= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
git.smarteching.com/zeni/go-chart/v2 v2.1.4/go.mod h1:b3ueW9h3pGGXyhkormZAvilHaG4+mQti+bMNPdQBeOQ=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY= github.com/dustin/go-humanize v1.0.0 h1:VSnTsYCnlFHaM2/igO1h6X3HA71jcobQuxemgkq4zYo=
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto= github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk=
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 h1:DACJavvAHhabrF08vX0COfcOBJRhZ8lUbR+ZWIs0Y5g= github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 h1:DACJavvAHhabrF08vX0COfcOBJRhZ8lUbR+ZWIs0Y5g=
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0/go.mod h1:E/TSTwGwJL78qG/PmXZO1EjYhfJinVAhrmmHX6Z8B9k= github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0/go.mod h1:E/TSTwGwJL78qG/PmXZO1EjYhfJinVAhrmmHX6Z8B9k=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMTY=
golang.org/x/image v0.21.0 h1:c5qV36ajHpdj4Qi0GnE0jUc/yuo33OLFaa0d+crTD5s= github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
golang.org/x/image v0.21.0/go.mod h1:vUbsLavqK/W303ZroQQVKQ+Af3Yl6Uz1Ppu5J/cLz78= github.com/wcharczuk/go-chart/v2 v2.1.0 h1:tY2slqVQ6bN+yHSnDYwZebLQFkphK4WNrVwnt7CJZ2I=
github.com/wcharczuk/go-chart/v2 v2.1.0/go.mod h1:yx7MvAVNcP/kN9lKXM/NTce4au4DFN99j6i1OwDclNA=
golang.org/x/image v0.0.0-20200927104501-e162460cd6b5/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
golang.org/x/image v0.0.0-20220413100746-70e8d0d3baa9 h1:LRtI4W37N+KFebI/qV0OFiLUv4GLOWeEW5hn/KEJvxE=
golang.org/x/image v0.0.0-20220413100746-70e8d0d3baa9/go.mod h1:023OzeP/+EPmXeapQh35lcL3II3LrY8Ic+EFFKVhULM=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

92
grid.go
View file

@ -1,92 +0,0 @@
// 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
type gridPainter struct {
p *Painter
opt *GridPainterOption
}
type GridPainterOption struct {
// The stroke width
StrokeWidth float64
// The stroke color
StrokeColor Color
// The spans of column
ColumnSpans []int
// The column of grid
Column int
// The row of grid
Row int
// Ignore first row
IgnoreFirstRow bool
// Ignore last row
IgnoreLastRow bool
// Ignore first column
IgnoreFirstColumn bool
// Ignore last column
IgnoreLastColumn bool
}
// NewGridPainter returns new a grid renderer
func NewGridPainter(p *Painter, opt GridPainterOption) *gridPainter {
return &gridPainter{
p: p,
opt: &opt,
}
}
func (g *gridPainter) Render() (Box, error) {
opt := g.opt
ignoreColumnLines := make([]int, 0)
if opt.IgnoreFirstColumn {
ignoreColumnLines = append(ignoreColumnLines, 0)
}
if opt.IgnoreLastColumn {
ignoreColumnLines = append(ignoreColumnLines, opt.Column)
}
ignoreRowLines := make([]int, 0)
if opt.IgnoreFirstRow {
ignoreRowLines = append(ignoreRowLines, 0)
}
if opt.IgnoreLastRow {
ignoreRowLines = append(ignoreRowLines, opt.Row)
}
strokeWidth := opt.StrokeWidth
if strokeWidth <= 0 {
strokeWidth = 1
}
g.p.SetDrawingStyle(Style{
StrokeWidth: strokeWidth,
StrokeColor: opt.StrokeColor,
})
g.p.Grid(GridOption{
Column: opt.Column,
ColumnSpans: opt.ColumnSpans,
Row: opt.Row,
IgnoreColumnLines: ignoreColumnLines,
IgnoreRowLines: ignoreRowLines,
})
return g.p.box, nil
}

View file

@ -1,87 +0,0 @@
// 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"
"git.smarteching.com/zeni/go-chart/v2/drawing"
)
func TestGrid(t *testing.T) {
assert := assert.New(t)
tests := []struct {
render func(*Painter) ([]byte, error)
result string
}{
{
render: func(p *Painter) ([]byte, error) {
_, err := NewGridPainter(p, GridPainterOption{
StrokeColor: drawing.ColorBlack,
Column: 6,
Row: 6,
IgnoreFirstRow: true,
IgnoreLastRow: true,
IgnoreFirstColumn: true,
IgnoreLastColumn: true,
}).Render()
if err != nil {
return nil, err
}
return p.Bytes()
},
result: "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"600\" height=\"400\">\\n<path d=\"M 100 0\nL 100 400\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 200 0\nL 200 400\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 300 0\nL 300 400\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 400 0\nL 400 400\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 500 0\nL 500 400\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 0 66\nL 600 66\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 0 133\nL 600 133\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 0 200\nL 600 200\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 0 266\nL 600 266\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 0 333\nL 600 333\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/></svg>",
},
{
render: func(p *Painter) ([]byte, error) {
_, err := NewGridPainter(p, GridPainterOption{
StrokeColor: drawing.ColorBlack,
ColumnSpans: []int{
2,
5,
3,
},
Row: 6,
}).Render()
if err != nil {
return nil, err
}
return p.Bytes()
},
result: "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"600\" height=\"400\">\\n<path d=\"M 0 0\nL 0 400\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 120 0\nL 120 400\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 420 0\nL 420 400\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 600 0\nL 600 400\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 0 0\nL 600 0\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 0 66\nL 600 66\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 0 133\nL 600 133\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 0 200\nL 600 200\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 0 266\nL 600 266\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 0 333\nL 600 333\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><path d=\"M 0 400\nL 600 400\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/></svg>",
},
}
for _, tt := range tests {
p, err := NewPainter(PainterOptions{
Type: ChartOutputSVG,
Width: 600,
Height: 400,
}, PainterThemeOption(defaultTheme))
assert.Nil(err)
data, err := tt.render(p)
assert.Nil(err)
assert.Equal(tt.result, string(data))
}
}

View file

@ -1,216 +0,0 @@
// 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 (
"github.com/golang/freetype/truetype"
"git.smarteching.com/zeni/go-chart/v2"
)
type horizontalBarChart struct {
p *Painter
opt *HorizontalBarChartOption
}
type HorizontalBarChartOption struct {
// The theme
Theme ColorPalette
// The font size
Font *truetype.Font
// The data series list
SeriesList SeriesList
// The x axis option
XAxis XAxisOption
// The padding of line chart
Padding Box
// The y axis option
YAxisOptions []YAxisOption
// The option of title
Title TitleOption
// The legend option
Legend LegendOption
BarHeight int
// Margin of bar
BarMargin int
}
// NewHorizontalBarChart returns a horizontal bar chart renderer
func NewHorizontalBarChart(p *Painter, opt HorizontalBarChartOption) *horizontalBarChart {
if opt.Theme == nil {
opt.Theme = defaultTheme
}
return &horizontalBarChart{
p: p,
opt: &opt,
}
}
func (h *horizontalBarChart) render(result *defaultRenderResult, seriesList SeriesList) (Box, error) {
p := h.p
opt := h.opt
seriesPainter := result.seriesPainter
yRange := result.axisRanges[0]
y0, y1 := yRange.GetRange(0)
height := int(y1 - y0)
// 每一块之间的margin
margin := 10
// 每一个bar之间的margin
barMargin := 5
if height < 20 {
margin = 2
barMargin = 2
} else if height < 50 {
margin = 5
barMargin = 3
}
if opt.BarMargin > 0 {
barMargin = opt.BarMargin
}
seriesCount := len(seriesList)
// 总的高度-两个margin-(总数-1)的barMargin
barHeight := (height - 2*margin - barMargin*(seriesCount-1)) / seriesCount
if opt.BarHeight > 0 && opt.BarHeight < barHeight {
barHeight = opt.BarHeight
margin = (height - seriesCount*barHeight - barMargin*(seriesCount-1)) / 2
}
theme := opt.Theme
max, min := seriesList.GetMaxMin(0)
xRange := NewRange(AxisRangeOption{
Painter: p,
Min: min,
Max: max,
DivideCount: defaultAxisDivideCount,
Size: seriesPainter.Width(),
})
seriesNames := seriesList.Names()
rendererList := []Renderer{}
for index := range seriesList {
series := seriesList[index]
seriesColor := theme.GetSeriesColor(series.index)
divideValues := yRange.AutoDivide()
var labelPainter *SeriesLabelPainter
if series.Label.Show {
labelPainter = NewSeriesLabelPainter(SeriesLabelPainterParams{
P: seriesPainter,
SeriesNames: seriesNames,
Label: series.Label,
Theme: opt.Theme,
Font: opt.Font,
})
rendererList = append(rendererList, labelPainter)
}
for j, item := range series.Data {
if j >= yRange.divideCount {
continue
}
// 显示位置切换
j = yRange.divideCount - j - 1
y := divideValues[j]
y += margin
if index != 0 {
y += index * (barHeight + barMargin)
}
w := int(xRange.getHeight(item.Value))
fillColor := seriesColor
if !item.Style.FillColor.IsZero() {
fillColor = item.Style.FillColor
}
right := w
if series.RoundRadius <= 0 {
seriesPainter.OverrideDrawingStyle(Style{
FillColor: fillColor,
}).Rect(chart.Box{
Top: y,
Left: 0,
Right: right,
Bottom: y + barHeight,
})
} else {
seriesPainter.OverrideDrawingStyle(Style{
FillColor: fillColor,
}).RoundedRect(chart.Box{
Top: y,
Left: 0,
Right: right,
Bottom: y + barHeight,
}, series.RoundRadius)
}
// 如果label不需要展示则返回
if labelPainter == nil {
continue
}
labelValue := LabelValue{
Orient: OrientHorizontal,
Index: index,
Value: item.Value,
X: right,
Y: y + barHeight>>1,
Offset: series.Label.Offset,
FontColor: series.Label.Color,
FontSize: series.Label.FontSize,
}
if series.Label.Position == PositionLeft {
labelValue.X = 0
if labelValue.FontColor.IsZero() {
if isLightColor(fillColor) {
labelValue.FontColor = defaultLightFontColor
} else {
labelValue.FontColor = defaultDarkFontColor
}
}
}
labelPainter.Add(labelValue)
}
}
err := doRender(rendererList...)
if err != nil {
return BoxZero, err
}
return p.box, nil
}
func (h *horizontalBarChart) Render() (Box, error) {
p := h.p
opt := h.opt
renderResult, err := defaultRender(p, defaultRenderOption{
Theme: opt.Theme,
Padding: opt.Padding,
SeriesList: opt.SeriesList,
XAxis: opt.XAxis,
YAxisOptions: opt.YAxisOptions,
TitleOption: opt.Title,
LegendOption: opt.Legend,
axisReversed: true,
})
if err != nil {
return BoxZero, err
}
seriesList := opt.SeriesList.Filter(ChartTypeHorizontalBar)
return h.render(renderResult, seriesList)
}

File diff suppressed because one or more lines are too long

301
legend.go
View file

@ -25,19 +25,16 @@ package charts
import ( import (
"strconv" "strconv"
"strings" "strings"
"github.com/wcharczuk/go-chart/v2"
) )
type legendPainter struct {
p *Painter
opt *LegendOption
}
const IconRect = "rect"
const IconLineDot = "lineDot"
type LegendOption struct { type LegendOption struct {
// The theme theme string
Theme ColorPalette // Legend show flag, if nil or true, the legend will be shown
Show *bool
// Legend text style
Style chart.Style
// Text array of legend // Text array of legend
Data []string Data []string
// Distance between legend component and the left side of the container. // Distance between legend component and the left side of the container.
@ -53,199 +50,177 @@ type LegendOption struct {
Orient string Orient string
// Icon of the legend. // Icon of the legend.
Icon string Icon string
// Font size of legend text
FontSize float64
// FontColor color of legend text
FontColor Color
// The flag for show legend, set this to *false will hide legend
Show *bool
// The padding of legend
Padding Box
} }
// NewLegendOption returns a legend option const (
func NewLegendOption(labels []string, left ...string) LegendOption { LegendIconRect = "rect"
)
// NewLegendOption creates a new legend option by legend text list
func NewLegendOption(data []string, position ...string) LegendOption {
opt := LegendOption{ opt := LegendOption{
Data: labels, Data: data,
} }
if len(left) != 0 { if len(position) != 0 {
opt.Left = left[0] opt.Left = position[0]
} }
return opt return opt
} }
// IsEmpty checks legend is empty type legend struct {
func (opt *LegendOption) IsEmpty() bool { d *Draw
isEmpty := true opt *LegendOption
for _, v := range opt.Data {
if v != "" {
isEmpty = false
break
}
}
return isEmpty
} }
// NewLegendPainter returns a legend renderer func NewLegend(d *Draw, opt LegendOption) *legend {
func NewLegendPainter(p *Painter, opt LegendOption) *legendPainter { return &legend{
return &legendPainter{ d: d,
p: p,
opt: &opt, opt: &opt,
} }
} }
func (l *legendPainter) Render() (Box, error) { func (l *legend) Render() (chart.Box, error) {
d := l.d
opt := l.opt opt := l.opt
theme := opt.Theme if len(opt.Data) == 0 || isFalse(opt.Show) {
if opt.IsEmpty() || return chart.BoxZero, nil
isFalse(opt.Show) {
return BoxZero, nil
} }
if theme == nil { theme := NewTheme(opt.theme)
theme = l.p.theme padding := opt.Style.Padding
legendDraw, err := NewDraw(DrawOption{
Parent: d,
}, PaddingOption(padding))
if err != nil {
return chart.BoxZero, err
} }
if opt.FontSize == 0 { r := legendDraw.Render
opt.FontSize = theme.GetFontSize() opt.Style.GetTextOptions().WriteToRenderer(r)
x := 0
y := 0
top := 0
// TODO TOP 暂只支持数值
if opt.Top != "" {
top, _ = strconv.Atoi(opt.Top)
y += top
} }
if opt.FontColor.IsZero() { legendWidth := 30
opt.FontColor = theme.GetTextColor() legendDotHeight := 5
} textPadding := 5
if opt.Left == "" { legendMargin := 10
opt.Left = PositionCenter // 往下移2倍dot的高度
} y += 2 * legendDotHeight
padding := opt.Padding
if padding.IsZero() { widthCount := 0
padding.Top = 5
}
p := l.p.Child(PainterPaddingOption(padding))
p.SetTextStyle(Style{
FontSize: opt.FontSize,
FontColor: opt.FontColor,
})
measureList := make([]Box, len(opt.Data))
maxTextWidth := 0 maxTextWidth := 0
for index, text := range opt.Data { // 文本宽度
b := p.MeasureText(text) for _, text := range opt.Data {
b := r.MeasureText(text)
if b.Width() > maxTextWidth { if b.Width() > maxTextWidth {
maxTextWidth = b.Width() maxTextWidth = b.Width()
} }
measureList[index] = b widthCount += b.Width()
} }
// 计算展示的宽高
width := 0
height := 0
offset := 20
textOffset := 2
legendWidth := 30
legendHeight := 20
itemMaxHeight := 0
for _, item := range measureList {
if item.Height() > itemMaxHeight {
itemMaxHeight = item.Height()
}
if opt.Orient == OrientVertical {
height += item.Height()
} else {
width += item.Width()
}
}
// 增加padding
itemMaxHeight += 10
if opt.Orient == OrientVertical { if opt.Orient == OrientVertical {
width = maxTextWidth + textOffset + legendWidth widthCount = maxTextWidth + legendWidth + textPadding
height = offset * len(opt.Data)
} else { } else {
height = legendHeight // 加上标记
offsetValue := (len(opt.Data) - 1) * (offset + textOffset) widthCount += legendWidth * len(opt.Data)
allLegendWidth := len(opt.Data) * legendWidth // 文本的padding
width += (offsetValue + allLegendWidth) widthCount += 2 * textPadding * len(opt.Data)
// margin的宽度
widthCount += legendMargin * (len(opt.Data) - 1)
} }
// 计算开始的位置
left := 0 left := 0
switch opt.Left { switch opt.Left {
case PositionRight: case PositionRight:
left = p.Width() - width left = legendDraw.Box.Width() - widthCount
case PositionCenter: case PositionCenter:
left = (p.Width() - width) >> 1 left = (legendDraw.Box.Width() - widthCount) >> 1
default: default:
if strings.HasSuffix(opt.Left, "%") { if strings.HasSuffix(opt.Left, "%") {
value, _ := strconv.Atoi(strings.ReplaceAll(opt.Left, "%", "")) value, _ := strconv.Atoi(strings.ReplaceAll(opt.Left, "%", ""))
left = p.Width() * value / 100 left = legendDraw.Box.Width() * value / 100
} else { } else {
value, _ := strconv.Atoi(opt.Left) value, _ := strconv.Atoi(opt.Left)
left = value left = value
} }
} }
top, _ := strconv.Atoi(opt.Top) x = left
if left < 0 {
left = 0
}
x := int(left)
y := int(top) + 10
startY := y
x0 := x
y0 := y
drawIcon := func(top, left int) int {
if opt.Icon == IconRect {
p.Rect(Box{
Top: top - legendHeight + 8,
Left: left,
Right: left + legendWidth,
Bottom: top + 1,
})
} else {
p.LegendLineDot(Box{
Top: top + 1,
Left: left,
Right: left + legendWidth,
Bottom: top + legendHeight + 1,
})
}
return left + legendWidth
}
lastIndex := len(opt.Data) - 1
for index, text := range opt.Data { for index, text := range opt.Data {
color := theme.GetSeriesColor(index) textBox := r.MeasureText(text)
p.SetDrawingStyle(Style{ var renderText func()
FillColor: color,
StrokeColor: color,
})
itemWidth := x0 + measureList[index].Width() + textOffset + offset + legendWidth
if lastIndex == index {
itemWidth = x0 + measureList[index].Width() + legendWidth
}
if itemWidth > p.Width() {
x0 = 0
y += itemMaxHeight
y0 = y
}
if opt.Align != AlignRight {
x0 = drawIcon(y0, x0)
x0 += textOffset
}
p.Text(text, x0, y0)
x0 += measureList[index].Width()
if opt.Align == AlignRight {
x0 += textOffset
x0 = drawIcon(y0, x0)
}
if opt.Orient == OrientVertical { if opt.Orient == OrientVertical {
y0 += offset // 垂直
x0 = x // 重置x的位置
} else { x = left
x0 += offset renderText = func() {
y0 = y x += textPadding
} legendDraw.text(text, x, y+legendDotHeight)
height = y0 - startY + 10 x += textBox.Width()
} y += (2*legendDotHeight + legendMargin)
}
return Box{ } else {
Right: width, // 水平
Bottom: height + padding.Bottom + padding.Top, if index != 0 {
}, nil x += legendMargin
}
renderText = func() {
x += textPadding
legendDraw.text(text, x, y+legendDotHeight)
x += textBox.Width()
x += textPadding
}
}
if opt.Align == PositionRight {
renderText()
}
seriesColor := theme.GetSeriesColor(index)
fillColor := seriesColor
if !theme.IsDark() {
fillColor = theme.GetBackgroundColor()
}
style := chart.Style{
StrokeColor: seriesColor,
FillColor: fillColor,
StrokeWidth: 3,
}
if opt.Icon == LegendIconRect {
style.FillColor = seriesColor
style.StrokeWidth = 1
}
style.GetFillAndStrokeOptions().WriteDrawingOptionsToRenderer(r)
if opt.Icon == LegendIconRect {
legendDraw.moveTo(x, y-legendDotHeight)
legendDraw.lineTo(x+legendWidth, y-legendDotHeight)
legendDraw.lineTo(x+legendWidth, y+legendDotHeight)
legendDraw.lineTo(x, y+legendDotHeight)
legendDraw.lineTo(x, y-legendDotHeight)
r.FillStroke()
} else {
legendDraw.moveTo(x, y)
legendDraw.lineTo(x+legendWidth, y)
r.Stroke()
legendDraw.circle(float64(legendDotHeight), x+legendWidth>>1, y)
r.FillStroke()
}
x += legendWidth
if opt.Align != PositionRight {
renderText()
}
}
legendBox := padding.Clone()
// 计算展示区域
if opt.Orient == OrientVertical {
legendBox.Right = legendBox.Left + left + maxTextWidth + legendWidth + textPadding
legendBox.Bottom = legendBox.Top + y
} else {
legendBox.Right = legendBox.Left + x
legendBox.Bottom = legendBox.Top + 2*legendDotHeight + top + textPadding
}
return legendBox, nil
} }

View file

@ -26,77 +26,160 @@ import (
"testing" "testing"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/wcharczuk/go-chart/v2"
"github.com/wcharczuk/go-chart/v2/drawing"
) )
func TestNewLegend(t *testing.T) { func TestNewLegendOption(t *testing.T) {
assert := assert.New(t) assert := assert.New(t)
opt := NewLegendOption([]string{
"a",
"b",
}, PositionRight)
assert.Equal(LegendOption{
Data: []string{
"a",
"b",
},
Left: PositionRight,
}, opt)
}
func TestLegendRender(t *testing.T) {
assert := assert.New(t)
newDraw := func() *Draw {
d, _ := NewDraw(DrawOption{
Width: 400,
Height: 300,
})
return d
}
style := chart.Style{
FontSize: 10,
FontColor: drawing.ColorBlack,
}
style.Font, _ = chart.GetDefaultFont()
tests := []struct { tests := []struct {
render func(*Painter) ([]byte, error) newDraw func() *Draw
result string newLegend func(*Draw) *legend
box chart.Box
result string
}{ }{
{ {
render: func(p *Painter) ([]byte, error) { newDraw: newDraw,
_, err := NewLegendPainter(p, LegendOption{ newLegend: func(d *Draw) *legend {
return NewLegend(d, LegendOption{
Top: "10",
Data: []string{ Data: []string{
"One", "Mon",
"Two", "Tue",
"Three", "Wed",
}, },
}).Render() Style: style,
if err != nil { })
return nil, err },
} result: "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"400\" height=\"300\">\\n<path d=\"M 0 20\nL 30 20\" style=\"stroke-width:3;stroke:rgba(84,112,198,1.0);fill:rgba(255,255,255,1.0)\"/><circle cx=\"15\" cy=\"20\" r=\"5\" style=\"stroke-width:3;stroke:rgba(84,112,198,1.0);fill:rgba(255,255,255,1.0)\"/><path d=\"\" style=\"stroke-width:3;stroke:rgba(84,112,198,1.0);fill:rgba(255,255,255,1.0)\"/><text x=\"35\" y=\"25\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,0,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">Mon</text><path d=\"M 76 20\nL 106 20\" style=\"stroke-width:3;stroke:rgba(145,204,117,1.0);fill:rgba(255,255,255,1.0)\"/><circle cx=\"91\" cy=\"20\" r=\"5\" style=\"stroke-width:3;stroke:rgba(145,204,117,1.0);fill:rgba(255,255,255,1.0)\"/><path d=\"\" style=\"stroke-width:3;stroke:rgba(145,204,117,1.0);fill:rgba(255,255,255,1.0)\"/><text x=\"111\" y=\"25\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,0,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">Tue</text><path d=\"M 148 20\nL 178 20\" style=\"stroke-width:3;stroke:rgba(250,200,88,1.0);fill:rgba(255,255,255,1.0)\"/><circle cx=\"163\" cy=\"20\" r=\"5\" style=\"stroke-width:3;stroke:rgba(250,200,88,1.0);fill:rgba(255,255,255,1.0)\"/><path d=\"\" style=\"stroke-width:3;stroke:rgba(250,200,88,1.0);fill:rgba(255,255,255,1.0)\"/><text x=\"183\" y=\"25\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,0,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">Wed</text></svg>",
return p.Bytes() box: chart.Box{
Right: 214,
Bottom: 25,
}, },
result: "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"600\" height=\"400\">\\n<path d=\"M 184 9\nL 214 9\" style=\"stroke-width:3;stroke:rgba(84,112,198,1.0);fill:rgba(84,112,198,1.0)\"/><circle cx=\"199\" cy=\"9\" r=\"5\" style=\"stroke-width:3;stroke:rgba(84,112,198,1.0);fill:rgba(84,112,198,1.0)\"/><path d=\"\" style=\"stroke-width:3;stroke:rgba(84,112,198,1.0);fill:rgba(84,112,198,1.0)\"/><text x=\"216\" y=\"15\" style=\"stroke-width:0;stroke:none;fill:rgba(70,70,70,1.0);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">One</text><path d=\"M 264 9\nL 294 9\" style=\"stroke-width:3;stroke:rgba(145,204,117,1.0);fill:rgba(145,204,117,1.0)\"/><circle cx=\"279\" cy=\"9\" r=\"5\" style=\"stroke-width:3;stroke:rgba(145,204,117,1.0);fill:rgba(145,204,117,1.0)\"/><path d=\"\" style=\"stroke-width:3;stroke:rgba(145,204,117,1.0);fill:rgba(145,204,117,1.0)\"/><text x=\"296\" y=\"15\" style=\"stroke-width:0;stroke:none;fill:rgba(70,70,70,1.0);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">Two</text><path d=\"M 346 9\nL 376 9\" style=\"stroke-width:3;stroke:rgba(250,200,88,1.0);fill:rgba(250,200,88,1.0)\"/><circle cx=\"361\" cy=\"9\" r=\"5\" style=\"stroke-width:3;stroke:rgba(250,200,88,1.0);fill:rgba(250,200,88,1.0)\"/><path d=\"\" style=\"stroke-width:3;stroke:rgba(250,200,88,1.0);fill:rgba(250,200,88,1.0)\"/><text x=\"378\" y=\"15\" style=\"stroke-width:0;stroke:none;fill:rgba(70,70,70,1.0);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">Three</text></svg>",
}, },
{ {
render: func(p *Painter) ([]byte, error) { newDraw: newDraw,
_, err := NewLegendPainter(p, LegendOption{ newLegend: func(d *Draw) *legend {
return NewLegend(d, LegendOption{
Top: "10",
Left: PositionRight,
Align: PositionRight,
Data: []string{ Data: []string{
"One", "Mon",
"Two", "Tue",
"Three", "Wed",
}, },
Style: style,
})
},
result: "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"400\" height=\"300\">\\n<text x=\"191\" y=\"25\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,0,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">Mon</text><path d=\"M 222 20\nL 252 20\" style=\"stroke-width:3;stroke:rgba(84,112,198,1.0);fill:rgba(255,255,255,1.0)\"/><circle cx=\"237\" cy=\"20\" r=\"5\" style=\"stroke-width:3;stroke:rgba(84,112,198,1.0);fill:rgba(255,255,255,1.0)\"/><path d=\"\" style=\"stroke-width:3;stroke:rgba(84,112,198,1.0);fill:rgba(255,255,255,1.0)\"/><text x=\"267\" y=\"25\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,0,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">Tue</text><path d=\"M 294 20\nL 324 20\" style=\"stroke-width:3;stroke:rgba(145,204,117,1.0);fill:rgba(255,255,255,1.0)\"/><circle cx=\"309\" cy=\"20\" r=\"5\" style=\"stroke-width:3;stroke:rgba(145,204,117,1.0);fill:rgba(255,255,255,1.0)\"/><path d=\"\" style=\"stroke-width:3;stroke:rgba(145,204,117,1.0);fill:rgba(255,255,255,1.0)\"/><text x=\"339\" y=\"25\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,0,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">Wed</text><path d=\"M 370 20\nL 400 20\" style=\"stroke-width:3;stroke:rgba(250,200,88,1.0);fill:rgba(255,255,255,1.0)\"/><circle cx=\"385\" cy=\"20\" r=\"5\" style=\"stroke-width:3;stroke:rgba(250,200,88,1.0);fill:rgba(255,255,255,1.0)\"/><path d=\"\" style=\"stroke-width:3;stroke:rgba(250,200,88,1.0);fill:rgba(255,255,255,1.0)\"/></svg>",
box: chart.Box{
Right: 400,
Bottom: 25,
},
},
{
newDraw: newDraw,
newLegend: func(d *Draw) *legend {
return NewLegend(d, LegendOption{
Top: "10",
Left: PositionCenter,
Data: []string{
"Mon",
"Tue",
"Wed",
},
Style: style,
})
},
result: "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"400\" height=\"300\">\\n<path d=\"M 93 20\nL 123 20\" style=\"stroke-width:3;stroke:rgba(84,112,198,1.0);fill:rgba(255,255,255,1.0)\"/><circle cx=\"108\" cy=\"20\" r=\"5\" style=\"stroke-width:3;stroke:rgba(84,112,198,1.0);fill:rgba(255,255,255,1.0)\"/><path d=\"\" style=\"stroke-width:3;stroke:rgba(84,112,198,1.0);fill:rgba(255,255,255,1.0)\"/><text x=\"128\" y=\"25\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,0,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">Mon</text><path d=\"M 169 20\nL 199 20\" style=\"stroke-width:3;stroke:rgba(145,204,117,1.0);fill:rgba(255,255,255,1.0)\"/><circle cx=\"184\" cy=\"20\" r=\"5\" style=\"stroke-width:3;stroke:rgba(145,204,117,1.0);fill:rgba(255,255,255,1.0)\"/><path d=\"\" style=\"stroke-width:3;stroke:rgba(145,204,117,1.0);fill:rgba(255,255,255,1.0)\"/><text x=\"204\" y=\"25\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,0,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">Tue</text><path d=\"M 241 20\nL 271 20\" style=\"stroke-width:3;stroke:rgba(250,200,88,1.0);fill:rgba(255,255,255,1.0)\"/><circle cx=\"256\" cy=\"20\" r=\"5\" style=\"stroke-width:3;stroke:rgba(250,200,88,1.0);fill:rgba(255,255,255,1.0)\"/><path d=\"\" style=\"stroke-width:3;stroke:rgba(250,200,88,1.0);fill:rgba(255,255,255,1.0)\"/><text x=\"276\" y=\"25\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,0,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">Wed</text></svg>",
box: chart.Box{
Right: 307,
Bottom: 25,
},
},
{
newDraw: newDraw,
newLegend: func(d *Draw) *legend {
return NewLegend(d, LegendOption{
Top: "10",
Left: PositionLeft, Left: PositionLeft,
}).Render() Data: []string{
if err != nil { "Mon",
return nil, err "Tue",
} "Wed",
return p.Bytes() },
Style: style,
Orient: OrientVertical,
})
},
result: "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"400\" height=\"300\">\\n<path d=\"M 0 20\nL 30 20\" style=\"stroke-width:3;stroke:rgba(84,112,198,1.0);fill:rgba(255,255,255,1.0)\"/><circle cx=\"15\" cy=\"20\" r=\"5\" style=\"stroke-width:3;stroke:rgba(84,112,198,1.0);fill:rgba(255,255,255,1.0)\"/><path d=\"\" style=\"stroke-width:3;stroke:rgba(84,112,198,1.0);fill:rgba(255,255,255,1.0)\"/><text x=\"35\" y=\"25\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,0,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">Mon</text><path d=\"M 0 40\nL 30 40\" style=\"stroke-width:3;stroke:rgba(145,204,117,1.0);fill:rgba(255,255,255,1.0)\"/><circle cx=\"15\" cy=\"40\" r=\"5\" style=\"stroke-width:3;stroke:rgba(145,204,117,1.0);fill:rgba(255,255,255,1.0)\"/><path d=\"\" style=\"stroke-width:3;stroke:rgba(145,204,117,1.0);fill:rgba(255,255,255,1.0)\"/><text x=\"35\" y=\"45\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,0,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">Tue</text><path d=\"M 0 60\nL 30 60\" style=\"stroke-width:3;stroke:rgba(250,200,88,1.0);fill:rgba(255,255,255,1.0)\"/><circle cx=\"15\" cy=\"60\" r=\"5\" style=\"stroke-width:3;stroke:rgba(250,200,88,1.0);fill:rgba(255,255,255,1.0)\"/><path d=\"\" style=\"stroke-width:3;stroke:rgba(250,200,88,1.0);fill:rgba(255,255,255,1.0)\"/><text x=\"35\" y=\"65\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,0,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">Wed</text></svg>",
box: chart.Box{
Right: 61,
Bottom: 80,
}, },
result: "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"600\" height=\"400\">\\n<path d=\"M 0 9\nL 30 9\" style=\"stroke-width:3;stroke:rgba(84,112,198,1.0);fill:rgba(84,112,198,1.0)\"/><circle cx=\"15\" cy=\"9\" r=\"5\" style=\"stroke-width:3;stroke:rgba(84,112,198,1.0);fill:rgba(84,112,198,1.0)\"/><path d=\"\" style=\"stroke-width:3;stroke:rgba(84,112,198,1.0);fill:rgba(84,112,198,1.0)\"/><text x=\"32\" y=\"15\" style=\"stroke-width:0;stroke:none;fill:rgba(70,70,70,1.0);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">One</text><path d=\"M 80 9\nL 110 9\" style=\"stroke-width:3;stroke:rgba(145,204,117,1.0);fill:rgba(145,204,117,1.0)\"/><circle cx=\"95\" cy=\"9\" r=\"5\" style=\"stroke-width:3;stroke:rgba(145,204,117,1.0);fill:rgba(145,204,117,1.0)\"/><path d=\"\" style=\"stroke-width:3;stroke:rgba(145,204,117,1.0);fill:rgba(145,204,117,1.0)\"/><text x=\"112\" y=\"15\" style=\"stroke-width:0;stroke:none;fill:rgba(70,70,70,1.0);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">Two</text><path d=\"M 162 9\nL 192 9\" style=\"stroke-width:3;stroke:rgba(250,200,88,1.0);fill:rgba(250,200,88,1.0)\"/><circle cx=\"177\" cy=\"9\" r=\"5\" style=\"stroke-width:3;stroke:rgba(250,200,88,1.0);fill:rgba(250,200,88,1.0)\"/><path d=\"\" style=\"stroke-width:3;stroke:rgba(250,200,88,1.0);fill:rgba(250,200,88,1.0)\"/><text x=\"194\" y=\"15\" style=\"stroke-width:0;stroke:none;fill:rgba(70,70,70,1.0);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">Three</text></svg>",
}, },
{ {
render: func(p *Painter) ([]byte, error) { newDraw: newDraw,
_, err := NewLegendPainter(p, LegendOption{ newLegend: func(d *Draw) *legend {
return NewLegend(d, LegendOption{
Top: "10",
Left: "10%",
Data: []string{ Data: []string{
"One", "Mon",
"Two", "Tue",
"Three", "Wed",
}, },
Style: style,
Orient: OrientVertical, Orient: OrientVertical,
Icon: IconRect, })
Left: "10%",
}).Render()
if err != nil {
return nil, err
}
return p.Bytes()
}, },
result: "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"600\" height=\"400\">\\n<path d=\"M 60 3\nL 90 3\nL 90 16\nL 60 16\nL 60 3\" style=\"stroke-width:0;stroke:rgba(84,112,198,1.0);fill:rgba(84,112,198,1.0)\"/><text x=\"92\" y=\"15\" style=\"stroke-width:0;stroke:none;fill:rgba(70,70,70,1.0);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">One</text><path d=\"M 60 23\nL 90 23\nL 90 36\nL 60 36\nL 60 23\" style=\"stroke-width:0;stroke:rgba(145,204,117,1.0);fill:rgba(145,204,117,1.0)\"/><text x=\"92\" y=\"35\" style=\"stroke-width:0;stroke:none;fill:rgba(70,70,70,1.0);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">Two</text><path d=\"M 60 43\nL 90 43\nL 90 56\nL 60 56\nL 60 43\" style=\"stroke-width:0;stroke:rgba(250,200,88,1.0);fill:rgba(250,200,88,1.0)\"/><text x=\"92\" y=\"55\" style=\"stroke-width:0;stroke:none;fill:rgba(70,70,70,1.0);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">Three</text></svg>", box: chart.Box{
Right: 101,
Bottom: 80,
},
result: "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"400\" height=\"300\">\\n<path d=\"M 40 20\nL 70 20\" style=\"stroke-width:3;stroke:rgba(84,112,198,1.0);fill:rgba(255,255,255,1.0)\"/><circle cx=\"55\" cy=\"20\" r=\"5\" style=\"stroke-width:3;stroke:rgba(84,112,198,1.0);fill:rgba(255,255,255,1.0)\"/><path d=\"\" style=\"stroke-width:3;stroke:rgba(84,112,198,1.0);fill:rgba(255,255,255,1.0)\"/><text x=\"75\" y=\"25\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,0,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">Mon</text><path d=\"M 40 40\nL 70 40\" style=\"stroke-width:3;stroke:rgba(145,204,117,1.0);fill:rgba(255,255,255,1.0)\"/><circle cx=\"55\" cy=\"40\" r=\"5\" style=\"stroke-width:3;stroke:rgba(145,204,117,1.0);fill:rgba(255,255,255,1.0)\"/><path d=\"\" style=\"stroke-width:3;stroke:rgba(145,204,117,1.0);fill:rgba(255,255,255,1.0)\"/><text x=\"75\" y=\"45\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,0,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">Tue</text><path d=\"M 40 60\nL 70 60\" style=\"stroke-width:3;stroke:rgba(250,200,88,1.0);fill:rgba(255,255,255,1.0)\"/><circle cx=\"55\" cy=\"60\" r=\"5\" style=\"stroke-width:3;stroke:rgba(250,200,88,1.0);fill:rgba(255,255,255,1.0)\"/><path d=\"\" style=\"stroke-width:3;stroke:rgba(250,200,88,1.0);fill:rgba(255,255,255,1.0)\"/><text x=\"75\" y=\"65\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,0,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">Wed</text></svg>",
}, },
} }
for _, tt := range tests { for _, tt := range tests {
p, err := NewPainter(PainterOptions{ d := tt.newDraw()
Type: ChartOutputSVG, b, err := tt.newLegend(d).Render()
Width: 600,
Height: 400,
}, PainterThemeOption(defaultTheme))
assert.Nil(err) assert.Nil(err)
data, err := tt.render(p) assert.Equal(tt.box, b)
data, err := d.Bytes()
assert.Nil(err) assert.Nil(err)
assert.NotEmpty(data)
assert.Equal(tt.result, string(data)) assert.Equal(tt.result, string(data))
} }
} }

103
line.go Normal file
View file

@ -0,0 +1,103 @@
// 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 (
"github.com/wcharczuk/go-chart/v2"
"github.com/wcharczuk/go-chart/v2/drawing"
)
type LineStyle struct {
ClassName string
StrokeDashArray []float64
StrokeColor drawing.Color
StrokeWidth float64
FillColor drawing.Color
DotWidth float64
DotColor drawing.Color
DotFillColor drawing.Color
}
func (ls *LineStyle) Style() chart.Style {
return chart.Style{
ClassName: ls.ClassName,
StrokeDashArray: ls.StrokeDashArray,
StrokeColor: ls.StrokeColor,
StrokeWidth: ls.StrokeWidth,
FillColor: ls.FillColor,
DotWidth: ls.DotWidth,
DotColor: ls.DotColor,
}
}
func (d *Draw) lineFill(points []Point, style LineStyle) {
s := style.Style()
if !(s.ShouldDrawStroke() && s.ShouldDrawFill()) {
return
}
newPoints := make([]Point, len(points))
copy(newPoints, points)
x0 := points[0].X
y0 := points[0].Y
height := d.Box.Height()
newPoints = append(newPoints, Point{
X: points[len(points)-1].X,
Y: height,
}, Point{
X: x0,
Y: height,
}, Point{
X: x0,
Y: y0,
})
d.fill(newPoints, style.Style())
}
func (d *Draw) lineDot(points []Point, style LineStyle) {
s := style.Style()
if !s.ShouldDrawDot() {
return
}
r := d.Render
dotWith := s.GetDotWidth()
s.GetDotOptions().WriteDrawingOptionsToRenderer(r)
for _, point := range points {
if !style.DotFillColor.IsZero() {
r.SetFillColor(style.DotFillColor)
}
r.SetStrokeColor(s.DotColor)
d.circle(dotWith, point.X, point.Y)
r.FillStroke()
}
}
func (d *Draw) Line(points []Point, style LineStyle) {
if len(points) == 0 {
return
}
d.lineFill(points, style)
d.lineStroke(points, style)
d.lineDot(points, style)
}

View file

@ -23,218 +23,109 @@
package charts package charts
import ( import (
"math"
"github.com/golang/freetype/truetype" "github.com/golang/freetype/truetype"
"git.smarteching.com/zeni/go-chart/v2/drawing" "github.com/wcharczuk/go-chart/v2"
"github.com/wcharczuk/go-chart/v2/drawing"
) )
type lineChart struct { type lineChartOption struct {
p *Painter Theme string
opt *LineChartOption
}
// NewLineChart returns a line chart render
func NewLineChart(p *Painter, opt LineChartOption) *lineChart {
if opt.Theme == nil {
opt.Theme = defaultTheme
}
return &lineChart{
p: p,
opt: &opt,
}
}
type LineChartOption struct {
// The theme
Theme ColorPalette
// The font size
Font *truetype.Font
// The data series list
SeriesList SeriesList SeriesList SeriesList
// The x axis option Font *truetype.Font
XAxis XAxisOption
// The padding of line chart
Padding Box
// The y axis option
YAxisOptions []YAxisOption
// The option of title
Title TitleOption
// The legend option
Legend LegendOption
// The flag for show symbol of line, set this to *false will hide symbol
SymbolShow *bool
// The stroke width of line
StrokeWidth float64
// Fill the area of line
FillArea bool
// background is filled
backgroundIsFilled bool
// background fill (alpha) opacity
Opacity uint8
} }
func (l *lineChart) render(result *defaultRenderResult, seriesList SeriesList) (Box, error) { func lineChartRender(opt lineChartOption, result *basicRenderResult) ([]markPointRenderOption, error) {
p := l.p
opt := l.opt
boundaryGap := true
if isFalse(opt.XAxis.BoundaryGap) {
boundaryGap = false
}
seriesPainter := result.seriesPainter theme := NewTheme(opt.Theme)
xDivideCount := len(opt.XAxis.Data) d, err := NewDraw(DrawOption{
if !boundaryGap { Parent: result.d,
xDivideCount-- }, PaddingOption(chart.Box{
Top: result.titleBox.Height(),
Left: YAxisWidth,
}))
if err != nil {
return nil, err
} }
xDivideValues := autoDivide(seriesPainter.Width(), xDivideCount) seriesNames := opt.SeriesList.Names()
xValues := make([]int, len(xDivideValues)-1)
if boundaryGap { r := d.Render
for i := 0; i < len(xDivideValues)-1; i++ { xRange := result.xRange
xValues[i] = (xDivideValues[i] + xDivideValues[i+1]) >> 1 markPointRenderOptions := make([]markPointRenderOption, 0)
for i, s := range opt.SeriesList {
// 由于series是for range为同一个数据因此需要clone
// 后续需要使用如mark point
series := s
index := series.index
if index == 0 {
index = i
} }
} else { seriesColor := theme.GetSeriesColor(index)
xValues = xDivideValues
} yRange := result.getYRange(series.YAxisIndex)
markPointPainter := NewMarkPointPainter(seriesPainter) points := make([]Point, 0, len(series.Data))
markLinePainter := NewMarkLinePainter(seriesPainter) // mark line
rendererList := []Renderer{ markLineRender(markLineRenderOption{
markPointPainter, Draw: d,
markLinePainter, FillColor: seriesColor,
} FontColor: theme.GetTextColor(),
strokeWidth := opt.StrokeWidth
if strokeWidth == 0 {
strokeWidth = defaultStrokeWidth
}
seriesNames := seriesList.Names()
for index := range seriesList {
series := seriesList[index]
seriesColor := opt.Theme.GetSeriesColor(series.index)
drawingStyle := Style{
StrokeColor: seriesColor, StrokeColor: seriesColor,
StrokeWidth: strokeWidth, Font: opt.Font,
} Series: &series,
if len(series.Style.StrokeDashArray) > 0 { Range: yRange,
drawingStyle.StrokeDashArray = series.Style.StrokeDashArray })
}
yRange := result.axisRanges[series.AxisIndex] for j, item := range series.Data {
points := make([]Point, 0) if j >= xRange.divideCount {
var labelPainter *SeriesLabelPainter
if series.Label.Show {
labelPainter = NewSeriesLabelPainter(SeriesLabelPainterParams{
P: seriesPainter,
SeriesNames: seriesNames,
Label: series.Label,
Theme: opt.Theme,
Font: opt.Font,
})
rendererList = append(rendererList, labelPainter)
}
for i, item := range series.Data {
h := yRange.getRestHeight(item.Value)
if item.Value == nullValue {
h = int(math.MaxInt32)
}
p := Point{
X: xValues[i],
Y: h,
}
points = append(points, p)
// 如果label不需要展示则返回
if labelPainter == nil {
continue continue
} }
labelPainter.Add(LabelValue{ y := yRange.getRestHeight(item.Value)
Index: index, x := xRange.getWidth(float64(j))
Value: item.Value, points = append(points, Point{
X: p.X, Y: y,
Y: p.Y, X: x,
// 字体大小
FontSize: series.Label.FontSize,
}) })
} if !series.Label.Show {
// 如果需要填充区域 continue
if opt.FillArea {
areaPoints := make([]Point, len(points))
copy(areaPoints, points)
bottomY := yRange.getRestHeight(yRange.min)
var opacity uint8 = 200
if opt.Opacity != 0 {
opacity = opt.Opacity
} }
areaPoints = append(areaPoints, Point{ distance := series.Label.Distance
X: areaPoints[len(areaPoints)-1].X, if distance == 0 {
Y: bottomY, distance = 5
}, Point{ }
X: areaPoints[0].X, text := NewValueLabelFormater(seriesNames, series.Label.Formatter)(i, item.Value, -1)
Y: bottomY, labelStyle := chart.Style{
}, areaPoints[0]) FontColor: theme.GetTextColor(),
seriesPainter.SetDrawingStyle(Style{ FontSize: labelFontSize,
FillColor: seriesColor.WithAlpha(opacity), Font: opt.Font,
}) }
seriesPainter.FillArea(areaPoints) if !series.Label.Color.IsZero() {
labelStyle.FontColor = series.Label.Color
}
labelStyle.GetTextOptions().WriteToRenderer(r)
textBox := r.MeasureText(text)
d.text(text, x-textBox.Width()>>1, y-distance)
} }
seriesPainter.SetDrawingStyle(drawingStyle)
// 画线 dotFillColor := drawing.ColorWhite
seriesPainter.LineStroke(points) if theme.IsDark() {
dotFillColor = seriesColor
// 画点
if opt.Theme.IsDark() {
drawingStyle.FillColor = drawingStyle.StrokeColor
} else {
drawingStyle.FillColor = drawing.ColorWhite
} }
drawingStyle.StrokeWidth = 1 d.Line(points, LineStyle{
seriesPainter.SetDrawingStyle(drawingStyle) StrokeColor: seriesColor,
if !isFalse(opt.SymbolShow) { StrokeWidth: 2,
seriesPainter.Dots(points) DotColor: seriesColor,
} DotWidth: defaultDotWidth,
markPointPainter.Add(markPointRenderOption{ DotFillColor: dotFillColor,
})
// draw mark point
markPointRenderOptions = append(markPointRenderOptions, markPointRenderOption{
Draw: d,
FillColor: seriesColor, FillColor: seriesColor,
Font: opt.Font, Font: opt.Font,
Points: points, Points: points,
Series: series, Series: &series,
})
markLinePainter.Add(markLineRenderOption{
FillColor: seriesColor,
FontColor: opt.Theme.GetTextColor(),
StrokeColor: seriesColor,
Font: opt.Font,
Series: series,
Range: yRange,
}) })
} }
// 最大、最小的mark point
err := doRender(rendererList...)
if err != nil {
return BoxZero, err
}
return p.box, nil return markPointRenderOptions, nil
}
func (l *lineChart) Render() (Box, error) {
p := l.p
opt := l.opt
renderResult, err := defaultRender(p, defaultRenderOption{
Theme: opt.Theme,
Padding: opt.Padding,
SeriesList: opt.SeriesList,
XAxis: opt.XAxis,
YAxisOptions: opt.YAxisOptions,
TitleOption: opt.Title,
LegendOption: opt.Legend,
backgroundIsFilled: opt.backgroundIsFilled,
})
if err != nil {
return BoxZero, err
}
seriesList := opt.SeriesList.Filter(ChartTypeLine)
return l.render(renderResult, seriesList)
} }

File diff suppressed because one or more lines are too long

165
line_test.go Normal file
View file

@ -0,0 +1,165 @@
// 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 TestLineStyle(t *testing.T) {
assert := assert.New(t)
ls := LineStyle{
ClassName: "test",
StrokeDashArray: []float64{
1.0,
},
StrokeColor: drawing.ColorBlack,
StrokeWidth: 1,
FillColor: drawing.ColorBlack.WithAlpha(60),
DotWidth: 2,
DotColor: drawing.ColorBlack,
DotFillColor: drawing.ColorWhite,
}
assert.Equal(chart.Style{
ClassName: "test",
StrokeDashArray: []float64{
1.0,
},
StrokeColor: drawing.ColorBlack,
StrokeWidth: 1,
FillColor: drawing.ColorBlack.WithAlpha(60),
DotWidth: 2,
DotColor: drawing.ColorBlack,
}, ls.Style())
}
func TestDrawLineFill(t *testing.T) {
assert := assert.New(t)
ls := LineStyle{
StrokeColor: drawing.ColorBlack,
StrokeWidth: 1,
FillColor: drawing.ColorBlack.WithAlpha(60),
DotWidth: 2,
DotColor: drawing.ColorBlack,
DotFillColor: drawing.ColorWhite,
}
d, err := NewDraw(DrawOption{
Width: 400,
Height: 300,
})
assert.Nil(err)
d.lineFill([]Point{
{
X: 0,
Y: 0,
},
{
X: 10,
Y: 20,
},
{
X: 50,
Y: 60,
},
}, ls)
data, err := d.Bytes()
assert.Nil(err)
assert.Equal("<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"400\" height=\"300\">\\n<path d=\"M 0 0\nL 10 20\nL 50 60\nL 50 300\nL 0 300\nL 0 0\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,0,0.2)\"/></svg>", string(data))
}
func TestDrawLineDot(t *testing.T) {
assert := assert.New(t)
ls := LineStyle{
StrokeColor: drawing.ColorBlack,
StrokeWidth: 1,
FillColor: drawing.ColorBlack.WithAlpha(60),
DotWidth: 2,
DotColor: drawing.ColorBlack,
DotFillColor: drawing.ColorWhite,
}
d, err := NewDraw(DrawOption{
Width: 400,
Height: 300,
})
assert.Nil(err)
d.lineDot([]Point{
{
X: 0,
Y: 0,
},
{
X: 10,
Y: 20,
},
{
X: 50,
Y: 60,
},
}, ls)
data, err := d.Bytes()
assert.Nil(err)
assert.Equal("<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"400\" height=\"300\">\\n<circle cx=\"0\" cy=\"0\" r=\"2\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:rgba(255,255,255,1.0)\"/><path d=\"\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:rgba(255,255,255,1.0)\"/><circle cx=\"10\" cy=\"20\" r=\"2\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:rgba(255,255,255,1.0)\"/><path d=\"\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:rgba(255,255,255,1.0)\"/><circle cx=\"50\" cy=\"60\" r=\"2\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:rgba(255,255,255,1.0)\"/><path d=\"\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:rgba(255,255,255,1.0)\"/></svg>", string(data))
}
func TestDrawLine(t *testing.T) {
assert := assert.New(t)
ls := LineStyle{
StrokeColor: drawing.ColorBlack,
StrokeWidth: 1,
FillColor: drawing.ColorBlack.WithAlpha(60),
DotWidth: 2,
DotColor: drawing.ColorBlack,
DotFillColor: drawing.ColorWhite,
}
d, err := NewDraw(DrawOption{
Width: 400,
Height: 300,
})
assert.Nil(err)
d.Line([]Point{
{
X: 0,
Y: 0,
},
{
X: 10,
Y: 20,
},
{
X: 50,
Y: 60,
},
}, ls)
data, err := d.Bytes()
assert.Nil(err)
assert.Equal("<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"400\" height=\"300\">\\n<path d=\"M 0 0\nL 10 20\nL 50 60\nL 50 300\nL 0 300\nL 0 0\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,0,0.2)\"/><path d=\"M 0 0\nL 10 20\nL 50 60\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:none\"/><circle cx=\"0\" cy=\"0\" r=\"2\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:rgba(255,255,255,1.0)\"/><path d=\"\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:rgba(255,255,255,1.0)\"/><circle cx=\"10\" cy=\"20\" r=\"2\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:rgba(255,255,255,1.0)\"/><path d=\"\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:rgba(255,255,255,1.0)\"/><circle cx=\"50\" cy=\"60\" r=\"2\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:rgba(255,255,255,1.0)\"/><path d=\"\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:rgba(255,255,255,1.0)\"/></svg>", string(data))
}

View file

@ -24,9 +24,10 @@ package charts
import ( import (
"github.com/golang/freetype/truetype" "github.com/golang/freetype/truetype"
"github.com/wcharczuk/go-chart/v2"
"github.com/wcharczuk/go-chart/v2/drawing"
) )
// NewMarkLine returns a series mark line
func NewMarkLine(markLineTypes ...string) SeriesMarkLine { func NewMarkLine(markLineTypes ...string) SeriesMarkLine {
data := make([]SeriesMarkData, len(markLineTypes)) data := make([]SeriesMarkData, len(markLineTypes))
for index, t := range markLineTypes { for index, t := range markLineTypes {
@ -39,75 +40,53 @@ func NewMarkLine(markLineTypes ...string) SeriesMarkLine {
} }
} }
type markLinePainter struct {
p *Painter
options []markLineRenderOption
}
func (m *markLinePainter) Add(opt markLineRenderOption) {
m.options = append(m.options, opt)
}
// NewMarkLinePainter returns a mark line renderer
func NewMarkLinePainter(p *Painter) *markLinePainter {
return &markLinePainter{
p: p,
options: make([]markLineRenderOption, 0),
}
}
type markLineRenderOption struct { type markLineRenderOption struct {
FillColor Color Draw *Draw
FontColor Color FillColor drawing.Color
StrokeColor Color FontColor drawing.Color
StrokeColor drawing.Color
Font *truetype.Font Font *truetype.Font
Series Series Series *Series
Range axisRange Range *Range
} }
func (m *markLinePainter) Render() (Box, error) { func markLineRender(opt markLineRenderOption) {
painter := m.p d := opt.Draw
for _, opt := range m.options { s := opt.Series
s := opt.Series if len(s.MarkLine.Data) == 0 {
if len(s.MarkLine.Data) == 0 { return
continue
}
font := opt.Font
if font == nil {
font, _ = GetDefaultFont()
}
summary := s.Summary()
for _, markLine := range s.MarkLine.Data {
// 由于mark line会修改style因此每次重新设置
painter.OverrideDrawingStyle(Style{
FillColor: opt.FillColor,
StrokeColor: opt.StrokeColor,
StrokeWidth: 1,
StrokeDashArray: []float64{
4,
2,
},
}).OverrideTextStyle(Style{
Font: font,
FontColor: opt.FontColor,
FontSize: labelFontSize,
})
value := float64(0)
switch markLine.Type {
case SeriesMarkDataTypeMax:
value = summary.MaxValue
case SeriesMarkDataTypeMin:
value = summary.MinValue
default:
value = summary.AverageValue
}
y := opt.Range.getRestHeight(value)
width := painter.Width()
text := commafWithDigits(value)
textBox := painter.MeasureText(text)
painter.MarkLine(0, y, width-2)
painter.Text(text, width, y+textBox.Height()>>1-2)
}
} }
return BoxZero, nil r := d.Render
summary := s.Summary()
for _, markLine := range s.MarkLine.Data {
// 由于mark line会修改style因此每次重新设置
chart.Style{
FillColor: opt.FillColor,
FontColor: opt.FontColor,
FontSize: labelFontSize,
StrokeColor: opt.StrokeColor,
StrokeWidth: 1,
Font: opt.Font,
StrokeDashArray: []float64{
4,
2,
},
}.WriteToRenderer(r)
value := float64(0)
switch markLine.Type {
case SeriesMarkDataTypeMax:
value = summary.MaxValue
case SeriesMarkDataTypeMin:
value = summary.MinValue
default:
value = summary.AverageValue
}
y := opt.Range.getRestHeight(value)
width := d.Box.Width()
text := commafWithDigits(value)
textBox := r.MeasureText(text)
d.makeLine(0, y, width-2)
d.text(text, width, y+textBox.Height()>>1-2)
}
} }

View file

@ -26,65 +26,74 @@ import (
"testing" "testing"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"git.smarteching.com/zeni/go-chart/v2/drawing" "github.com/wcharczuk/go-chart/v2"
"github.com/wcharczuk/go-chart/v2/drawing"
) )
func TestMarkLine(t *testing.T) { func TestNewMarkLine(t *testing.T) {
assert := assert.New(t) assert := assert.New(t)
tests := []struct { markLine := NewMarkLine(
render func(*Painter) ([]byte, error) SeriesMarkDataTypeMax,
result string SeriesMarkDataTypeMin,
}{ SeriesMarkDataTypeAverage,
{ )
render: func(p *Painter) ([]byte, error) {
markLine := NewMarkLinePainter(p) assert.Equal(SeriesMarkLine{
series := NewSeriesFromValues([]float64{ Data: []SeriesMarkData{
1, {
2, Type: SeriesMarkDataTypeMax,
3, },
}) {
series.MarkLine = NewMarkLine( Type: SeriesMarkDataTypeMin,
SeriesMarkDataTypeMax, },
SeriesMarkDataTypeAverage, {
SeriesMarkDataTypeMin, Type: SeriesMarkDataTypeAverage,
)
markLine.Add(markLineRenderOption{
FillColor: drawing.ColorBlack,
FontColor: drawing.ColorBlack,
StrokeColor: drawing.ColorBlack,
Series: series,
Range: NewRange(AxisRangeOption{
Painter: p,
Min: 0,
Max: 5,
Size: p.Height(),
DivideCount: 6,
}),
})
_, err := markLine.Render()
if err != nil {
return nil, err
}
return p.Bytes()
}, },
result: "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"600\" height=\"400\">\\n<circle cx=\"23\" cy=\"272\" r=\"3\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:rgba(0,0,0,1.0)\"/><path stroke-dasharray=\"4.0, 2.0\" d=\"\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:rgba(0,0,0,1.0)\"/><path stroke-dasharray=\"4.0, 2.0\" d=\"M 29 272\nL 562 272\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:rgba(0,0,0,1.0)\"/><path stroke-dasharray=\"4.0, 2.0\" d=\"M 562 267\nL 578 272\nL 562 277\nL 567 272\nL 562 267\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:rgba(0,0,0,1.0)\"/><text x=\"580\" y=\"276\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,0,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">3</text><circle cx=\"23\" cy=\"308\" r=\"3\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:rgba(0,0,0,1.0)\"/><path stroke-dasharray=\"4.0, 2.0\" d=\"\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:rgba(0,0,0,1.0)\"/><path stroke-dasharray=\"4.0, 2.0\" d=\"M 29 308\nL 562 308\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:rgba(0,0,0,1.0)\"/><path stroke-dasharray=\"4.0, 2.0\" d=\"M 562 303\nL 578 308\nL 562 313\nL 567 308\nL 562 303\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:rgba(0,0,0,1.0)\"/><text x=\"580\" y=\"312\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,0,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">2</text><circle cx=\"23\" cy=\"344\" r=\"3\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:rgba(0,0,0,1.0)\"/><path stroke-dasharray=\"4.0, 2.0\" d=\"\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:rgba(0,0,0,1.0)\"/><path stroke-dasharray=\"4.0, 2.0\" d=\"M 29 344\nL 562 344\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:rgba(0,0,0,1.0)\"/><path stroke-dasharray=\"4.0, 2.0\" d=\"M 562 339\nL 578 344\nL 562 349\nL 567 344\nL 562 339\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:rgba(0,0,0,1.0)\"/><text x=\"580\" y=\"348\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,0,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">1</text></svg>",
}, },
} }, markLine)
for _, tt := range tests { }
p, err := NewPainter(PainterOptions{
Type: ChartOutputSVG, func TestMarkLineRender(t *testing.T) {
Width: 600, assert := assert.New(t)
Height: 400,
}, PainterThemeOption(defaultTheme)) d, err := NewDraw(DrawOption{
assert.Nil(err) Width: 400,
data, err := tt.render(p.Child(PainterPaddingOption(Box{ Height: 300,
Left: 20, }, PaddingOption(chart.Box{
Top: 20, Left: 20,
Right: 20, Right: 20,
Bottom: 20, }))
}))) assert.Nil(err)
assert.Nil(err) f, _ := chart.GetDefaultFont()
assert.Equal(tt.result, string(data))
} markLineRender(markLineRenderOption{
Draw: d,
FillColor: drawing.ColorBlack,
FontColor: drawing.ColorBlack,
StrokeColor: drawing.ColorBlack,
Font: f,
Series: &Series{
MarkLine: NewMarkLine(
SeriesMarkDataTypeMax,
SeriesMarkDataTypeMin,
SeriesMarkDataTypeAverage,
),
Data: NewSeriesDataFromValues([]float64{
1,
3,
5,
7,
9,
}),
},
Range: &Range{
Min: 0,
Max: 10,
Size: 200,
},
})
data, err := d.Bytes()
assert.Nil(err)
assert.Equal("<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"400\" height=\"300\">\\n<circle cx=\"20\" cy=\"20\" r=\"3\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:rgba(0,0,0,1.0)\"/><path stroke-dasharray=\"4.0, 2.0\" d=\"\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:rgba(0,0,0,1.0)\"/><path stroke-dasharray=\"4.0, 2.0\" d=\"M 25 20\nL 362 20\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:rgba(0,0,0,1.0)\"/><path d=\"M 362 15\nL 378 20\nL 362 25\nL 367 20\nL 362 15\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:rgba(0,0,0,1.0)\"/><text x=\"380\" y=\"24\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,0,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">9</text><circle cx=\"20\" cy=\"180\" r=\"3\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:rgba(0,0,0,1.0)\"/><path stroke-dasharray=\"4.0, 2.0\" d=\"\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:rgba(0,0,0,1.0)\"/><path stroke-dasharray=\"4.0, 2.0\" d=\"M 25 180\nL 362 180\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:rgba(0,0,0,1.0)\"/><path d=\"M 362 175\nL 378 180\nL 362 185\nL 367 180\nL 362 175\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:rgba(0,0,0,1.0)\"/><text x=\"380\" y=\"184\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,0,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">1</text><circle cx=\"20\" cy=\"100\" r=\"3\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:rgba(0,0,0,1.0)\"/><path stroke-dasharray=\"4.0, 2.0\" d=\"\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:rgba(0,0,0,1.0)\"/><path stroke-dasharray=\"4.0, 2.0\" d=\"M 25 100\nL 362 100\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:rgba(0,0,0,1.0)\"/><path d=\"M 362 95\nL 378 100\nL 362 105\nL 367 100\nL 362 95\" style=\"stroke-width:1;stroke:rgba(0,0,0,1.0);fill:rgba(0,0,0,1.0)\"/><text x=\"380\" y=\"104\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,0,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">5</text></svg>", string(data))
} }

View file

@ -24,9 +24,10 @@ package charts
import ( import (
"github.com/golang/freetype/truetype" "github.com/golang/freetype/truetype"
"github.com/wcharczuk/go-chart/v2"
"github.com/wcharczuk/go-chart/v2/drawing"
) )
// NewMarkPoint returns a series mark point
func NewMarkPoint(markPointTypes ...string) SeriesMarkPoint { func NewMarkPoint(markPointTypes ...string) SeriesMarkPoint {
data := make([]SeriesMarkData, len(markPointTypes)) data := make([]SeriesMarkData, len(markPointTypes))
for index, t := range markPointTypes { for index, t := range markPointTypes {
@ -39,77 +40,50 @@ func NewMarkPoint(markPointTypes ...string) SeriesMarkPoint {
} }
} }
type markPointPainter struct {
p *Painter
options []markPointRenderOption
}
func (m *markPointPainter) Add(opt markPointRenderOption) {
m.options = append(m.options, opt)
}
type markPointRenderOption struct { type markPointRenderOption struct {
FillColor Color Draw *Draw
FillColor drawing.Color
Font *truetype.Font Font *truetype.Font
Series Series Series *Series
Points []Point Points []Point
} }
// NewMarkPointPainter returns a mark point renderer func markPointRender(opt markPointRenderOption) {
func NewMarkPointPainter(p *Painter) *markPointPainter { d := opt.Draw
return &markPointPainter{ s := opt.Series
p: p, if len(s.MarkPoint.Data) == 0 {
options: make([]markPointRenderOption, 0), return
}
points := opt.Points
summary := s.Summary()
symbolSize := s.MarkPoint.SymbolSize
if symbolSize == 0 {
symbolSize = 30
}
r := d.Render
// 设置填充样式
chart.Style{
FillColor: opt.FillColor,
}.WriteToRenderer(r)
// 设置文本样式
chart.Style{
FontColor: NewTheme(ThemeDark).GetTextColor(),
FontSize: labelFontSize,
StrokeWidth: 1,
Font: opt.Font,
}.WriteTextOptionsToRenderer(r)
for _, markPointData := range s.MarkPoint.Data {
p := points[summary.MinIndex]
value := summary.MinValue
switch markPointData.Type {
case SeriesMarkDataTypeMax:
p = points[summary.MaxIndex]
value = summary.MaxValue
}
d.pin(p.X, p.Y-symbolSize>>1, symbolSize)
text := commafWithDigits(value)
textBox := r.MeasureText(text)
d.text(text, p.X-textBox.Width()>>1, p.Y-symbolSize>>1-2)
} }
} }
func (m *markPointPainter) Render() (Box, error) {
painter := m.p
for _, opt := range m.options {
s := opt.Series
if len(s.MarkPoint.Data) == 0 {
continue
}
points := opt.Points
summary := s.Summary()
symbolSize := s.MarkPoint.SymbolSize
if symbolSize == 0 {
symbolSize = 30
}
textStyle := Style{
FontSize: labelFontSize,
StrokeWidth: 1,
Font: opt.Font,
}
if isLightColor(opt.FillColor) {
textStyle.FontColor = defaultLightFontColor
} else {
textStyle.FontColor = defaultDarkFontColor
}
painter.OverrideDrawingStyle(Style{
FillColor: opt.FillColor,
}).OverrideTextStyle(textStyle)
for _, markPointData := range s.MarkPoint.Data {
textStyle.FontSize = labelFontSize
painter.OverrideTextStyle(textStyle)
p := points[summary.MinIndex]
value := summary.MinValue
switch markPointData.Type {
case SeriesMarkDataTypeMax:
p = points[summary.MaxIndex]
value = summary.MaxValue
}
painter.Pin(p.X, p.Y-symbolSize>>1, symbolSize)
text := commafWithDigits(value)
textBox := painter.MeasureText(text)
if textBox.Width() > symbolSize {
textStyle.FontSize = smallLabelFontSize
painter.OverrideTextStyle(textStyle)
textBox = painter.MeasureText(text)
}
painter.Text(text, p.X-textBox.Width()>>1, p.Y-symbolSize>>1-2)
}
}
return BoxZero, nil
}

View file

@ -26,67 +26,78 @@ import (
"testing" "testing"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"git.smarteching.com/zeni/go-chart/v2/drawing" "github.com/wcharczuk/go-chart/v2"
"github.com/wcharczuk/go-chart/v2/drawing"
) )
func TestMarkPoint(t *testing.T) { func TestNewMarkPoint(t *testing.T) {
assert := assert.New(t) assert := assert.New(t)
tests := []struct { markPoint := NewMarkPoint(
render func(*Painter) ([]byte, error) SeriesMarkDataTypeMax,
result string SeriesMarkDataTypeMin,
}{ SeriesMarkDataTypeAverage,
{ )
render: func(p *Painter) ([]byte, error) {
series := NewSeriesFromValues([]float64{
1,
2,
3,
})
series.MarkPoint = NewMarkPoint(SeriesMarkDataTypeMax)
markPoint := NewMarkPointPainter(p)
markPoint.Add(markPointRenderOption{
FillColor: drawing.ColorBlack,
Series: series,
Points: []Point{
{
X: 10,
Y: 10,
},
{
X: 30,
Y: 30,
},
{
X: 50,
Y: 50,
},
},
})
_, err := markPoint.Render()
if err != nil {
return nil, err
}
return p.Bytes()
},
result: "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"600\" height=\"400\">\\n<path d=\"M 67 62\nA 15 15 330.00 1 1 73 62\nL 70 48\nZ\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,0,1.0)\"/><path d=\"M 55 48\nQ70,85 85,48\nZ\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,0,1.0)\"/><text x=\"66\" y=\"53\" style=\"stroke-width:0;stroke:none;fill:rgba(238,238,238,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">3</text></svg>",
},
}
for _, tt := range tests { assert.Equal(SeriesMarkPoint{
p, err := NewPainter(PainterOptions{ Data: []SeriesMarkData{
Type: ChartOutputSVG, {
Width: 600, Type: SeriesMarkDataTypeMax,
Height: 400, },
}, PainterThemeOption(defaultTheme)) {
assert.Nil(err) Type: SeriesMarkDataTypeMin,
data, err := tt.render(p.Child(PainterPaddingOption(Box{ },
Left: 20, {
Top: 20, Type: SeriesMarkDataTypeAverage,
Right: 20, },
Bottom: 20, },
}))) }, markPoint)
assert.Nil(err) }
assert.Equal(tt.result, string(data))
} func TestMarkPointRender(t *testing.T) {
assert := assert.New(t)
d, err := NewDraw(DrawOption{
Width: 400,
Height: 300,
}, PaddingOption(chart.Box{
Left: 20,
Right: 20,
}))
assert.Nil(err)
f, _ := chart.GetDefaultFont()
markPointRender(markPointRenderOption{
Draw: d,
FillColor: drawing.ColorBlack,
Font: f,
Series: &Series{
MarkPoint: NewMarkPoint(
SeriesMarkDataTypeMax,
SeriesMarkDataTypeMin,
),
Data: NewSeriesDataFromValues([]float64{
1,
3,
5,
}),
},
Points: []Point{
{
X: 1,
Y: 50,
},
{
X: 100,
Y: 100,
},
{
X: 200,
Y: 200,
},
},
})
data, err := d.Bytes()
assert.Nil(err)
assert.Equal("<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"400\" height=\"300\">\\n<path d=\"M 217 192\nA 15 15 330.00 1 1 223 192\nL 220 178\nZ\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,0,1.0)\"/><path d=\"M 205 178\nQ220,215 235,178\nZ\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,0,1.0)\"/><text x=\"216\" y=\"183\" style=\"stroke-width:0;stroke:none;fill:rgba(238,238,238,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">5</text><path d=\"M 18 42\nA 15 15 330.00 1 1 24 42\nL 21 28\nZ\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,0,1.0)\"/><path d=\"M 6 28\nQ21,65 36,28\nZ\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,0,1.0)\"/><text x=\"17\" y=\"33\" style=\"stroke-width:0;stroke:none;fill:rgba(238,238,238,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">1</text></svg>", string(data))
} }

View file

@ -1,866 +0,0 @@
// 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 (
"bytes"
"errors"
"math"
"github.com/golang/freetype/truetype"
"git.smarteching.com/zeni/go-chart/v2"
)
type ValueFormatter func(float64) string
type Painter struct {
render chart.Renderer
box Box
font *truetype.Font
parent *Painter
style Style
theme ColorPalette
// 类型
outputType string
valueFormatter ValueFormatter
}
type PainterOptions struct {
// Draw type, "svg" or "png", default type is "png"
Type string
// The width of draw painter
Width int
// The height of draw painter
Height int
// The font for painter
Font *truetype.Font
}
type PainterOption func(*Painter)
type TicksOption struct {
// the first tick
First int
Length int
Orient string
Count int
Unit int
}
type MultiTextOption struct {
TextList []string
Orient string
Unit int
Position string
Align string
// The text rotation of label
TextRotation float64
Offset Box
// The first text index
First int
}
type GridOption struct {
Column int
Row int
ColumnSpans []int
// 忽略不展示的column
IgnoreColumnLines []int
// 忽略不展示的row
IgnoreRowLines []int
}
// PainterPaddingOption sets the padding of draw painter
func PainterPaddingOption(padding Box) PainterOption {
return func(p *Painter) {
p.box.Left += padding.Left
p.box.Top += padding.Top
p.box.Right -= padding.Right
p.box.Bottom -= padding.Bottom
}
}
// PainterBoxOption sets the box of draw painter
func PainterBoxOption(box Box) PainterOption {
return func(p *Painter) {
if box.IsZero() {
return
}
p.box = box
}
}
// PainterFontOption sets the font of draw painter
func PainterFontOption(font *truetype.Font) PainterOption {
return func(p *Painter) {
if font == nil {
return
}
p.font = font
}
}
// PainterStyleOption sets the style of draw painter
func PainterStyleOption(style Style) PainterOption {
return func(p *Painter) {
p.SetStyle(style)
}
}
// PainterThemeOption sets the theme of draw painter
func PainterThemeOption(theme ColorPalette) PainterOption {
return func(p *Painter) {
if theme == nil {
return
}
p.theme = theme
}
}
// PainterWidthHeightOption set width or height of draw painter
func PainterWidthHeightOption(width, height int) PainterOption {
return func(p *Painter) {
if width > 0 {
p.box.Right = p.box.Left + width
}
if height > 0 {
p.box.Bottom = p.box.Top + height
}
}
}
// NewPainter creates a painter
func NewPainter(opts PainterOptions, opt ...PainterOption) (*Painter, error) {
if opts.Width <= 0 || opts.Height <= 0 {
return nil, errors.New("width/height can not be nil")
}
font := opts.Font
if font == nil {
f, err := GetDefaultFont()
if err != nil {
return nil, err
}
font = f
}
fn := chart.PNG
if opts.Type == ChartOutputSVG {
fn = chart.SVG
}
width := opts.Width
height := opts.Height
r, err := fn(width, height)
if err != nil {
return nil, err
}
r.SetFont(font)
p := &Painter{
render: r,
box: Box{
Right: opts.Width,
Bottom: opts.Height,
},
font: font,
// 类型
outputType: opts.Type,
}
p.setOptions(opt...)
if p.theme == nil {
p.theme = NewTheme(ThemeLight)
}
return p, nil
}
func (p *Painter) setOptions(opts ...PainterOption) {
for _, fn := range opts {
fn(p)
}
}
func (p *Painter) Child(opt ...PainterOption) *Painter {
child := &Painter{
// 格式化
valueFormatter: p.valueFormatter,
// render
render: p.render,
box: p.box.Clone(),
font: p.font,
parent: p,
style: p.style,
theme: p.theme,
}
child.setOptions(opt...)
return child
}
func (p *Painter) SetStyle(style Style) {
if style.Font == nil {
style.Font = p.font
}
p.style = style
style.WriteToRenderer(p.render)
}
func overrideStyle(defaultStyle Style, style Style) Style {
if style.StrokeWidth == 0 {
style.StrokeWidth = defaultStyle.StrokeWidth
}
if style.StrokeColor.IsZero() {
style.StrokeColor = defaultStyle.StrokeColor
}
if style.StrokeDashArray == nil {
style.StrokeDashArray = defaultStyle.StrokeDashArray
}
if style.DotColor.IsZero() {
style.DotColor = defaultStyle.DotColor
}
if style.DotWidth == 0 {
style.DotWidth = defaultStyle.DotWidth
}
if style.FillColor.IsZero() {
style.FillColor = defaultStyle.FillColor
}
if style.FontSize == 0 {
style.FontSize = defaultStyle.FontSize
}
if style.FontColor.IsZero() {
style.FontColor = defaultStyle.FontColor
}
if style.Font == nil {
style.Font = defaultStyle.Font
}
return style
}
func (p *Painter) OverrideDrawingStyle(style Style) *Painter {
s := overrideStyle(p.style, style)
p.SetDrawingStyle(s)
return p
}
func (p *Painter) SetDrawingStyle(style Style) *Painter {
style.WriteDrawingOptionsToRenderer(p.render)
return p
}
func (p *Painter) SetTextStyle(style Style) *Painter {
if style.Font == nil {
style.Font = p.font
}
style.WriteTextOptionsToRenderer(p.render)
return p
}
func (p *Painter) OverrideTextStyle(style Style) *Painter {
s := overrideStyle(p.style, style)
p.SetTextStyle(s)
return p
}
func (p *Painter) ResetStyle() *Painter {
p.style.WriteToRenderer(p.render)
return p
}
// Bytes returns the data of draw canvas
func (p *Painter) Bytes() ([]byte, error) {
buffer := bytes.Buffer{}
err := p.render.Save(&buffer)
if err != nil {
return nil, err
}
return buffer.Bytes(), err
}
// MoveTo moves the cursor to a given point
func (p *Painter) MoveTo(x, y int) *Painter {
p.render.MoveTo(x+p.box.Left, y+p.box.Top)
return p
}
func (p *Painter) ArcTo(cx, cy int, rx, ry, startAngle, delta float64) *Painter {
p.render.ArcTo(cx+p.box.Left, cy+p.box.Top, rx, ry, startAngle, delta)
return p
}
func (p *Painter) LineTo(x, y int) *Painter {
p.render.LineTo(x+p.box.Left, y+p.box.Top)
return p
}
func (p *Painter) QuadCurveTo(cx, cy, x, y int) *Painter {
p.render.QuadCurveTo(cx+p.box.Left, cy+p.box.Top, x+p.box.Left, y+p.box.Top)
return p
}
func (p *Painter) Pin(x, y, width int) *Painter {
r := float64(width) / 2
y -= width / 4
angle := chart.DegreesToRadians(15)
box := p.box
startAngle := math.Pi/2 + angle
delta := 2*math.Pi - 2*angle
p.ArcTo(x, y, r, r, startAngle, delta)
p.LineTo(x, y)
p.Close()
p.FillStroke()
startX := x - int(r)
startY := y
endX := x + int(r)
endY := y
p.MoveTo(startX, startY)
left := box.Left
top := box.Top
cx := x
cy := y + int(r*2.5)
p.render.QuadCurveTo(cx+left, cy+top, endX+left, endY+top)
p.Close()
p.Fill()
return p
}
func (p *Painter) arrow(x, y, width, height int, direction string) *Painter {
halfWidth := width >> 1
halfHeight := height >> 1
if direction == PositionTop || direction == PositionBottom {
x0 := x - halfWidth
x1 := x0 + width
dy := -height / 3
y0 := y
y1 := y0 - height
if direction == PositionBottom {
y0 = y - height
y1 = y
dy = 2 * dy
}
p.MoveTo(x0, y0)
p.LineTo(x0+halfWidth, y1)
p.LineTo(x1, y0)
p.LineTo(x0+halfWidth, y+dy)
p.LineTo(x0, y0)
} else {
x0 := x + width
x1 := x0 - width
y0 := y - halfHeight
dx := -width / 3
if direction == PositionRight {
x0 = x - width
dx = -dx
x1 = x0 + width
}
p.MoveTo(x0, y0)
p.LineTo(x1, y0+halfHeight)
p.LineTo(x0, y0+height)
p.LineTo(x0+dx, y0+halfHeight)
p.LineTo(x0, y0)
}
p.FillStroke()
return p
}
func (p *Painter) ArrowLeft(x, y, width, height int) *Painter {
p.arrow(x, y, width, height, PositionLeft)
return p
}
func (p *Painter) ArrowRight(x, y, width, height int) *Painter {
p.arrow(x, y, width, height, PositionRight)
return p
}
func (p *Painter) ArrowTop(x, y, width, height int) *Painter {
p.arrow(x, y, width, height, PositionTop)
return p
}
func (p *Painter) ArrowBottom(x, y, width, height int) *Painter {
p.arrow(x, y, width, height, PositionBottom)
return p
}
func (p *Painter) Circle(radius float64, x, y int) *Painter {
p.render.Circle(radius, x+p.box.Left, y+p.box.Top)
return p
}
func (p *Painter) Stroke() *Painter {
p.render.Stroke()
return p
}
func (p *Painter) Close() *Painter {
p.render.Close()
return p
}
func (p *Painter) FillStroke() *Painter {
p.render.FillStroke()
return p
}
func (p *Painter) Fill() *Painter {
p.render.Fill()
return p
}
func (p *Painter) Width() int {
return p.box.Width()
}
func (p *Painter) Height() int {
return p.box.Height()
}
func (p *Painter) MeasureText(text string) Box {
return p.render.MeasureText(text)
}
func (p *Painter) MeasureTextMaxWidthHeight(textList []string) (int, int) {
maxWidth := 0
maxHeight := 0
for _, text := range textList {
box := p.MeasureText(text)
if maxWidth < box.Width() {
maxWidth = box.Width()
}
if maxHeight < box.Height() {
maxHeight = box.Height()
}
}
return maxWidth, maxHeight
}
func (p *Painter) LineStroke(points []Point) *Painter {
shouldMoveTo := false
for index, point := range points {
x := point.X
y := point.Y
if y == int(math.MaxInt32) {
p.Stroke()
shouldMoveTo = true
continue
}
if shouldMoveTo || index == 0 {
p.MoveTo(x, y)
shouldMoveTo = false
} else {
p.LineTo(x, y)
}
}
p.Stroke()
return p
}
func (p *Painter) SmoothLineStroke(points []Point) *Painter {
prevX := 0
prevY := 0
// TODO 如何生成平滑的折线
for index, point := range points {
x := point.X
y := point.Y
if index == 0 {
p.MoveTo(x, y)
} else {
cx := prevX + (x-prevX)/5
cy := y + (y-prevY)/2
p.QuadCurveTo(cx, cy, x, y)
}
prevX = x
prevY = y
}
p.Stroke()
return p
}
func (p *Painter) SetBackground(width, height int, color Color, inside ...bool) *Painter {
r := p.render
s := chart.Style{
FillColor: color,
}
// 背景色
p.SetDrawingStyle(s)
defer p.ResetStyle()
if len(inside) != 0 && inside[0] {
p.MoveTo(0, 0)
p.LineTo(width, 0)
p.LineTo(width, height)
p.LineTo(0, height)
p.LineTo(0, 0)
} else {
// 设置背景色不使用box因此不直接使用Painter
r.MoveTo(0, 0)
r.LineTo(width, 0)
r.LineTo(width, height)
r.LineTo(0, height)
r.LineTo(0, 0)
}
p.FillStroke()
return p
}
func (p *Painter) MarkLine(x, y, width int) *Painter {
arrowWidth := 16
arrowHeight := 10
endX := x + width
radius := 3
p.Circle(3, x+radius, y)
p.render.Fill()
p.MoveTo(x+radius*3, y)
p.LineTo(endX-arrowWidth, y)
p.Stroke()
p.ArrowRight(endX, y, arrowWidth, arrowHeight)
return p
}
func (p *Painter) Polygon(center Point, radius float64, sides int) *Painter {
points := getPolygonPoints(center, radius, sides)
for i, item := range points {
if i == 0 {
p.MoveTo(item.X, item.Y)
} else {
p.LineTo(item.X, item.Y)
}
}
p.LineTo(points[0].X, points[0].Y)
p.Stroke()
return p
}
func (p *Painter) FillArea(points []Point) *Painter {
var x, y int
for index, point := range points {
x = point.X
y = point.Y
if index == 0 {
p.MoveTo(x, y)
} else {
p.LineTo(x, y)
}
}
p.Fill()
return p
}
func (p *Painter) Text(body string, x, y int) *Painter {
p.render.Text(body, x+p.box.Left, y+p.box.Top)
return p
}
func (p *Painter) TextRotation(body string, x, y int, radians float64) {
p.render.SetTextRotation(radians)
p.render.Text(body, x+p.box.Left, y+p.box.Top)
p.render.ClearTextRotation()
}
func (p *Painter) SetTextRotation(radians float64) {
p.render.SetTextRotation(radians)
}
func (p *Painter) ClearTextRotation() {
p.render.ClearTextRotation()
}
func (p *Painter) TextFit(body string, x, y, width int, textAligns ...string) chart.Box {
style := p.style
textWarp := style.TextWrap
style.TextWrap = chart.TextWrapWord
r := p.render
lines := chart.Text.WrapFit(r, body, width, style)
p.SetTextStyle(style)
var output chart.Box
textAlign := ""
if len(textAligns) != 0 {
textAlign = textAligns[0]
}
for index, line := range lines {
if line == "" {
continue
}
x0 := x
y0 := y + output.Height()
lineBox := r.MeasureText(line)
switch textAlign {
case AlignRight:
x0 += width - lineBox.Width()
case AlignCenter:
x0 += (width - lineBox.Width()) >> 1
}
p.Text(line, x0, y0)
output.Right = chart.MaxInt(lineBox.Right, output.Right)
output.Bottom += lineBox.Height()
if index < len(lines)-1 {
output.Bottom += +style.GetTextLineSpacing()
}
}
p.style.TextWrap = textWarp
return output
}
func (p *Painter) Ticks(opt TicksOption) *Painter {
if opt.Count <= 0 || opt.Length <= 0 {
return p
}
count := opt.Count
first := opt.First
width := p.Width()
height := p.Height()
unit := 1
if opt.Unit > 1 {
unit = opt.Unit
}
var values []int
isVertical := opt.Orient == OrientVertical
if isVertical {
values = autoDivide(height, count)
} else {
values = autoDivide(width, count)
}
for index, value := range values {
if index < first {
continue
}
if (index-first)%unit != 0 {
continue
}
if isVertical {
p.LineStroke([]Point{
{
X: 0,
Y: value,
},
{
X: opt.Length,
Y: value,
},
})
} else {
p.LineStroke([]Point{
{
X: value,
Y: opt.Length,
},
{
X: value,
Y: 0,
},
})
}
}
return p
}
func (p *Painter) MultiText(opt MultiTextOption) *Painter {
if len(opt.TextList) == 0 {
return p
}
count := len(opt.TextList)
positionCenter := true
showIndex := opt.Unit / 2
if containsString([]string{
PositionLeft,
PositionTop,
}, opt.Position) {
positionCenter = false
count--
// 非居中
showIndex = 0
}
width := p.Width()
height := p.Height()
var values []int
isVertical := opt.Orient == OrientVertical
if isVertical {
values = autoDivide(height, count)
} else {
values = autoDivide(width, count)
}
isTextRotation := opt.TextRotation != 0
offset := opt.Offset
for index, text := range opt.TextList {
if index < opt.First {
continue
}
if opt.Unit != 0 && (index-opt.First)%opt.Unit != showIndex {
continue
}
if isTextRotation {
p.ClearTextRotation()
p.SetTextRotation(opt.TextRotation)
}
box := p.MeasureText(text)
start := values[index]
if positionCenter {
start = (values[index] + values[index+1]) >> 1
}
x := 0
y := 0
if isVertical {
y = start + box.Height()>>1
switch opt.Align {
case AlignRight:
x = width - box.Width()
case AlignCenter:
x = width - box.Width()>>1
default:
x = 0
}
} else {
x = start - box.Width()>>1
}
x += offset.Left
y += offset.Top
p.Text(text, x, y)
}
if isTextRotation {
p.ClearTextRotation()
}
return p
}
func (p *Painter) Grid(opt GridOption) *Painter {
width := p.Width()
height := p.Height()
drawLines := func(values []int, ignoreIndexList []int, isVertical bool) {
for index, v := range values {
if containsInt(ignoreIndexList, index) {
continue
}
x0 := 0
y0 := 0
x1 := 0
y1 := 0
if isVertical {
x0 = v
x1 = v
y1 = height
} else {
x1 = width
y0 = v
y1 = v
}
p.LineStroke([]Point{
{
X: x0,
Y: y0,
},
{
X: x1,
Y: y1,
},
})
}
}
columnCount := sumInt(opt.ColumnSpans)
if columnCount == 0 {
columnCount = opt.Column
}
if columnCount > 0 {
values := autoDivideSpans(width, columnCount, opt.ColumnSpans)
drawLines(values, opt.IgnoreColumnLines, true)
}
if opt.Row > 0 {
values := autoDivide(height, opt.Row)
drawLines(values, opt.IgnoreRowLines, false)
}
return p
}
func (p *Painter) Dots(points []Point) *Painter {
for _, item := range points {
p.Circle(2, item.X, item.Y)
}
p.FillStroke()
return p
}
func (p *Painter) Rect(box Box) *Painter {
p.MoveTo(box.Left, box.Top)
p.LineTo(box.Right, box.Top)
p.LineTo(box.Right, box.Bottom)
p.LineTo(box.Left, box.Bottom)
p.LineTo(box.Left, box.Top)
p.FillStroke()
return p
}
func (p *Painter) RoundedRect(box Box, radius int) *Painter {
r := (box.Right - box.Left) / 2
if radius > r {
radius = r
}
rx := float64(radius)
ry := float64(radius)
p.MoveTo(box.Left+radius, box.Top)
p.LineTo(box.Right-radius, box.Top)
cx := box.Right - radius
cy := box.Top + radius
// right top
p.ArcTo(cx, cy, rx, ry, -math.Pi/2, math.Pi/2)
p.LineTo(box.Right, box.Bottom-radius)
// right bottom
cx = box.Right - radius
cy = box.Bottom - radius
p.ArcTo(cx, cy, rx, ry, 0.0, math.Pi/2)
p.LineTo(box.Left+radius, box.Bottom)
// left bottom
cx = box.Left + radius
cy = box.Bottom - radius
p.ArcTo(cx, cy, rx, ry, math.Pi/2, math.Pi/2)
p.LineTo(box.Left, box.Top+radius)
// left top
cx = box.Left + radius
cy = box.Top + radius
p.ArcTo(cx, cy, rx, ry, math.Pi, math.Pi/2)
p.Close()
p.FillStroke()
p.Fill()
return p
}
func (p *Painter) LegendLineDot(box Box) *Painter {
width := box.Width()
height := box.Height()
strokeWidth := 3
dotHeight := 5
p.render.SetStrokeWidth(float64(strokeWidth))
center := (height-strokeWidth)>>1 - 1
p.MoveTo(box.Left, box.Top-center)
p.LineTo(box.Right, box.Top-center)
p.Stroke()
p.Circle(float64(dotHeight), box.Left+width>>1, box.Top-center)
p.FillStroke()
return p
}
func (p *Painter) GetRenderer() chart.Renderer {
return p.render
}

View file

@ -27,138 +27,38 @@ import (
"math" "math"
"github.com/golang/freetype/truetype" "github.com/golang/freetype/truetype"
"git.smarteching.com/zeni/go-chart/v2" "github.com/wcharczuk/go-chart/v2"
) )
type pieChart struct { func getPieStyle(theme *Theme, index int) chart.Style {
p *Painter seriesColor := theme.GetSeriesColor(index)
opt *PieChartOption return chart.Style{
StrokeColor: seriesColor,
StrokeWidth: 1,
FillColor: seriesColor,
}
} }
type PieChartOption struct { type pieChartOption struct {
// The theme Theme string
Theme ColorPalette Font *truetype.Font
// The font size
Font *truetype.Font
// The data series list
SeriesList SeriesList SeriesList SeriesList
// The padding of line chart
Padding Box
// The option of title
Title TitleOption
// The legend option
Legend LegendOption
// background is filled
backgroundIsFilled bool
} }
// NewPieChart returns a pie chart renderer func pieChartRender(opt pieChartOption, result *basicRenderResult) error {
func NewPieChart(p *Painter, opt PieChartOption) *pieChart { d, err := NewDraw(DrawOption{
if opt.Theme == nil { Parent: result.d,
opt.Theme = defaultTheme }, PaddingOption(chart.Box{
Top: result.titleBox.Height(),
}))
if err != nil {
return err
} }
return &pieChart{
p: p,
opt: &opt,
}
}
type sector struct { values := make([]float64, len(opt.SeriesList))
value float64
percent float64
cx int
cy int
rx float64
ry float64
start float64
delta float64
offset int
quadrant int
lineStartX int
lineStartY int
lineBranchX int
lineBranchY int
lineEndX int
lineEndY int
showLabel bool
label string
series Series
color Color
}
func NewSector(cx int, cy int, radius float64, labelRadius float64, value float64, currentValue float64, totalValue float64, labelLineLength int, label string, series Series, color Color) sector {
s := sector{}
s.value = value
s.percent = value / totalValue
s.cx = cx
s.cy = cy
s.rx = radius
s.ry = radius
p := (currentValue + value/2) / totalValue
if p < 0.25 {
s.quadrant = 1
} else if p < 0.5 {
s.quadrant = 4
} else if p < 0.75 {
s.quadrant = 3
} else {
s.quadrant = 2
}
s.start = chart.PercentToRadians(currentValue/totalValue) - math.Pi/2
s.delta = chart.PercentToRadians(value / totalValue)
angle := s.start + s.delta/2
s.lineStartX = cx + int(radius*math.Cos(angle))
s.lineStartY = cy + int(radius*math.Sin(angle))
s.lineBranchX = cx + int(labelRadius*math.Cos(angle))
s.lineBranchY = cy + int(labelRadius*math.Sin(angle))
s.offset = labelLineLength
if s.lineBranchX <= cx {
s.offset *= -1
}
s.lineEndX = s.lineBranchX + s.offset
s.lineEndY = s.lineBranchY
s.series = series
s.color = color
s.showLabel = series.Label.Show
s.label = NewPieLabelFormatter([]string{label}, series.Label.Formatter)(0, s.value, s.percent)
return s
}
func (s *sector) calculateY(prevY int) int {
for i := 0; i <= s.cy; i++ {
if s.quadrant <= 2 {
if (prevY - s.lineBranchY) > labelFontSize+5 {
break
}
s.lineBranchY -= 1
} else {
if (s.lineBranchY - prevY) > labelFontSize+5 {
break
}
s.lineBranchY += 1
}
}
s.lineEndY = s.lineBranchY
return s.lineBranchY
}
func (s *sector) calculateTextXY(textBox Box) (x int, y int) {
textMargin := 3
x = s.lineEndX + textMargin
y = s.lineEndY + textBox.Height()>>1 - 1
if s.offset < 0 {
textWidth := textBox.Width()
x = s.lineEndX - textWidth - textMargin
}
return
}
func (p *pieChart) render(result *defaultRenderResult, seriesList SeriesList) (Box, error) {
opt := p.opt
values := make([]float64, len(seriesList))
total := float64(0) total := float64(0)
radiusValue := "" radiusValue := ""
for index, series := range seriesList { for index, series := range opt.SeriesList {
if len(series.Radius) != 0 { if len(series.Radius) != 0 {
radiusValue = series.Radius radiusValue = series.Radius
} }
@ -170,13 +70,16 @@ func (p *pieChart) render(result *defaultRenderResult, seriesList SeriesList) (B
total += value total += value
} }
if total <= 0 { if total <= 0 {
return BoxZero, errors.New("The sum value of pie chart should gt 0") return errors.New("The sum value of pie chart should gt 0")
} }
seriesPainter := result.seriesPainter r := d.Render
cx := seriesPainter.Width() >> 1 theme := NewTheme(opt.Theme)
cy := seriesPainter.Height() >> 1
diameter := chart.MinInt(seriesPainter.Width(), seriesPainter.Height()) box := d.Box
cx := box.Width() >> 1
cy := box.Height() >> 1
diameter := chart.MinInt(box.Width(), box.Height())
radius := getRadius(float64(diameter), radiusValue) radius := getRadius(float64(diameter), radiusValue)
labelLineWidth := 15 labelLineWidth := 15
@ -184,135 +87,83 @@ func (p *pieChart) render(result *defaultRenderResult, seriesList SeriesList) (B
labelLineWidth = 10 labelLineWidth = 10
} }
labelRadius := radius + float64(labelLineWidth) labelRadius := radius + float64(labelLineWidth)
seriesNames := opt.Legend.Data
if len(seriesNames) == 0 {
seriesNames = seriesList.Names()
}
theme := opt.Theme
currentValue := float64(0) seriesNames := opt.SeriesList.Names()
var quadrant1, quadrant2, quadrant3, quadrant4 []sector if len(values) == 1 {
for index, v := range values { getPieStyle(theme, 0).WriteToRenderer(r)
series := seriesList[index] d.moveTo(cx, cy)
color := theme.GetSeriesColor(index) d.circle(radius, cx, cy)
if index == len(values)-1 { } else {
if color == theme.GetSeriesColor(0) { currentValue := float64(0)
color = theme.GetSeriesColor(1) prevEndX := 0
} prevEndY := 0
} for index, v := range values {
s := NewSector(cx, cy, radius, labelRadius, v, currentValue, total, labelLineWidth, seriesNames[index], series, color)
switch quadrant := s.quadrant; quadrant {
case 1:
quadrant1 = append([]sector{s}, quadrant1...)
case 2:
quadrant2 = append(quadrant2, s)
case 3:
quadrant3 = append([]sector{s}, quadrant3...)
case 4:
quadrant4 = append(quadrant4, s)
}
currentValue += v
}
sectors := append(quadrant1, quadrant4...)
sectors = append(sectors, quadrant3...)
sectors = append(sectors, quadrant2...)
currentQuadrant := 0 pieStyle := getPieStyle(theme, index)
prevY := 0 pieStyle.WriteToRenderer(r)
maxY := 0 d.moveTo(cx, cy)
minY := 0 start := chart.PercentToRadians(currentValue/total) - math.Pi/2
for _, s := range sectors { currentValue += v
seriesPainter.OverrideDrawingStyle(Style{ percent := (v / total)
StrokeWidth: 1, delta := chart.PercentToRadians(percent)
StrokeColor: s.color, d.arcTo(cx, cy, radius, radius, start, delta)
FillColor: s.color, d.lineTo(cx, cy)
}) r.Close()
seriesPainter.MoveTo(s.cx, s.cy) r.FillStroke()
seriesPainter.ArcTo(s.cx, s.cy, s.rx, s.ry, s.start, s.delta).LineTo(s.cx, s.cy).Close().FillStroke()
if !s.showLabel { series := opt.SeriesList[index]
continue // 是否显示label
} showLabel := series.Label.Show
if currentQuadrant != s.quadrant { if !showLabel {
if s.quadrant == 1 { continue
minY = cy * 2
maxY = 0
prevY = cy * 2
} }
if s.quadrant == 2 {
if currentQuadrant != 3 { // label的角度为饼块中间
prevY = s.lineEndY angle := start + delta/2
} else { startx := cx + int(radius*math.Cos(angle))
prevY = minY starty := cy + int(radius*math.Sin(angle))
}
endx := cx + int(labelRadius*math.Cos(angle))
endy := cy + int(labelRadius*math.Sin(angle))
// 计算是否有重叠如果有则调整y坐标位置
if index != 0 &&
math.Abs(float64(endx-prevEndX)) < labelFontSize &&
math.Abs(float64(endy-prevEndY)) < labelFontSize {
endy -= (labelFontSize << 1)
} }
if s.quadrant == 3 { prevEndX = endx
if currentQuadrant != 4 { prevEndY = endy
prevY = s.lineEndY d.moveTo(startx, starty)
} else { d.lineTo(endx, endy)
minY = cy * 2 offset := labelLineWidth
maxY = 0 if endx < cx {
prevY = 0 offset *= -1
}
} }
if s.quadrant == 4 { d.moveTo(endx, endy)
if currentQuadrant != 1 { endx += offset
prevY = s.lineEndY d.lineTo(endx, endy)
} else { r.Stroke()
prevY = maxY textStyle := chart.Style{
} FontColor: theme.GetTextColor(),
FontSize: labelFontSize,
Font: opt.Font,
} }
currentQuadrant = s.quadrant if !series.Label.Color.IsZero() {
textStyle.FontColor = series.Label.Color
}
textStyle.GetTextOptions().WriteToRenderer(r)
text := NewPieLabelFormatter(seriesNames, series.Label.Formatter)(index, v, percent)
textBox := r.MeasureText(text)
textMargin := 3
x := endx + textMargin
y := endy + textBox.Height()>>1 - 1
if offset < 0 {
textWidth := textBox.Width()
x = endx - textWidth - textMargin
}
d.text(text, x, y)
} }
prevY = s.calculateY(prevY)
if prevY > maxY {
maxY = prevY
}
if prevY < minY {
minY = prevY
}
seriesPainter.MoveTo(s.lineStartX, s.lineStartY)
seriesPainter.LineTo(s.lineBranchX, s.lineBranchY)
seriesPainter.MoveTo(s.lineBranchX, s.lineBranchY)
seriesPainter.LineTo(s.lineEndX, s.lineEndY)
seriesPainter.Stroke()
textStyle := Style{
FontColor: theme.GetTextColor(),
FontSize: labelFontSize,
Font: opt.Font,
}
if !s.series.Label.Color.IsZero() {
textStyle.FontColor = s.series.Label.Color
}
seriesPainter.OverrideTextStyle(textStyle)
x, y := s.calculateTextXY(seriesPainter.MeasureText(s.label))
seriesPainter.Text(s.label, x, y)
} }
return p.p.box, nil return nil
}
func (p *pieChart) Render() (Box, error) {
opt := p.opt
renderResult, err := defaultRender(p.p, defaultRenderOption{
Theme: opt.Theme,
Padding: opt.Padding,
SeriesList: opt.SeriesList,
XAxis: XAxisOption{
Show: FalseFlag(),
},
YAxisOptions: []YAxisOption{
{
Show: FalseFlag(),
},
},
TitleOption: opt.Title,
LegendOption: opt.Legend,
backgroundIsFilled: opt.backgroundIsFilled,
})
if err != nil {
return BoxZero, err
}
seriesList := opt.SeriesList.Filter(ChartTypePie)
return p.render(renderResult, seriesList)
} }

File diff suppressed because one or more lines are too long

View file

@ -25,17 +25,11 @@ package charts
import ( import (
"errors" "errors"
"github.com/dustin/go-humanize"
"github.com/golang/freetype/truetype" "github.com/golang/freetype/truetype"
"git.smarteching.com/zeni/go-chart/v2" "github.com/wcharczuk/go-chart/v2"
"git.smarteching.com/zeni/go-chart/v2/drawing" "github.com/wcharczuk/go-chart/v2/drawing"
) )
type radarChart struct {
p *Painter
opt *RadarChartOption
}
type RadarIndicator struct { type RadarIndicator struct {
// Indicator's name // Indicator's name
Name string Name string
@ -45,118 +39,89 @@ type RadarIndicator struct {
Min float64 Min float64
} }
type RadarChartOption struct { type radarChartOption struct {
// The theme Theme string
Theme ColorPalette Font *truetype.Font
// The font size
Font *truetype.Font
// The data series list
SeriesList SeriesList SeriesList SeriesList
// The padding of line chart Indicators []RadarIndicator
Padding Box
// The option of title
Title TitleOption
// The legend option
Legend LegendOption
// The radar indicator list
RadarIndicators []RadarIndicator
// background is filled
backgroundIsFilled bool
} }
// NewRadarIndicators returns a radar indicator list func radarChartRender(opt radarChartOption, result *basicRenderResult) error {
func NewRadarIndicators(names []string, values []float64) []RadarIndicator { sides := len(opt.Indicators)
if len(names) != len(values) {
return nil
}
indicators := make([]RadarIndicator, len(names))
for index, name := range names {
indicators[index] = RadarIndicator{
Name: name,
Max: values[index],
}
}
return indicators
}
// NewRadarChart returns a radar chart renderer
func NewRadarChart(p *Painter, opt RadarChartOption) *radarChart {
if opt.Theme == nil {
opt.Theme = defaultTheme
}
return &radarChart{
p: p,
opt: &opt,
}
}
func (r *radarChart) render(result *defaultRenderResult, seriesList SeriesList) (Box, error) {
opt := r.opt
indicators := opt.RadarIndicators
sides := len(indicators)
if sides < 3 { if sides < 3 {
return BoxZero, errors.New("The count of indicator should be >= 3") return errors.New("The count of indicator should be >= 3")
} }
maxValues := make([]float64, len(indicators)) maxValues := make([]float64, len(opt.Indicators))
for _, series := range seriesList { for _, series := range opt.SeriesList {
for index, item := range series.Data { for index, item := range series.Data {
if index < len(maxValues) && item.Value > maxValues[index] { if index < len(maxValues) && item.Value > maxValues[index] {
maxValues[index] = item.Value maxValues[index] = item.Value
} }
} }
} }
for index, indicator := range indicators { for index, indicator := range opt.Indicators {
if indicator.Max <= 0 { if indicator.Max <= 0 {
indicators[index].Max = maxValues[index] opt.Indicators[index].Max = maxValues[index]
} }
} }
d, err := NewDraw(DrawOption{
Parent: result.d,
}, PaddingOption(chart.Box{
Top: result.titleBox.Height(),
}))
if err != nil {
return err
}
radiusValue := "" radiusValue := ""
for _, series := range seriesList { for _, series := range opt.SeriesList {
if len(series.Radius) != 0 { if len(series.Radius) != 0 {
radiusValue = series.Radius radiusValue = series.Radius
} }
} }
seriesPainter := result.seriesPainter box := d.Box
theme := opt.Theme cx := box.Width() >> 1
cy := box.Height() >> 1
cx := seriesPainter.Width() >> 1 diameter := chart.MinInt(box.Width(), box.Height())
cy := seriesPainter.Height() >> 1
diameter := chart.MinInt(seriesPainter.Width(), seriesPainter.Height())
radius := getRadius(float64(diameter), radiusValue) radius := getRadius(float64(diameter), radiusValue)
theme := NewTheme(opt.Theme)
divideCount := 5 divideCount := 5
divideRadius := float64(int(radius / float64(divideCount))) divideRadius := float64(int(radius / float64(divideCount)))
radius = divideRadius * float64(divideCount) radius = divideRadius * float64(divideCount)
seriesPainter.OverrideDrawingStyle(Style{ style := chart.Style{
StrokeColor: theme.GetAxisSplitLineColor(), StrokeColor: theme.GetAxisSplitLineColor(),
StrokeWidth: 1, StrokeWidth: 1,
}) }
r := d.Render
style.WriteToRenderer(r)
center := Point{ center := Point{
X: cx, X: cx,
Y: cy, Y: cy,
} }
for i := 0; i < divideCount; i++ { for i := 0; i < divideCount; i++ {
seriesPainter.Polygon(center, divideRadius*float64(i+1), sides) d.polygon(center, divideRadius*float64(i+1), sides)
} }
points := getPolygonPoints(center, radius, sides) points := getPolygonPoints(center, radius, sides)
for _, p := range points { for _, p := range points {
seriesPainter.MoveTo(center.X, center.Y) d.moveTo(center.X, center.Y)
seriesPainter.LineTo(p.X, p.Y) d.lineTo(p.X, p.Y)
seriesPainter.Stroke() d.Render.Stroke()
} }
seriesPainter.OverrideTextStyle(Style{ // 文本
textStyle := chart.Style{
FontColor: theme.GetTextColor(), FontColor: theme.GetTextColor(),
FontSize: labelFontSize, FontSize: labelFontSize,
Font: opt.Font, Font: opt.Font,
}) }
textStyle.GetTextOptions().WriteToRenderer(r)
offset := 5 offset := 5
// 文本生成 // 文本生成
for index, p := range points { for index, p := range points {
name := indicators[index].Name name := opt.Indicators[index].Name
b := seriesPainter.MeasureText(name) b := r.MeasureText(name)
isXCenter := p.X == center.X isXCenter := p.X == center.X
isYCenter := p.Y == center.Y isYCenter := p.Y == center.Y
isRight := p.X > center.X isRight := p.X > center.X
@ -188,24 +153,20 @@ func (r *radarChart) render(result *defaultRenderResult, seriesList SeriesList)
if isLeft { if isLeft {
x -= (b.Width() + offset) x -= (b.Width() + offset)
} }
seriesPainter.Text(name, x, y) d.text(name, x, y)
} }
// 雷达图 // 雷达图
angles := getPolygonPointAngles(sides) angles := getPolygonPointAngles(sides)
maxCount := len(indicators) maxCount := len(opt.Indicators)
for _, series := range seriesList { for _, series := range opt.SeriesList {
linePoints := make([]Point, 0, maxCount) linePoints := make([]Point, 0, maxCount)
for j, item := range series.Data { for j, item := range series.Data {
if j >= maxCount { if j >= maxCount {
continue continue
} }
indicator := indicators[j] indicator := opt.Indicators[j]
var percent float64 percent := (item.Value - indicator.Min) / (indicator.Max - indicator.Min)
offset := indicator.Max - indicator.Min
if offset > 0 {
percent = (item.Value - indicator.Min) / offset
}
r := percent * radius r := percent * radius
p := getPolygonPoint(center, r, angles[j]) p := getPolygonPoint(center, r, angles[j])
linePoints = append(linePoints, p) linePoints = append(linePoints, p)
@ -216,58 +177,17 @@ func (r *radarChart) render(result *defaultRenderResult, seriesList SeriesList)
dotFillColor = color dotFillColor = color
} }
linePoints = append(linePoints, linePoints[0]) linePoints = append(linePoints, linePoints[0])
seriesPainter.OverrideDrawingStyle(Style{ s := LineStyle{
StrokeColor: color, StrokeColor: color,
StrokeWidth: defaultStrokeWidth, StrokeWidth: defaultStrokeWidth,
DotWidth: defaultDotWidth, DotWidth: defaultDotWidth,
DotColor: color, DotColor: color,
FillColor: color.WithAlpha(20), DotFillColor: dotFillColor,
}) FillColor: color.WithAlpha(20),
seriesPainter.LineStroke(linePoints).
FillArea(linePoints)
dotWith := 2.0
seriesPainter.OverrideDrawingStyle(Style{
StrokeWidth: defaultStrokeWidth,
StrokeColor: color,
FillColor: dotFillColor,
})
for index, point := range linePoints {
seriesPainter.Circle(dotWith, point.X, point.Y)
seriesPainter.FillStroke()
if series.Label.Show && index < len(series.Data) {
value := humanize.FtoaWithDigits(series.Data[index].Value, 2)
b := seriesPainter.MeasureText(value)
seriesPainter.Text(value, point.X-b.Width()/2, point.Y)
}
} }
d.lineStroke(linePoints, s)
d.fill(linePoints, s.Style())
d.lineDot(linePoints[0:len(linePoints)-1], s)
} }
return nil
return r.p.box, nil
}
func (r *radarChart) Render() (Box, error) {
p := r.p
opt := r.opt
renderResult, err := defaultRender(p, defaultRenderOption{
Theme: opt.Theme,
Padding: opt.Padding,
SeriesList: opt.SeriesList,
XAxis: XAxisOption{
Show: FalseFlag(),
},
YAxisOptions: []YAxisOption{
{
Show: FalseFlag(),
},
},
TitleOption: opt.Title,
LegendOption: opt.Legend,
backgroundIsFilled: opt.backgroundIsFilled,
})
if err != nil {
return BoxZero, err
}
seriesList := opt.SeriesList.Filter(ChartTypeRadar)
return r.render(renderResult, seriesList)
} }

File diff suppressed because one or more lines are too long

101
range.go
View file

@ -26,46 +26,19 @@ import (
"math" "math"
) )
const defaultAxisDivideCount = 6 type Range struct {
type axisRange struct {
p *Painter
divideCount int divideCount int
min float64 Min float64
max float64 Max float64
size int Size int
boundary bool Boundary bool
} }
type AxisRangeOption struct { func NewRange(min, max float64, divideCount int) Range {
Painter *Painter
// The min value of axis
Min float64
// The max value of axis
Max float64
// The size of axis
Size int
// Boundary gap
Boundary bool
// The count of divide
DivideCount int
}
// NewRange returns a axis range
func NewRange(opt AxisRangeOption) axisRange {
max := opt.Max
min := opt.Min
max += math.Abs(max * 0.1)
min -= math.Abs(min * 0.1)
divideCount := opt.DivideCount
r := math.Abs(max - min) r := math.Abs(max - min)
// 最小单位计算 // 最小单位计算
unit := 1 unit := 2
if r > 5 {
unit = 2
}
if r > 10 { if r > 10 {
unit = 4 unit = 4
} }
@ -90,55 +63,47 @@ func NewRange(opt AxisRangeOption) axisRange {
} }
} }
max = min + float64(unit*divideCount) max = min + float64(unit*divideCount)
expectMax := opt.Max * 2 return Range{
if max > expectMax { Min: min,
max = float64(ceilFloatToInt(expectMax)) Max: max,
}
return axisRange{
p: opt.Painter,
divideCount: divideCount, divideCount: divideCount,
min: min,
max: max,
size: opt.Size,
boundary: opt.Boundary,
} }
} }
// Values returns values of range func (r Range) Values() []string {
func (r axisRange) Values() []string { offset := (r.Max - r.Min) / float64(r.divideCount)
offset := (r.max - r.min) / float64(r.divideCount)
values := make([]string, 0) values := make([]string, 0)
formatter := commafWithDigits
if r.p != nil && r.p.valueFormatter != nil {
formatter = r.p.valueFormatter
}
for i := 0; i <= r.divideCount; i++ { for i := 0; i <= r.divideCount; i++ {
v := r.min + float64(i)*offset v := r.Min + float64(i)*offset
value := formatter(v) value := commafWithDigits(v)
values = append(values, value) values = append(values, value)
} }
return values return values
} }
func (r *axisRange) getHeight(value float64) int { func (r *Range) getHeight(value float64) int {
if r.max <= r.min { v := (value - r.Min) / (r.Max - r.Min)
return 0 return int(v * float64(r.Size))
}
v := (value - r.min) / (r.max - r.min)
return int(v * float64(r.size))
} }
func (r *axisRange) getRestHeight(value float64) int { func (r *Range) getRestHeight(value float64) int {
return r.size - r.getHeight(value) return r.Size - r.getHeight(value)
} }
// GetRange returns a range of index func (r *Range) GetRange(index int) (float64, float64) {
func (r *axisRange) GetRange(index int) (float64, float64) { unit := float64(r.Size) / float64(r.divideCount)
unit := float64(r.size) / float64(r.divideCount)
return unit * float64(index), unit * float64(index+1) return unit * float64(index), unit * float64(index+1)
} }
func (r *Range) AutoDivide() []int {
// AutoDivide divides the axis return autoDivide(r.Size, r.divideCount)
func (r *axisRange) AutoDivide() []int { }
return autoDivide(r.size, r.divideCount)
func (r *Range) getWidth(value float64) int {
v := value / (r.Max - r.Min)
// 移至居中
if r.Boundary &&
r.divideCount != 0 {
v += 1 / float64(r.divideCount*2)
}
return int(v * float64(r.Size))
} }

94
range_test.go Normal file
View file

@ -0,0 +1,94 @@
// 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"
)
func TestRange(t *testing.T) {
assert := assert.New(t)
r := NewRange(0, 8, 6)
assert.Equal(0.0, r.Min)
assert.Equal(12.0, r.Max)
r = NewRange(0, 12, 6)
assert.Equal(0.0, r.Min)
assert.Equal(24.0, r.Max)
r = NewRange(-13, 18, 6)
assert.Equal(-20.0, r.Min)
assert.Equal(40.0, r.Max)
r = NewRange(0, 150, 6)
assert.Equal(0.0, r.Min)
assert.Equal(180.0, r.Max)
r = NewRange(0, 400, 6)
assert.Equal(0.0, r.Min)
assert.Equal(480.0, r.Max)
}
func TestRangeHeightWidth(t *testing.T) {
assert := assert.New(t)
r := NewRange(0, 8, 6)
r.Size = 100
assert.Equal(33, r.getHeight(4))
assert.Equal(67, r.getRestHeight(4))
assert.Equal(33, r.getWidth(4))
r.Boundary = true
assert.Equal(41, r.getWidth(4))
}
func TestRangeGetRange(t *testing.T) {
assert := assert.New(t)
r := NewRange(0, 8, 6)
r.Size = 120
f1, f2 := r.GetRange(0)
assert.Equal(0.0, f1)
assert.Equal(20.0, f2)
f1, f2 = r.GetRange(2)
assert.Equal(40.0, f1)
assert.Equal(60.0, f2)
}
func TestRangeAutoDivide(t *testing.T) {
assert := assert.New(t)
r := Range{
Size: 120,
divideCount: 6,
}
assert.Equal([]int{0, 20, 40, 60, 80, 100, 120}, r.AutoDivide())
r.Size = 130
assert.Equal([]int{0, 22, 44, 66, 88, 109, 130}, r.AutoDivide())
}

109
series.go
View file

@ -19,6 +19,7 @@
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // 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 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE. // SOFTWARE.
package charts package charts
import ( import (
@ -26,26 +27,17 @@ import (
"strings" "strings"
"github.com/dustin/go-humanize" "github.com/dustin/go-humanize"
"git.smarteching.com/zeni/go-chart/v2" "github.com/wcharczuk/go-chart/v2"
"github.com/wcharczuk/go-chart/v2/drawing"
) )
type SeriesData struct { type SeriesData struct {
// The value of series data // The value of series data
Value float64 Value float64
// The style of series data // The style of series data
Style Style Style chart.Style
} }
// NewSeriesListDataFromValues returns a series list
func NewSeriesListDataFromValues(values [][]float64, chartType ...string) SeriesList {
seriesList := make(SeriesList, len(values))
for index, value := range values {
seriesList[index] = NewSeriesFromValues(value, chartType...)
}
return seriesList
}
// NewSeriesFromValues returns a series
func NewSeriesFromValues(values []float64, chartType ...string) Series { func NewSeriesFromValues(values []float64, chartType ...string) Series {
s := Series{ s := Series{
Data: NewSeriesDataFromValues(values), Data: NewSeriesDataFromValues(values),
@ -56,7 +48,6 @@ func NewSeriesFromValues(values []float64, chartType ...string) Series {
return s return s
} }
// NewSeriesDataFromValues return a series data
func NewSeriesDataFromValues(values []float64) []SeriesData { func NewSeriesDataFromValues(values []float64) []SeriesData {
data := make([]SeriesData, len(values)) data := make([]SeriesData, len(values))
for index, value := range values { for index, value := range values {
@ -74,17 +65,11 @@ type SeriesLabel struct {
// {d}: the percent of a data item(pie chart). // {d}: the percent of a data item(pie chart).
Formatter string Formatter string
// The color for label // The color for label
Color Color Color drawing.Color
// Show flag for label // Show flag for label
Show bool Show bool
// Distance to the host graphic element. // Distance to the host graphic element.
Distance int Distance int
// The position of label
Position string
// The offset of label's position
Offset Box
// The font size of label
FontSize float64
} }
const ( const (
@ -116,8 +101,8 @@ type Series struct {
// The data list of series // The data list of series
Data []SeriesData Data []SeriesData
// The Y axis index, it should be 0 or 1. // The Y axis index, it should be 0 or 1.
// Default value is 0 // Default value is 1
AxisIndex int YAxisIndex int
// The style for series // The style for series
Style chart.Style Style chart.Style
// The label for series // The label for series
@ -126,8 +111,6 @@ type Series struct {
Name string Name string
// Radius for Pie chart, e.g.: 40%, default is "40%" // Radius for Pie chart, e.g.: 40%, default is "40%"
Radius string Radius string
// Round for bar chart
RoundRadius int
// Mark point for series // Mark point for series
MarkPoint SeriesMarkPoint MarkPoint SeriesMarkPoint
// Make line for series // Make line for series
@ -139,55 +122,6 @@ type Series struct {
} }
type SeriesList []Series type SeriesList []Series
func (sl SeriesList) init() {
if len(sl) == 0 {
return
}
if sl[len(sl)-1].index != 0 {
return
}
for i := 0; i < len(sl); i++ {
if sl[i].Type == "" {
sl[i].Type = ChartTypeLine
}
sl[i].index = i
}
}
func (sl SeriesList) Filter(chartType string) SeriesList {
arr := make(SeriesList, 0)
for index, item := range sl {
if item.Type == chartType {
arr = append(arr, sl[index])
}
}
return arr
}
// GetMaxMin get max and min value of series list
func (sl SeriesList) GetMaxMin(axisIndex int) (float64, float64) {
min := math.MaxFloat64
max := -math.MaxFloat64
for _, series := range sl {
if series.AxisIndex != axisIndex {
continue
}
for _, item := range series.Data {
// 如果为空值,忽略
if item.Value == nullValue {
continue
}
if item.Value > max {
max = item.Value
}
if item.Value < min {
min = item.Value
}
}
}
return max, min
}
type PieSeriesOption struct { type PieSeriesOption struct {
Radius string Radius string
Label SeriesLabel Label SeriesLabel
@ -222,19 +156,13 @@ func NewPieSeriesList(values []float64, opts ...PieSeriesOption) SeriesList {
} }
type seriesSummary struct { type seriesSummary struct {
// The index of max value MaxIndex int
MaxIndex int MaxValue float64
// The max value MinIndex int
MaxValue float64 MinValue float64
// The index of min value
MinIndex int
// The min value
MinValue float64
// THe average value
AverageValue float64 AverageValue float64
} }
// Summary get summary of series
func (s *Series) Summary() seriesSummary { func (s *Series) Summary() seriesSummary {
minIndex := -1 minIndex := -1
maxIndex := -1 maxIndex := -1
@ -261,7 +189,6 @@ func (s *Series) Summary() seriesSummary {
} }
} }
// Names returns the names of series list
func (sl SeriesList) Names() []string { func (sl SeriesList) Names() []string {
names := make([]string, len(sl)) names := make([]string, len(sl))
for index, s := range sl { for index, s := range sl {
@ -270,10 +197,8 @@ func (sl SeriesList) Names() []string {
return names return names
} }
// LabelFormatter label formatter
type LabelFormatter func(index int, value float64, percent float64) string type LabelFormatter func(index int, value float64, percent float64) string
// NewPieLabelFormatter returns a pie label formatter
func NewPieLabelFormatter(seriesNames []string, layout string) LabelFormatter { func NewPieLabelFormatter(seriesNames []string, layout string) LabelFormatter {
if len(layout) == 0 { if len(layout) == 0 {
layout = "{b}: {d}" layout = "{b}: {d}"
@ -281,23 +206,13 @@ func NewPieLabelFormatter(seriesNames []string, layout string) LabelFormatter {
return NewLabelFormatter(seriesNames, layout) return NewLabelFormatter(seriesNames, layout)
} }
// NewFunnelLabelFormatter returns a funner label formatter func NewValueLabelFormater(seriesNames []string, layout string) LabelFormatter {
func NewFunnelLabelFormatter(seriesNames []string, layout string) LabelFormatter {
if len(layout) == 0 {
layout = "{b}({d})"
}
return NewLabelFormatter(seriesNames, layout)
}
// NewValueLabelFormatter returns a value formatter
func NewValueLabelFormatter(seriesNames []string, layout string) LabelFormatter {
if len(layout) == 0 { if len(layout) == 0 {
layout = "{c}" layout = "{c}"
} }
return NewLabelFormatter(seriesNames, layout) return NewLabelFormatter(seriesNames, layout)
} }
// NewLabelFormatter returns a label formaatter
func NewLabelFormatter(seriesNames []string, layout string) LabelFormatter { func NewLabelFormatter(seriesNames []string, layout string) LabelFormatter {
return func(index int, value, percent float64) string { return func(index int, value, percent float64) string {
// 如果无percent的则设置为<0 // 如果无percent的则设置为<0

View file

@ -1,148 +0,0 @@
// 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 (
"github.com/golang/freetype/truetype"
"git.smarteching.com/zeni/go-chart/v2"
)
type labelRenderValue struct {
Text string
Style Style
X int
Y int
// 旋转
Radians float64
}
type LabelValue struct {
Index int
Value float64
X int
Y int
// 旋转
Radians float64
// 字体颜色
FontColor Color
// 字体大小
FontSize float64
Orient string
Offset Box
}
type SeriesLabelPainter struct {
p *Painter
seriesNames []string
label *SeriesLabel
theme ColorPalette
font *truetype.Font
values []labelRenderValue
}
type SeriesLabelPainterParams struct {
P *Painter
SeriesNames []string
Label SeriesLabel
Theme ColorPalette
Font *truetype.Font
}
func NewSeriesLabelPainter(params SeriesLabelPainterParams) *SeriesLabelPainter {
return &SeriesLabelPainter{
p: params.P,
seriesNames: params.SeriesNames,
label: &params.Label,
theme: params.Theme,
font: params.Font,
values: make([]labelRenderValue, 0),
}
}
func (o *SeriesLabelPainter) Add(value LabelValue) {
label := o.label
distance := label.Distance
if distance == 0 {
distance = 5
}
text := NewValueLabelFormatter(o.seriesNames, label.Formatter)(value.Index, value.Value, -1)
labelStyle := Style{
FontColor: o.theme.GetTextColor(),
FontSize: labelFontSize,
Font: o.font,
}
if value.FontSize != 0 {
labelStyle.FontSize = value.FontSize
}
if !value.FontColor.IsZero() {
label.Color = value.FontColor
}
if !label.Color.IsZero() {
labelStyle.FontColor = label.Color
}
p := o.p
p.OverrideDrawingStyle(labelStyle)
rotated := value.Radians != 0
if rotated {
p.SetTextRotation(value.Radians)
}
textBox := p.MeasureText(text)
renderValue := labelRenderValue{
Text: text,
Style: labelStyle,
X: value.X,
Y: value.Y,
Radians: value.Radians,
}
if value.Orient != OrientHorizontal {
renderValue.X -= textBox.Width() >> 1
renderValue.Y -= distance
} else {
renderValue.X += distance
renderValue.Y += textBox.Height() >> 1
renderValue.Y -= 2
}
if rotated {
renderValue.X = value.X + textBox.Width()>>1 - 1
p.ClearTextRotation()
} else {
if textBox.Width()%2 != 0 {
renderValue.X++
}
}
renderValue.X += value.Offset.Left
renderValue.Y += value.Offset.Top
o.values = append(o.values, renderValue)
}
func (o *SeriesLabelPainter) Render() (Box, error) {
for _, item := range o.values {
o.p.OverrideTextStyle(item.Style)
if item.Radians != 0 {
o.p.TextRotation(item.Text, item.X, item.Y, item.Radians)
} else {
o.p.Text(item.Text, item.X, item.Y)
}
}
return chart.BoxZero, nil
}

View file

@ -19,6 +19,7 @@
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // 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 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE. // SOFTWARE.
package charts package charts
import ( import (
@ -27,63 +28,139 @@ import (
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
func TestNewSeriesListDataFromValues(t *testing.T) { func TestNewSeriesFromValues(t *testing.T) {
assert := assert.New(t)
assert.Equal(Series{
Data: []SeriesData{
{
Value: 1,
},
{
Value: 2,
},
},
Type: ChartTypeBar,
}, NewSeriesFromValues([]float64{
1,
2,
}, ChartTypeBar))
}
func TestNewSeriesDataFromValues(t *testing.T) {
assert := assert.New(t)
assert.Equal([]SeriesData{
{
Value: 1,
},
{
Value: 2,
},
}, NewSeriesDataFromValues([]float64{
1,
2,
}))
}
func TestNewPieSeriesList(t *testing.T) {
assert := assert.New(t) assert := assert.New(t)
assert.Equal(SeriesList{ assert.Equal(SeriesList{
{ {
Type: ChartTypeBar, Type: ChartTypePie,
Name: "a",
Label: SeriesLabel{
Show: true,
},
Radius: "30%",
Data: []SeriesData{ Data: []SeriesData{
{ {
Value: 1.0, Value: 1,
}, },
}, },
}, },
}, NewSeriesListDataFromValues([][]float64{
{ {
1, Type: ChartTypePie,
Name: "b",
Label: SeriesLabel{
Show: true,
},
Radius: "30%",
Data: []SeriesData{
{
Value: 2,
},
},
}, },
}, ChartTypeBar)) }, NewPieSeriesList([]float64{
1,
2,
}, PieSeriesOption{
Radius: "30%",
Label: SeriesLabel{
Show: true,
},
Names: []string{
"a",
"b",
},
}))
} }
func TestSeriesLists(t *testing.T) { func TestSeriesSummary(t *testing.T) {
assert := assert.New(t) assert := assert.New(t)
seriesList := NewSeriesListDataFromValues([][]float64{
{ s := Series{
Data: NewSeriesDataFromValues([]float64{
1, 1,
2, 3,
}, 5,
{ 7,
10, 9,
}, }),
}, ChartTypeBar) }
assert.Equal(2, len(seriesList.Filter(ChartTypeBar)))
assert.Equal(0, len(seriesList.Filter(ChartTypeLine)))
max, min := seriesList.GetMaxMin(0)
assert.Equal(float64(10), max)
assert.Equal(float64(1), min)
assert.Equal(seriesSummary{ assert.Equal(seriesSummary{
MaxIndex: 1, MaxIndex: 4,
MaxValue: 2, MaxValue: 9,
MinIndex: 0, MinIndex: 0,
MinValue: 1, MinValue: 1,
AverageValue: 1.5, AverageValue: 5,
}, seriesList[0].Summary()) }, s.Summary())
} }
func TestFormatter(t *testing.T) { func TestGetSeriesNames(t *testing.T) {
assert := assert.New(t) assert := assert.New(t)
assert.Equal("a: 12%", NewPieLabelFormatter([]string{ sl := SeriesList{
{
Name: "a",
},
{
Name: "b",
},
}
assert.Equal([]string{
"a", "a",
"b", "b",
}, "")(0, 10, 0.12)) }, sl.Names())
}
assert.Equal("10", NewValueLabelFormatter([]string{
"a", func TestNewPieLabelFormatter(t *testing.T) {
"b", assert := assert.New(t)
}, "")(0, 10, 0.12))
fn := NewPieLabelFormatter([]string{
"a",
"b",
}, "")
assert.Equal("a: 35%", fn(0, 1.2, 0.35))
}
func TestNewValueLabelFormater(t *testing.T) {
assert := assert.New(t)
fn := NewValueLabelFormater([]string{
"a",
"b",
}, "")
assert.Equal("1.2", fn(0, 1.2, 0.35))
} }

View file

@ -1,254 +0,0 @@
# go-charts
`go-charts`主要分为了下几个模块:
- `标题`:图表的标题,包括主副标题,位置为图表的顶部
- `图例`:图表的图例列表,用于标识每个图例对应的颜色与名称信息,默认为图表的顶部,可自定义位置
- `X轴`图表的x轴用于折线图、柱状图中表示每个点对应的时间位置图表的底部
- `Y轴`图表的y轴用于折线图、柱状图中最多可使用两组y轴一左一右默认位置图表的左侧
- `内容`: 图表的内容,折线图、柱状图、饼图等,在图表的中间区域
## 标题
### 常用设置
标题一般仅需要设置主副标题即可,其它的属性均会设置默认值,常用的方式是使用`TitleTextOptionFunc`设置,其中副标题为可选值,方式如下:
```go
charts.TitleTextOptionFunc("Text", "Subtext"),
```
### 个性化设置
```go
func(opt *charts.ChartOption) {
opt.Title = charts.TitleOption{
// 主标题
Text: "Text",
// 副标题
Subtext: "Subtext",
// 标题左侧位置,可设置为"center""right",数值("20")或百份比("20%")
Left: charts.PositionRight,
// 标题顶部位置,只可调为数值
Top: "20",
// 主标题文字大小
FontSize: 14,
// 副标题文字大小
SubtextFontSize: 12,
// 主标题字体颜色
FontColor: charts.Color{
R: 100,
G: 100,
B: 100,
A: 255,
},
// 副标题字体影响
SubtextFontColor: charts.Color{
R: 200,
G: 200,
B: 200,
A: 255,
},
}
},
```
### 部分属性个性化设置
```go
charts.TitleTextOptionFunc("Text", "Subtext"),
func(opt *charts.ChartOption) {
// 修改top的值
opt.Title.Top = "20"
},
```
## 图例
### 常用设置
图例组件与图表中的数据一一对应,常用仅设置其名称及左侧的值即可(可选),方式如下:
```go
charts.LegendLabelsOptionFunc([]string{
"Email",
"Union Ads",
"Video Ads",
"Direct",
"Search Engine",
}, "50"),
```
### 个性化设置
```go
func(opt *charts.ChartOption) {
opt.Legend = charts.LegendOption{
// 图例名称
Data: []string{
"Email",
"Union Ads",
"Video Ads",
"Direct",
"Search Engine",
},
// 图例左侧位置,可设置为"center""right",数值("20")或百份比("20%")
// 如果示例有多行,只影响第一行,而且对于多行的示例,设置"center", "right"无效
Left: "50",
// 图例顶部位置,只可调为数值
Top: "10",
// 图例图标的位置,默认为左侧,只允许左或右
Align: charts.AlignRight,
// 图例排列方式,默认为水平,只允许水平或垂直
Orient: charts.OrientVertical,
// 图标类型,提供"rect"与"lineDot"两种类型
Icon: charts.IconRect,
// 字体大小
FontSize: 14,
// 字体颜色
FontColor: charts.Color{
R: 150,
G: 150,
B: 150,
A: 255,
},
// 是否展示,如果不需要展示则设置
// Show: charts.FalseFlag(),
// 图例区域的padding值
Padding: charts.Box{
Top: 10,
Left: 10,
},
}
},
```
### 部分属性个性化设置
```go
charts.LegendLabelsOptionFunc([]string{
"Email",
"Union Ads",
"Video Ads",
"Direct",
"Search Engine",
}, "50"),
func(opt *charts.ChartOption) {
opt.Legend.Top = "10"
},
```
## X轴
### 常用设置
图表中X轴的展示常用的设置方式是指定数组即可
```go
charts.XAxisDataOptionFunc([]string{
"Mon",
"Tue",
"Wed",
"Thu",
"Fri",
"Sat",
"Sun",
}),
```
### 个性化设置
```go
func(opt *charts.ChartOption) {
opt.XAxis = charts.XAxisOption{
// X轴内容
Data: []string{
"01",
"02",
"03",
"04",
"05",
"06",
"07",
"08",
"09",
},
// 如果数据点不居中则设置为false
BoundaryGap: charts.FalseFlag(),
// 字体大小
FontSize: 14,
// 是否展示,如果不需要展示则设置
// Show: charts.FalseFlag(),
// 会根据文本内容以及此值选择适合的分块大小,一般不需要设置
// SplitNumber: 3,
// 线条颜色
StrokeColor: charts.Color{
R: 200,
G: 200,
B: 200,
A: 255,
},
// 文字颜色
FontColor: charts.Color{
R: 100,
G: 100,
B: 100,
A: 255,
},
}
},
```
### 部分属性个性化设置
```go
charts.XAxisDataOptionFunc([]string{
"Mon",
"Tue",
"Wed",
"Thu",
"Fri",
"Sat",
"Sun",
}),
func(opt *charts.ChartOption) {
opt.XAxis.FontColor = charts.Color{
R: 100,
G: 100,
B: 100,
A: 255,
},
},
```
## Y轴
图表中的y轴展示的相关数据会根据图表中的数据自动生成适合的值如果需要自定义则可自定义以下部分数据
```go
func(opt *charts.ChartOption) {
opt.YAxisOptions = []charts.YAxisOption{
{
// 字体大小
FontSize: 16,
// 字体颜色
FontColor: charts.Color{
R: 100,
G: 100,
B: 100,
A: 255,
},
// 内容,{value}会替换为对应的值
Formatter: "{value} ml",
// Y轴颜色如果设置此值会覆盖font color
Color: charts.Color{
R: 255,
G: 0,
B: 0,
A: 255,
},
},
}
},
```

485
table.go
View file

@ -25,414 +25,121 @@ package charts
import ( import (
"errors" "errors"
"github.com/golang/freetype/truetype" "github.com/wcharczuk/go-chart/v2"
"git.smarteching.com/zeni/go-chart/v2"
"git.smarteching.com/zeni/go-chart/v2/drawing"
) )
type tableChart struct { type TableOption struct {
p *Painter // draw
opt *TableChartOption Draw *Draw
}
// NewTableChart returns a table chart render
func NewTableChart(p *Painter, opt TableChartOption) *tableChart {
if opt.Theme == nil {
opt.Theme = defaultTheme
}
return &tableChart{
p: p,
opt: &opt,
}
}
type TableCell struct {
// Text the text of table cell
Text string
// Style the current style of table cell
Style Style
// Row the row index of table cell
Row int
// Column the column index of table cell
Column int
}
type TableChartOption struct {
// The output type
Type string
// The width of table // The width of table
Width int Width int
// The theme // The header of table
Theme ColorPalette
// The padding of table cell
Padding Box
// The header data of table
Header []string Header []string
// The data of table // The style of table
Data [][]string Style chart.Style
// The span list of table column ColumnWidths []float64
Spans []int // 是否仅测量高度
// The text align list of table cell measurement bool
TextAligns []string
// The font size of table
FontSize float64
// The font family, which should be installed first
FontFamily string
Font *truetype.Font
// The font color of table
FontColor Color
// The background color of header
HeaderBackgroundColor Color
// The header font color
HeaderFontColor Color
// The background color of row
RowBackgroundColors []Color
// The background color
BackgroundColor Color
// CellTextStyle customize text style of table cell
CellTextStyle func(TableCell) *Style
// CellStyle customize drawing style of table cell
CellStyle func(TableCell) *Style
} }
type TableSetting struct { var ErrTableColumnWidthInvalid = errors.New("table column width is invalid")
// The color of header
HeaderColor Color
// The color of heder text
HeaderFontColor Color
// The color of table text
FontColor Color
// The color list of row
RowColors []Color
// The padding of cell
Padding Box
}
var TableLightThemeSetting = TableSetting{ func tableDivideWidth(width, size int, columnWidths []float64) ([]int, error) {
HeaderColor: Color{ widths := make([]int, size)
R: 240,
G: 240,
B: 240,
A: 255,
},
HeaderFontColor: Color{
R: 98,
G: 105,
B: 118,
A: 255,
},
FontColor: Color{
R: 70,
G: 70,
B: 70,
A: 255,
},
RowColors: []Color{
drawing.ColorWhite,
{
R: 247,
G: 247,
B: 247,
A: 255,
},
},
Padding: Box{
Left: 10,
Top: 10,
Right: 10,
Bottom: 10,
},
}
var TableDarkThemeSetting = TableSetting{ autoFillCount := size
HeaderColor: Color{ restWidth := width
R: 38, if len(columnWidths) != 0 {
G: 38, for index, v := range columnWidths {
B: 42, if v <= 0 {
A: 255, continue
}, }
HeaderFontColor: Color{ autoFillCount--
R: 216, // 小于1的表示占比
G: 217, if v < 1 {
B: 218, widths[index] = int(v * float64(width))
A: 255,
},
FontColor: Color{
R: 216,
G: 217,
B: 218,
A: 255,
},
RowColors: []Color{
{
R: 24,
G: 24,
B: 28,
A: 255,
},
{
R: 38,
G: 38,
B: 42,
A: 255,
},
},
Padding: Box{
Left: 10,
Top: 10,
Right: 10,
Bottom: 10,
},
}
var tableDefaultSetting = TableLightThemeSetting
// SetDefaultTableSetting sets the default setting for table
func SetDefaultTableSetting(setting TableSetting) {
tableDefaultSetting = setting
}
type renderInfo struct {
Width int
Height int
HeaderHeight int
RowHeights []int
ColumnWidths []int
}
func (t *tableChart) render() (*renderInfo, error) {
info := renderInfo{
RowHeights: make([]int, 0),
}
p := t.p
opt := t.opt
if len(opt.Header) == 0 {
return nil, errors.New("header can not be nil")
}
theme := opt.Theme
if theme == nil {
theme = p.theme
}
fontSize := opt.FontSize
if fontSize == 0 {
fontSize = 12
}
fontColor := opt.FontColor
if fontColor.IsZero() {
fontColor = tableDefaultSetting.FontColor
}
font := opt.Font
if font == nil {
font = theme.GetFont()
}
headerFontColor := opt.HeaderFontColor
if opt.HeaderFontColor.IsZero() {
headerFontColor = tableDefaultSetting.HeaderFontColor
}
spans := opt.Spans
if len(spans) != len(opt.Header) {
newSpans := make([]int, len(opt.Header))
for index := range opt.Header {
if index >= len(spans) {
newSpans[index] = 1
} else { } else {
newSpans[index] = spans[index] widths[index] = int(v)
}
restWidth -= widths[index]
}
}
if restWidth < 0 {
return nil, ErrTableColumnWidthInvalid
}
// 填充其它未指定的宽度
if autoFillCount > 0 {
autoWidth := restWidth / autoFillCount
for index, v := range widths {
if v == 0 {
widths[index] = autoWidth
} }
} }
spans = newSpans
} }
widthSum := 0
sum := sumInt(spans) for _, v := range widths {
values := autoDivideSpans(p.Width(), sum, spans) widthSum += v
columnWidths := make([]int, 0)
for index, v := range values {
if index == len(values)-1 {
break
}
columnWidths = append(columnWidths, values[index+1]-v)
} }
info.ColumnWidths = columnWidths if widthSum > width {
return nil, ErrTableColumnWidthInvalid
height := 0
textStyle := Style{
FontSize: fontSize,
FontColor: headerFontColor,
FillColor: headerFontColor,
Font: font,
} }
return widths, nil
headerHeight := 0
padding := opt.Padding
if padding.IsZero() {
padding = tableDefaultSetting.Padding
}
getCellTextStyle := opt.CellTextStyle
if getCellTextStyle == nil {
getCellTextStyle = func(_ TableCell) *Style {
return nil
}
}
// textAligns := opt.TextAligns
getTextAlign := func(index int) string {
if len(opt.TextAligns) <= index {
return ""
}
return opt.TextAligns[index]
}
// 表格单元的处理
renderTableCells := func(
currentStyle Style,
rowIndex int,
textList []string,
currentHeight int,
cellPadding Box,
) int {
cellMaxHeight := 0
paddingHeight := cellPadding.Top + cellPadding.Bottom
paddingWidth := cellPadding.Left + cellPadding.Right
for index, text := range textList {
cellStyle := getCellTextStyle(TableCell{
Text: text,
Row: rowIndex,
Column: index,
Style: currentStyle,
})
if cellStyle == nil {
cellStyle = &currentStyle
}
p.SetStyle(*cellStyle)
x := values[index]
y := currentHeight + cellPadding.Top
width := values[index+1] - x
x += cellPadding.Left
width -= paddingWidth
box := p.TextFit(text, x, y+int(fontSize), width, getTextAlign(index))
// 计算最高的高度
if box.Height()+paddingHeight > cellMaxHeight {
cellMaxHeight = box.Height() + paddingHeight
}
}
return cellMaxHeight
}
// 表头的处理
headerHeight = renderTableCells(textStyle, 0, opt.Header, height, padding)
height += headerHeight
info.HeaderHeight = headerHeight
// 表格内容的处理
textStyle.FontColor = fontColor
textStyle.FillColor = fontColor
for index, textList := range opt.Data {
cellHeight := renderTableCells(textStyle, index+1, textList, height, padding)
info.RowHeights = append(info.RowHeights, cellHeight)
height += cellHeight
}
info.Width = p.Width()
info.Height = height
return &info, nil
} }
func (t *tableChart) renderWithInfo(info *renderInfo) (Box, error) { func TableMeasure(opt TableOption) (chart.Box, error) {
p := t.p d, err := NewDraw(DrawOption{
opt := t.opt Width: opt.Width,
if !opt.BackgroundColor.IsZero() { Height: 600,
p.SetBackground(p.Width(), p.Height(), opt.BackgroundColor) })
}
headerBGColor := opt.HeaderBackgroundColor
if headerBGColor.IsZero() {
headerBGColor = tableDefaultSetting.HeaderColor
}
// 如果设置表头背景色
p.SetBackground(info.Width, info.HeaderHeight, headerBGColor, true)
currentHeight := info.HeaderHeight
rowColors := opt.RowBackgroundColors
if rowColors == nil {
rowColors = tableDefaultSetting.RowColors
}
for index, h := range info.RowHeights {
color := rowColors[index%len(rowColors)]
child := p.Child(PainterPaddingOption(Box{
Top: currentHeight,
}))
child.SetBackground(p.Width(), h, color, true)
currentHeight += h
}
// 根据是否有设置表格样式调整背景色
getCellStyle := opt.CellStyle
if getCellStyle != nil {
arr := [][]string{
opt.Header,
}
arr = append(arr, opt.Data...)
top := 0
heights := []int{
info.HeaderHeight,
}
heights = append(heights, info.RowHeights...)
// 循环所有表格单元,生成背景色
for i, textList := range arr {
left := 0
for j, v := range textList {
style := getCellStyle(TableCell{
Text: v,
Row: i,
Column: j,
})
if style != nil && !style.FillColor.IsZero() {
padding := style.Padding
child := p.Child(PainterPaddingOption(Box{
Top: top + padding.Top,
Left: left + padding.Left,
}))
w := info.ColumnWidths[j] - padding.Left - padding.Top
h := heights[i] - padding.Top - padding.Bottom
child.SetBackground(w, h, style.FillColor, true)
}
left += info.ColumnWidths[j]
}
top += heights[i]
}
}
_, err := t.render()
if err != nil { if err != nil {
return BoxZero, err return chart.BoxZero, err
}
opt.Draw = d
opt.measurement = true
return tableRender(opt)
}
func tableRender(opt TableOption) (chart.Box, error) {
if opt.Draw == nil {
return chart.BoxZero, errors.New("draw can not be nil")
}
if len(opt.Header) == 0 {
return chart.BoxZero, errors.New("header can not be nil")
}
width := opt.Width
if width == 0 {
width = opt.Draw.Box.Width()
} }
return Box{ columnWidths, err := tableDivideWidth(width, len(opt.Header), opt.ColumnWidths)
Right: info.Width, if err != nil {
Bottom: info.Height, return chart.BoxZero, err
}
d := opt.Draw
style := opt.Style
y := 0
x := 0
headerMaxHeight := 0
for index, text := range opt.Header {
var box chart.Box
w := columnWidths[index]
y0 := y + int(opt.Style.FontSize)
if opt.measurement {
box = d.measureTextFit(text, x, y0, w, style)
} else {
box = d.textFit(text, x, y0, w, style)
}
if box.Height() > headerMaxHeight {
headerMaxHeight = box.Height()
}
x += w
}
y += headerMaxHeight
return chart.Box{
Right: width,
Bottom: y,
}, nil }, nil
} }
func (t *tableChart) Render() (Box, error) {
p := t.p
opt := t.opt
if !opt.BackgroundColor.IsZero() {
p.SetBackground(p.Width(), p.Height(), opt.BackgroundColor)
}
if opt.Font == nil && opt.FontFamily != "" {
opt.Font, _ = GetFont(opt.FontFamily)
}
r := p.render
fn := chart.PNG
if p.outputType == ChartOutputSVG {
fn = chart.SVG
}
newRender, err := fn(p.Width(), 100)
if err != nil {
return BoxZero, err
}
p.render = newRender
info, err := t.render()
if err != nil {
return BoxZero, err
}
p.render = r
return t.renderWithInfo(info)
}

View file

@ -1,140 +0,0 @@
// 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"
)
func TestTableChart(t *testing.T) {
assert := assert.New(t)
tests := []struct {
render func(*Painter) ([]byte, error)
result string
}{
{
render: func(p *Painter) ([]byte, error) {
_, err := NewTableChart(p, TableChartOption{
Header: []string{
"Name",
"Age",
"Address",
"Tag",
"Action",
},
Spans: []int{
1,
1,
2,
1,
// span和header不匹配最后自动设置为1
// 1,
},
Data: [][]string{
{
"John Brown",
"32",
"New York No. 1 Lake Park",
"nice, developer",
"Send Mail",
},
{
"Jim Green ",
"42",
"London No. 1 Lake Park",
"wow",
"Send Mail",
},
{
"Joe Black ",
"32",
"Sidney No. 1 Lake Park",
"cool, teacher",
"Send Mail",
},
},
}).Render()
if err != nil {
return nil, err
}
return p.Bytes()
},
result: "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"600\" height=\"400\">\\n<path d=\"M 0 0\nL 600 0\nL 600 35\nL 0 35\nL 0 0\" style=\"stroke-width:0;stroke:none;fill:rgba(240,240,240,1.0)\"/><path d=\"M 0 35\nL 600 35\nL 600 90\nL 0 90\nL 0 35\" style=\"stroke-width:0;stroke:none;fill:rgba(255,255,255,1.0)\"/><path d=\"M 0 90\nL 600 90\nL 600 125\nL 0 125\nL 0 90\" style=\"stroke-width:0;stroke:none;fill:rgba(247,247,247,1.0)\"/><path d=\"M 0 125\nL 600 125\nL 600 180\nL 0 180\nL 0 125\" style=\"stroke-width:0;stroke:none;fill:rgba(255,255,255,1.0)\"/><text x=\"10\" y=\"22\" style=\"stroke-width:0;stroke:none;fill:rgba(98,105,118,1.0);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">Name</text><text x=\"110\" y=\"22\" style=\"stroke-width:0;stroke:none;fill:rgba(98,105,118,1.0);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">Age</text><text x=\"210\" y=\"22\" style=\"stroke-width:0;stroke:none;fill:rgba(98,105,118,1.0);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">Address</text><text x=\"410\" y=\"22\" style=\"stroke-width:0;stroke:none;fill:rgba(98,105,118,1.0);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">Tag</text><text x=\"510\" y=\"22\" style=\"stroke-width:0;stroke:none;fill:rgba(98,105,118,1.0);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">Action</text><text x=\"10\" y=\"57\" style=\"stroke-width:0;stroke:none;fill:rgba(70,70,70,1.0);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">John</text><text x=\"10\" y=\"77\" style=\"stroke-width:0;stroke:none;fill:rgba(70,70,70,1.0);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">Brown</text><text x=\"110\" y=\"57\" style=\"stroke-width:0;stroke:none;fill:rgba(70,70,70,1.0);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">32</text><text x=\"210\" y=\"57\" style=\"stroke-width:0;stroke:none;fill:rgba(70,70,70,1.0);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">New York No. 1 Lake Park</text><text x=\"410\" y=\"57\" style=\"stroke-width:0;stroke:none;fill:rgba(70,70,70,1.0);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">nice,</text><text x=\"410\" y=\"77\" style=\"stroke-width:0;stroke:none;fill:rgba(70,70,70,1.0);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">developer</text><text x=\"510\" y=\"57\" style=\"stroke-width:0;stroke:none;fill:rgba(70,70,70,1.0);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">Send Mail</text><text x=\"10\" y=\"112\" style=\"stroke-width:0;stroke:none;fill:rgba(70,70,70,1.0);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">Jim Green</text><text x=\"110\" y=\"112\" style=\"stroke-width:0;stroke:none;fill:rgba(70,70,70,1.0);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">42</text><text x=\"210\" y=\"112\" style=\"stroke-width:0;stroke:none;fill:rgba(70,70,70,1.0);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">London No. 1 Lake Park</text><text x=\"410\" y=\"112\" style=\"stroke-width:0;stroke:none;fill:rgba(70,70,70,1.0);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">wow</text><text x=\"510\" y=\"112\" style=\"stroke-width:0;stroke:none;fill:rgba(70,70,70,1.0);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">Send Mail</text><text x=\"10\" y=\"147\" style=\"stroke-width:0;stroke:none;fill:rgba(70,70,70,1.0);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">Joe Black</text><text x=\"110\" y=\"147\" style=\"stroke-width:0;stroke:none;fill:rgba(70,70,70,1.0);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">32</text><text x=\"210\" y=\"147\" style=\"stroke-width:0;stroke:none;fill:rgba(70,70,70,1.0);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">Sidney No. 1 Lake Park</text><text x=\"410\" y=\"147\" style=\"stroke-width:0;stroke:none;fill:rgba(70,70,70,1.0);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">cool,</text><text x=\"410\" y=\"167\" style=\"stroke-width:0;stroke:none;fill:rgba(70,70,70,1.0);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">teacher</text><text x=\"510\" y=\"147\" style=\"stroke-width:0;stroke:none;fill:rgba(70,70,70,1.0);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">Send Mail</text></svg>",
},
{
render: func(p *Painter) ([]byte, error) {
_, err := NewTableChart(p, TableChartOption{
Header: []string{
"Name",
"Age",
"Address",
"Tag",
"Action",
},
Data: [][]string{
{
"John Brown",
"32",
"New York No. 1 Lake Park",
"nice, developer",
"Send Mail",
},
{
"Jim Green ",
"42",
"London No. 1 Lake Park",
"wow",
"Send Mail",
},
{
"Joe Black ",
"32",
"Sidney No. 1 Lake Park",
"cool, teacher",
"Send Mail",
},
},
}).Render()
if err != nil {
return nil, err
}
return p.Bytes()
},
result: "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"600\" height=\"400\">\\n<path d=\"M 0 0\nL 600 0\nL 600 35\nL 0 35\nL 0 0\" style=\"stroke-width:0;stroke:none;fill:rgba(240,240,240,1.0)\"/><path d=\"M 0 35\nL 600 35\nL 600 90\nL 0 90\nL 0 35\" style=\"stroke-width:0;stroke:none;fill:rgba(255,255,255,1.0)\"/><path d=\"M 0 90\nL 600 90\nL 600 145\nL 0 145\nL 0 90\" style=\"stroke-width:0;stroke:none;fill:rgba(247,247,247,1.0)\"/><path d=\"M 0 145\nL 600 145\nL 600 200\nL 0 200\nL 0 145\" style=\"stroke-width:0;stroke:none;fill:rgba(255,255,255,1.0)\"/><text x=\"10\" y=\"22\" style=\"stroke-width:0;stroke:none;fill:rgba(98,105,118,1.0);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">Name</text><text x=\"130\" y=\"22\" style=\"stroke-width:0;stroke:none;fill:rgba(98,105,118,1.0);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">Age</text><text x=\"250\" y=\"22\" style=\"stroke-width:0;stroke:none;fill:rgba(98,105,118,1.0);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">Address</text><text x=\"370\" y=\"22\" style=\"stroke-width:0;stroke:none;fill:rgba(98,105,118,1.0);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">Tag</text><text x=\"490\" y=\"22\" style=\"stroke-width:0;stroke:none;fill:rgba(98,105,118,1.0);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">Action</text><text x=\"10\" y=\"57\" style=\"stroke-width:0;stroke:none;fill:rgba(70,70,70,1.0);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">John Brown</text><text x=\"130\" y=\"57\" style=\"stroke-width:0;stroke:none;fill:rgba(70,70,70,1.0);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">32</text><text x=\"250\" y=\"57\" style=\"stroke-width:0;stroke:none;fill:rgba(70,70,70,1.0);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">New York No.</text><text x=\"250\" y=\"77\" style=\"stroke-width:0;stroke:none;fill:rgba(70,70,70,1.0);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">1 Lake Park</text><text x=\"370\" y=\"57\" style=\"stroke-width:0;stroke:none;fill:rgba(70,70,70,1.0);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">nice,</text><text x=\"370\" y=\"77\" style=\"stroke-width:0;stroke:none;fill:rgba(70,70,70,1.0);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">developer</text><text x=\"490\" y=\"57\" style=\"stroke-width:0;stroke:none;fill:rgba(70,70,70,1.0);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">Send Mail</text><text x=\"10\" y=\"112\" style=\"stroke-width:0;stroke:none;fill:rgba(70,70,70,1.0);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">Jim Green</text><text x=\"130\" y=\"112\" style=\"stroke-width:0;stroke:none;fill:rgba(70,70,70,1.0);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">42</text><text x=\"250\" y=\"112\" style=\"stroke-width:0;stroke:none;fill:rgba(70,70,70,1.0);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">London No. 1</text><text x=\"250\" y=\"132\" style=\"stroke-width:0;stroke:none;fill:rgba(70,70,70,1.0);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">Lake Park</text><text x=\"370\" y=\"112\" style=\"stroke-width:0;stroke:none;fill:rgba(70,70,70,1.0);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">wow</text><text x=\"490\" y=\"112\" style=\"stroke-width:0;stroke:none;fill:rgba(70,70,70,1.0);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">Send Mail</text><text x=\"10\" y=\"167\" style=\"stroke-width:0;stroke:none;fill:rgba(70,70,70,1.0);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">Joe Black</text><text x=\"130\" y=\"167\" style=\"stroke-width:0;stroke:none;fill:rgba(70,70,70,1.0);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">32</text><text x=\"250\" y=\"167\" style=\"stroke-width:0;stroke:none;fill:rgba(70,70,70,1.0);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">Sidney No. 1</text><text x=\"250\" y=\"187\" style=\"stroke-width:0;stroke:none;fill:rgba(70,70,70,1.0);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">Lake Park</text><text x=\"370\" y=\"167\" style=\"stroke-width:0;stroke:none;fill:rgba(70,70,70,1.0);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">cool, teacher</text><text x=\"490\" y=\"167\" style=\"stroke-width:0;stroke:none;fill:rgba(70,70,70,1.0);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">Send Mail</text></svg>",
},
}
for _, tt := range tests {
p, err := NewPainter(PainterOptions{
Type: ChartOutputSVG,
Width: 600,
Height: 400,
}, PainterThemeOption(defaultTheme))
assert.Nil(err)
data, err := tt.render(p)
assert.Nil(err)
assert.Equal(tt.result, string(data))
}
}

348
theme.go
View file

@ -23,8 +23,7 @@
package charts package charts
import ( import (
"github.com/golang/freetype/truetype" "github.com/wcharczuk/go-chart/v2/drawing"
"git.smarteching.com/zeni/go-chart/v2/drawing"
) )
const ThemeDark = "dark" const ThemeDark = "dark"
@ -32,65 +31,23 @@ const ThemeLight = "light"
const ThemeGrafana = "grafana" const ThemeGrafana = "grafana"
const ThemeAnt = "ant" const ThemeAnt = "ant"
type ColorPalette interface { type Theme struct {
IsDark() bool palette *themeColorPalette
GetAxisStrokeColor() Color
SetAxisStrokeColor(Color)
GetAxisSplitLineColor() Color
SetAxisSplitLineColor(Color)
GetSeriesColor(int) Color
SetSeriesColor([]Color)
GetBackgroundColor() Color
SetBackgroundColor(Color)
GetTextColor() Color
SetTextColor(Color)
GetFontSize() float64
SetFontSize(float64)
GetFont() *truetype.Font
SetFont(*truetype.Font)
} }
type themeColorPalette struct { type themeColorPalette struct {
isDarkMode bool isDarkMode bool
axisStrokeColor Color axisStrokeColor drawing.Color
axisSplitLineColor Color axisSplitLineColor drawing.Color
backgroundColor Color backgroundColor drawing.Color
textColor Color textColor drawing.Color
seriesColors []Color seriesColors []drawing.Color
fontSize float64
font *truetype.Font
}
type ThemeOption struct {
IsDarkMode bool
AxisStrokeColor Color
AxisSplitLineColor Color
BackgroundColor Color
TextColor Color
SeriesColors []Color
} }
var palettes = map[string]*themeColorPalette{} var palettes = map[string]*themeColorPalette{}
const defaultFontSize = 12.0
var defaultTheme ColorPalette
var defaultLightFontColor = drawing.Color{
R: 70,
G: 70,
B: 70,
A: 255,
}
var defaultDarkFontColor = drawing.Color{
R: 238,
G: 238,
B: 238,
A: 255,
}
func init() { func init() {
echartSeriesColors := []Color{ echartSeriesColors := []drawing.Color{
parseColor("#5470c6"), parseColor("#5470c6"),
parseColor("#91cc75"), parseColor("#91cc75"),
parseColor("#fac858"), parseColor("#fac858"),
@ -101,7 +58,7 @@ func init() {
parseColor("#9a60b4"), parseColor("#9a60b4"),
parseColor("#ea7ccc"), parseColor("#ea7ccc"),
} }
grafanaSeriesColors := []Color{ grafanaSeriesColors := []drawing.Color{
parseColor("#7EB26D"), parseColor("#7EB26D"),
parseColor("#EAB839"), parseColor("#EAB839"),
parseColor("#6ED0E0"), parseColor("#6ED0E0"),
@ -111,7 +68,7 @@ func init() {
parseColor("#705DA0"), parseColor("#705DA0"),
parseColor("#508642"), parseColor("#508642"),
} }
antSeriesColors := []Color{ antSeriesColors := []drawing.Color{
parseColor("#5b8ff9"), parseColor("#5b8ff9"),
parseColor("#5ad8a6"), parseColor("#5ad8a6"),
parseColor("#5d7092"), parseColor("#5d7092"),
@ -123,210 +80,155 @@ func init() {
} }
AddTheme( AddTheme(
ThemeDark, ThemeDark,
ThemeOption{ true,
IsDarkMode: true, drawing.Color{
AxisStrokeColor: Color{ R: 185,
R: 185, G: 184,
G: 184, B: 206,
B: 206, A: 255,
A: 255,
},
AxisSplitLineColor: Color{
R: 72,
G: 71,
B: 83,
A: 255,
},
BackgroundColor: Color{
R: 16,
G: 12,
B: 42,
A: 255,
},
TextColor: Color{
R: 238,
G: 238,
B: 238,
A: 255,
},
SeriesColors: echartSeriesColors,
}, },
drawing.Color{
R: 72,
G: 71,
B: 83,
A: 255,
},
drawing.Color{
R: 16,
G: 12,
B: 42,
A: 255,
},
drawing.Color{
R: 238,
G: 238,
B: 238,
A: 255,
},
echartSeriesColors,
) )
AddTheme( AddTheme(
ThemeLight, ThemeLight,
ThemeOption{ false,
IsDarkMode: false, drawing.Color{
AxisStrokeColor: Color{ R: 110,
R: 110, G: 112,
G: 112, B: 121,
B: 121, A: 255,
A: 255,
},
AxisSplitLineColor: Color{
R: 224,
G: 230,
B: 242,
A: 255,
},
BackgroundColor: drawing.ColorWhite,
TextColor: Color{
R: 70,
G: 70,
B: 70,
A: 255,
},
SeriesColors: echartSeriesColors,
}, },
drawing.Color{
R: 224,
G: 230,
B: 242,
A: 255,
},
drawing.ColorWhite,
drawing.Color{
R: 70,
G: 70,
B: 70,
A: 255,
},
echartSeriesColors,
) )
AddTheme( AddTheme(
ThemeAnt, ThemeAnt,
ThemeOption{ false,
IsDarkMode: false, drawing.Color{
AxisStrokeColor: Color{ R: 110,
R: 110, G: 112,
G: 112, B: 121,
B: 121, A: 255,
A: 255,
},
AxisSplitLineColor: Color{
R: 224,
G: 230,
B: 242,
A: 255,
},
BackgroundColor: drawing.ColorWhite,
TextColor: drawing.Color{
R: 70,
G: 70,
B: 70,
A: 255,
},
SeriesColors: antSeriesColors,
}, },
drawing.Color{
R: 224,
G: 230,
B: 242,
A: 255,
},
drawing.ColorWhite,
drawing.Color{
R: 70,
G: 70,
B: 70,
A: 255,
},
antSeriesColors,
) )
AddTheme( AddTheme(
ThemeGrafana, ThemeGrafana,
ThemeOption{ true,
IsDarkMode: true, drawing.Color{
AxisStrokeColor: Color{ R: 185,
R: 185, G: 184,
G: 184, B: 206,
B: 206, A: 255,
A: 255,
},
AxisSplitLineColor: Color{
R: 68,
G: 67,
B: 67,
A: 255,
},
BackgroundColor: drawing.Color{
R: 31,
G: 29,
B: 29,
A: 255,
},
TextColor: Color{
R: 216,
G: 217,
B: 218,
A: 255,
},
SeriesColors: grafanaSeriesColors,
}, },
drawing.Color{
R: 68,
G: 67,
B: 67,
A: 255,
},
drawing.Color{
R: 31,
G: 29,
B: 29,
A: 255,
},
drawing.Color{
R: 216,
G: 217,
B: 218,
A: 255,
},
grafanaSeriesColors,
) )
SetDefaultTheme(ThemeLight)
} }
// SetDefaultTheme sets default theme func AddTheme(name string, isDarkMode bool, axisStrokeColor, axisSplitLineColor, backgroundColor, textColor drawing.Color, seriesColors []drawing.Color) {
func SetDefaultTheme(name string) {
defaultTheme = NewTheme(name)
}
func AddTheme(name string, opt ThemeOption) {
palettes[name] = &themeColorPalette{ palettes[name] = &themeColorPalette{
isDarkMode: opt.IsDarkMode, isDarkMode: isDarkMode,
axisStrokeColor: opt.AxisStrokeColor, axisStrokeColor: axisStrokeColor,
axisSplitLineColor: opt.AxisSplitLineColor, axisSplitLineColor: axisSplitLineColor,
backgroundColor: opt.BackgroundColor, backgroundColor: backgroundColor,
textColor: opt.TextColor, textColor: textColor,
seriesColors: opt.SeriesColors, seriesColors: seriesColors,
} }
} }
func NewTheme(name string) ColorPalette { func NewTheme(name string) *Theme {
p, ok := palettes[name] p, ok := palettes[name]
if !ok { if !ok {
p = palettes[ThemeLight] p = palettes[ThemeLight]
} }
clone := *p return &Theme{
return &clone palette: p,
}
} }
func (t *themeColorPalette) IsDark() bool { func (t *Theme) IsDark() bool {
return t.isDarkMode return t.palette.isDarkMode
} }
func (t *themeColorPalette) GetAxisStrokeColor() Color { func (t *Theme) GetAxisStrokeColor() drawing.Color {
return t.axisStrokeColor return t.palette.axisStrokeColor
} }
func (t *themeColorPalette) SetAxisStrokeColor(c Color) { func (t *Theme) GetAxisSplitLineColor() drawing.Color {
t.axisStrokeColor = c return t.palette.axisSplitLineColor
} }
func (t *themeColorPalette) GetAxisSplitLineColor() Color { func (t *Theme) GetSeriesColor(index int) drawing.Color {
return t.axisSplitLineColor colors := t.palette.seriesColors
}
func (t *themeColorPalette) SetAxisSplitLineColor(c Color) {
t.axisSplitLineColor = c
}
func (t *themeColorPalette) GetSeriesColor(index int) Color {
colors := t.seriesColors
return colors[index%len(colors)] return colors[index%len(colors)]
} }
func (t *themeColorPalette) SetSeriesColor(colors []Color) {
t.seriesColors = colors func (t *Theme) GetBackgroundColor() drawing.Color {
return t.palette.backgroundColor
} }
func (t *themeColorPalette) GetBackgroundColor() Color { func (t *Theme) GetTextColor() drawing.Color {
return t.backgroundColor return t.palette.textColor
}
func (t *themeColorPalette) SetBackgroundColor(c Color) {
t.backgroundColor = c
}
func (t *themeColorPalette) GetTextColor() Color {
return t.textColor
}
func (t *themeColorPalette) SetTextColor(c Color) {
t.textColor = c
}
func (t *themeColorPalette) GetFontSize() float64 {
if t.fontSize != 0 {
return t.fontSize
}
return defaultFontSize
}
func (t *themeColorPalette) SetFontSize(fontSize float64) {
t.fontSize = fontSize
}
func (t *themeColorPalette) GetFont() *truetype.Font {
if t.font != nil {
return t.font
}
f, _ := GetDefaultFont()
return f
}
func (t *themeColorPalette) SetFont(f *truetype.Font) {
t.font = f
} }

87
theme_test.go Normal file
View file

@ -0,0 +1,87 @@
// 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/drawing"
)
func TestTheme(t *testing.T) {
assert := assert.New(t)
darkTheme := NewTheme(ThemeDark)
lightTheme := NewTheme(ThemeLight)
assert.True(darkTheme.IsDark())
assert.False(lightTheme.IsDark())
assert.Equal(drawing.Color{
R: 185,
G: 184,
B: 206,
A: 255,
}, darkTheme.GetAxisStrokeColor())
assert.Equal(drawing.Color{
R: 110,
G: 112,
B: 121,
A: 255,
}, lightTheme.GetAxisStrokeColor())
assert.Equal(drawing.Color{
R: 72,
G: 71,
B: 83,
A: 255,
}, darkTheme.GetAxisSplitLineColor())
assert.Equal(drawing.Color{
R: 224,
G: 230,
B: 242,
A: 255,
}, lightTheme.GetAxisSplitLineColor())
assert.Equal(drawing.Color{
R: 16,
G: 12,
B: 42,
A: 255,
}, darkTheme.GetBackgroundColor())
assert.Equal(drawing.ColorWhite, lightTheme.GetBackgroundColor())
assert.Equal(drawing.Color{
R: 238,
G: 238,
B: 238,
A: 255,
}, darkTheme.GetTextColor())
assert.Equal(drawing.Color{
R: 70,
G: 70,
B: 70,
A: 255,
}, lightTheme.GetTextColor())
}

112
title.go
View file

@ -26,16 +26,18 @@ import (
"strconv" "strconv"
"strings" "strings"
"github.com/golang/freetype/truetype" "github.com/wcharczuk/go-chart/v2"
) )
type TitleOption struct { type TitleOption struct {
// The theme of chart
Theme ColorPalette
// Title text, support \n for new line // Title text, support \n for new line
Text string Text string
// Subtitle text, support \n for new line // Subtitle text, support \n for new line
Subtext string Subtext string
// Title style
Style chart.Style
// Subtitle style
SubtextStyle chart.Style
// Distance between title component and the left side of the container. // Distance between title component and the left side of the container.
// It can be pixel value: 20, percentage value: 20%, // It can be pixel value: 20, percentage value: 20%,
// or position value: right, center. // or position value: right, center.
@ -43,23 +45,12 @@ type TitleOption struct {
// Distance between title component and the top side of the container. // Distance between title component and the top side of the container.
// It can be pixel value: 20. // It can be pixel value: 20.
Top string Top string
// The font of label
Font *truetype.Font
// The font size of label
FontSize float64
// The color of label
FontColor Color
// The subtext font size of label
SubtextFontSize float64
// The subtext font color of label
SubtextFontColor Color
} }
type titleMeasureOption struct { type titleMeasureOption struct {
width int width int
height int height int
text string text string
style Style style chart.Style
} }
func splitTitleText(text string) []string { func splitTitleText(text string) []string {
@ -75,78 +66,44 @@ func splitTitleText(text string) []string {
return result return result
} }
type titlePainter struct { func drawTitle(p *Draw, opt *TitleOption) (chart.Box, error) {
p *Painter if len(opt.Text) == 0 {
opt *TitleOption return chart.BoxZero, nil
}
// NewTitlePainter returns a title renderer
func NewTitlePainter(p *Painter, opt TitleOption) *titlePainter {
return &titlePainter{
p: p,
opt: &opt,
} }
}
func (t *titlePainter) Render() (Box, error) { padding := opt.Style.Padding
opt := t.opt d, err := NewDraw(DrawOption{
p := t.p Parent: p,
theme := opt.Theme }, PaddingOption(padding))
if err != nil {
return chart.BoxZero, err
}
if theme == nil { r := d.Render
theme = p.theme
}
if opt.Text == "" && opt.Subtext == "" {
return BoxZero, nil
}
measureOptions := make([]titleMeasureOption, 0) measureOptions := make([]titleMeasureOption, 0)
if opt.Font == nil {
opt.Font = theme.GetFont()
}
if opt.FontColor.IsZero() {
opt.FontColor = theme.GetTextColor()
}
if opt.FontSize == 0 {
opt.FontSize = theme.GetFontSize()
}
if opt.SubtextFontColor.IsZero() {
opt.SubtextFontColor = opt.FontColor
}
if opt.SubtextFontSize == 0 {
opt.SubtextFontSize = opt.FontSize
}
titleTextStyle := Style{
Font: opt.Font,
FontSize: opt.FontSize,
FontColor: opt.FontColor,
}
// 主标题 // 主标题
for _, v := range splitTitleText(opt.Text) { for _, v := range splitTitleText(opt.Text) {
measureOptions = append(measureOptions, titleMeasureOption{ measureOptions = append(measureOptions, titleMeasureOption{
text: v, text: v,
style: titleTextStyle, style: opt.Style.GetTextOptions(),
}) })
} }
subtextStyle := Style{
Font: opt.Font,
FontSize: opt.SubtextFontSize,
FontColor: opt.SubtextFontColor,
}
// 副标题 // 副标题
for _, v := range splitTitleText(opt.Subtext) { for _, v := range splitTitleText(opt.Subtext) {
measureOptions = append(measureOptions, titleMeasureOption{ measureOptions = append(measureOptions, titleMeasureOption{
text: v, text: v,
style: subtextStyle, style: opt.SubtextStyle.GetTextOptions(),
}) })
} }
textMaxWidth := 0 textMaxWidth := 0
textMaxHeight := 0 textMaxHeight := 0
width := 0
for index, item := range measureOptions { for index, item := range measureOptions {
p.OverrideTextStyle(item.style) item.style.WriteTextOptionsToRenderer(r)
textBox := p.MeasureText(item.text) textBox := r.MeasureText(item.text)
w := textBox.Width() w := textBox.Width()
h := textBox.Height() h := textBox.Height()
@ -159,18 +116,18 @@ func (t *titlePainter) Render() (Box, error) {
measureOptions[index].height = h measureOptions[index].height = h
measureOptions[index].width = w measureOptions[index].width = w
} }
width := textMaxWidth width = textMaxWidth
titleX := 0 titleX := 0
b := d.Box
switch opt.Left { switch opt.Left {
case PositionRight: case PositionRight:
titleX = p.Width() - textMaxWidth titleX = b.Width() - textMaxWidth
case PositionCenter: case PositionCenter:
titleX = p.Width()>>1 - (textMaxWidth >> 1) titleX = b.Width()>>1 - (textMaxWidth >> 1)
default: default:
if strings.HasSuffix(opt.Left, "%") { if strings.HasSuffix(opt.Left, "%") {
value, _ := strconv.Atoi(strings.ReplaceAll(opt.Left, "%", "")) value, _ := strconv.Atoi(strings.ReplaceAll(opt.Left, "%", ""))
titleX = p.Width() * value / 100 titleX = b.Width() * value / 100
} else { } else {
value, _ := strconv.Atoi(opt.Left) value, _ := strconv.Atoi(opt.Left)
titleX = value titleX = value
@ -183,15 +140,16 @@ func (t *titlePainter) Render() (Box, error) {
titleY += value titleY += value
} }
for _, item := range measureOptions { for _, item := range measureOptions {
p.OverrideTextStyle(item.style) item.style.WriteTextOptionsToRenderer(r)
x := titleX + (textMaxWidth-item.width)>>1 x := titleX + (textMaxWidth-item.width)>>1
y := titleY + item.height y := titleY + item.height
p.Text(item.text, x, y) d.text(item.text, x, y)
titleY += item.height titleY += item.height
} }
height := titleY + padding.Top + padding.Bottom
box := padding.Clone()
box.Right = box.Left + titleX + width
box.Bottom = box.Top + height
return Box{ return box, nil
Bottom: titleY,
Right: titleX + width,
}, nil
} }

View file

@ -26,68 +26,117 @@ import (
"testing" "testing"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/wcharczuk/go-chart/v2"
"github.com/wcharczuk/go-chart/v2/drawing"
) )
func TestTitleRenderer(t *testing.T) { func TestSplitTitleText(t *testing.T) {
assert := assert.New(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 { tests := []struct {
render func(*Painter) ([]byte, error) newDraw func() *Draw
result string newOption func() *TitleOption
result string
box chart.Box
}{ }{
{ {
render: func(p *Painter) ([]byte, error) { newDraw: newDraw,
_, err := NewTitlePainter(p, TitleOption{ newOption: newOption,
Text: "title", result: "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"400\" height=\"300\">\\n<text x=\"6\" y=\"17\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,0,1.0);font-size:17.9px;font-family:'Roboto Medium',sans-serif\">title</text><text x=\"0\" y=\"34\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,0,1.0);font-size:17.9px;font-family:'Roboto Medium',sans-serif\">Hello</text><text x=\"0\" y=\"46\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,255,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">subtitle</text><text x=\"3\" y=\"58\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,255,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">World!</text></svg>",
Subtext: "subTitle", box: chart.Box{
Left: "20", Right: 43,
Top: "20", Bottom: 58,
}).Render()
if err != nil {
return nil, err
}
return p.Bytes()
}, },
result: "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"600\" height=\"400\">\\n<text x=\"34\" y=\"35\" style=\"stroke-width:0;stroke:none;fill:rgba(70,70,70,1.0);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">title</text><text x=\"20\" y=\"50\" style=\"stroke-width:0;stroke:none;fill:rgba(70,70,70,1.0);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">subTitle</text></svg>",
}, },
{ {
render: func(p *Painter) ([]byte, error) { newDraw: newDraw,
_, err := NewTitlePainter(p, TitleOption{ newOption: func() *TitleOption {
Text: "title", opt := newOption()
Subtext: "subTitle", opt.Left = PositionRight
Left: "20%", opt.Top = "50"
Top: "20", return opt
}).Render() },
if err != nil { result: "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"400\" height=\"300\">\\n<text x=\"363\" y=\"67\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,0,1.0);font-size:17.9px;font-family:'Roboto Medium',sans-serif\">title</text><text x=\"357\" y=\"84\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,0,1.0);font-size:17.9px;font-family:'Roboto Medium',sans-serif\">Hello</text><text x=\"357\" y=\"96\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,255,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">subtitle</text><text x=\"360\" y=\"108\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,255,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">World!</text></svg>",
return nil, err box: chart.Box{
} Right: 400,
return p.Bytes() Bottom: 108,
}, },
result: "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"600\" height=\"400\">\\n<text x=\"134\" y=\"35\" style=\"stroke-width:0;stroke:none;fill:rgba(70,70,70,1.0);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">title</text><text x=\"120\" y=\"50\" style=\"stroke-width:0;stroke:none;fill:rgba(70,70,70,1.0);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">subTitle</text></svg>",
}, },
{ {
render: func(p *Painter) ([]byte, error) { newDraw: newDraw,
_, err := NewTitlePainter(p, TitleOption{ newOption: func() *TitleOption {
Text: "title", opt := newOption()
Subtext: "subTitle", opt.Left = PositionCenter
Left: PositionRight, opt.Top = "10"
}).Render() return opt
if err != nil { },
return nil, err result: "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"400\" height=\"300\">\\n<text x=\"185\" y=\"27\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,0,1.0);font-size:17.9px;font-family:'Roboto Medium',sans-serif\">title</text><text x=\"179\" y=\"44\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,0,1.0);font-size:17.9px;font-family:'Roboto Medium',sans-serif\">Hello</text><text x=\"179\" y=\"56\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,255,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">subtitle</text><text x=\"182\" y=\"68\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,255,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">World!</text></svg>",
} box: chart.Box{
return p.Bytes() Right: 222,
Bottom: 68,
},
},
{
newDraw: newDraw,
newOption: func() *TitleOption {
opt := newOption()
opt.Left = "10%"
opt.Top = "10"
return opt
},
result: "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"400\" height=\"300\">\\n<text x=\"46\" y=\"27\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,0,1.0);font-size:17.9px;font-family:'Roboto Medium',sans-serif\">title</text><text x=\"40\" y=\"44\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,0,1.0);font-size:17.9px;font-family:'Roboto Medium',sans-serif\">Hello</text><text x=\"40\" y=\"56\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,255,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">subtitle</text><text x=\"43\" y=\"68\" style=\"stroke-width:0;stroke:none;fill:rgba(0,0,255,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">World!</text></svg>",
box: chart.Box{
Right: 83,
Bottom: 68,
}, },
result: "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"600\" height=\"400\">\\n<text x=\"558\" y=\"15\" style=\"stroke-width:0;stroke:none;fill:rgba(70,70,70,1.0);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">title</text><text x=\"544\" y=\"30\" style=\"stroke-width:0;stroke:none;fill:rgba(70,70,70,1.0);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">subTitle</text></svg>",
}, },
} }
for _, tt := range tests { for _, tt := range tests {
p, err := NewPainter(PainterOptions{ d := tt.newDraw()
Type: ChartOutputSVG, o := tt.newOption()
Width: 600, b, err := drawTitle(d, o)
Height: 400,
}, PainterThemeOption(defaultTheme))
assert.Nil(err) assert.Nil(err)
data, err := tt.render(p) assert.Equal(tt.box, b)
data, err := d.Bytes()
assert.Nil(err) assert.Nil(err)
assert.NotEmpty(data)
assert.Equal(tt.result, string(data)) assert.Equal(tt.result, string(data))
} }
} }

98
util.go
View file

@ -29,8 +29,8 @@ import (
"strings" "strings"
"github.com/dustin/go-humanize" "github.com/dustin/go-humanize"
"git.smarteching.com/zeni/go-chart/v2" "github.com/wcharczuk/go-chart/v2"
"git.smarteching.com/zeni/go-chart/v2/drawing" "github.com/wcharczuk/go-chart/v2/drawing"
) )
func TrueFlag() *bool { func TrueFlag() *bool {
@ -43,24 +43,6 @@ func FalseFlag() *bool {
return &f return &f
} }
func containsInt(values []int, value int) bool {
for _, v := range values {
if v == value {
return true
}
}
return false
}
func containsString(values []string, value string) bool {
for _, v := range values {
if v == value {
return true
}
}
return false
}
func ceilFloatToInt(value float64) int { func ceilFloatToInt(value float64) int {
i := int(value) i := int(value)
if value == float64(i) { if value == float64(i) {
@ -77,49 +59,28 @@ func getDefaultInt(value, defaultValue int) int {
} }
func autoDivide(max, size int) []int { func autoDivide(max, size int) []int {
unit := float64(max) / float64(size) unit := max / size
rest := max - unit*size
values := make([]int, size+1) values := make([]int, size+1)
for i := 0; i < size+1; i++ { value := 0
if i == size { for i := 0; i < size; i++ {
values[i] = max values[i] = value
} else { if i < rest {
values[i] = int(float64(i) * unit) value++
} }
value += unit
} }
values[size] = max
return values return values
} }
func autoDivideSpans(max, size int, spans []int) []int {
values := autoDivide(max, size)
// 重新合并
if len(spans) != 0 {
newValues := make([]int, len(spans)+1)
newValues[0] = 0
end := 0
for index, v := range spans {
end += v
newValues[index+1] = values[end]
}
values = newValues
}
return values
}
func sumInt(values []int) int {
sum := 0
for _, v := range values {
sum += v
}
return sum
}
// measureTextMaxWidthHeight returns maxWidth and maxHeight of text list // measureTextMaxWidthHeight returns maxWidth and maxHeight of text list
func measureTextMaxWidthHeight(textList []string, p *Painter) (int, int) { func measureTextMaxWidthHeight(textList []string, r chart.Renderer) (int, int) {
maxWidth := 0 maxWidth := 0
maxHeight := 0 maxHeight := 0
for _, text := range textList { for _, text := range textList {
box := p.MeasureText(text) box := r.MeasureText(text)
maxWidth = chart.MaxInt(maxWidth, box.Width()) maxWidth = chart.MaxInt(maxWidth, box.Width())
maxHeight = chart.MaxInt(maxHeight, box.Height()) maxHeight = chart.MaxInt(maxHeight, box.Height())
} }
@ -160,31 +121,21 @@ func NewFloatPoint(f float64) *float64 {
v := f v := f
return &v return &v
} }
const K_VALUE = float64(1000)
const M_VALUE = K_VALUE * K_VALUE
const G_VALUE = M_VALUE * K_VALUE
const T_VALUE = G_VALUE * K_VALUE
func commafWithDigits(value float64) string { func commafWithDigits(value float64) string {
decimals := 2 decimals := 2
if value >= T_VALUE { m := float64(1000 * 1000)
return humanize.CommafWithDigits(value/T_VALUE, decimals) + "T" if value >= m {
return humanize.CommafWithDigits(value/m, decimals) + "M"
} }
if value >= G_VALUE { k := float64(1000)
return humanize.CommafWithDigits(value/G_VALUE, decimals) + "G" if value >= k {
} return humanize.CommafWithDigits(value/k, decimals) + "k"
if value >= M_VALUE {
return humanize.CommafWithDigits(value/M_VALUE, decimals) + "M"
}
if value >= K_VALUE {
return humanize.CommafWithDigits(value/K_VALUE, decimals) + "k"
} }
return humanize.CommafWithDigits(value, decimals) return humanize.CommafWithDigits(value, decimals)
} }
func parseColor(color string) Color { func parseColor(color string) drawing.Color {
c := Color{} c := drawing.Color{}
if color == "" { if color == "" {
return c return c
} }
@ -262,10 +213,3 @@ func getPolygonPoints(center Point, radius float64, sides int) []Point {
} }
return points return points
} }
func isLightColor(c Color) bool {
r := float64(c.R) * float64(c.R) * 0.299
g := float64(c.G) * float64(c.G) * 0.587
b := float64(c.B) * float64(c.B) * 0.114
return math.Sqrt(r+g+b) > 127.5
}

View file

@ -26,8 +26,8 @@ import (
"testing" "testing"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"git.smarteching.com/zeni/go-chart/v2" "github.com/wcharczuk/go-chart/v2"
"git.smarteching.com/zeni/go-chart/v2/drawing" "github.com/wcharczuk/go-chart/v2/drawing"
) )
func TestGetDefaultInt(t *testing.T) { func TestGetDefaultInt(t *testing.T) {
@ -60,12 +60,12 @@ func TestAutoDivide(t *testing.T) {
assert.Equal([]int{ assert.Equal([]int{
0, 0,
85, 86,
171, 172,
257, 258,
342, 344,
428, 430,
514, 515,
600, 600,
}, autoDivide(600, 7)) }, autoDivide(600, 7))
} }
@ -80,15 +80,13 @@ func TestGetRadius(t *testing.T) {
func TestMeasureTextMaxWidthHeight(t *testing.T) { func TestMeasureTextMaxWidthHeight(t *testing.T) {
assert := assert.New(t) assert := assert.New(t)
p, err := NewPainter(PainterOptions{ r, err := chart.SVG(400, 300)
Width: 400,
Height: 300,
})
assert.Nil(err) assert.Nil(err)
style := chart.Style{ style := chart.Style{
FontSize: 10, FontSize: 10,
} }
p.SetStyle(style) style.Font, _ = chart.GetDefaultFont()
style.WriteToRenderer(r)
maxWidth, maxHeight := measureTextMaxWidthHeight([]string{ maxWidth, maxHeight := measureTextMaxWidthHeight([]string{
"Mon", "Mon",
@ -98,8 +96,8 @@ func TestMeasureTextMaxWidthHeight(t *testing.T) {
"Fri", "Fri",
"Sat", "Sat",
"Sun", "Sun",
}, p) }, r)
assert.Equal(31, maxWidth) assert.Equal(26, maxWidth)
assert.Equal(12, maxHeight) assert.Equal(12, maxHeight)
} }
@ -189,35 +187,3 @@ func TestParseColor(t *testing.T) {
A: 250, A: 250,
}, c) }, c)
} }
func TestIsLightColor(t *testing.T) {
assert := assert.New(t)
assert.True(isLightColor(drawing.Color{
R: 255,
G: 255,
B: 255,
}))
assert.True(isLightColor(drawing.Color{
R: 145,
G: 204,
B: 117,
}))
assert.False(isLightColor(drawing.Color{
R: 88,
G: 112,
B: 198,
}))
assert.False(isLightColor(drawing.Color{
R: 0,
G: 0,
B: 0,
}))
assert.False(isLightColor(drawing.Color{
R: 16,
G: 12,
B: 42,
}))
}

View file

@ -24,10 +24,10 @@ package charts
import ( import (
"github.com/golang/freetype/truetype" "github.com/golang/freetype/truetype"
"github.com/wcharczuk/go-chart/v2"
) )
type XAxisOption struct { type XAxisOption struct {
// The font of x axis
Font *truetype.Font Font *truetype.Font
// The boundary gap on both sides of a coordinate axis. // The boundary gap on both sides of a coordinate axis.
// Nil or *true means the center part of two axis ticks // Nil or *true means the center part of two axis ticks
@ -35,31 +35,13 @@ type XAxisOption struct {
// The data value of x axis // The data value of x axis
Data []string Data []string
// The theme of chart // The theme of chart
Theme ColorPalette Theme string
// The font size of x axis label // Hidden x axis
FontSize float64 Hidden bool
// The flag for show axis, set this to *false will hide axis
Show *bool
// Number of segments that the axis is split into. Note that this number serves only as a recommendation. // Number of segments that the axis is split into. Note that this number serves only as a recommendation.
SplitNumber int SplitNumber int
// The position of axis, it can be 'top' or 'bottom'
Position string
// The line color of axis
StrokeColor Color
// The color of label
FontColor Color
// The text rotation of label
TextRotation float64
// The first axis
FirstAxis int
// The offset of label
LabelOffset Box
isValueAxis bool
} }
const defaultXAxisHeight = 30
// NewXAxisOption returns a x axis option
func NewXAxisOption(data []string, boundaryGap ...*bool) XAxisOption { func NewXAxisOption(data []string, boundaryGap ...*bool) XAxisOption {
opt := XAxisOption{ opt := XAxisOption{
Data: data, Data: data,
@ -70,36 +52,51 @@ func NewXAxisOption(data []string, boundaryGap ...*bool) XAxisOption {
return opt return opt
} }
func (opt *XAxisOption) ToAxisOption() AxisOption { // drawXAxis draws x axis, and returns the height, range of if.
position := PositionBottom func drawXAxis(p *Draw, opt *XAxisOption, yAxisCount int) (int, *Range, error) {
if opt.Position == PositionTop { if opt.Hidden {
position = PositionTop return 0, nil, nil
} }
axisOpt := AxisOption{ left := YAxisWidth
Theme: opt.Theme, right := (yAxisCount - 1) * YAxisWidth
Data: opt.Data, dXAxis, err := NewDraw(
BoundaryGap: opt.BoundaryGap, DrawOption{
Position: position, Parent: p,
SplitNumber: opt.SplitNumber, },
StrokeColor: opt.StrokeColor, PaddingOption(chart.Box{
FontSize: opt.FontSize, Left: left,
Font: opt.Font, Right: right,
FontColor: opt.FontColor, }),
Show: opt.Show, )
SplitLineColor: opt.Theme.GetAxisSplitLineColor(), if opt.Font != nil {
TextRotation: opt.TextRotation, dXAxis.Font = opt.Font
LabelOffset: opt.LabelOffset,
FirstAxis: opt.FirstAxis,
} }
if opt.isValueAxis { if err != nil {
axisOpt.SplitLineShow = true return 0, nil, err
axisOpt.StrokeWidth = -1 }
axisOpt.BoundaryGap = FalseFlag() theme := NewTheme(opt.Theme)
data := NewAxisDataListFromStringList(opt.Data)
style := AxisOption{
BoundaryGap: opt.BoundaryGap,
StrokeColor: theme.GetAxisStrokeColor(),
FontColor: theme.GetAxisStrokeColor(),
StrokeWidth: 1,
SplitNumber: opt.SplitNumber,
} }
return axisOpt
}
// NewBottomXAxis returns a bottom x axis renderer boundary := true
func NewBottomXAxis(p *Painter, opt XAxisOption) *axisPainter { max := float64(len(opt.Data))
return NewAxisPainter(p, opt.ToAxisOption()) if isFalse(opt.BoundaryGap) {
boundary = false
max--
}
axis := NewAxis(dXAxis, data, style)
axis.Render()
return axis.measure().Height, &Range{
divideCount: len(opt.Data),
Min: 0,
Max: max,
Size: dXAxis.Box.Width(),
Boundary: boundary,
}, nil
} }

108
xaxis_test.go Normal file
View file

@ -0,0 +1,108 @@
// 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"
)
func TestNewXAxisOption(t *testing.T) {
assert := assert.New(t)
opt := NewXAxisOption([]string{
"a",
"b",
}, FalseFlag())
assert.Equal(XAxisOption{
Data: []string{
"a",
"b",
},
BoundaryGap: FalseFlag(),
}, opt)
}
func TestDrawXAxis(t *testing.T) {
assert := assert.New(t)
newDraw := func() *Draw {
d, _ := NewDraw(DrawOption{
Width: 400,
Height: 300,
})
return d
}
tests := []struct {
newDraw func() *Draw
newOption func() *XAxisOption
result string
}{
{
newDraw: newDraw,
newOption: func() *XAxisOption {
return &XAxisOption{
BoundaryGap: FalseFlag(),
Data: []string{
"Mon",
"Tue",
},
}
},
result: "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"400\" height=\"300\">\\n<path d=\"M 40 275\nL 400 275\" style=\"stroke-width:1;stroke:rgba(110,112,121,1.0);fill:none\"/><path d=\"M 40 275\nL 40 280\" style=\"stroke-width:1;stroke:rgba(110,112,121,1.0);fill:none\"/><path d=\"M 400 275\nL 400 280\" style=\"stroke-width:1;stroke:rgba(110,112,121,1.0);fill:none\"/><text x=\"27\" y=\"292\" style=\"stroke-width:0;stroke:none;fill:rgba(110,112,121,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">Mon</text><text x=\"389\" y=\"292\" style=\"stroke-width:0;stroke:none;fill:rgba(110,112,121,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">Tue</text></svg>",
},
{
newDraw: newDraw,
newOption: func() *XAxisOption {
return &XAxisOption{
Data: []string{
"01-01",
"01-02",
"01-03",
"01-04",
"01-05",
"01-06",
"01-07",
"01-08",
"01-09",
},
SplitNumber: 3,
}
},
result: "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"400\" height=\"300\">\\n<path d=\"M 40 275\nL 400 275\" style=\"stroke-width:1;stroke:rgba(110,112,121,1.0);fill:none\"/><path d=\"M 40 275\nL 40 280\" style=\"stroke-width:1;stroke:rgba(110,112,121,1.0);fill:none\"/><path d=\"M 160 275\nL 160 280\" style=\"stroke-width:1;stroke:rgba(110,112,121,1.0);fill:none\"/><path d=\"M 280 275\nL 280 280\" style=\"stroke-width:1;stroke:rgba(110,112,121,1.0);fill:none\"/><path d=\"M 400 275\nL 400 280\" style=\"stroke-width:1;stroke:rgba(110,112,121,1.0);fill:none\"/><text x=\"83\" y=\"292\" style=\"stroke-width:0;stroke:none;fill:rgba(110,112,121,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">01-02</text><text x=\"203\" y=\"292\" style=\"stroke-width:0;stroke:none;fill:rgba(110,112,121,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">01-05</text><text x=\"323\" y=\"292\" style=\"stroke-width:0;stroke:none;fill:rgba(110,112,121,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">01-08</text></svg>",
},
}
for _, tt := range tests {
d := tt.newDraw()
height, _, err := drawXAxis(d, tt.newOption(), 1)
assert.Nil(err)
assert.Equal(25, height)
data, err := d.Bytes()
assert.Nil(err)
assert.Equal(tt.result, string(data))
}
}

145
yaxis.go
View file

@ -22,107 +22,84 @@
package charts package charts
import "github.com/golang/freetype/truetype" import (
"strings"
"github.com/wcharczuk/go-chart/v2"
"github.com/wcharczuk/go-chart/v2/drawing"
)
type YAxisOption struct { type YAxisOption struct {
// The minimun value of axis. // The minimun value of axis.
Min *float64 Min *float64
// The maximum value of axis. // The maximum value of axis.
Max *float64 Max *float64
// The font of y axis // Hidden y axis
Font *truetype.Font Hidden bool
// The data value of x axis
Data []string
// The theme of chart
Theme ColorPalette
// The font size of x axis label
FontSize float64
// The position of axis, it can be 'left' or 'right'
Position string
// The color of label
FontColor Color
// Formatter for y axis text value // Formatter for y axis text value
Formatter string Formatter string
// Color for y axis // Color for y axis
Color Color Color drawing.Color
// The flag for show axis, set this to *false will hide axis
Show *bool
DivideCount int
Unit int
isCategoryAxis bool
// The flag for show axis split line, set this to true will show axis split line
SplitLineShow *bool
} }
// NewYAxisOptions returns a y axis option // TODO 长度是否可以变化
func NewYAxisOptions(data []string, others ...[]string) []YAxisOption { const YAxisWidth = 40
arr := [][]string{
data,
}
arr = append(arr, others...)
opts := make([]YAxisOption, 0)
for _, data := range arr {
opts = append(opts, YAxisOption{
Data: data,
})
}
return opts
}
func (opt *YAxisOption) ToAxisOption(p *Painter) AxisOption { func drawYAxis(p *Draw, opt *ChartOption, axisIndex, xAxisHeight int, padding chart.Box) (*Range, error) {
position := PositionLeft theme := NewTheme(opt.Theme)
if opt.Position == PositionRight { yRange := opt.newYRange(axisIndex)
position = PositionRight values := yRange.Values()
yAxis := opt.YAxisList[axisIndex]
formatter := yAxis.Formatter
if len(formatter) != 0 {
for index, text := range values {
values[index] = strings.ReplaceAll(formatter, "{value}", text)
}
} }
theme := opt.Theme
if theme == nil { data := NewAxisDataListFromStringList(values)
theme = p.theme style := AxisOption{
} Position: PositionLeft,
axisOpt := AxisOption{
Formatter: opt.Formatter,
Theme: theme,
Data: opt.Data,
Position: position,
FontSize: opt.FontSize,
StrokeWidth: -1,
Font: opt.Font,
FontColor: opt.FontColor,
BoundaryGap: FalseFlag(), BoundaryGap: FalseFlag(),
SplitLineShow: true, FontColor: theme.GetAxisStrokeColor(),
TickShow: FalseFlag(),
StrokeWidth: 1,
SplitLineColor: theme.GetAxisSplitLineColor(), SplitLineColor: theme.GetAxisSplitLineColor(),
Show: opt.Show, SplitLineShow: true,
Unit: opt.Unit,
} }
if !opt.Color.IsZero() { if !yAxis.Color.IsZero() {
axisOpt.FontColor = opt.Color style.FontColor = yAxis.Color
axisOpt.StrokeColor = opt.Color style.StrokeColor = yAxis.Color
} }
if opt.isCategoryAxis { width := NewAxis(p, data, style).measure().Width
axisOpt.BoundaryGap = TrueFlag()
axisOpt.StrokeWidth = 1
axisOpt.SplitLineShow = false
}
if opt.SplitLineShow != nil {
axisOpt.SplitLineShow = *opt.SplitLineShow
}
return axisOpt
}
// NewLeftYAxis returns a left y axis renderer yAxisCount := len(opt.YAxisList)
func NewLeftYAxis(p *Painter, opt YAxisOption) *axisPainter { boxWidth := p.Box.Width()
p = p.Child(PainterPaddingOption(Box{ if axisIndex > 0 {
Bottom: defaultXAxisHeight, style.SplitLineShow = false
})) style.Position = PositionRight
return NewAxisPainter(p, opt.ToAxisOption(p)) padding.Right += (axisIndex - 1) * YAxisWidth
} } else {
boxWidth = p.Box.Width() - (yAxisCount-1)*YAxisWidth
padding.Left += (YAxisWidth - width)
}
// NewRightYAxis returns a right y axis renderer dYAxis, err := NewDraw(
func NewRightYAxis(p *Painter, opt YAxisOption) *axisPainter { DrawOption{
p = p.Child(PainterPaddingOption(Box{ Parent: p,
Bottom: defaultXAxisHeight, Width: boxWidth,
})) // 减去x轴的高
axisOpt := opt.ToAxisOption(p) Height: p.Box.Height() - xAxisHeight,
axisOpt.Position = PositionRight },
axisOpt.SplitLineShow = false PaddingOption(padding),
return NewAxisPainter(p, axisOpt) )
if err != nil {
return nil, err
}
if opt.Font != nil {
dYAxis.Font = opt.Font
}
NewAxis(dYAxis, data, style).Render()
yRange.Size = dYAxis.Box.Height()
return &yRange, nil
} }

View file

@ -26,44 +26,93 @@ import (
"testing" "testing"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/wcharczuk/go-chart/v2"
) )
func TestRightYAxis(t *testing.T) { func TestDrawYAxis(t *testing.T) {
assert := assert.New(t) assert := assert.New(t)
newDraw := func() *Draw {
d, _ := NewDraw(DrawOption{
Width: 400,
Height: 300,
})
return d
}
tests := []struct { tests := []struct {
render func(*Painter) ([]byte, error) newDraw func() *Draw
result string newOption func() *ChartOption
axisIndex int
xAxisHeight int
result string
}{ }{
{ {
render: func(p *Painter) ([]byte, error) { newDraw: newDraw,
opt := NewYAxisOptions([]string{ newOption: func() *ChartOption {
"a", return &ChartOption{
"b", YAxisList: []YAxisOption{
"c", {
"d", Max: NewFloatPoint(20),
})[0] },
_, err := NewRightYAxis(p, opt).Render() },
if err != nil { SeriesList: []Series{
return nil, err {
Data: []SeriesData{
{
Value: 1,
},
{
Value: 2,
},
},
},
},
} }
return p.Bytes()
}, },
result: "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"600\" height=\"400\">\\n<text x=\"581\" y=\"17\" style=\"stroke-width:0;stroke:none;fill:rgba(70,70,70,1.0);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">a</text><text x=\"581\" y=\"133\" style=\"stroke-width:0;stroke:none;fill:rgba(70,70,70,1.0);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">b</text><text x=\"581\" y=\"250\" style=\"stroke-width:0;stroke:none;fill:rgba(70,70,70,1.0);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">c</text><text x=\"581\" y=\"367\" style=\"stroke-width:0;stroke:none;fill:rgba(70,70,70,1.0);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">d</text></svg>", result: "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"400\" height=\"300\">\\n<path d=\"M 50 10\nL 50 290\" style=\"stroke-width:1;stroke:none;fill:none\"/><path d=\"M 50 10\nL 390 10\" style=\"stroke-width:1;stroke:rgba(224,230,242,1.0);fill:none\"/><path d=\"M 50 57\nL 390 57\" style=\"stroke-width:1;stroke:rgba(224,230,242,1.0);fill:none\"/><path d=\"M 50 104\nL 390 104\" style=\"stroke-width:1;stroke:rgba(224,230,242,1.0);fill:none\"/><path d=\"M 50 151\nL 390 151\" style=\"stroke-width:1;stroke:rgba(224,230,242,1.0);fill:none\"/><path d=\"M 50 198\nL 390 198\" style=\"stroke-width:1;stroke:rgba(224,230,242,1.0);fill:none\"/><path d=\"M 50 244\nL 390 244\" style=\"stroke-width:1;stroke:rgba(224,230,242,1.0);fill:none\"/><text x=\"36\" y=\"294\" style=\"stroke-width:0;stroke:none;fill:rgba(110,112,121,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">0</text><text x=\"18\" y=\"248\" style=\"stroke-width:0;stroke:none;fill:rgba(110,112,121,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">3.33</text><text x=\"18\" y=\"202\" style=\"stroke-width:0;stroke:none;fill:rgba(110,112,121,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">6.66</text><text x=\"29\" y=\"155\" style=\"stroke-width:0;stroke:none;fill:rgba(110,112,121,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">10</text><text x=\"11\" y=\"108\" style=\"stroke-width:0;stroke:none;fill:rgba(110,112,121,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">13.33</text><text x=\"11\" y=\"61\" style=\"stroke-width:0;stroke:none;fill:rgba(110,112,121,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">16.66</text><text x=\"29\" y=\"14\" style=\"stroke-width:0;stroke:none;fill:rgba(110,112,121,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">20</text></svg>",
},
{
newDraw: newDraw,
newOption: func() *ChartOption {
return &ChartOption{
YAxisList: []YAxisOption{
{},
{
Max: NewFloatPoint(20),
Formatter: "{value} C",
},
},
SeriesList: []Series{
{
YAxisIndex: 1,
Data: []SeriesData{
{
Value: 1,
},
{
Value: 2,
},
},
},
},
}
},
axisIndex: 1,
result: "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"400\" height=\"300\">\\n<path d=\"M 337 10\nL 337 290\" style=\"stroke-width:1;stroke:none;fill:none\"/><text x=\"345\" y=\"294\" style=\"stroke-width:0;stroke:none;fill:rgba(110,112,121,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">0 C</text><text x=\"345\" y=\"248\" style=\"stroke-width:0;stroke:none;fill:rgba(110,112,121,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">3.33 C</text><text x=\"345\" y=\"202\" style=\"stroke-width:0;stroke:none;fill:rgba(110,112,121,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">6.66 C</text><text x=\"345\" y=\"155\" style=\"stroke-width:0;stroke:none;fill:rgba(110,112,121,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">10 C</text><text x=\"345\" y=\"108\" style=\"stroke-width:0;stroke:none;fill:rgba(110,112,121,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">13.33 C</text><text x=\"345\" y=\"61\" style=\"stroke-width:0;stroke:none;fill:rgba(110,112,121,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">16.66 C</text><text x=\"345\" y=\"14\" style=\"stroke-width:0;stroke:none;fill:rgba(110,112,121,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">20 C</text></svg>",
}, },
} }
for _, tt := range tests { for _, tt := range tests {
p, err := NewPainter(PainterOptions{ d := tt.newDraw()
Type: ChartOutputSVG, r, err := drawYAxis(d, tt.newOption(), tt.axisIndex, tt.xAxisHeight, chart.NewBox(10, 10, 10, 10))
Width: 600,
Height: 400,
}, PainterThemeOption(defaultTheme), PainterPaddingOption(Box{
Top: 10,
Right: 10,
Bottom: 10,
Left: 10,
}))
assert.Nil(err) assert.Nil(err)
data, err := tt.render(p) assert.Equal(&Range{
divideCount: 6,
Max: 20,
Size: 280,
}, r)
data, err := d.Bytes()
assert.Nil(err) assert.Nil(err)
assert.Equal(tt.result, string(data)) assert.Equal(tt.result, string(data))
} }