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 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 { func (p *Painter) Ticks(opt TicksOption) *Painter {
if opt.Count <= 0 || opt.Length <= 0 { if opt.Count <= 0 || opt.Length <= 0 {
return p return p
@ -638,7 +649,7 @@ func (p *Painter) Ticks(opt TicksOption) *Painter {
if index < first { if index < first {
continue continue
} }
if (index-first)%unit != 0 { if ! isTick(len(values), unit, index) {
continue continue
} }
if isVertical { if isVertical {
@ -674,15 +685,13 @@ func (p *Painter) MultiText(opt MultiTextOption) *Painter {
} }
count := len(opt.TextList) count := len(opt.TextList)
positionCenter := true positionCenter := true
showIndex := opt.Unit / 2 tickLimit := true
if containsString([]string{ if containsString([]string{
PositionLeft, PositionLeft,
PositionTop, PositionTop,
}, opt.Position) { }, opt.Position) {
positionCenter = false positionCenter = false
count-- count--
// 非居中
showIndex = 0
} }
width := p.Width() width := p.Width()
height := p.Height() height := p.Height()
@ -690,6 +699,7 @@ func (p *Painter) MultiText(opt MultiTextOption) *Painter {
isVertical := opt.Orient == OrientVertical isVertical := opt.Orient == OrientVertical
if isVertical { if isVertical {
values = autoDivide(height, count) values = autoDivide(height, count)
tickLimit = false
} else { } else {
values = autoDivide(width, count) values = autoDivide(width, count)
} }
@ -699,7 +709,7 @@ func (p *Painter) MultiText(opt MultiTextOption) *Painter {
if index < opt.First { if index < opt.First {
continue 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 continue
} }
if isTextRotation { if isTextRotation {
@ -724,7 +734,11 @@ func (p *Painter) MultiText(opt MultiTextOption) *Painter {
x = 0 x = 0
} }
} else { } else {
x = start - box.Width()>>1 if index == len(opt.TextList) - 1 {
x = start - box.Width()
} else {
x = start - box.Width()>>1
}
} }
x += offset.Left x += offset.Left
y += offset.Top y += offset.Top
@ -749,7 +763,6 @@ func (p *Painter) Grid(opt GridOption) *Painter {
x1 := 0 x1 := 0
y1 := 0 y1 := 0
if isVertical { if isVertical {
x0 = v x0 = v
x1 = v x1 = v
y1 = height y1 = height