refactor: adjust font size of mark point

This commit is contained in:
vicanso 2022-06-22 20:30:10 +08:00
parent 4121829e6e
commit 92458aece2
7 changed files with 25 additions and 11 deletions

View file

@ -5,7 +5,7 @@
[中文](./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`. `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`.
`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`.

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` `go-charts`基于[go-chart](https://github.com/wcharczuk/go-chart),更简单方便的形式生成数据图表,支持`svg``png`两种方式的输出,支持主题`light`, `dark`, `grafana`以及`ant`默认的输入格式为`png`,默认主题为`light`
`Apache ECharts`在前端开发中得到众多开发者的认可,因此`go-charts`提供了兼容`Apache ECharts`的配置参数,简单快捷的生成相似的图表(`svg``png`)方便插入至Email或分享使用。下面为常用的图表截图(主题为light与grafana) `Apache ECharts`在前端开发中得到众多开发者的认可,因此`go-charts`提供了兼容`Apache ECharts`的配置参数,简单快捷的生成相似的图表(`svg``png`)方便插入至Email或分享使用。下面为常用的图表截图(主题为light与grafana)

File diff suppressed because one or more lines are too long

View file

@ -28,6 +28,7 @@ import (
) )
const labelFontSize = 10 const labelFontSize = 10
const smallLabelFontSize = 8
const defaultDotWidth = 2.0 const defaultDotWidth = 2.0
const defaultStrokeWidth = 2.0 const defaultStrokeWidth = 2.0

File diff suppressed because one or more lines are too long

View file

@ -24,6 +24,7 @@ package charts
import ( import (
"github.com/golang/freetype/truetype" "github.com/golang/freetype/truetype"
"github.com/wcharczuk/go-chart/v2/drawing"
) )
func NewMarkPoint(markPointTypes ...string) SeriesMarkPoint { func NewMarkPoint(markPointTypes ...string) SeriesMarkPoint {
@ -63,7 +64,6 @@ func NewMarkPointPainter(p *Painter) *markPointPainter {
func (m *markPointPainter) Render() (Box, error) { func (m *markPointPainter) Render() (Box, error) {
painter := m.p painter := m.p
theme := m.p.theme
for _, opt := range m.options { for _, opt := range m.options {
s := opt.Series s := opt.Series
if len(s.MarkPoint.Data) == 0 { if len(s.MarkPoint.Data) == 0 {
@ -75,15 +75,23 @@ func (m *markPointPainter) Render() (Box, error) {
if symbolSize == 0 { if symbolSize == 0 {
symbolSize = 30 symbolSize = 30
} }
painter.OverrideDrawingStyle(Style{ textStyle := Style{
FillColor: opt.FillColor, FontColor: drawing.Color{
}).OverrideTextStyle(Style{ R: 238,
FontColor: theme.GetTextColor(), G: 238,
B: 238,
A: 255,
},
FontSize: labelFontSize, FontSize: labelFontSize,
StrokeWidth: 1, StrokeWidth: 1,
Font: opt.Font, Font: opt.Font,
}) }
painter.OverrideDrawingStyle(Style{
FillColor: opt.FillColor,
}).OverrideTextStyle(textStyle)
for _, markPointData := range s.MarkPoint.Data { for _, markPointData := range s.MarkPoint.Data {
textStyle.FontSize = labelFontSize
painter.OverrideTextStyle(textStyle)
p := points[summary.MinIndex] p := points[summary.MinIndex]
value := summary.MinValue value := summary.MinValue
switch markPointData.Type { switch markPointData.Type {
@ -95,6 +103,11 @@ func (m *markPointPainter) Render() (Box, error) {
painter.Pin(p.X, p.Y-symbolSize>>1, symbolSize) painter.Pin(p.X, p.Y-symbolSize>>1, symbolSize)
text := commafWithDigits(value) text := commafWithDigits(value)
textBox := painter.MeasureText(text) 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) painter.Text(text, p.X-textBox.Width()>>1, p.Y-symbolSize>>1-2)
} }
} }

View file

@ -69,7 +69,7 @@ func TestMarkPoint(t *testing.T) {
} }
return p.Bytes() 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(70,70,70,1.0);font-size:12.8px;font-family:'Roboto Medium',sans-serif\">3</text></svg>", 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>",
}, },
} }