B4J Question XChart format Y axis on line chart

EnriqueGonzalez

Well-Known Member
Licensed User
Longtime User
Hi!
When i create a chart with decimal values and automaticScale = false i can't format the Y axis.

If i assign the YminValue = 11.80 and YMaxValue = 16.90 only those 2 values will be shown as 2 decimals but values on the middle will be with 3 decimals.

thank you in advance!
 

klaus

Expert
Licensed User
Longtime User
The number of decimal digits is calculated automatically, these cannot be formatted.
I am astonished that you get 11.80 and 16.90 and not 11.8 and 16.9.
How many Y intervals do you have, or how many Y scale values do you display ?
 
Last edited:
Upvote 0

EnriqueGonzalez

Well-Known Member
Licensed User
Longtime User
Sorry the 11.8 and 16.9 were only examples.
this is the final result
1686922797893.png

if min and max value has 3 digits all the values in the middle will have 4 digits and if they have 2 digits then all the values in the middle will have 3
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
The none significant zeros at the end are systematically removed.
How do you calculate the scale, it seems that you take the max value minus the min value and divide by 10 getting strange scales.
You could 'play' with the min and max values to get an interval with less decimal characters.
In your case if you set the min value to 11.6 and the max value to 18.6 you get scales with 1 decimal digit for all scale values.
You could also use automatic scales with the second ScaleValues option 1!1.2!1.5!1.8!2!2.5!3!4!5!6!7!8!9!10.
 
Last edited:
Upvote 0

EnriqueGonzalez

Well-Known Member
Licensed User
Longtime User
How do you calculate the scale, it seems that your take the max value minus the min value and divide by 10 getting strange scales.
i dont calculate the scale, just add YMinValue and YMaxValue and the values in the middle are created as is

In your case if you set the min value to 11.6 and the max value to 18.6 you get scales with 1 decimal digit for all scale values.
okey thank you i will try this option
 
Upvote 0

EnriqueGonzalez

Well-Known Member
Licensed User
Longtime User
they are calculated like this:
B4X:
    Dim min_v, max_v As Double = 0
    For i = 0 To laps.RowsCount -1
        Dim done_time As Double = laps.getCellValue(i, "done_time")
        xChartGraph1.AddLinePointData(laps.getCellValue(i, "number"), done_time, True)
        If min_v = 0 Then min_v = done_time
        min_v = Min(min_v, done_time)
        max_v = Max(max_v, done_time)
    Next

    xChartGraph1.YMaxValue = max_v
    xChartGraph1.YMinValue = min_v
 
Upvote 0

John Naylor

Active Member
Licensed User
Longtime User
Try this :-

It returns a map with the names of the relevant settings and the values you need to set. You'll need to tweak it if your minY value is greater than 0. It'll keep your Y axis values as whole numbers.

B4X:
Public Sub CalculateYScale (maxY As Float) As Map
 
    Dim m As Map
    m.Initialize
 
    Dim intY As Int = maxY
    Dim evenY As Int

    If intY < maxY Then intY = intY + 1
 
    If intY Mod 2 = 0 Then
        evenY = intY
    Else
        evenY = intY + 1
    End If
 
 
    If intY < 11 Then
        m.Put ("NbyIntervals", evenY)
        m.Put ("YMaxValue", evenY)
    Else
        Dim range As Float = evenY
        Dim tickRange As Float = range / 8            'Number of Y lines
 
        'If tickRange isn't a 'nice' value then we'll round it up a bit....
 
        If tickRange <= 1 Then tickRange = 1
        If tickRange >1 And tickRange <= 2 Then tickRange = 2
        If tickRange >2 And tickRange <= 5 Then tickRange = 5
        If tickRange >5 And tickRange <= 10 Then tickRange = 10
        If tickRange >10 And tickRange <= 25 Then tickRange = 25
        If tickRange >25 And tickRange <= 50 Then tickRange = 50
        If tickRange >50 And tickRange <= 1000 Then tickRange = 100
 
        Dim newUpper As Int = tickRange * (Ceil (evenY / tickRange))

        m.Put ("NbyIntervals", newUpper / tickRange)
        m.Put ("YMaxValue", newUpper)
    End If

    Return m

End Sub
 
Last edited:
Upvote 0

klaus

Expert
Licensed User
Longtime User
they are calculated like this:
This is exactly what I supposed in post #4.
Anyway, I would use automatic scales with the 1!1.2!1.5!1.8!2!2.5!3!4!5!6!7!8!9!10 option for the ScaleValues.

1686982412264.png
 
Upvote 0
Top