从最大x轴值和给定的刻度宽度正确设置x轴刻度的功能

This should be a relatively simple thing to do, but I can't seem to get it right here. I am generating PDF and so it's not as simple as using coordinates

What I am doing is making an x axis that's larger than the max x value for cosmetic reasons. In my example, the max x is 19, but I want the max value on the axis to be 20

The tricky bit is that the gaps between the points on the chart are set based on the number of points and available space for the chart

Laying out my example, I know these pieces of information:

  • max x = 19
  • max x axis value = 20
  • The width of the x axis = 518.3
  • The number of tick marks I want = 10

I thought it would be as simple as this to get the space between each tick to set in a loop that draws the little tick marks:

    interateXPositionsForEachTick := (xAxisWidth / xAxisMaxValue) * (xAxisMaxValue / NumberOfTickMarksOnXAxis)

In the above we are doing:

(518.3 / 20) * (20 / 10)

This logic is right isn't it?

But it doesn't seem to work in my loop at all:

xAxisTickPosition := startOfYAxis
for i := 1.0; i <= NumberOfTickMarksOnXAxis; i++ {

    LabelToUse := (xAxisMaxValue / NumberOfTickMarksOnXAxis) * i

    Line(xAxisTickPosition, placeYTickLocation, xAxisTickPosition, placeYTickLocation+tickmarkSize)

    xAxisTickPosition = xAxisTickPosition + interateXPositionsForEachTick

But this doesn't work:

enter image description here

Can anyone see what i'm doing wrong here? I am baffled

The 20 at the end is being set outside of this, but I want this function to be able to basically put a 20 in the same place I set it outside so I can test my function's alignment

I found out that it is 1.5 widths too large compared to the x axis:

When running this to make it 15% smaller:

interateXPositionsForEachTick := ((xAxisWidth / xAxisMaxValue) * (xAxisMaxValue NumberOfTickMarksOnXAxis)) * 0.85

The chart's axis looks correct: enter image description here

The 20 is bolded because it's overwritten outside of my loop