From 406d35c6a6d89f6d0668148b898f738bd3c717f9 Mon Sep 17 00:00:00 2001 From: Vijay Karthik Date: Tue, 18 Feb 2025 14:56:15 -0800 Subject: [PATCH] Support custom annotation --- mark_line.go | 24 ++++++++++++++++++++---- mark_point.go | 29 +++++++++++++++++++++-------- series.go | 8 ++++++++ 3 files changed, 49 insertions(+), 12 deletions(-) diff --git a/mark_line.go b/mark_line.go index bc850bb..019dcd3 100644 --- a/mark_line.go +++ b/mark_line.go @@ -78,10 +78,24 @@ func (m *markLinePainter) Render() (Box, error) { } summary := s.Summary() for _, markLine := range s.MarkLine.Data { - // 由于mark line会修改style,因此每次重新设置 + fillColor := opt.FillColor + if markLine.FillColor != nil { + fillColor = *markLine.FillColor + } + + strokeColor := opt.StrokeColor + if markLine.StrokeColor != nil { + strokeColor = *markLine.StrokeColor + } + + fontColor := opt.FontColor + if markLine.FontColor != nil { + fontColor = *markLine.FontColor + } + painter.OverrideDrawingStyle(Style{ - FillColor: opt.FillColor, - StrokeColor: opt.StrokeColor, + FillColor: fillColor, + StrokeColor: strokeColor, StrokeWidth: 1, StrokeDashArray: []float64{ 4, @@ -89,7 +103,7 @@ func (m *markLinePainter) Render() (Box, error) { }, }).OverrideTextStyle(Style{ Font: font, - FontColor: opt.FontColor, + FontColor: fontColor, FontSize: labelFontSize, }) value := float64(0) @@ -98,6 +112,8 @@ func (m *markLinePainter) Render() (Box, error) { value = summary.MaxValue case SeriesMarkDataTypeMin: value = summary.MinValue + case SeriesMarkDataTypeCustom: + value = markLine.CustomYVal default: value = summary.AverageValue } diff --git a/mark_point.go b/mark_point.go index fd8a88b..bd1af83 100644 --- a/mark_point.go +++ b/mark_point.go @@ -81,15 +81,25 @@ func (m *markPointPainter) Render() (Box, error) { 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 { + fillColor := opt.FillColor + if markPointData.FillColor != nil { + fillColor = *markPointData.FillColor + } + + if markPointData.FontColor != nil { + textStyle.FontColor = *markPointData.FontColor + } else { + if isLightColor(fillColor) { + textStyle.FontColor = defaultLightFontColor + } else { + textStyle.FontColor = defaultDarkFontColor + } + } + + painter.OverrideDrawingStyle(Style{ + FillColor: fillColor, + }).OverrideTextStyle(textStyle) textStyle.FontSize = labelFontSize painter.OverrideTextStyle(textStyle) p := points[summary.MinIndex] @@ -98,6 +108,9 @@ func (m *markPointPainter) Render() (Box, error) { case SeriesMarkDataTypeMax: p = points[summary.MaxIndex] value = summary.MaxValue + case SeriesMarkDataTypeCustom: + p = points[markPointData.XAxisIndex] + value = markPointData.CustomYVal } painter.Pin(p.X, p.Y-symbolSize>>1, symbolSize) diff --git a/series.go b/series.go index 0ad135f..aee6278 100644 --- a/series.go +++ b/series.go @@ -91,12 +91,20 @@ const ( SeriesMarkDataTypeMax = "max" SeriesMarkDataTypeMin = "min" SeriesMarkDataTypeAverage = "average" + SeriesMarkDataTypeCustom = "custom" ) type SeriesMarkData struct { // The mark data type, it can be "max", "min", "average". // The "average" is only for mark line Type string + + // Custom options. + XAxisIndex int + CustomYVal float64 + FillColor *Color + StrokeColor *Color + FontColor *Color } type SeriesMarkPoint struct { // The width of symbol, default value is 30