B4J Library [B4X] [XUI] xChart Class and b4xlib

klaus

Expert
Licensed User
Longtime User
The xChart b4xlib has been updated to Version 9.1.
Replaced all Props.Get by Props.GetDefault
Amended problem with DisplayValues, DisplayCursor and DisplayValuesOnHover
Added GetMaxNumberBars2, does not need to know the data values.
Added PieGapDegrees and PieAddPercentage properties in the code
Only the xChart.b4xlib and xChart.xml files have been updated.
 

bdunkleysmith

Active Member
Licensed User
Longtime User
Has xChart.b4xlib actually been updated @klaus because it seems to me that manifest.txt within that shows it to be still 9.0 and the documentation still shows 9.0:



Or am I still downloading the old version for some reason?
 

klaus

Expert
Licensed User
Longtime User
Can you try the attached xChart.b4xlib version 9.2
I added a SetCustomFont(CustomFontName, CustomFontScale) method.
In B4J CustomFontName can be ether a font file with ttf extension in the File.DirAssets folder or a font name like "Times New Roman".
In B4A CustomFontName can only be ether a font file with ttf extension in the File.DirAssets folder.
In B4i no custom font.
CustomFontScale can be used if a font is bigger or smaller than the default font. Default value CustomFontScale = 1.

EDIT: The xChart.b4xlib file has been removed.
 
Last edited:

aeric

Expert
Licensed User
Longtime User
Thanks klaus. It looks much better now.

After:


Before:
 

klaus

Expert
Licensed User
Longtime User
The xChart library has been updated to version 9.2.
Added SetCustomFont(CustomFontName, CustomFontScale) method,example HERE.
Amended legend top position overlapping with the title.
Only the xChart.b4xlib and xChart.xml files have been updated.
 
Last edited:

aeric

Expert
Licensed User
Longtime User
Based on my code snippet for B4XCustomFont HERE,
I suggest to modify the code as following (starting line #2271):

B4X:
'Initializes text sizes.
Private Sub InitTextSizes
    Private fntx As B4XFont
    
    If Texts.AutomaticTextSizes = True Then
        Private GraphSize As Int
        GraphSize = Min(xpnlGraph.Width, xpnlGraph.Height) / xui.Scale
        Texts.TitleTextSize = (1 + (GraphSize - 250)/1000) * 18 * Texts.CustomFontScale
        Texts.SubtitleTextSize = (1 + (GraphSize - 250)/1000) * 16 * Texts.CustomFontScale
        Texts.AxisTextSize = (1 + (GraphSize - 250)/1000) * 14 * Texts.CustomFontScale
        Legend.TextSize = (1 + (GraphSize - 250)/1000) * 14 * Texts.CustomFontScale
        Texts.ScaleTextSize = (1 + (GraphSize - 250)/1000) * 12 * Texts.CustomFontScale
        Values.TextSize = (1 + (GraphSize - 250)/1000) * 14 * Texts.CustomFontScale
    End If
    
    If Texts.CustomFontName <> "" Then
        If Texts.CustomFontName.EndsWith("ttf") Then
            #If B4J
                'Private fnt As Font
                'fnt = fx.LoadFont(File.DirAssets, Texts.CustomFontName, 10)
                'fnt = fx.LoadFont(File.DirAssets, "commercial-script-bt.ttf", 10)
                'fntx = xui.CreateFont(fnt, 10)

                fntx = xui.CreateFont(fx.LoadFont(File.DirAssets, Texts.CustomFontName, 10), 10)
            #Else If B4A
                'Private fnt As Typeface
                'fnt = Typeface.LoadFromAssets(Texts.CustomFontName)
                'fntx = xui.CreateFont(fnt, 10)

                fntx = xui.CreateFont(Typeface.LoadFromAssets(Texts.CustomFontName), 10)
            #Else
                'Private fnt As Font
                'fnt = fx.LoadFont(File.DirAssets, Texts.CustomFontName, 10)
                'fntx = xui.CreateFont(fnt, 10)

                fntx = xui.CreateFont(Font.CreateNew2(Texts.CustomFontName, 10), 10)
            #End If       
        Else
            #If B4J
                'fnt = fx.CreateFont(Texts.CustomFontName, 10, False, False)
                'fntx = xui.CreateFont(fnt, 10)

                fntx = xui.CreateFont(fx.CreateFont(Texts.CustomFontName, 10, False, False), 10)
            #Else If B4A
                'Private fnt As Typeface
                'fnt = Typeface.LoadFromAssets(Texts.CustomFontName)
                'fnt = Typeface
                'fntx = xui.CreateFont(fnt, 10)

                fntx = xui.CreateFont(Typeface.LoadFromAssets(Texts.CustomFontName), 10)
            #Else

            #End If       
        End If
    End If

    If Texts.CustomFontName = "" Then
        Texts.TitleFont = xui.CreateDefaultFont(Texts.TitleTextSize)
        Texts.SubtitleFont = xui.CreateDefaultFont(Texts.SubtitleTextSize)
        Texts.AxisFont = xui.CreateDefaultFont(Texts.AxisTextSize)
        Texts.ScaleFont = xui.CreateDefaultFont(Texts.ScaleTextSize)
        Legend.TextFont = xui.CreateDefaultFont(Legend.TextSize)
        Values.TextFont = xui.CreateDefaultFont(Values.TextSize)
    Else
        Texts.TitleFont = xui.CreateFont2(fntx, Texts.TitleTextSize)
        Texts.SubtitleFont = xui.CreateFont2(fntx, Texts.SubtitleTextSize)
        Texts.AxisFont = xui.CreateFont2(fntx, Texts.AxisTextSize)
        Texts.ScaleFont = xui.CreateFont2(fntx, Texts.ScaleTextSize)
        Legend.TextFont = xui.CreateFont2(fntx, Legend.TextSize)
        Values.TextFont = xui.CreateFont2(fntx, Values.TextSize)
    End If
    
    Texts.TitleTextHeight = MeasureTextHeight("Mg", Texts.TitleFont)
    Texts.TitleTextWidth = MeasureTextWidth(Graph.Title, Texts.TitleFont)
    Texts.SubtitleTextHeight = MeasureTextHeight("Mg", Texts.SubtitleFont)
    Texts.SubtitleTextWidth = MeasureTextWidth(Graph.Subtitle, Texts.SubtitleFont)
    Texts.AxisTextHeight = MeasureTextHeight("Mg", Texts.AxisFont)
    Texts.ScaleTextHeight = MeasureTextHeight("Mg", Texts.ScaleFont)
    Legend.TextHeight = MeasureTextHeight("Mg", Legend.TextFont)
    Values.TextHeight = MeasureTextHeight("Mg", Values.TextFont)
    
    Graph.Error = False
    Graph.ErrorText = ""
End Sub
 

Mark Read

Well-Known Member
Licensed User
Longtime User
Hello to everyone (especially Klaus).

I need to plot two lines of data, both are temperatures but have different date/times for the x-values. Is that possible with this library. I have already used the xChart.bas class but I cannot see if this is possible, even after reading this marathon thread!

Thanks for the help.
 

klaus

Expert
Licensed User
Longtime User
I need to plot two lines of data, both are temperatures but have different date/times for the x-values.
I do not really understand the purpose.
You mean having two different X axis tick values?
If yes, sorry, this is not possible.
The X tick values are the same for multi-line charts.
 

emexes

Expert
Licensed User
I need to plot two lines of data, both are temperatures but have different date/times for the x-values.

Presumably measurements of two different temperatures, from probably two devices that are not in sync, and so the date+time values for the two traces don't match up.

Like freezer temperature and ambient temperature, or wet and dry temperatures of a weather station.

So I think along the bottom you'd have just one common (shared) x scale, and ditto for temperature y scale, and plot two lines of (x, y) points.

But I'd have thought plotting two lines would be simple to work out, so perhaps there is some esotericness that I'm not seeing. Like maybe the two lines are of one temperature sensor but on different days, and so can't plot using x = date+time, have to plot using say x = time only (no date).
 

Mark Read

Well-Known Member
Licensed User
Longtime User
Yes exactly. I have temperature data from a weather station and from inlet temperature on my heating. All data is sent to a cloud and is data stamped with the time it arrives in the cloud (I have no control over this).

Emexes is basically correct.
 

Mark Read

Well-Known Member
Licensed User
Longtime User
Exactly!

The last values on each line can be up to 15 minutes apart, as I am logging every 15 minutes. The chart will show (via buttons), the data for 1 day/week/month or max 8000 points.
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…