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:
parent
20e8d4a078
commit
e7a49c2c21
1 changed files with 20 additions and 7 deletions
27
painter.go
27
painter.go
|
|
@ -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 {
|
||||
|
|
@ -724,7 +734,11 @@ func (p *Painter) MultiText(opt MultiTextOption) *Painter {
|
|||
x = 0
|
||||
}
|
||||
} 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
|
||||
y += offset.Top
|
||||
|
|
@ -749,7 +763,6 @@ func (p *Painter) Grid(opt GridOption) *Painter {
|
|||
x1 := 0
|
||||
y1 := 0
|
||||
if isVertical {
|
||||
|
||||
x0 = v
|
||||
x1 = v
|
||||
y1 = height
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue