feat: support text align for table cell
This commit is contained in:
parent
d53fa1a329
commit
f483e2a850
4 changed files with 101 additions and 35 deletions
Binary file not shown.
|
Before Width: | Height: | Size: 36 KiB After Width: | Height: | Size: 30 KiB |
|
|
@ -5,6 +5,7 @@ import (
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/vicanso/go-charts/v2"
|
"github.com/vicanso/go-charts/v2"
|
||||||
"github.com/wcharczuk/go-chart/v2/drawing"
|
"github.com/wcharczuk/go-chart/v2/drawing"
|
||||||
|
|
@ -84,38 +85,83 @@ func main() {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bgColor := charts.Color{
|
||||||
|
R: 16,
|
||||||
|
G: 22,
|
||||||
|
B: 30,
|
||||||
|
A: 255,
|
||||||
|
}
|
||||||
p, err = charts.TableOptionRender(charts.TableChartOption{
|
p, err = charts.TableOptionRender(charts.TableChartOption{
|
||||||
Header: header,
|
Header: []string{
|
||||||
Data: data,
|
"Name",
|
||||||
CellTextStyle: func(tc charts.TableCell) *charts.Style {
|
"Price",
|
||||||
row := tc.Row
|
"Change",
|
||||||
column := tc.Column
|
},
|
||||||
style := tc.Style
|
BackgroundColor: bgColor,
|
||||||
if column == 1 && row != 0 {
|
HeaderBackgroundColor: bgColor,
|
||||||
age, _ := strconv.Atoi(tc.Text)
|
RowBackgroundColors: []charts.Color{
|
||||||
if age < 40 {
|
bgColor,
|
||||||
style.FontColor = drawing.ColorGreen
|
},
|
||||||
} else {
|
HeaderFontColor: drawing.ColorWhite,
|
||||||
style.FontColor = drawing.ColorRed
|
FontColor: drawing.ColorWhite,
|
||||||
}
|
Padding: charts.Box{
|
||||||
return &style
|
Top: 15,
|
||||||
}
|
Right: 10,
|
||||||
return nil
|
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 {
|
CellStyle: func(tc charts.TableCell) *charts.Style {
|
||||||
row := tc.Row
|
|
||||||
column := tc.Column
|
column := tc.Column
|
||||||
if row == 2 && column == 1 {
|
if column != 2 {
|
||||||
return &charts.Style{
|
return nil
|
||||||
FillColor: drawing.ColorBlue,
|
}
|
||||||
|
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,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if row == 3 && column == 4 {
|
return &style
|
||||||
return &charts.Style{
|
|
||||||
FillColor: drawing.ColorRed.WithAlpha(100),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
||||||
14
painter.go
14
painter.go
|
|
@ -545,7 +545,7 @@ func (p *Painter) Text(body string, x, y int) *Painter {
|
||||||
return p
|
return p
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Painter) TextFit(body string, x, y, width int) chart.Box {
|
func (p *Painter) TextFit(body string, x, y, width int, textAligns ...string) chart.Box {
|
||||||
style := p.style
|
style := p.style
|
||||||
textWarp := style.TextWrap
|
textWarp := style.TextWrap
|
||||||
style.TextWrap = chart.TextWrapWord
|
style.TextWrap = chart.TextWrapWord
|
||||||
|
|
@ -554,14 +554,24 @@ func (p *Painter) TextFit(body string, x, y, width int) chart.Box {
|
||||||
p.SetTextStyle(style)
|
p.SetTextStyle(style)
|
||||||
var output chart.Box
|
var output chart.Box
|
||||||
|
|
||||||
|
textAlign := ""
|
||||||
|
if len(textAligns) != 0 {
|
||||||
|
textAlign = textAligns[0]
|
||||||
|
}
|
||||||
for index, line := range lines {
|
for index, line := range lines {
|
||||||
if line == "" {
|
if line == "" {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
x0 := x
|
x0 := x
|
||||||
y0 := y + output.Height()
|
y0 := y + output.Height()
|
||||||
p.Text(line, x0, y0)
|
|
||||||
lineBox := r.MeasureText(line)
|
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.Right = chart.MaxInt(lineBox.Right, output.Right)
|
||||||
output.Bottom += lineBox.Height()
|
output.Bottom += lineBox.Height()
|
||||||
if index < len(lines)-1 {
|
if index < len(lines)-1 {
|
||||||
|
|
|
||||||
24
table.go
24
table.go
|
|
@ -70,8 +70,10 @@ type TableChartOption struct {
|
||||||
Header []string
|
Header []string
|
||||||
// The data of table
|
// The data of table
|
||||||
Data [][]string
|
Data [][]string
|
||||||
// The span of table column
|
// The span list of table column
|
||||||
Spans []int
|
Spans []int
|
||||||
|
// The text align list of table cell
|
||||||
|
TextAligns []string
|
||||||
// The font size of table
|
// The font size of table
|
||||||
FontSize float64
|
FontSize float64
|
||||||
// The font family, which should be installed first
|
// The font family, which should be installed first
|
||||||
|
|
@ -271,6 +273,13 @@ func (t *tableChart) render() (*renderInfo, error) {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// textAligns := opt.TextAligns
|
||||||
|
getTextAlign := func(index int) string {
|
||||||
|
if len(opt.TextAligns) <= index {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
return opt.TextAligns[index]
|
||||||
|
}
|
||||||
|
|
||||||
// 表格单元的处理
|
// 表格单元的处理
|
||||||
renderTableCells := func(
|
renderTableCells := func(
|
||||||
|
|
@ -299,7 +308,7 @@ func (t *tableChart) render() (*renderInfo, error) {
|
||||||
width := values[index+1] - x
|
width := values[index+1] - x
|
||||||
x += cellPadding.Left
|
x += cellPadding.Left
|
||||||
width -= paddingWidth
|
width -= paddingWidth
|
||||||
box := p.TextFit(text, x, y+int(fontSize), width)
|
box := p.TextFit(text, x, y+int(fontSize), width, getTextAlign(index))
|
||||||
// 计算最高的高度
|
// 计算最高的高度
|
||||||
if box.Height()+paddingHeight > cellMaxHeight {
|
if box.Height()+paddingHeight > cellMaxHeight {
|
||||||
cellMaxHeight = box.Height() + paddingHeight
|
cellMaxHeight = box.Height() + paddingHeight
|
||||||
|
|
@ -342,7 +351,7 @@ func (t *tableChart) renderWithInfo(info *renderInfo) (Box, error) {
|
||||||
p.SetBackground(info.Width, info.HeaderHeight, headerBGColor, true)
|
p.SetBackground(info.Width, info.HeaderHeight, headerBGColor, true)
|
||||||
currentHeight := info.HeaderHeight
|
currentHeight := info.HeaderHeight
|
||||||
rowColors := opt.RowBackgroundColors
|
rowColors := opt.RowBackgroundColors
|
||||||
if len(rowColors) == 0 {
|
if rowColors == nil {
|
||||||
rowColors = tableDefaultSetting.RowColors
|
rowColors = tableDefaultSetting.RowColors
|
||||||
}
|
}
|
||||||
for index, h := range info.RowHeights {
|
for index, h := range info.RowHeights {
|
||||||
|
|
@ -375,12 +384,13 @@ func (t *tableChart) renderWithInfo(info *renderInfo) (Box, error) {
|
||||||
Column: j,
|
Column: j,
|
||||||
})
|
})
|
||||||
if style != nil && !style.FillColor.IsZero() {
|
if style != nil && !style.FillColor.IsZero() {
|
||||||
|
padding := style.Padding
|
||||||
child := p.Child(PainterPaddingOption(Box{
|
child := p.Child(PainterPaddingOption(Box{
|
||||||
Top: top,
|
Top: top + padding.Top,
|
||||||
Left: left,
|
Left: left + padding.Left,
|
||||||
}))
|
}))
|
||||||
w := info.ColumnWidths[j]
|
w := info.ColumnWidths[j] - padding.Left - padding.Top
|
||||||
h := heights[i]
|
h := heights[i] - padding.Top - padding.Bottom
|
||||||
child.SetBackground(w, h, style.FillColor, true)
|
child.SetBackground(w, h, style.FillColor, true)
|
||||||
}
|
}
|
||||||
left += info.ColumnWidths[j]
|
left += info.ColumnWidths[j]
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue