Improvements to how the X Axis is rendered

This provides two improvements to how the X Axis is rendered:
* The calculation for where a tick should exist has been improved.  It now will ensure a tick is always at both the start of the axis and the end of the axis.  This makes it clear exactly what data span is captured in the graph.
* The second improvement is how the label on the last tick is written.  It used to often get partially cut off, and with the change to ensure a tick is always at the end this could be seen more easily.  Now the last tick has it's label written to the left so that it can be fully displayed.
This commit is contained in:
Mike Jensen 2023-05-04 12:52:28 -06:00
parent 20e8d4a078
commit e7a49c2c21
No known key found for this signature in database
GPG key ID: 2C909C9AAFB1047D

View file

@ -615,6 +615,17 @@ func (p *Painter) TextFit(body string, x, y, width int, textAligns ...string) ch
return output
}
func isTick(totalRange int, numTicks int, index int) bool {
step := float64(totalRange-1) / float64(numTicks-1)
for i := 0; i < numTicks; i++ {
value := float64(i) * step
if int(value + 0.5) == index {
return true
}
}
return false
}
func (p *Painter) Ticks(opt TicksOption) *Painter {
if opt.Count <= 0 || opt.Length <= 0 {
return p
@ -638,7 +649,7 @@ func (p *Painter) Ticks(opt TicksOption) *Painter {
if index < first {
continue
}
if (index-first)%unit != 0 {
if ! isTick(len(values), unit, index) {
continue
}
if isVertical {
@ -674,15 +685,13 @@ func (p *Painter) MultiText(opt MultiTextOption) *Painter {
}
count := len(opt.TextList)
positionCenter := true
showIndex := opt.Unit / 2
tickLimit := true
if containsString([]string{
PositionLeft,
PositionTop,
}, opt.Position) {
positionCenter = false
count--
// 非居中
showIndex = 0
}
width := p.Width()
height := p.Height()
@ -690,6 +699,7 @@ func (p *Painter) MultiText(opt MultiTextOption) *Painter {
isVertical := opt.Orient == OrientVertical
if isVertical {
values = autoDivide(height, count)
tickLimit = false
} else {
values = autoDivide(width, count)
}
@ -699,7 +709,7 @@ func (p *Painter) MultiText(opt MultiTextOption) *Painter {
if index < opt.First {
continue
}
if opt.Unit != 0 && (index-opt.First)%opt.Unit != showIndex {
if opt.Unit != 0 && tickLimit && ! isTick(len(opt.TextList)-opt.First, opt.Unit, index-opt.First) {
continue
}
if isTextRotation {
@ -723,9 +733,13 @@ func (p *Painter) MultiText(opt MultiTextOption) *Painter {
default:
x = 0
}
} else {
if index == len(opt.TextList) - 1 {
x = start - box.Width()
} else {
x = start - box.Width()>>1
}
}
x += offset.Left
y += offset.Top
p.Text(text, x, y)
@ -749,7 +763,6 @@ func (p *Painter) Grid(opt GridOption) *Painter {
x1 := 0
y1 := 0
if isVertical {
x0 = v
x1 = v
y1 = height