Skip to content

Commit

Permalink
Changes after PR review
Browse files Browse the repository at this point in the history
  • Loading branch information
jentfoo committed Jan 2, 2025
1 parent ca49e1b commit d279dff
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 81 deletions.
32 changes: 16 additions & 16 deletions axis.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,12 +233,12 @@ func (a *axisPainter) Render() (Box, error) {
Left: ticksPaddingLeft,
IsSet: true,
})).ticks(ticksOption{
LabelCount: labelCount,
TickCount: tickCount,
TickSpaces: tickSpaces,
Length: tickLength,
Vertical: isVertical,
First: opt.DataStartIndex,
labelCount: labelCount,
tickCount: tickCount,
tickSpaces: tickSpaces,
length: tickLength,
vertical: isVertical,
firstIndex: opt.DataStartIndex,
})
p.LineStroke([]Point{
{X: x0, Y: y0},
Expand All @@ -252,16 +252,16 @@ func (a *axisPainter) Render() (Box, error) {
Right: labelPaddingRight,
IsSet: true,
})).multiText(multiTextOption{
First: opt.DataStartIndex,
Align: textAlign,
TextList: opt.Data,
Vertical: isVertical,
LabelCount: labelCount,
TickCount: tickCount,
LabelSkipCount: opt.LabelSkipCount,
CenterLabels: centerLabels,
TextRotation: opt.TextRotation,
Offset: opt.LabelOffset,
firstIndex: opt.DataStartIndex,
align: textAlign,
textList: opt.Data,
vertical: isVertical,
labelCount: labelCount,
tickCount: tickCount,
labelSkipCount: opt.LabelSkipCount,
centerLabels: centerLabels,
textRotation: opt.TextRotation,
offset: opt.LabelOffset,
})

if opt.SplitLineShow { // show auxiliary lines
Expand Down
110 changes: 51 additions & 59 deletions painter.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,27 +41,25 @@ type PainterOptions struct {
type PainterOption func(*Painter)

type ticksOption struct {
// the first tick index
First int
Length int
Vertical bool
LabelCount int
TickCount int
TickSpaces int
firstIndex int
length int
vertical bool
labelCount int
tickCount int
tickSpaces int
}

type multiTextOption struct {
TextList []string
Vertical bool
CenterLabels bool
Align string
TextRotation float64
Offset OffsetInt
// The first text index
First int
LabelCount int
TickCount int
LabelSkipCount int
textList []string
vertical bool
centerLabels bool
align string
textRotation float64
offset OffsetInt
firstIndex int
labelCount int
tickCount int
labelSkipCount int
}

// PainterPaddingOption sets the padding of draw painter
Expand Down Expand Up @@ -264,9 +262,7 @@ func (p *Painter) arcTo(cx, cy int, rx, ry, startAngle, delta float64) *Painter

// Line renders a line from the first xy coordinates to the second point.
func (p *Painter) Line(x1, y1, x2, y2 int) *Painter {
p.moveTo(x1, y1)
p.render.LineTo(x2+p.box.Left, y2+p.box.Top)
return p
return p.moveTo(x1, y1).lineTo(x2, y2)
}

// lineTo renders a line from the current cursor to the given point.
Expand Down Expand Up @@ -541,8 +537,7 @@ func (p *Painter) MarkLine(x, y, width int) *Painter {
radius := 3
p.Circle(3, x+radius, y)
p.render.Fill()
p.moveTo(x+radius*3, y)
p.lineTo(endX-arrowWidth, y)
p.Line(x+radius*3, y, endX-arrowWidth, y)
p.stroke()
p.ArrowRight(endX, y, arrowWidth, arrowHeight)
return p
Expand Down Expand Up @@ -725,29 +720,29 @@ func isTick(totalRange int, numTicks int, index int) bool {
}

func (p *Painter) ticks(opt ticksOption) *Painter {
if opt.LabelCount <= 0 || opt.Length <= 0 {
if opt.labelCount <= 0 || opt.length <= 0 {
return p
}
var values []int
if opt.Vertical {
values = autoDivide(p.Height(), opt.TickSpaces)
if opt.vertical {
values = autoDivide(p.Height(), opt.tickSpaces)
} else {
values = autoDivide(p.Width(), opt.TickSpaces)
values = autoDivide(p.Width(), opt.tickSpaces)
}
for index, value := range values {
if index < opt.First {
if index < opt.firstIndex {
continue
} else if !isTick(len(values)-opt.First, opt.TickCount, index-opt.First) {
} else if !isTick(len(values)-opt.firstIndex, opt.tickCount, index-opt.firstIndex) {
continue
}
if opt.Vertical {
if opt.vertical {
p.LineStroke([]Point{
{X: 0, Y: value},
{X: opt.Length, Y: value},
{X: opt.length, Y: value},
})
} else {
p.LineStroke([]Point{
{X: value, Y: opt.Length},
{X: value, Y: opt.length},
{X: value, Y: 0},
})
}
Expand All @@ -756,41 +751,41 @@ func (p *Painter) ticks(opt ticksOption) *Painter {
}

func (p *Painter) multiText(opt multiTextOption) *Painter {
if len(opt.TextList) == 0 {
if len(opt.textList) == 0 {
return p
}
count := len(opt.TextList)
count := len(opt.textList)
width := p.Width()
height := p.Height()
var positions []int
if opt.Vertical {
if opt.CenterLabels {
if opt.vertical {
if opt.centerLabels {
positions = autoDivide(height, count)
} else {
positions = autoDivide(height, count-1)
}
} else {
if opt.CenterLabels {
if opt.centerLabels {
positions = autoDivide(width, count)
} else {
positions = autoDivide(width, count-1)
}
}
isTextRotation := opt.TextRotation != 0
isTextRotation := opt.textRotation != 0
positionCount := len(positions)

skippedLabels := opt.LabelSkipCount // specify the skip count to ensure the top value is listed
skippedLabels := opt.labelSkipCount // specify the skip count to ensure the top value is listed
for index, start := range positions {
if opt.CenterLabels && index == positionCount-1 {
if opt.centerLabels && index == positionCount-1 {
break // positions have one item more than we can map to text, this extra value is used to center against
} else if index < opt.First {
} else if index < opt.firstIndex {
continue
} else if !opt.Vertical &&
} else if !opt.vertical &&
index != count-1 && // one off case for last label due to values and label qty difference
!isTick(positionCount-opt.First, opt.TickCount, index-opt.First) {
!isTick(positionCount-opt.firstIndex, opt.tickCount, index-opt.firstIndex) {
continue
} else if index != count-1 && // ensure the bottom value is always printed
skippedLabels < opt.LabelSkipCount {
skippedLabels < opt.labelSkipCount {
skippedLabels++
continue
} else {
Expand All @@ -799,20 +794,20 @@ func (p *Painter) multiText(opt multiTextOption) *Painter {

if isTextRotation {
p.clearTextRotation()
p.setTextRotation(opt.TextRotation)
p.setTextRotation(opt.textRotation)
}
text := opt.TextList[index]
text := opt.textList[index]
box := p.MeasureText(text)
x := 0
y := 0
if opt.Vertical {
if opt.CenterLabels {
if opt.vertical {
if opt.centerLabels {
start = (positions[index] + positions[index+1]) >> 1
} else {
start = positions[index]
}
y = start + box.Height()>>1
switch opt.Align {
switch opt.align {
case AlignRight:
x = width - box.Width()
case AlignCenter:
Expand All @@ -821,11 +816,11 @@ func (p *Painter) multiText(opt multiTextOption) *Painter {
x = 0
}
} else {
if opt.CenterLabels {
if opt.centerLabels {
// graphs with limited data samples generally look better with the samples directly below the label
// for that reason we will exactly center these graphs, but graphs with higher sample counts will
// attempt to space the labels better rather than line up directly to the graph points
exactLabels := count == opt.LabelCount
exactLabels := count == opt.labelCount
if !exactLabels && index == 0 {
x = start - 1 // align to the actual start (left side of tick space)
} else if !exactLabels && index == count-1 {
Expand All @@ -842,8 +837,8 @@ func (p *Painter) multiText(opt multiTextOption) *Painter {
}
}
}
x += opt.Offset.Left
y += opt.Offset.Top
x += opt.offset.Left
y += opt.offset.Top
p.Text(text, x, y)
}
if isTextRotation {
Expand Down Expand Up @@ -888,16 +883,14 @@ func (p *Painter) roundedRect(box Box, radius int, roundTop, roundBottom bool) *

if roundTop {
// Start at the appropriate point depending on rounding at the top
p.moveTo(box.Left+radius, box.Top)
p.lineTo(box.Right-radius, box.Top)
p.Line(box.Left+radius, box.Top, box.Right-radius, box.Top)

// right top
cx := box.Right - radius
cy := box.Top + radius
p.arcTo(cx, cy, rx, ry, -math.Pi/2, math.Pi/2)
} else {
p.moveTo(box.Left, box.Top)
p.lineTo(box.Right, box.Top)
p.Line(box.Left, box.Top, box.Right, box.Top)
}

if roundBottom {
Expand Down Expand Up @@ -943,8 +936,7 @@ func (p *Painter) legendLineDot(box Box) *Painter {

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.Line(box.Left, box.Top-center, box.Right, box.Top-center)
p.stroke()
p.Circle(float64(dotHeight), box.Left+width>>1, box.Top-center)
p.fillStroke()
Expand Down
6 changes: 2 additions & 4 deletions pie_chart.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,10 +235,8 @@ func (p *pieChart) render(result *defaultRenderResult, seriesList SeriesList) (B
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.Line(s.lineStartX, s.lineStartY, s.lineBranchX, s.lineBranchY)
seriesPainter.Line(s.lineBranchX, s.lineBranchY, s.lineEndX, s.lineEndY)
seriesPainter.stroke()
textStyle := FontStyle{
FontColor: theme.GetTextColor(),
Expand Down
3 changes: 1 addition & 2 deletions radar_chart.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,7 @@ func (r *radarChart) render(result *defaultRenderResult, seriesList SeriesList)
}
points := getPolygonPoints(center, radius, sides)
for _, p := range points {
seriesPainter.moveTo(center.X, center.Y)
seriesPainter.lineTo(p.X, p.Y)
seriesPainter.Line(center.X, center.Y, p.X, p.Y)
seriesPainter.stroke()
}
seriesPainter.OverrideFontStyle(FontStyle{
Expand Down

0 comments on commit d279dff

Please sign in to comment.