feat: support mark line and mark point render
This commit is contained in:
parent
72e11e49b1
commit
8a5990fe8f
10 changed files with 2046 additions and 19 deletions
120
chart_option.go
Normal file
120
chart_option.go
Normal file
|
|
@ -0,0 +1,120 @@
|
||||||
|
// 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 (
|
||||||
|
"sort"
|
||||||
|
|
||||||
|
"github.com/golang/freetype/truetype"
|
||||||
|
"github.com/wcharczuk/go-chart/v2"
|
||||||
|
)
|
||||||
|
|
||||||
|
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 child charts
|
||||||
|
Children []ChartOption
|
||||||
|
}
|
||||||
|
|
||||||
|
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, _ = 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,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// 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]
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
117
charts.go
117
charts.go
|
|
@ -22,6 +22,24 @@
|
||||||
|
|
||||||
package charts
|
package charts
|
||||||
|
|
||||||
|
const labelFontSize = 10
|
||||||
|
const defaultDotWidth = 2.0
|
||||||
|
const defaultStrokeWidth = 2.0
|
||||||
|
|
||||||
|
var defaultChartWidth = 600
|
||||||
|
var defaultChartHeight = 400
|
||||||
|
|
||||||
|
func SetDefaultWidth(width int) {
|
||||||
|
if width > 0 {
|
||||||
|
defaultChartWidth = width
|
||||||
|
}
|
||||||
|
}
|
||||||
|
func SetDefaultHeight(height int) {
|
||||||
|
if height > 0 {
|
||||||
|
defaultChartHeight = height
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
type Renderer interface {
|
type Renderer interface {
|
||||||
Render() (Box, error)
|
Render() (Box, error)
|
||||||
}
|
}
|
||||||
|
|
@ -34,6 +52,10 @@ type defaultRenderOption struct {
|
||||||
YAxisOptions []YAxisOption
|
YAxisOptions []YAxisOption
|
||||||
// The x axis option
|
// The x axis option
|
||||||
XAxis XAxisOption
|
XAxis XAxisOption
|
||||||
|
// The title option
|
||||||
|
TitleOption TitleOption
|
||||||
|
// The legend option
|
||||||
|
LegendOption LegendOption
|
||||||
}
|
}
|
||||||
|
|
||||||
type defaultRenderResult struct {
|
type defaultRenderResult struct {
|
||||||
|
|
@ -42,10 +64,37 @@ type defaultRenderResult struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func defaultRender(p *Painter, opt defaultRenderOption) (*defaultRenderResult, error) {
|
func defaultRender(p *Painter, opt defaultRenderOption) (*defaultRenderResult, error) {
|
||||||
p.SetBackground(p.Width(), p.Height(), opt.Theme.GetBackgroundColor())
|
|
||||||
if !opt.Padding.IsZero() {
|
if !opt.Padding.IsZero() {
|
||||||
p = p.Child(PainterPaddingOption(opt.Padding))
|
p = p.Child(PainterPaddingOption(opt.Padding))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if len(opt.LegendOption.Data) != 0 {
|
||||||
|
if opt.LegendOption.Theme == nil {
|
||||||
|
opt.LegendOption.Theme = opt.Theme
|
||||||
|
}
|
||||||
|
_, err := NewLegendPainter(p, opt.LegendOption).Render()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 如果有标题
|
||||||
|
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
|
||||||
|
}
|
||||||
|
p = p.Child(PainterPaddingOption(Box{
|
||||||
|
// 标题下留白
|
||||||
|
Top: titleBox.Height() + 20,
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
|
||||||
result := defaultRenderResult{
|
result := defaultRenderResult{
|
||||||
axisRanges: make(map[int]axisRange),
|
axisRanges: make(map[int]axisRange),
|
||||||
}
|
}
|
||||||
|
|
@ -60,7 +109,8 @@ func defaultRender(p *Painter, opt defaultRenderOption) (*defaultRenderResult, e
|
||||||
}
|
}
|
||||||
// 高度需要减去x轴的高度
|
// 高度需要减去x轴的高度
|
||||||
rangeHeight := p.Height() - defaultXAxisHeight
|
rangeHeight := p.Height() - defaultXAxisHeight
|
||||||
rangeWidth := 0
|
rangeWidthLeft := 0
|
||||||
|
rangeWidthRight := 0
|
||||||
|
|
||||||
// 计算对应的axis range
|
// 计算对应的axis range
|
||||||
for _, index := range axisIndexList {
|
for _, index := range axisIndexList {
|
||||||
|
|
@ -89,28 +139,28 @@ func defaultRender(p *Painter, opt defaultRenderOption) (*defaultRenderResult, e
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
rangeWidth += yAxisBox.Width()
|
if index == 0 {
|
||||||
|
rangeWidthLeft += yAxisBox.Width()
|
||||||
|
} else {
|
||||||
|
rangeWidthRight += yAxisBox.Width()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if opt.XAxis.Theme == nil {
|
if opt.XAxis.Theme == nil {
|
||||||
opt.XAxis.Theme = opt.Theme
|
opt.XAxis.Theme = opt.Theme
|
||||||
}
|
}
|
||||||
xAxis := NewBottomXAxis(p.Child(PainterPaddingOption(Box{
|
xAxis := NewBottomXAxis(p.Child(PainterPaddingOption(Box{
|
||||||
Left: rangeWidth,
|
Left: rangeWidthLeft,
|
||||||
})), opt.XAxis)
|
})), opt.XAxis)
|
||||||
_, err := xAxis.Render()
|
_, err := xAxis.Render()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// // 生成Y轴
|
|
||||||
// for _, yAxisOption := range opt.YAxisOptions {
|
|
||||||
|
|
||||||
// }
|
|
||||||
|
|
||||||
result.p = p.Child(PainterPaddingOption(Box{
|
result.p = p.Child(PainterPaddingOption(Box{
|
||||||
Bottom: rangeHeight,
|
Bottom: rangeHeight,
|
||||||
Left: rangeWidth,
|
Left: rangeWidthLeft,
|
||||||
|
Right: rangeWidthRight,
|
||||||
}))
|
}))
|
||||||
return &result, nil
|
return &result, nil
|
||||||
}
|
}
|
||||||
|
|
@ -124,3 +174,50 @@ func doRender(renderers ...Renderer) error {
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func Render(opt ChartOption) (*Painter, error) {
|
||||||
|
opt.fillDefault()
|
||||||
|
|
||||||
|
if opt.Parent == nil {
|
||||||
|
p, err := NewPainter(PainterOptions{
|
||||||
|
Type: opt.Type,
|
||||||
|
Width: opt.Width,
|
||||||
|
Height: opt.Height,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
opt.Parent = p
|
||||||
|
}
|
||||||
|
p := opt.Parent
|
||||||
|
p.SetBackground(p.Width(), p.Height(), opt.BackgroundColor)
|
||||||
|
seriesList := opt.SeriesList
|
||||||
|
seriesList.init()
|
||||||
|
|
||||||
|
rendererList := make([]Renderer, 0)
|
||||||
|
|
||||||
|
// line chart
|
||||||
|
lineChartSeriesList := seriesList.Filter(ChartTypeLine)
|
||||||
|
if len(lineChartSeriesList) != 0 {
|
||||||
|
renderer := NewLineChart(p, LineChartOption{
|
||||||
|
Theme: opt.theme,
|
||||||
|
Font: opt.font,
|
||||||
|
SeriesList: lineChartSeriesList,
|
||||||
|
XAxis: opt.XAxis,
|
||||||
|
Padding: opt.Padding,
|
||||||
|
YAxisOptions: opt.YAxisOptions,
|
||||||
|
TitleOption: opt.Title,
|
||||||
|
LegendOption: opt.Legend,
|
||||||
|
})
|
||||||
|
rendererList = append(rendererList, renderer)
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, renderer := range rendererList {
|
||||||
|
_, err := renderer.Render()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return p, nil
|
||||||
|
}
|
||||||
|
|
|
||||||
1321
examples/charts/main.go
Normal file
1321
examples/charts/main.go
Normal file
File diff suppressed because it is too large
Load diff
|
|
@ -39,6 +39,19 @@ func main() {
|
||||||
Right: 10,
|
Right: 10,
|
||||||
Bottom: 10,
|
Bottom: 10,
|
||||||
},
|
},
|
||||||
|
TitleOption: charts.TitleOption{
|
||||||
|
Text: "Line",
|
||||||
|
},
|
||||||
|
LegendOption: charts.LegendOption{
|
||||||
|
Data: []string{
|
||||||
|
"Email",
|
||||||
|
"Union Ads",
|
||||||
|
"Video Ads",
|
||||||
|
"Direct",
|
||||||
|
"Search Engine",
|
||||||
|
},
|
||||||
|
Left: charts.PositionCenter,
|
||||||
|
},
|
||||||
XAxis: charts.NewXAxisOption([]string{
|
XAxis: charts.NewXAxisOption([]string{
|
||||||
"Mon",
|
"Mon",
|
||||||
"Tue",
|
"Tue",
|
||||||
|
|
|
||||||
24
legend.go
24
legend.go
|
|
@ -29,13 +29,13 @@ import (
|
||||||
|
|
||||||
type legendPainter struct {
|
type legendPainter struct {
|
||||||
p *Painter
|
p *Painter
|
||||||
opt *LegendPainterOption
|
opt *LegendOption
|
||||||
}
|
}
|
||||||
|
|
||||||
const IconRect = "rect"
|
const IconRect = "rect"
|
||||||
const IconLineDot = "lineDot"
|
const IconLineDot = "lineDot"
|
||||||
|
|
||||||
type LegendPainterOption struct {
|
type LegendOption struct {
|
||||||
Theme ColorPalette
|
Theme ColorPalette
|
||||||
// Text array of legend
|
// Text array of legend
|
||||||
Data []string
|
Data []string
|
||||||
|
|
@ -58,7 +58,17 @@ type LegendPainterOption struct {
|
||||||
FontColor Color
|
FontColor Color
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewLegendPainter(p *Painter, opt LegendPainterOption) *legendPainter {
|
func NewLegendOption(labels []string, left ...string) LegendOption {
|
||||||
|
opt := LegendOption{
|
||||||
|
Data: labels,
|
||||||
|
}
|
||||||
|
if len(left) != 0 {
|
||||||
|
opt.Left = left[0]
|
||||||
|
}
|
||||||
|
return opt
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewLegendPainter(p *Painter, opt LegendOption) *legendPainter {
|
||||||
return &legendPainter{
|
return &legendPainter{
|
||||||
p: p,
|
p: p,
|
||||||
opt: &opt,
|
opt: &opt,
|
||||||
|
|
@ -71,6 +81,12 @@ func (l *legendPainter) Render() (Box, error) {
|
||||||
if theme == nil {
|
if theme == nil {
|
||||||
theme = l.p.theme
|
theme = l.p.theme
|
||||||
}
|
}
|
||||||
|
if opt.FontSize == 0 {
|
||||||
|
opt.FontSize = theme.GetFontSize()
|
||||||
|
}
|
||||||
|
if opt.FontColor.IsZero() {
|
||||||
|
opt.FontColor = theme.GetTextColor()
|
||||||
|
}
|
||||||
p := l.p
|
p := l.p
|
||||||
p.SetTextStyle(Style{
|
p.SetTextStyle(Style{
|
||||||
FontSize: opt.FontSize,
|
FontSize: opt.FontSize,
|
||||||
|
|
@ -129,7 +145,7 @@ func (l *legendPainter) Render() (Box, error) {
|
||||||
top, _ := strconv.Atoi(opt.Top)
|
top, _ := strconv.Atoi(opt.Top)
|
||||||
|
|
||||||
x := int(left)
|
x := int(left)
|
||||||
y := int(top)
|
y := int(top) + 10
|
||||||
x0 := x
|
x0 := x
|
||||||
y0 := y
|
y0 := y
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -23,7 +23,9 @@
|
||||||
package charts
|
package charts
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"github.com/golang/freetype/truetype"
|
||||||
"github.com/wcharczuk/go-chart/v2"
|
"github.com/wcharczuk/go-chart/v2"
|
||||||
|
"github.com/wcharczuk/go-chart/v2/drawing"
|
||||||
)
|
)
|
||||||
|
|
||||||
type lineChart struct {
|
type lineChart struct {
|
||||||
|
|
@ -43,6 +45,8 @@ func NewLineChart(p *Painter, opt LineChartOption) *lineChart {
|
||||||
|
|
||||||
type LineChartOption struct {
|
type LineChartOption struct {
|
||||||
Theme ColorPalette
|
Theme ColorPalette
|
||||||
|
// The font size
|
||||||
|
Font *truetype.Font
|
||||||
// The data series list
|
// The data series list
|
||||||
SeriesList SeriesList
|
SeriesList SeriesList
|
||||||
// The x axis option
|
// The x axis option
|
||||||
|
|
@ -51,6 +55,10 @@ type LineChartOption struct {
|
||||||
Padding Box
|
Padding Box
|
||||||
// The y axis option
|
// The y axis option
|
||||||
YAxisOptions []YAxisOption
|
YAxisOptions []YAxisOption
|
||||||
|
// The option of title
|
||||||
|
TitleOption TitleOption
|
||||||
|
// The legend option
|
||||||
|
LegendOption LegendOption
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *lineChart) Render() (Box, error) {
|
func (l *lineChart) Render() (Box, error) {
|
||||||
|
|
@ -64,6 +72,8 @@ func (l *lineChart) Render() (Box, error) {
|
||||||
SeriesList: seriesList,
|
SeriesList: seriesList,
|
||||||
XAxis: opt.XAxis,
|
XAxis: opt.XAxis,
|
||||||
YAxisOptions: opt.YAxisOptions,
|
YAxisOptions: opt.YAxisOptions,
|
||||||
|
TitleOption: opt.TitleOption,
|
||||||
|
LegendOption: opt.LegendOption,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return chart.BoxZero, err
|
return chart.BoxZero, err
|
||||||
|
|
@ -78,13 +88,20 @@ func (l *lineChart) Render() (Box, error) {
|
||||||
for i := 0; i < len(xDivideValues)-1; i++ {
|
for i := 0; i < len(xDivideValues)-1; i++ {
|
||||||
xValues[i] = (xDivideValues[i] + xDivideValues[i+1]) >> 1
|
xValues[i] = (xDivideValues[i] + xDivideValues[i+1]) >> 1
|
||||||
}
|
}
|
||||||
|
markPointPainter := NewMarkPointPainter(seriesPainter)
|
||||||
|
markLinePainter := NewMarkLinePainter(seriesPainter)
|
||||||
|
rendererList := []Renderer{
|
||||||
|
markPointPainter,
|
||||||
|
markLinePainter,
|
||||||
|
}
|
||||||
for index, series := range seriesList {
|
for index, series := range seriesList {
|
||||||
seriesColor := opt.Theme.GetSeriesColor(index)
|
seriesColor := opt.Theme.GetSeriesColor(index)
|
||||||
seriesPainter.SetDrawingStyle(Style{
|
drawingStyle := Style{
|
||||||
StrokeColor: seriesColor,
|
StrokeColor: seriesColor,
|
||||||
StrokeWidth: 2,
|
StrokeWidth: defaultStrokeWidth,
|
||||||
FillColor: seriesColor,
|
}
|
||||||
})
|
|
||||||
|
seriesPainter.SetDrawingStyle(drawingStyle)
|
||||||
yr := renderResult.axisRanges[series.AxisIndex]
|
yr := renderResult.axisRanges[series.AxisIndex]
|
||||||
points := make([]Point, 0)
|
points := make([]Point, 0)
|
||||||
for i, item := range series.Data {
|
for i, item := range series.Data {
|
||||||
|
|
@ -95,8 +112,39 @@ func (l *lineChart) Render() (Box, error) {
|
||||||
}
|
}
|
||||||
points = append(points, p)
|
points = append(points, p)
|
||||||
}
|
}
|
||||||
|
// 画线
|
||||||
seriesPainter.LineStroke(points)
|
seriesPainter.LineStroke(points)
|
||||||
|
|
||||||
|
// 画点
|
||||||
|
if opt.Theme.IsDark() {
|
||||||
|
drawingStyle.FillColor = drawingStyle.StrokeColor
|
||||||
|
} else {
|
||||||
|
drawingStyle.FillColor = drawing.ColorWhite
|
||||||
|
}
|
||||||
|
drawingStyle.StrokeWidth = 1
|
||||||
|
seriesPainter.SetDrawingStyle(drawingStyle)
|
||||||
seriesPainter.Dots(points)
|
seriesPainter.Dots(points)
|
||||||
|
markPointPainter.Add(markPointRenderOption{
|
||||||
|
FillColor: seriesColor,
|
||||||
|
Font: opt.Font,
|
||||||
|
Points: points,
|
||||||
|
Series: series,
|
||||||
|
})
|
||||||
|
markLinePainter.Add(markLineRenderOption{
|
||||||
|
FillColor: seriesColor,
|
||||||
|
FontColor: opt.Theme.GetTextColor(),
|
||||||
|
StrokeColor: seriesColor,
|
||||||
|
Font: opt.Font,
|
||||||
|
Series: series,
|
||||||
|
Range: yr,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
// 最大、最小的mark point
|
||||||
|
for _, renderer := range rendererList {
|
||||||
|
_, err = renderer.Render()
|
||||||
|
if err != nil {
|
||||||
|
return chart.BoxZero, err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return p.box, nil
|
return p.box, nil
|
||||||
|
|
|
||||||
118
mark_line.go
Normal file
118
mark_line.go
Normal file
|
|
@ -0,0 +1,118 @@
|
||||||
|
// 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"
|
||||||
|
"github.com/wcharczuk/go-chart/v2"
|
||||||
|
)
|
||||||
|
|
||||||
|
func NewMarkLine(markLineTypes ...string) SeriesMarkLine {
|
||||||
|
data := make([]SeriesMarkData, len(markLineTypes))
|
||||||
|
for index, t := range markLineTypes {
|
||||||
|
data[index] = SeriesMarkData{
|
||||||
|
Type: t,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return SeriesMarkLine{
|
||||||
|
Data: data,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type markLinePainter struct {
|
||||||
|
p *Painter
|
||||||
|
options []markLineRenderOption
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *markLinePainter) Add(opt markLineRenderOption) {
|
||||||
|
m.options = append(m.options, opt)
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewMarkLinePainter(p *Painter) *markLinePainter {
|
||||||
|
return &markLinePainter{
|
||||||
|
p: p,
|
||||||
|
options: make([]markLineRenderOption, 0),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type markLineRenderOption struct {
|
||||||
|
FillColor Color
|
||||||
|
FontColor Color
|
||||||
|
StrokeColor Color
|
||||||
|
Font *truetype.Font
|
||||||
|
Series Series
|
||||||
|
Range axisRange
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *markLinePainter) Render() (Box, error) {
|
||||||
|
painter := m.p
|
||||||
|
for _, opt := range m.options {
|
||||||
|
s := opt.Series
|
||||||
|
if len(s.MarkLine.Data) == 0 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
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: opt.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 chart.BoxZero, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func markLineRender(opt markLineRenderOption) {
|
||||||
|
// d := opt.Draw
|
||||||
|
// s := opt.Series
|
||||||
|
// if len(s.MarkLine.Data) == 0 {
|
||||||
|
// return
|
||||||
|
// }
|
||||||
|
// r := d.Render
|
||||||
|
|
||||||
|
}
|
||||||
102
mark_point.go
Normal file
102
mark_point.go
Normal file
|
|
@ -0,0 +1,102 @@
|
||||||
|
// 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"
|
||||||
|
"github.com/wcharczuk/go-chart/v2"
|
||||||
|
)
|
||||||
|
|
||||||
|
func NewMarkPoint(markPointTypes ...string) SeriesMarkPoint {
|
||||||
|
data := make([]SeriesMarkData, len(markPointTypes))
|
||||||
|
for index, t := range markPointTypes {
|
||||||
|
data[index] = SeriesMarkData{
|
||||||
|
Type: t,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return SeriesMarkPoint{
|
||||||
|
Data: data,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type markPointPainter struct {
|
||||||
|
p *Painter
|
||||||
|
options []markPointRenderOption
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *markPointPainter) Add(opt markPointRenderOption) {
|
||||||
|
m.options = append(m.options, opt)
|
||||||
|
}
|
||||||
|
|
||||||
|
type markPointRenderOption struct {
|
||||||
|
FillColor Color
|
||||||
|
Font *truetype.Font
|
||||||
|
Series Series
|
||||||
|
Points []Point
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewMarkPointPainter(p *Painter) *markPointPainter {
|
||||||
|
return &markPointPainter{
|
||||||
|
p: p,
|
||||||
|
options: make([]markPointRenderOption, 0),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
|
painter.OverrideDrawingStyle(Style{
|
||||||
|
FillColor: opt.FillColor,
|
||||||
|
}).OverrideTextStyle(Style{
|
||||||
|
FontColor: NewTheme(ThemeDark).GetTextColor(),
|
||||||
|
FontSize: labelFontSize,
|
||||||
|
StrokeWidth: 1,
|
||||||
|
Font: opt.Font,
|
||||||
|
})
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
painter.Pin(p.X, p.Y-symbolSize>>1, symbolSize)
|
||||||
|
text := commafWithDigits(value)
|
||||||
|
textBox := painter.MeasureText(text)
|
||||||
|
painter.Text(text, p.X-textBox.Width()>>1, p.Y-symbolSize>>1-2)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return chart.BoxZero, nil
|
||||||
|
}
|
||||||
|
|
@ -700,7 +700,7 @@ func (p *Painter) Grid(opt GridOption) *Painter {
|
||||||
|
|
||||||
func (p *Painter) Dots(points []Point) *Painter {
|
func (p *Painter) Dots(points []Point) *Painter {
|
||||||
for _, item := range points {
|
for _, item := range points {
|
||||||
p.Circle(3, item.X, item.Y)
|
p.Circle(2, item.X, item.Y)
|
||||||
}
|
}
|
||||||
p.FillStroke()
|
p.FillStroke()
|
||||||
return p
|
return p
|
||||||
|
|
|
||||||
192
title.go
Normal file
192
title.go
Normal file
|
|
@ -0,0 +1,192 @@
|
||||||
|
// 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 (
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/golang/freetype/truetype"
|
||||||
|
)
|
||||||
|
|
||||||
|
type TitleOption struct {
|
||||||
|
// The theme of chart
|
||||||
|
Theme ColorPalette
|
||||||
|
// Title text, support \n for new line
|
||||||
|
Text string
|
||||||
|
// Subtitle text, support \n for new line
|
||||||
|
Subtext string
|
||||||
|
// // Title style
|
||||||
|
// Style Style
|
||||||
|
// // Subtitle style
|
||||||
|
// SubtextStyle Style
|
||||||
|
// Distance between title component and the left side of the container.
|
||||||
|
// It can be pixel value: 20, percentage value: 20%,
|
||||||
|
// or position value: right, center.
|
||||||
|
Left string
|
||||||
|
// Distance between title component and the top side of the container.
|
||||||
|
// It can be pixel value: 20.
|
||||||
|
Top string
|
||||||
|
// 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 {
|
||||||
|
width int
|
||||||
|
height int
|
||||||
|
text string
|
||||||
|
style Style
|
||||||
|
}
|
||||||
|
|
||||||
|
func splitTitleText(text string) []string {
|
||||||
|
arr := strings.Split(text, "\n")
|
||||||
|
result := make([]string, 0)
|
||||||
|
for _, v := range arr {
|
||||||
|
v = strings.TrimSpace(v)
|
||||||
|
if v == "" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
result = append(result, v)
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
type titlePainter struct {
|
||||||
|
p *Painter
|
||||||
|
opt *TitleOption
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewTitlePainter(p *Painter, opt TitleOption) *titlePainter {
|
||||||
|
return &titlePainter{
|
||||||
|
p: p,
|
||||||
|
opt: &opt,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *titlePainter) Render() (Box, error) {
|
||||||
|
opt := t.opt
|
||||||
|
p := t.p
|
||||||
|
theme := opt.Theme
|
||||||
|
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) {
|
||||||
|
measureOptions = append(measureOptions, titleMeasureOption{
|
||||||
|
text: v,
|
||||||
|
style: titleTextStyle,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
subtextStyle := Style{
|
||||||
|
Font: opt.Font,
|
||||||
|
FontSize: opt.SubtextFontSize,
|
||||||
|
FontColor: opt.SubtextFontColor,
|
||||||
|
}
|
||||||
|
// 副标题
|
||||||
|
for _, v := range splitTitleText(opt.Subtext) {
|
||||||
|
measureOptions = append(measureOptions, titleMeasureOption{
|
||||||
|
text: v,
|
||||||
|
style: subtextStyle,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
textMaxWidth := 0
|
||||||
|
textMaxHeight := 0
|
||||||
|
for index, item := range measureOptions {
|
||||||
|
p.OverrideTextStyle(item.style)
|
||||||
|
textBox := p.MeasureText(item.text)
|
||||||
|
|
||||||
|
w := textBox.Width()
|
||||||
|
h := textBox.Height()
|
||||||
|
if w > textMaxWidth {
|
||||||
|
textMaxWidth = w
|
||||||
|
}
|
||||||
|
if h > textMaxHeight {
|
||||||
|
textMaxHeight = h
|
||||||
|
}
|
||||||
|
measureOptions[index].height = h
|
||||||
|
measureOptions[index].width = w
|
||||||
|
}
|
||||||
|
width := textMaxWidth
|
||||||
|
|
||||||
|
titleX := 0
|
||||||
|
switch opt.Left {
|
||||||
|
case PositionRight:
|
||||||
|
titleX = p.Width() - textMaxWidth
|
||||||
|
case PositionCenter:
|
||||||
|
titleX = p.Width()>>1 - (textMaxWidth >> 1)
|
||||||
|
default:
|
||||||
|
if strings.HasSuffix(opt.Left, "%") {
|
||||||
|
value, _ := strconv.Atoi(strings.ReplaceAll(opt.Left, "%", ""))
|
||||||
|
titleX = p.Width() * value / 100
|
||||||
|
} else {
|
||||||
|
value, _ := strconv.Atoi(opt.Left)
|
||||||
|
titleX = value
|
||||||
|
}
|
||||||
|
}
|
||||||
|
titleY := 0
|
||||||
|
// TODO TOP 暂只支持数值
|
||||||
|
if opt.Top != "" {
|
||||||
|
value, _ := strconv.Atoi(opt.Top)
|
||||||
|
titleY += value
|
||||||
|
}
|
||||||
|
for _, item := range measureOptions {
|
||||||
|
p.OverrideTextStyle(item.style)
|
||||||
|
x := titleX + (textMaxWidth-item.width)>>1
|
||||||
|
y := titleY + item.height
|
||||||
|
p.Text(item.text, x, y)
|
||||||
|
titleY += item.height
|
||||||
|
}
|
||||||
|
|
||||||
|
return Box{
|
||||||
|
Bottom: titleY,
|
||||||
|
Right: titleX + width,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue