B4J Tutorial https://github.com/knowm/XChart

Wanted to see what is in the demo's of this Github Project. Have compiled the library and demo code into a single jar (JHSxChart.jar). Download and copy JHSxChart.jar to your B4J additional library folder.

You will also need to download the other required JAR's from here:


Download the zip, extract it and copy the JAR's to your additional library folder.

You also need to download XChart_64_64.png (attached.). Once you have run the attached B4J project for the first time you need to copy the png file into the /Objects folder of the B4J project.

Run the B4J project again and select from the ComboBox the chart you would like to view. There are 65 different charts to view. Some are RealTime charts.

Who would like to convert this into a B4J library.....?

Enjoy ;)

Sample Code:

B4J Sample Code:
#Region Project Attributes
    #MainFormWidth: 600
    #MainFormHeight: 600
#End Region

#AdditionalJar: JHSxChart
#AdditionalJar: animated-gif-lib-1.4
#AdditionalJar: commons-logging-1.2
#AdditionalJar: fontbox-2.0.27
#AdditionalJar: graphics2d-0.42
#AdditionalJar: pdfbox-2.0.27
#AdditionalJar: VectorGraphics2D-0.13


Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Private xui As XUI
    Dim mychart As JavaObject
    Private ComboBox1 As ComboBox

End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.RootPane.LoadLayout("Layout1")
    MainForm.Show
    
    Dim selectionlist As List = Array As String("AreaChart01", "AreaChart02", "AreaChart03", "AreaChart04", "AreaChart05",  _
                                                "BarChart01", "BarChart02", "BarChart03", "BarChart04", "BarChart05", "BarChart06", "BarChart07", "BarChart08", "BarChart09", "BarChart10", "BarChart11", _
                                                "BoxChart01", "BoxChart02", "BoxChart03", _
                                                "BubbleChart01", _
                                                "DateChart01", "DateChart02", "DateChart03", _
                                                "DialChart01", "DialChart02", _
                                                "HeatMapChart01", "HeatMapChart02", "HeatMapChart03", "HeatMapChart04", "HeatMapChart05", _
                                                "LineChart01", "LineChart02", "LineChart03", "LineChart04", "LineChart05", "LineChart06", "LineChart07", "LineChart08", "LineChart09", "LineChart10", _
                                                "OHLCChart01", "OHLCChart02", "OHLCChart03", _
                                                "PieChart01", "PieChart02", "PieChart03", "PieChart04", "PieChart05", _
                                                "RadarChart01", "RadarChart02", _
                                                "RealtimeChart01", "RealtimeChart02", "RealtimeChart03", "RealtimeChart04", "RealtimeChart05", "RealtimeChart06", _
                                                "ScatterChart01", "ScatterChart02", "ScatterChart03", "ScatterChart04", _
                                                "StickChart01", _
                                                "ThemeChart01", "ThemeChart02", "ThemeChart03", "ThemeChart04")
                                                
    ComboBox1.Items.AddAll(selectionlist)
    
End Sub

Private Sub ComboBox1_ValueChanged (Value As Object)
    
    Dim myval As String = Value
    Select myval
        
        Case "AreaChart01"
            'AreaChart01
            mychart.InitializeNewInstance("org.knowm.xchart.demo.charts.area.AreaChart01", Null)
            Dim aa() As String = Array As String("")
            mychart.RunMethod("main", Array(aa))
        Case "AreaChart02"
            'AreaChart02
            mychart.InitializeNewInstance("org.knowm.xchart.demo.charts.area.AreaChart02", Null)
            Dim aa() As String = Array As String("")
            mychart.RunMethod("main", Array(aa))
        Case "AreaChart03"
            'AreaChart03
            mychart.InitializeNewInstance("org.knowm.xchart.demo.charts.area.AreaChart03", Null)
            Dim aa() As String = Array As String("")
            mychart.RunMethod("main", Array(aa))
        Case "AreaChart04"
            'AreaChart04
            mychart.InitializeNewInstance("org.knowm.xchart.demo.charts.area.AreaChart04", Null)
            Dim aa() As String = Array As String("")
            mychart.RunMethod("main", Array(aa))
        Case "AreaChart05"
            'AreaChart05
            mychart.InitializeNewInstance("org.knowm.xchart.demo.charts.area.AreaChart05", Null)
            Dim aa() As String = Array As String("")
            mychart.RunMethod("main", Array(aa))
        Case "BarChart01"
            'BarChart01
            mychart.InitializeNewInstance("org.knowm.xchart.demo.charts.bar.BarChart01", Null)
            Dim aa() As String = Array As String("")
            mychart.RunMethod("main", Array(aa))
        Case "BarChart02"
            'BarChart02
            mychart.InitializeNewInstance("org.knowm.xchart.demo.charts.bar.BarChart02", Null)
            Dim aa() As String = Array As String("")
            mychart.RunMethod("main", Array(aa))
        Case "BarChart03"
            'BarChart03
            mychart.InitializeNewInstance("org.knowm.xchart.demo.charts.bar.BarChart03", Null)
            Dim aa() As String = Array As String("")
            mychart.RunMethod("main", Array(aa))
        Case "BarChart04"
            'BarChart04
            mychart.InitializeNewInstance("org.knowm.xchart.demo.charts.bar.BarChart04", Null)
            Dim aa() As String = Array As String("")
            mychart.RunMethod("main", Array(aa))
        Case "BarChart05"
            'BarChart05
            mychart.InitializeNewInstance("org.knowm.xchart.demo.charts.bar.BarChart05", Null)
            Dim aa() As String = Array As String("")
            mychart.RunMethod("main", Array(aa))
        Case "BarChart06"
            'BarChart06
            mychart.InitializeNewInstance("org.knowm.xchart.demo.charts.bar.BarChart06", Null)
            Dim aa() As String = Array As String("")
            mychart.RunMethod("main", Array(aa))
        Case "BarChart07"
            'BarChart07
            mychart.InitializeNewInstance("org.knowm.xchart.demo.charts.bar.BarChart07", Null)
            Dim aa() As String = Array As String("")
            mychart.RunMethod("main", Array(aa))
        Case "BarChart08"
            'BarChart08
            mychart.InitializeNewInstance("org.knowm.xchart.demo.charts.bar.BarChart08", Null)
            Dim aa() As String = Array As String("")
            mychart.RunMethod("main", Array(aa))
        Case "BarChart09"
            'BarChart09
            mychart.InitializeNewInstance("org.knowm.xchart.demo.charts.bar.BarChart09", Null)
            Dim aa() As String = Array As String("")
            mychart.RunMethod("main", Array(aa))
        Case "BarChart10"
            'BarChart10
            mychart.InitializeNewInstance("org.knowm.xchart.demo.charts.bar.BarChart10", Null)
            Dim aa() As String = Array As String("")
            mychart.RunMethod("main", Array(aa))
        Case "BarChart11"
            'BarChart11
            mychart.InitializeNewInstance("org.knowm.xchart.demo.charts.bar.BarChart11", Null)
            Dim aa() As String = Array As String("")
            mychart.RunMethod("main", Array(aa))
        Case "BoxChart01"
            'BoxChart01
            mychart.InitializeNewInstance("org.knowm.xchart.demo.charts.box.BoxChart01", Null)
            Dim aa() As String = Array As String("")
            mychart.RunMethod("main", Array(aa))
        Case "BoxChart02"
            'BoxChart02
            mychart.InitializeNewInstance("org.knowm.xchart.demo.charts.box.BoxChart02", Null)
            Dim aa() As String = Array As String("")
            mychart.RunMethod("main", Array(aa))
        Case "BoxChart03"
            'BoxChart03
            mychart.InitializeNewInstance("org.knowm.xchart.demo.charts.box.BoxChart03", Null)
            Dim aa() As String = Array As String("")
            mychart.RunMethod("main", Array(aa))
        Case "BubbleChart01"
            'BubbleChart01
            mychart.InitializeNewInstance("org.knowm.xchart.demo.charts.bubble.BubbleChart01", Null)
            Dim aa() As String = Array As String("")
            mychart.RunMethod("main", Array(aa))
        Case "DateChart01"
            'DateChart01
            mychart.InitializeNewInstance("org.knowm.xchart.demo.charts.date.DateChart01", Null)
            Dim aa() As String = Array As String("")
            mychart.RunMethod("main", Array(aa))
        Case "DateChart02"
            'DateChart02
            mychart.InitializeNewInstance("org.knowm.xchart.demo.charts.date.DateChart02", Null)
            Dim aa() As String = Array As String("")
            mychart.RunMethod("main", Array(aa))
        Case "DateChart03"
            'DateChart03
            mychart.InitializeNewInstance("org.knowm.xchart.demo.charts.date.DateChart03", Null)
            Dim aa() As String = Array As String("")
            mychart.RunMethod("main", Array(aa))
        Case "DialChart01"
            'DailChart01
            mychart.InitializeNewInstance("org.knowm.xchart.demo.charts.dial.DialChart01", Null)
            Dim aa() As String = Array As String("")
            mychart.RunMethod("main", Array(aa))
        Case "DialChart02"
            'DialChart02
            mychart.InitializeNewInstance("org.knowm.xchart.demo.charts.dial.DialChart02", Null)
            Dim aa() As String = Array As String("")
            mychart.RunMethod("main", Array(aa))
        Case "HeatMapChart01"
            'HeatMapChart01
            mychart.InitializeNewInstance("org.knowm.xchart.demo.charts.heatmap.HeatMapChart01", Null)
            Dim aa() As String = Array As String("")
            mychart.RunMethod("main", Array(aa))
        Case "HeatMapChart02"
            'HeatMapChart02
            mychart.InitializeNewInstance("org.knowm.xchart.demo.charts.heatmap.HeatMapChart02", Null)
            Dim aa() As String = Array As String("")
            mychart.RunMethod("main", Array(aa))
        Case "HeatMapChart03"
            'HeatMapChart03
            mychart.InitializeNewInstance("org.knowm.xchart.demo.charts.heatmap.HeatMapChart03", Null)
            Dim aa() As String = Array As String("")
            mychart.RunMethod("main", Array(aa))
        Case "HeatMapChart04"
            'HeatMapChart04
            mychart.InitializeNewInstance("org.knowm.xchart.demo.charts.heatmap.HeatMapChart04", Null)
            Dim aa() As String = Array As String("")
            mychart.RunMethod("main", Array(aa))
        Case "HeatMapChart05"
            'HeatMapChart05
            mychart.InitializeNewInstance("org.knowm.xchart.demo.charts.heatmap.HeatMapChart05", Null)
            Dim aa() As String = Array As String("")
            mychart.RunMethod("main", Array(aa))
        Case "LineChart01"
            'LineChart01
            mychart.InitializeNewInstance("org.knowm.xchart.demo.charts.line.LineChart01", Null)
            Dim aa() As String = Array As String("")
            mychart.RunMethod("main", Array(aa))
        Case "LineChart02"
            'LineChart02
            mychart.InitializeNewInstance("org.knowm.xchart.demo.charts.line.LineChart02", Null)
            Dim aa() As String = Array As String("")
            mychart.RunMethod("main", Array(aa))
        Case "LineChart03"
            'LineChart03
            mychart.InitializeNewInstance("org.knowm.xchart.demo.charts.line.LineChart03", Null)
            Dim aa() As String = Array As String("")
            mychart.RunMethod("main", Array(aa))
        Case "LineChart04"
            'LineChart04
            mychart.InitializeNewInstance("org.knowm.xchart.demo.charts.line.LineChart04", Null)
            Dim aa() As String = Array As String("")
            mychart.RunMethod("main", Array(aa))
        Case "LineChart05"
            'LineChart05
            mychart.InitializeNewInstance("org.knowm.xchart.demo.charts.line.LineChart05", Null)
            Dim aa() As String = Array As String("")
            mychart.RunMethod("main", Array(aa))
        Case "LineChart06"
            'LineChart06
            mychart.InitializeNewInstance("org.knowm.xchart.demo.charts.line.LineChart06", Null)
            Dim aa() As String = Array As String("")
            mychart.RunMethod("main", Array(aa))
        Case "LineChart07"
            'LineChart07
            mychart.InitializeNewInstance("org.knowm.xchart.demo.charts.line.LineChart07", Null)
            Dim aa() As String = Array As String("")
            mychart.RunMethod("main", Array(aa))
        Case "LineChart08"
            'LineChart08
            mychart.InitializeNewInstance("org.knowm.xchart.demo.charts.line.LineChart08", Null)
            Dim aa() As String = Array As String("")
            mychart.RunMethod("main", Array(aa))
        Case "LineChart09"
            'LineChart09
            mychart.InitializeNewInstance("org.knowm.xchart.demo.charts.line.LineChart09", Null)
            Dim aa() As String = Array As String("")
            mychart.RunMethod("main", Array(aa))
        Case "LineChart10"
            'LineChart10
            mychart.InitializeNewInstance("org.knowm.xchart.demo.charts.line.LineChart10", Null)
            Dim aa() As String = Array As String("")
            mychart.RunMethod("main", Array(aa))
        Case "OHLCChart01"
            'OHLCChart01
            mychart.InitializeNewInstance("org.knowm.xchart.demo.charts.ohlc.OHLCChart01", Null)
            Dim aa() As String = Array As String("")
            mychart.RunMethod("main", Array(aa))
        Case "OHLCChart02"
            'OHLCChart02
            mychart.InitializeNewInstance("org.knowm.xchart.demo.charts.ohlc.OHLCChart02", Null)
            Dim aa() As String = Array As String("")
            mychart.RunMethod("main", Array(aa))
        Case "OHLCChart03"
            'OHLCChart03
            mychart.InitializeNewInstance("org.knowm.xchart.demo.charts.ohlc.OHLCChart03", Null)
            Dim aa() As String = Array As String("")
            mychart.RunMethod("main", Array(aa))
        Case "PieChart01"
            'PieChart01
            mychart.InitializeNewInstance("org.knowm.xchart.demo.charts.pie.PieChart01", Null)
            Dim aa() As String = Array As String("")
            mychart.RunMethod("main", Array(aa))
        Case "PieChart02"
            'PieChart02
            mychart.InitializeNewInstance("org.knowm.xchart.demo.charts.pie.PieChart02", Null)
            Dim aa() As String = Array As String("")
            mychart.RunMethod("main", Array(aa))
        Case "PieChart03"
            'PieChart03
            mychart.InitializeNewInstance("org.knowm.xchart.demo.charts.pie.PieChart03", Null)
            Dim aa() As String = Array As String("")
            mychart.RunMethod("main", Array(aa))
        Case "PieChart04"
            'PieChart04
            mychart.InitializeNewInstance("org.knowm.xchart.demo.charts.pie.PieChart04", Null)
            Dim aa() As String = Array As String("")
            mychart.RunMethod("main", Array(aa))
        Case "PieChart05"
            'PieChart05
            mychart.InitializeNewInstance("org.knowm.xchart.demo.charts.pie.PieChart05", Null)
            Dim aa() As String = Array As String("")
            mychart.RunMethod("main", Array(aa))
        Case "RadarChart01"
            'RadarChart01
            mychart.InitializeNewInstance("org.knowm.xchart.demo.charts.radar.RadarChart01", Null)
            Dim aa() As String = Array As String("")
            mychart.RunMethod("main", Array(aa))
        Case "RadarChart02"
            'RadarChart02
            mychart.InitializeNewInstance("org.knowm.xchart.demo.charts.radar.RadarChart02", Null)
            Dim aa() As String = Array As String("")
            mychart.RunMethod("main", Array(aa))
        Case "RealtimeChart01"
            'RealtimeChart01
            mychart.InitializeNewInstance("org.knowm.xchart.demo.charts.realtime.RealtimeChart01", Null)
            Dim aa() As String = Array As String("")
            mychart.RunMethod("main", Array(aa))
        Case "RealtimeChart02"
            'RealtimeChart02
            mychart.InitializeNewInstance("org.knowm.xchart.demo.charts.realtime.RealtimeChart02", Null)
            Dim aa() As String = Array As String("")
            mychart.RunMethod("main", Array(aa))
        Case "RealtimeChart03"
            'RealtimeChart03
            mychart.InitializeNewInstance("org.knowm.xchart.demo.charts.realtime.RealtimeChart03", Null)
            Dim aa() As String = Array As String("")
            mychart.RunMethod("main", Array(aa))
        Case "RealtimeChart04"
            'RealtimeChart04
            mychart.InitializeNewInstance("org.knowm.xchart.demo.charts.realtime.RealtimeChart04", Null)
            Dim aa() As String = Array As String("")
            mychart.RunMethod("main", Array(aa))
        Case "RealtimeChart05"
            'RealtimeChart05
            mychart.InitializeNewInstance("org.knowm.xchart.demo.charts.realtime.RealtimeChart05", Null)
            Dim aa() As String = Array As String("")
            mychart.RunMethod("main", Array(aa))
        Case "RealtimeChart06"
            'RealtimeChart06
            mychart.InitializeNewInstance("org.knowm.xchart.demo.charts.realtime.RealtimeChart06", Null)
            Dim aa() As String = Array As String("")
            mychart.RunMethod("main", Array(aa))
        Case "ScatterChart01"
            'ScatterChart01
            mychart.InitializeNewInstance("org.knowm.xchart.demo.charts.scatter.ScatterChart01", Null)
            Dim aa() As String = Array As String("")
            mychart.RunMethod("main", Array(aa))
        Case "ScatterChart02"
            'ScatterChart02
            mychart.InitializeNewInstance("org.knowm.xchart.demo.charts.scatter.ScatterChart02", Null)
            Dim aa() As String = Array As String("")
            mychart.RunMethod("main", Array(aa))
        Case "ScatterChart03"
            'ScatterChart03
            mychart.InitializeNewInstance("org.knowm.xchart.demo.charts.scatter.ScatterChart03", Null)
            Dim aa() As String = Array As String("")
            mychart.RunMethod("main", Array(aa))
        Case "ScatterChart04"
            'ScatterChart04
            mychart.InitializeNewInstance("org.knowm.xchart.demo.charts.scatter.ScatterChart04", Null)
            Dim aa() As String = Array As String("")
            mychart.RunMethod("main", Array(aa))
        Case "StickChart01"
            'StickChart01
            mychart.InitializeNewInstance("org.knowm.xchart.demo.charts.stick.StickChart01", Null)
            Dim aa() As String = Array As String("")
            mychart.RunMethod("main", Array(aa))
        Case "ThemeChart01"
            'ThemeChart01
            mychart.InitializeNewInstance("org.knowm.xchart.demo.charts.theme.ThemeChart01", Null)
            Dim aa() As String = Array As String("")
            mychart.RunMethod("main", Array(aa))
        Case "ThemeChart02"
            'ThemeChart02
            mychart.InitializeNewInstance("org.knowm.xchart.demo.charts.theme.ThemeChart02", Null)
            Dim aa() As String = Array As String("")
            mychart.RunMethod("main", Array(aa))
        Case "ThemeChart03"
            'ThemeChart03
            mychart.InitializeNewInstance("org.knowm.xchart.demo.charts.theme.ThemeChart03", Null)
            Dim aa() As String = Array As String("")
            mychart.RunMethod("main", Array(aa))
        Case "ThemeChart04"
            'ThemeChart04
            mychart.InitializeNewInstance("org.knowm.xchart.demo.charts.theme.ThemeChart04", Null)
            Dim aa() As String = Array As String("")
            mychart.RunMethod("main", Array(aa))

    End Select
    
End Sub
 

Attachments

  • XChart_64_64.png
    XChart_64_64.png
    2.9 KB · Views: 194
  • JHSxChart.jar
    467.1 KB · Views: 181
  • b4jXchart.zip
    3.9 KB · Views: 169

GMan

Well-Known Member
Licensed User
Longtime User
Where can i get the missing jars ?

B4X:
#AdditionalJar: fontbox-2.0.27
#AdditionalJar: graphics2d-0.42
#AdditionalJar: pdfbox-2.0.27
#AdditionalJar: VectorGraphics2D-0.13
 

Johan Schoeman

Expert
Licensed User
Longtime User
Where can i get the missing jars ?

B4X:
#AdditionalJar: fontbox-2.0.27
#AdditionalJar: graphics2d-0.42
#AdditionalJar: pdfbox-2.0.27
#AdditionalJar: VectorGraphics2D-0.13
They are included in the zip in post #1.
 

GMan

Well-Known Member
Licensed User
Longtime User
Not in my download ?!
But i can download them seperatly
 

Johan Schoeman

Expert
Licensed User
Longtime User
Using the githuburl in the Threadsubject is not a good idea i think ;-)
Away from that. Nice. Thank you for sharing ;-)
I dont want to confuse it with the (XUI) XChart lib by @klaus

Open for any suggestion(s) to rename the thread.
 

Johan Schoeman

Expert
Licensed User
Longtime User
Here is the PieChart and DonutChart making use of B4J, B4J Java Object, and inline Java Code.
Copy the attached JAR to your B4J additional library folder

Note that the piechart and donutchart differ by a single line of code only:

For donut chart:
B4X:
........PieSeriesRenderStyle_Donut

For pie chart:
B4X:
.......PieSeriesRenderStyle_Pie

1.gif


Changing
B4X:
xxxxchart.LabelsDistance = 1.7
...and
B4X:
xxxxchart.PlotContentSize = 0.4
...yields this

1682854837470.png
 

Attachments

  • b4jXchartPie.zip
    7.5 KB · Views: 86
  • JHSxChart.jar
    467.1 KB · Views: 93
Last edited:

Johan Schoeman

Expert
Licensed User
Longtime User
This is where I will leave Pie Chart and Donut Chart. A mixture of B4J, JavaObject, and inline Java code. Copy the attached JAR to your B4J additional library folder.

It will "raise events" (actually call Subs) in the Main module of the project when:
1. Entering a chart
2. Exiting a chart
3. Mouse Pressed
4. Mouse Released

It will log the name and value of the series when clicking on a donut/pie slice - you see something like this in the B4J logs:

B4j Logs:
MOUSE ENTERED ON PIE CHART
MOUSE EXITED ON PIE CHART
MOUSE ENTERED ON DONUT CHART
MOUSE EXITED ON DONUT CHART
MOUSE ENTERED ON DONUT CHART
MOUSE EXITED ON DONUT CHART
MOUSE ENTERED ON DONUT CHART
MOUSE EXITED ON DONUT CHART
MOUSE ENTERED ON PIE CHART
MOUSE PRESSED ON PIE CHART
PIE Series Name = Apples
PIE Series Value = 146
MOUSE RELEASED ON PIE CHART
MOUSE EXITED ON PIE CHART
MOUSE ENTERED ON DONUT CHART
MOUSE PRESSED ON DONUT CHART
DONUT Series Name = Oranges
DONUT Series Value = 149
MOUSE RELEASED ON DONUT CHART
MOUSE PRESSED ON DONUT CHART
DONUT Series Name = Banana
DONUT Series Value = 176
MOUSE RELEASED ON DONUT CHART
MOUSE PRESSED ON DONUT CHART
MOUSE RELEASED ON DONUT CHART
MOUSE EXITED ON DONUT CHART
MOUSE ENTERED ON PIE CHART
MOUSE EXITED ON PIE CHART
MOUSE ENTERED ON DONUT CHART
MOUSE PRESSED ON DONUT CHART
DONUT Series Name = Pears
DONUT Series Value = 154
MOUSE RELEASED ON DONUT CHART
MOUSE PRESSED ON DONUT CHART
DONUT Series Name = Apples
DONUT Series Value = 74
MOUSE RELEASED ON DONUT CHART
MOUSE EXITED ON DONUT CHART
MOUSE ENTERED ON PIE CHART
MOUSE PRESSED ON PIE CHART
PIE Series Name = Apples
PIE Series Value = 298
MOUSE RELEASED ON PIE CHART
MOUSE PRESSED ON PIE CHART
PIE Series Name = Oranges
PIE Series Value = 124
MOUSE RELEASED ON PIE CHART
MOUSE PRESSED ON PIE CHART
PIE Series Name = Apples
PIE Series Value = 54
MOUSE RELEASED ON PIE CHART
MOUSE PRESSED ON PIE CHART
PIE Series Name = Oranges
PIE Series Value = 34
MOUSE RELEASED ON PIE CHART
MOUSE EXITED ON PIE CHART

1683375282203.png


Main Code:
Main:
#Region Project Attributes
    #MainFormWidth: 990
    #MainFormHeight: 500

#End Region

#AdditionalJar: JHSxChart
'#VirtualMachineArgs: -Dprism.order=sw

Sub Process_Globals
   
    Private fx As JFX
    Private MainForm As Form
    Private xui As XUI
    Dim donutPane, piePane As JavaObject            'this is and instance of the pie/donut chart object that will be added to the RootPane of the Mainform
    Dim donutchart, piechart As xPieChart           'this creates an instance of class xdonutchart - it also handles a donut chart
    Dim t As Timer
   
    Dim obj1 As Reflector
   
End Sub

Sub AppStart (Form1 As Form, Args() As String)
   
    t.Initialize("t", 2000)                                                   'just for demo purposes
    MainForm = Form1
   
    MainForm.RootPane.LoadLayout("main")
    MainForm.Show
    'initialize the pie chart
   
    donutchart.initializePieChart("donut",480, 480, "The Fruit & Veg Shop")   'set the width, eight, title of the pie/donut chart
    piechart.initializePieChart("pie",480, 480, "The Fruit & Veg Shop")       'set the width, eight, title of the pie/pie chart
   
    'draw the donut chart
    drawDonutChart
    'draw the pie chart
    drawPieChart
   
    'NOTE - THE ABOVE TWO CALLS ONLY DIFFER IN CODE BY this single line of code:
    'donutchart.PieSeriesRenderStyle = donutchart.PieSeriesRenderStyle_Donut
    'for pie chart it becomes:
    'piechart.PieSeriesRenderStyle = piechart.PieSeriesRenderStyle_Pie
   
    MainForm.RootPane.AddNode(donutPane, 10, 10, 480, 480)
    MainForm.RootPane.AddNode(piePane, 500, 10, 480, 480)
   
    'The below 3 lines of code is for my laptop with Windows 10 else the graph will not display - seems to be working without these lines of code on later versions of Windows....?
    Sleep(300)
    MainForm.WindowLeft = MainForm.WindowLeft - 1
    MainForm.WindowLeft = MainForm.WindowLeft + 1
   
    t.Enabled = True
   
End Sub


'change the value(s) of the series every xxx milliseconds amd repaint the chart to see the updated chart
Sub t_tick
   
    Dim plotBackGroundColorObject As JavaObject                                'using JavaObject to pass an instance of java.awt.Color
    plotBackGroundColorObject.InitializeNewInstance("java.awt.Color", Array(Rnd(0,256).As(Int), Rnd(0,256).As(Int), Rnd(0,256).As(Int)))
    donutchart.PlotBackgroundColor = plotBackGroundColorObject
   
    donutchart.updatePieSeries("Apples", Rnd(30, 300))
    donutchart.updatePieSeries("Pears", Rnd(30, 300))
    donutchart.updatePieSeries("Oranges", Rnd(30, 300))
    donutchart.updatePieSeries("Banana", Rnd(30, 300))
   
    Dim thickValInt As Int = Rnd(10, 81)
    Dim thickValDouble As Double  = thickValInt/100
    donutchart.DonutTickness = thickValDouble
   
    donutchart.repaintChart
   
   
    Dim plotBackGroundColorObject As JavaObject                                'using JavaObject to pass an instance of java.awt.Color
    plotBackGroundColorObject.InitializeNewInstance("java.awt.Color", Array(Rnd(0,256).As(Int), Rnd(0,256).As(Int), Rnd(0,256).As(Int)))
    piechart.PlotBackgroundColor = plotBackGroundColorObject
       
    piechart.updatePieSeries("Apples", Rnd(30, 300))
    piechart.updatePieSeries("Pears", Rnd(30, 300))
    piechart.updatePieSeries("Oranges", Rnd(30, 300))
    piechart.updatePieSeries("Banana", Rnd(30, 300))
       
    Dim thickValInt As Int = Rnd(10, 81)
    Dim thickValDouble As Double  = thickValInt/100
    piechart.donutTickness = thickValDouble
   
    piechart.repaintChart
   
End Sub


'**************************************************************************************************************************************************
Public Sub drawDonutChart
   
'    donutchart.initializePieChart(480, 480, "The Fruit & Veg Shop")   'set the width, eight, title of the pie/donut chart
   
    donutPane = donutchart.drawPieChart            'basically draw a blank chart
   
    donutchart.addSeries("Apples", 100)           'set 1st series
    donutchart.addSeries("Pears", 75)             'set 2nd series
    donutchart.addSeries("Oranges", 50)           'set 3rd series
    donutchart.addSeries("Banana", 30)            'set 4th series
   
    donutchart.GraphCircular = True               'make the chart circular or oval
   
    'set the colors of the pie/donut slices
    Dim slice1ColorObject, slice2ColorObject, slice3ColorObject, slice4ColorObject  As JavaObject   'using JavaObject to pass an instance of java.awt.Color for the pie slice colors
   
    slice1ColorObject.InitializeNewInstance("java.awt.Color", Array(255, 0, 0))         'color of slice/series 1
    slice2ColorObject.InitializeNewInstance("java.awt.Color", Array(0, 255, 0))         'color of slice/series 2
    slice3ColorObject.InitializeNewInstance("java.awt.Color", Array(0, 0, 255))         'color of slice/series 3
    slice4ColorObject.InitializeNewInstance("java.awt.Color", Array(255, 0, 255))       'color of slice/series 4
   
    'add the slice colors to the chart
    Dim sliceColorArray As JavaObject
    sliceColorArray.InitializeArray("java.awt.Color", Array(slice1ColorObject, slice2ColorObject, slice3ColorObject, slice4ColorObject))
    donutchart.SliceColors = sliceColorArray


    donutchart.StartAngle = 0                                                                 'start angle of from where to draw
    donutchart.ToolTipsEnabled = True                                                         'must tooltips be enabled?
    donutchart.SliceBorderWidth = 5                                                           'set the gap between slices
    donutchart.LegendPosition = donutchart.LPosition_OutsideS                                   'see the other options in the pop-up
    donutchart.LegendLayout = donutchart.LLayout_Horizontal                                     'Horizontal or Vertical
    donutchart.LegendVisible = True                                                           'must the legend be visible?
    donutchart.SumVisible = True                                                              'must the sum of the slices be visible?
   
    donutchart.PlotContentSize = 0.4                                                          'how much of the plot area must be used for the chart
   
    'set the chart background color
    Dim plotBackGroundColorObject As JavaObject                                             'using JavaObject to pass an instance of java.awt.Color
    plotBackGroundColorObject.InitializeNewInstance("java.awt.Color", Array(Rnd(0,256).As(Int), Rnd(0,256).As(Int), Rnd(0,256).As(Int)))
    donutchart.PlotBackgroundColor = plotBackGroundColorObject
   
    'set the border colors
    Dim plotBorderColorObject As JavaObject                                                 'using JavaObject to pass an instance of java.awt.Color
    plotBorderColorObject.InitializeNewInstance("java.awt.Color", Array(0,0,0))
    donutchart.PlotBorderColor = plotBorderColorObject
   
    'set what to display insicde the slices
    donutchart.LabelType = donutchart.labelType_NameAndValue
    donutchart.LabelsVisible = True
    donutchart.PieSeriesRenderStyle = donutchart.PieSeriesRenderStyle_Donut                     'make a donut chart. Change PieSeriesRenderStyle_Donut to PieSeriesRenderStyle_Pie to draw a Pie chart
    donutchart.DonutTickness = 0.33
   
    'set the tect color of the labels
    Dim labelsFontColorObject As JavaObject
    labelsFontColorObject.InitializeNewInstance("java.awt.Color", Array(0, 0, 0))                'set the text color of the labels to black
    donutchart.LabelsFontColor = labelsFontColorObject

    'position the labels - see the intellisense help when hovering the mouse on donutchart.LabelsDistance
    donutchart.LabelsDistance = 1.7
   
    'set the font of the pie slice labels using JavaObject -> java.awt.Font
    Dim fontType As JavaObject
    fontType.InitializeStatic("java.awt.Font")
    Dim labelFont As JavaObject
    labelFont.InitializeNewInstance("java.awt.Font", Array(fontType.GetField("DIALOG"), fontType.GetField("HANGING_BASELINE"), 12))   'MONOSPACED, SANS_SERIF, SERIF, DIALOG, DIALOG_INPUT
                                                                                                                                      'BOLD, PLAIN, CENTER_BASELINE, HANGING_BASELINE, ITALIC, TRUETYPE_FONT, TYPE1_FONT, ROMAN_BASELINE
    donutchart.LabelsFont = labelFont
   
    'set the font of the chart title using JavaObject -> java.awt.Font
    Dim fontType As JavaObject
    fontType.InitializeStatic("java.awt.Font")
    Dim titleFont As JavaObject
    titleFont.InitializeNewInstance("java.awt.Font", Array(fontType.GetField("SANS_SERIF"), fontType.GetField("BOLD"), 18))           'MONOSPACED, SANS_SERIF, SERIF, DIALOG, DIALOG_INPUT
                                                                                                                                      'BOLD, PLAIN, CENTER_BASELINE, HANGING_BASELINE, ITALIC, TRUETYPE_FONT, TYPE1_FONT, ROMAN_BASELINE
    donutchart.ChartTitleFont = titleFont
   
    'set the text color of the chart title, axes labes, and legend
    Dim chartFontColorObject As JavaObject
    chartFontColorObject.InitializeNewInstance("java.awt.Color", Array(25, 50, 200))
    donutchart.ChartFontColor = chartFontColorObject
   
    'set the background color of the legend
    Dim legendBackgroundColorObject As JavaObject
    legendBackgroundColorObject.InitializeNewInstance("java.awt.Color", Array(200, 200, 0))
    donutchart.LegendBackgroundColor = legendBackgroundColorObject
   
    'set the border color of the legend
    Dim legendBorderColorObject As JavaObject
    legendBorderColorObject.InitializeNewInstance("java.awt.Color", Array(0, 0, 0))
    donutchart.LegendBorderColor = legendBorderColorObject
   
   
    'set the font of the legend using JavaObject -> java.awt.Font
    Dim fontType As JavaObject
    fontType.InitializeStatic("java.awt.Font")
    Dim legendFont As JavaObject
    legendFont.InitializeNewInstance("java.awt.Font", Array(fontType.GetField("SANS_SERIF"), fontType.GetField("ITALIC"), 15))           'MONOSPACED, SANS_SERIF, SERIF, DIALOG, DIALOG_INPUT
                                                                                                                                         'BOLD, PLAIN, CENTER_BASELINE, HANGING_BASELINE, ITALIC, TRUETYPE_FONT, TYPE1_FONT, ROMAN_BASELINE
    donutchart.LegendFont = legendFont
       
    'set the background color of the title box
    Dim titleBackgroundColorObject As JavaObject
    titleBackgroundColorObject.InitializeNewInstance("java.awt.Color", Array(255, 0, 0))
    donutchart.ChartTitleBoxBackgroundColor = titleBackgroundColorObject
   
    'set the border color of the title box
    Dim titleBoxBorderColorObject As JavaObject
    titleBoxBorderColorObject.InitializeNewInstance("java.awt.Color", Array(0, 0, 0))
    donutchart.ChartTitleBoxBorderColor = titleBoxBorderColorObject
       
    donutchart.ChartTitleBoxVisible = True
   
    Dim tooltipBorderColorObject As JavaObject
    tooltipBorderColorObject.InitializeNewInstance("java.awt.Color", Array(255, 0, 0))
    donutchart.ToolTipBorderColor = tooltipBorderColorObject
   
    Dim tooltipBackgroundColorObject As JavaObject
    tooltipBackgroundColorObject.InitializeNewInstance("java.awt.Color", Array(255, 200, 200))
    donutchart.ToolTipBackgroundColor = tooltipBackgroundColorObject
   
    'set the font of the tooltip
    Dim fontType As JavaObject
    fontType.InitializeStatic("java.awt.Font")
    Dim tooltipFontObject As JavaObject
    tooltipFontObject.InitializeNewInstance("java.awt.Font", Array(fontType.GetField("SANS_SERIF"), fontType.GetField("ITALIC"), 15))           'MONOSPACED, SANS_SERIF, SERIF, DIALOG, DIALOG_INPUT
                                                                                                                                                'BOLD, PLAIN, CENTER_BASELINE, HANGING_BASELINE, ITALIC, TRUETYPE_FONT, TYPE1_FONT, ROMAN_BASELINE
    donutchart.ToolTipFont = tooltipFontObject
   
   
End Sub

'********************************************************************************************************************************************
Public Sub drawPieChart
   
'    piechart.initializePieChart(480, 480, "The Fruit & Veg Shop")   'set the width, eight, title of the pie/pie chart
   
    piePane = piechart.drawPieChart            'basically draw a blank chart
   
    piechart.addSeries("Apples", 100)           'set 1st series
    piechart.addSeries("Pears", 75)             'set 2nd series
    piechart.addSeries("Oranges", 50)           'set 3rd series
    piechart.addSeries("Banana", 30)            'set 4th series
   
    piechart.GraphCircular = True               'make the chart circular or oval
   
    'set the colors of the pie/pie slices
    Dim slice1ColorObject, slice2ColorObject, slice3ColorObject, slice4ColorObject As JavaObject   'using JavaObject to pass an instance of java.awt.Color for the pie slice colors
   
    slice1ColorObject.InitializeNewInstance("java.awt.Color", Array(255, 0, 0))         'color of slice/series 1
    slice2ColorObject.InitializeNewInstance("java.awt.Color", Array(0, 255, 0))         'color of slice/series 2
    slice3ColorObject.InitializeNewInstance("java.awt.Color", Array(0, 0, 255))         'color of slice/series 3
    slice4ColorObject.InitializeNewInstance("java.awt.Color", Array(255, 0, 255))       'color of slice/series 4
   
    'add the slice colors to the chart
    Dim sliceColorArray As JavaObject
    sliceColorArray.InitializeArray("java.awt.Color", Array(slice1ColorObject, slice2ColorObject, slice3ColorObject, slice4ColorObject))
    piechart.SliceColors = sliceColorArray


    piechart.StartAngle = 0                                                                 'start angle of from where to draw
    piechart.ToolTipsEnabled = True                                                         'must tooltips be enabled?
    piechart.SliceBorderWidth = 5                                                           'set the gap between slices
    piechart.LegendPosition = piechart.LPosition_OutsideS                                   'see the other options in the pop-up
    piechart.LegendLayout = piechart.LLayout_Horizontal                                     'Horizontal or Vertical
    piechart.LegendVisible = True                                                           'must the legend be visible?
    piechart.SumVisible = True                                                              'must the sum of the slices be visible?
   
    piechart.PlotContentSize = 0.4                                                          'how much of the plot area must be used for the chart
   
    'set the chart background color
    Dim plotBackGroundColorObject As JavaObject                                             'using JavaObject to pass an instance of java.awt.Color
    plotBackGroundColorObject.InitializeNewInstance("java.awt.Color", Array(Rnd(0,256).As(Int), Rnd(0,256).As(Int), Rnd(0,256).As(Int)))
    piechart.PlotBackgroundColor = plotBackGroundColorObject
   
    'set the border colors
    Dim plotBorderColorObject As JavaObject                                                 'using JavaObject to pass an instance of java.awt.Color
    plotBorderColorObject.InitializeNewInstance("java.awt.Color", Array(0,0,0))
    piechart.PlotBorderColor = plotBorderColorObject
   
    'set what to display insicde the slices
    piechart.LabelType = piechart.labelType_NameAndValue
    piechart.LabelsVisible = True
    piechart.PieSeriesRenderStyle = piechart.PieSeriesRenderStyle_Pie                     'make a pie chart. Change PieSeriesRenderStyle_pie to PieSeriesRenderStyle_Pie to draw a Pie chart
    piechart.donutTickness = 0.33
   
    'set the tect color of the labels
    Dim labelsFontColorObject As JavaObject
    labelsFontColorObject.InitializeNewInstance("java.awt.Color", Array(0, 0, 0))                'set the text color of the labels to black
    piechart.LabelsFontColor = labelsFontColorObject

    'position the labels - see the intellisense help when hovering the mouse on piechart.LabelsDistance
    piechart.LabelsDistance = 1.7
   
    'set the font of the pie slice labels using JavaObject -> java.awt.Font
    Dim fontType As JavaObject
    fontType.InitializeStatic("java.awt.Font")
    Dim labelFont As JavaObject
    labelFont.InitializeNewInstance("java.awt.Font", Array(fontType.GetField("DIALOG"), fontType.GetField("HANGING_BASELINE"), 12))   'MONOSPACED, SANS_SERIF, SERIF, DIALOG, DIALOG_INPUT
                                                                                                                                      'BOLD, PLAIN, CENTER_BASELINE, HANGING_BASELINE, ITALIC, TRUETYPE_FONT, TYPE1_FONT, ROMAN_BASELINE
    piechart.LabelsFont = labelFont
   
    'set the font of the chart title using JavaObject -> java.awt.Font
    Dim fontType As JavaObject
    fontType.InitializeStatic("java.awt.Font")
    Dim titleFont As JavaObject
    titleFont.InitializeNewInstance("java.awt.Font", Array(fontType.GetField("DIALOG"), fontType.GetField("ITALIC"), 18))           'MONOSPACED, SANS_SERIF, SERIF, DIALOG, DIALOG_INPUT
                                                                                                                                      'BOLD, PLAIN, CENTER_BASELINE, HANGING_BASELINE, ITALIC, TRUETYPE_FONT, TYPE1_FONT, ROMAN_BASELINE
    piechart.ChartTitleFont = titleFont
   
    'set the text color of the chart title, axes labes, and legend
    Dim chartFontColorObject As JavaObject
    chartFontColorObject.InitializeNewInstance("java.awt.Color", Array(25, 50, 200))
    piechart.ChartFontColor = chartFontColorObject
   
    'set the background color of the legend
    Dim legendBackgroundColorObject As JavaObject
    legendBackgroundColorObject.InitializeNewInstance("java.awt.Color", Array(200, 200, 0))
    piechart.LegendBackgroundColor = legendBackgroundColorObject
   
    'set the border color of the legend
    Dim legendBorderColorObject As JavaObject
    legendBorderColorObject.InitializeNewInstance("java.awt.Color", Array(0, 0, 0))
    piechart.LegendBorderColor = legendBorderColorObject
   
   
    'set the font of the legend using JavaObject -> java.awt.Font
    Dim fontType As JavaObject
    fontType.InitializeStatic("java.awt.Font")
    Dim legendFont As JavaObject
    legendFont.InitializeNewInstance("java.awt.Font", Array(fontType.GetField("SANS_SERIF"), fontType.GetField("ITALIC"), 15))           'MONOSPACED, SANS_SERIF, SERIF, DIALOG, DIALOG_INPUT
                                                                                                                                         'BOLD, PLAIN, CENTER_BASELINE, HANGING_BASELINE, ITALIC, TRUETYPE_FONT, TYPE1_FONT, ROMAN_BASELINE
    piechart.LegendFont = legendFont
       
    'set the background color of the title box
    Dim titleBackgroundColorObject As JavaObject
    titleBackgroundColorObject.InitializeNewInstance("java.awt.Color", Array(255, 0, 0))
    piechart.ChartTitleBoxBackgroundColor = titleBackgroundColorObject
   
    'set the border color of the title box
    Dim titleBoxBorderColorObject As JavaObject
    titleBoxBorderColorObject.InitializeNewInstance("java.awt.Color", Array(0, 0, 0))
    piechart.ChartTitleBoxBorderColor = titleBoxBorderColorObject
   
    piechart.ChartTitleBoxVisible = False
   
    Dim tooltipBorderColorObject As JavaObject
    tooltipBorderColorObject.InitializeNewInstance("java.awt.Color", Array(255, 0, 0))
    piechart.ToolTipBorderColor = tooltipBorderColorObject
   
    Dim tooltipBackgroundColorObject As JavaObject
    tooltipBackgroundColorObject.InitializeNewInstance("java.awt.Color", Array(255, 200, 200))
    piechart.ToolTipBackgroundColor = tooltipBackgroundColorObject
   
    'set the font of the tooltip
    Dim fontType As JavaObject
    fontType.InitializeStatic("java.awt.Font")
    Dim tooltipFontObject As JavaObject
    tooltipFontObject.InitializeNewInstance("java.awt.Font", Array(fontType.GetField("SANS_SERIF"), fontType.GetField("ITALIC"), 15))           'MONOSPACED, SANS_SERIF, SERIF, DIALOG, DIALOG_INPUT
                                                                                                                                                'BOLD, PLAIN, CENTER_BASELINE, HANGING_BASELINE, ITALIC, TRUETYPE_FONT, TYPE1_FONT, ROMAN_BASELINE
    piechart.ToolTipFont = tooltipFontObject
   
End Sub


public Sub pie_chartvalue_clicked (tooltip As Object)                    'This is an Event and is raised from class "piechart" - the data comes in as a List (passed to here as an object)

    Dim answer As String = tooltip
    Dim starting As Int = answer.IndexOf("'")
    Dim ending As Int = answer.IndexOf2("'", starting + 1)
    answer = answer.SubString2(starting+1, ending)
    Dim series() As String = Regex.Split(" ", answer)
    Dim name As String = series(0)
    Dim val As String  = series(1)
    val = val.Replace("(", "")
    val = val.Replace(")", "")
    val = val.Replace(",", ".")
   
    Dim valDouble As Double = val
   
    Log("PIE Series Name = " & name)
    Log("PIE Series Value = " & valDouble)
   
End Sub

public Sub donut_chartvalue_clicked (tooltip As Object)                  'This is an Event and is raised from class "piechart" - the data comes in as a List (passed to here as an object)

    Dim answer As String = tooltip
    Dim starting As Int = answer.IndexOf("'")
    Dim ending As Int = answer.IndexOf2("'", starting + 1)
    answer = answer.SubString2(starting+1, ending)
    Dim series() As String = Regex.Split(" ", answer)
    Dim name As String = series(0)
    Dim val As String  = series(1)
    val = val.Replace("(", "")
    val = val.Replace(")", "")
    val = val.Replace(",", ".")
   
    Dim valDouble As Double = val
   
    Log("DONUT Series Name = " & name)
    Log("DONUT Series Value = " & valDouble)
 
End Sub

public Sub pie_mouse_pressed (pressed As String)                

    Log("MOUSE PRESSED ON PIE CHART")
 
End Sub

public Sub donut_mouse_pressed (pressed As String)        

    Log("MOUSE PRESSED ON DONUT CHART")
 
End Sub

public Sub pie_mouse_released (pressed As String)                

    Log("MOUSE RELEASED ON PIE CHART")
 
End Sub

public Sub donut_mouse_released (pressed As String)        

    Log("MOUSE RELEASED ON DONUT CHART")
 
End Sub

public Sub pie_mouse_exited (pressed As String)                

    Log("MOUSE EXITED ON PIE CHART")
 
End Sub

public Sub donut_mouse_exited (pressed As String)        

    Log("MOUSE EXITED ON DONUT CHART")
 
End Sub

public Sub pie_mouse_entered (pressed As String)                

    Log("MOUSE ENTERED ON PIE CHART")
 
End Sub

public Sub donut_mouse_entered (pressed As String)        

    Log("MOUSE ENTERED ON DONUT CHART")
 
End Sub


Class Code:
Class Code:
Sub Class_Globals
    Private fx As JFX
   
    Private nativeMe, pc As JavaObject
   
    Public LPosition_InsideNW As Int
    Public LPosition_InsideNE As Int
    Public LPosition_InsideSE As Int
    Public LPosition_InsideSW As Int
    Public LPosition_InsideN As Int
    Public LPosition_InsideS As Int
    Public LPosition_OutsideS As Int
   
    Public LLayout_Horizontal As Int
    Public LLayout_Vertical As Int
   
'    Public ClockwiseDirection_Clockwise As Int
'    Public ClockwiseDirection_Counter_Clockwise As Int
   
    Public labelType_Value As Int
    Public labelType_Percentage As Int
    Public labelType_Name As Int
    Public labelType_NameAndPercentage As Int
    Public labelType_NameAndValue As Int
    Public PieSeriesRenderStyle_Donut As Int
    Public PieSeriesRenderStyle_Pie As Int
    Dim eventname As String
       
End Sub

'Initializes the object. You can add parameters to this method if needed.
Public Sub initializePieChart(e As String, wdth As Int, hght As Int, ttl As String)
   
    eventname = e
    LPosition_InsideNW = 1
    LPosition_InsideNE = 2
    LPosition_InsideSE = 3
    LPosition_InsideSW = 4
    LPosition_InsideN = 5
    LPosition_InsideS = 6
    LPosition_OutsideS = 7
   
    LLayout_Horizontal = 1
    LLayout_Vertical = 2
   
'    ClockwiseDirection_Clockwise = 1
'    ClockwiseDirection_Counter_Clockwise = 2
   
    labelType_Value = 1
    labelType_Percentage = 2
    labelType_Name = 3
    labelType_NameAndPercentage = 4
    labelType_NameAndValue = 5
   
    PieSeriesRenderStyle_Pie = 1
    PieSeriesRenderStyle_Donut = 2
       
    nativeMe = Me
   
    'In the line below --->>>> Main.obj1.GetModuleBA("Main") = get the BA of the Main module and pass it to the inline java code
    nativeMe.RunMethod("initPieChart", Array(Main.obj1.GetModuleBA("Main"), eventname, wdth, hght, ttl))
   
End Sub
 
'set the text color of the lables
public Sub setLabelsFontColor(labelFontColor As JavaObject)
   
    nativeMe.RunMethod("setLabelsFontColor", Array(labelFontColor))
   
End Sub

'set the label  type to display
'can be one of the following:
'    labelType_Value = 1
'    labelType_Percentage = 2
'    labelType_Name = 3
'    labelType_NameAndPercentage = 4
'    labelType_NameAndValue = 5
public Sub setLabelType(labeltype As Int)
   
    nativeMe.RunMethod("setLabelType", Array(labeltype))
End Sub

'draw the pie chart
public Sub drawPieChart As JavaObject
   
    pc = nativeMe.RunMethod("drawChart", Null)
    Return pc
   
End Sub

'set the graph to be circular or to follow the width/height of the pane
'the default value is true
public Sub setGraphCircular(circ As Boolean)
   
    nativeMe.RunMethod("setCircular", Array(circ))
   
End Sub

'add a series to the pie chart
public Sub addSeries(name As String, value As Double)
   
    nativeMe.RunMethod("addSeries", Array(name, value))
   
End Sub

'remove a series - pass the name of the series
public Sub removeSeries(name As String)
   
    nativeMe.RunMethod("removeSeries", Array(name))
   
End Sub

'set the colors of the pie slices
'<code>
'Dim slice1ColorObject, slice2ColorObject, slice3ColorObject As JavaObject   'using JavaObject to pass an instance of java.awt.Color for the pie slice colors
'  
'slice1ColorObject.InitializeNewInstance("java.awt.Color", Array(255, 0, 0))         'color of slice/series 1
'slice2ColorObject.InitializeNewInstance("java.awt.Color", Array(0, 255, 0))         'color of slice/series 2
'slice3ColorObject.InitializeNewInstance("java.awt.Color", Array(0, 0, 255))         'color of slice/series 3
'  
'Dim sliceColorArray As JavaObject
'sliceColorArray.InitializeArray("java.awt.Color", Array(slice1ColorObject, slice2ColorObject, slice3ColorObject))
'piechart.SliceColors = sliceColorArray
'</code>
public Sub setSliceColors(col As JavaObject)
   
    nativeMe.RunMethod("setSliceColors", Array(col))
   
End Sub

'set the start angle of the pie chart
'the default value is 0 degrees
public Sub setStartAngle(angle As Int)
    nativeMe.RunMethod("setStartAngle", Array(angle))
   
End Sub

'set tooltips enabled
'the default value is false
public Sub setToolTipsEnabled (tooltipEnabled As Boolean)
   
    nativeMe.RunMethod("setToolTipEnabled", Array(tooltipEnabled))
   
End Sub

'set the border width of the pie slices
'the default value is 10
public Sub setSliceBorderWidth (borderWidth As Int)
   
    nativeMe.RunMethod("setSliceBorderWidth", Array(borderWidth))
   
End Sub

'set the legend position
public Sub setLegendPosition(position As Int)
   
    nativeMe.RunMethod("setLegendPosition", Array(position))
   
End Sub

'set the layout of the Legend to Vertical or Horizontal
public Sub setLegendLayout(layout As Int)
   
    nativeMe.RunMethod("setLegendLayout", Array(layout))
   
End Sub

'make the legend visible / not visible
'default value is true
public Sub setLegendVisible(visible As Boolean)
   
    nativeMe.RunMethod("setLegendVisible", Array(visible))
   
End Sub

'set visibility of the sum of the slices
'default value is false
public Sub setSumVisible(sumVisible As Boolean)
   
    nativeMe.RunMethod("setSumVisible", Array(sumVisible))
   
End Sub

'Sets the content size of the plot inside the plot area of the chart. To fill the area 100%, use a value of 1.0.
'The default value is 0.9
public Sub setPlotContentSize (size As Double)
   
    nativeMe.RunMethod("setPlotContentSize", Array(size))
   
End Sub

'set the background color of the plot area
public Sub setPlotBackgroundColor(backColor As JavaObject)
   
    nativeMe.RunMethod("setPlotBackgroundColor", Array(backColor))
   
End Sub

'set the border color of the plot area
public Sub setPlotBorderColor(plotBorderColor As JavaObject)
   
    nativeMe.RunMethod("setPlotBorderColor", Array(plotBorderColor))
   
End Sub

'set the visibility of the labels
'default value is TRUE
public Sub setLabelsVisible(labelVisible As Boolean)
       
        nativeMe.RunMethod("setLabelsVisible", Array(labelVisible))
   
End Sub


'set the style to Pie or to Donut
'The default is Pie
public Sub setPieSeriesRenderStyle(style As Int)
   
    nativeMe.RunMethod("setPieSeriesRenderStyle", Array(style))
   
End Sub

'Set the thickness of the donut ring for donut style pie chart series.
'Valid range Is between 0 And 1
'Default value is 0.33
public Sub setDonutTickness(thick As Double)
   
    nativeMe.RunMethod("setDonutThickness", Array(thick))
   
End Sub

'update the value of a series
public Sub updatePieSeries(seriesName As String, seriesValue As Double)

    nativeMe.RunMethod("setUpdatePieSeries", Array(seriesName, seriesValue))
   
End Sub

'repaint the chart after an update of a series
public Sub repaintChart()
   
    nativeMe.RunMethod("repaintChart", Null)
End Sub

'Sets the distance of the pie chart's annotation where 0 is the center, 1 is at the edge and greater than 1 is outside of the pie chart.
Public Sub setLabelsDistance(distance As Double)
   
    nativeMe.RunMethod("setLabelsDistance", Array(distance))
   
End Sub

'set the font for the text of the labels
'<code>
'Dim fontType As JavaObject
'fontType.InitializeStatic("java.awt.Font")
'Dim labelFont As JavaObject
'labelFont.InitializeNewInstance("java.awt.Font", Array(fontType.GetField("DIALOG"), fontType.GetField("HANGING_BASELINE"), 12))   'MONOSPACED, SANS_SERIF, SERIF, DIALOG, DIALOG_INPUT
'                                                                                                                                  'BOLD, PLAIN, CENTER_BASELINE, HANGING_BASELINE, ITALIC, TRUETYPE_FONT, TYPE1_FONT, ROMAN_BASELINE
'piechart.LabelsFont = labelFont
'</code>
public Sub setLabelsFont(lblfnt As JavaObject)
   
    nativeMe.RunMethod("setLabelsFont", Array(lblfnt))
   
End Sub


'set the chart title font
public Sub setChartTitleFont(ttlfnt As JavaObject)
   
    nativeMe.RunMethod("setChartTitleFont", Array(ttlfnt))
   
End Sub

'Set the chart font color. Includes: Chart title, axes label, legend
Public Sub setChartFontColor(chartFontColor As JavaObject)
   
    nativeMe.RunMethod("setChartFontColor", Array(chartFontColor))
   
End Sub

'set the background color of the legend
Public Sub setLegendBackgroundColor(lbgc As JavaObject)
   
    nativeMe.RunMethod("setLegendBackgroundColor", Array(lbgc))
   
End Sub

'set the border color of the legend
Public Sub setLegendBorderColor(lbc As JavaObject)
   
    nativeMe.RunMethod("setLegendBorderColor", Array(lbc))
   
End Sub

'set the font of the legend
Public Sub setLegendFont(fnt As JavaObject)
   
    nativeMe.RunMethod("setLegendFont", Array(fnt))
   
End Sub

'set the background color of the title box
Public Sub setChartTitleBoxBackgroundColor(ctbbc As JavaObject)
    nativeMe.RunMethod("setChartTitleBoxBackgroundColor", Array(ctbbc))
End Sub

'set the color of the title box    border
Public Sub setChartTitleBoxBorderColor(ccbbc As JavaObject)
    nativeMe.RunMethod("setChartTitleBoxBorderColor", Array(ccbbc))
End Sub


'set the TitleBoxVisible to true / false
Public Sub setChartTitleBoxVisible(visible As Boolean)
   
    nativeMe.RunMethod("setChartTitleBoxVisible", Array(visible))
   
End Sub

'set the tooltip background color
Public Sub setToolTipBackgroundColor (ttbgc As JavaObject)
   
    nativeMe.RunMethod("setToolTipBackgroundColor", Array(ttbgc))
   
End Sub

'set the tooltip border color
Public Sub setToolTipBorderColor (ttbc As JavaObject)
   
    nativeMe.RunMethod("setToolTipBorderColor", Array(ttbc))
   
End Sub

'set the font of the tooltip
Public Sub setToolTipFont(fnt As JavaObject)
   
    nativeMe.RunMethod("setToolTipFont", Array(fnt))
   
End Sub

Private Sub myevent_fire (tooltipinfo As Object)
   
    Try
        CallSubDelayed2(Main, eventname & "_chartvalue_clicked", tooltipinfo)      'call/raise event in xxx_chartvalue_clicked of module Main
    Catch
        Log("ERROR = " & LastException)
    End Try
   
End Sub

Private Sub myevent_mouse_pressed (nothing As String)
   
    Try
        CallSubDelayed2(Main, eventname & "_mouse_pressed", nothing)      'call/raise event in xxx_chartvalue_clicked of module Main
    Catch
        Log("ERROR = " & LastException)
    End Try
   
End Sub

Private Sub myevent_mouse_released (nothing As String)
   
    Try
        CallSubDelayed2(Main, eventname & "_mouse_released", nothing)      'call/raise event in xxx_chartvalue_clicked of module Main
    Catch
        Log("ERROR = " & LastException)
    End Try
   
End Sub

Private Sub myevent_mouse_exited (nothing As String)
   
    Try
        CallSubDelayed2(Main, eventname & "_mouse_exited", nothing)      'call/raise event in xxx_chartvalue_clicked of module Main
    Catch
        Log("ERROR = " & LastException)
    End Try
   
End Sub

Private Sub myevent_mouse_entered (nothing As String)
   
    Try
        CallSubDelayed2(Main, eventname & "_mouse_entered", nothing)      'call/raise event in xxx_chartvalue_clicked of module Main
    Catch
        Log("ERROR = " & LastException)
    End Try
   
End Sub


'set the direction to draw the pie chart i.e clockwise or counter clockwise
'public Sub setClockwiseDirectionType(direction As Int)
'  
'    nativeMe.RunMethod("setClockwiseDirectionType", Array(direction))
'  
'End Sub


#If Java

import org.knowm.xchart.PieChart;
import org.knowm.xchart.PieChartBuilder;
import org.knowm.xchart.style.Styler;
import javafx.scene.Node;
import javax.swing.JPanel;
import java.awt.Color;

import org.knowm.xchart.QuickChart;
import org.knowm.xchart.XChartPanel;
import org.knowm.xchart.XYChart;
import org.knowm.xchart.XYChartBuilder;
import org.knowm.xchart.XYSeries.XYSeriesRenderStyle;
import org.knowm.xchart.internal.chartpart.Chart;
import org.knowm.xchart.style.Styler.LegendPosition;
import org.knowm.xchart.style.PieStyler.ClockwiseDirectionType;
import javafx.embed.swing.SwingNode;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.Pane;
import java.awt.Font;

import org.knowm.xchart.internal.chartpart.ToolTips;
import org.knowm.xchart.style.PieStyler.LabelType;
import org.knowm.xchart.PieSeries.PieSeriesRenderStyle;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseMotionAdapter;
import java.awt.event.MouseEvent;

import java.awt.event.MouseMotionListener;
import java.awt.event.MouseListener;
import anywheresoftware.b4a.keywords.Common;

    private BA ba;
    XChartPanel chartPanel;
    Color[] sliceColors;

    public void setSliceColors (Color[] sliceColors) {
        this.sliceColors = sliceColors;
        chart.getStyler().setSeriesColors(sliceColors);  
    }
   
    String oldstring = "";
       
    private int wdth = 400;
    private int hght = 400;
    private String graphTitle = "";

    private boolean circ = true;
    public void setCircular(boolean circ) {
        chart.getStyler().setCircular(circ);
    }  
   
    String EventName = "";
    PieChart chart;
    public void initPieChart(BA ba, String EventName,int wdth, int hght, String graphTitle) {
        this.EventName = EventName;
        chart = new PieChartBuilder().width(wdth).height(hght).title(graphTitle).build();
        this.ba = ba;
    }
   
    public Object drawChart() {
       
        SwingNode swingNode = new SwingNode();  
        chartPanel = new XChartPanel(chart);
        swingNode.setContent(chartPanel);  
        Pane pane = new Pane();
        pane.getChildren().add(swingNode);
           
        chartPanel.addMouseListener(new MouseAdapter() {

                @Override
                public void mousePressed(MouseEvent e) {
                    try {
                               _myevent_mouse_pressed("");     //TAKE NOTE OF THIS SUB BEING CALLED IN THE B4J CLASS ABOVE - NAMES MUST MATCH AND PREECEDED BY "_"
                           
                    }  catch (Exception fnuckup) {
                   
                              BA.Log("Fnuckup = " + fnuckup);
                    }                                  
                   
                    if (chartPanel.getToolTips().getSelectedTooltipData() != null) {
                       String myString = "" + chartPanel.getToolTips().getSelectedTooltipData();
                       if (!oldstring.equals(myString)) {
                               oldstring = myString;
                            try {
                                       _myevent_fire(myString);     //TAKE NOTE OF THIS SUB BEING CALLED IN THE B4J CLASS ABOVE - NAMES MUST MATCH AND PREECEDED BY "_"
                                   
                            }  catch (Exception fnuckup) {
                           
                                      BA.Log("Fnuckup = " + fnuckup);
                            }                                  
                        }  
                     }                        
                }

                @Override
                public void mouseReleased(MouseEvent e) {
                    try {
                               _myevent_mouse_released("");     //TAKE NOTE OF THIS SUB BEING CALLED IN THE B4J CLASS ABOVE - NAMES MUST MATCH AND PREECEDED BY "_"
                           
                    }  catch (Exception fnuckup) {
                   
                              BA.Log("Fnuckup = " + fnuckup);
                    }      
                }
               
                @Override
                public void mouseExited(MouseEvent e) {
                    try {
                               _myevent_mouse_exited("");     //TAKE NOTE OF THIS SUB BEING CALLED IN THE B4J CLASS ABOVE - NAMES MUST MATCH AND PREECEDED BY "_"
                           
                    }  catch (Exception fnuckup) {
                   
                              BA.Log("Fnuckup = " + fnuckup);
                    }      
                }
               
                @Override
                public void mouseEntered(MouseEvent e) {
                    try {
                               _myevent_mouse_entered("");     //TAKE NOTE OF THIS SUB BEING CALLED IN THE B4J CLASS ABOVE - NAMES MUST MATCH AND PREECEDED BY "_"
                           
                    }  catch (Exception fnuckup) {
                   
                              BA.Log("Fnuckup = " + fnuckup);
                    }      
                }

            });      
           
        return pane;  
    }  
   
    public void repaintChart() {
        chartPanel.repaint();
    }
   
    public void addSeries(String name, double value) {
        chart.addSeries(name, value);
    }
   
    public void removeSeries(String name) {
        chart.removeSeries(name);
    }  
   
    private int borderWidth = 10;
    public void setSliceBorderWidth (int borderWidth) {
        this.borderWidth = borderWidth;
        chart.getStyler().setSliceBorderWidth(borderWidth);
    }
   
    int layout = 1;
    public void setLegendLayout(int layout) {
        this.layout = layout;
        if (layout == 1) chart.getStyler().setLegendLayout(Styler.LegendLayout.Horizontal);
        if (layout == 2) chart.getStyler().setLegendLayout(Styler.LegendLayout.Vertical);
    }
   
    private boolean visible = true;
    public void setLegendVisible (boolean visible) {
        this.visible = visible;
        chart.getStyler().setLegendVisible(visible);
    }
   
    private boolean sumVisible = false;
    public void setSumVisible (boolean sumVisible) {
        this.sumVisible = sumVisible;
        chart.getStyler().setSumVisible(sumVisible);
    }
   
    private double plotContentSize = 0.9;
    public void setPlotContentSize(double plotContentSize) {
        this.plotContentSize = plotContentSize;
        chart.getStyler().setPlotContentSize(plotContentSize);  
    }
   
    public void setPlotBackgroundColor(Color backGroundColor) {
        chart.getStyler().setPlotBackgroundColor(backGroundColor);  
    }
   
    public void setPlotBorderColor(Color borderColor) {
        chart.getStyler().setPlotBorderColor(borderColor);  
    }
   
    public void setLabelType(int labelType) {
   
        if (labelType == 1) chart.getStyler().setLabelType(LabelType.Value);
        if (labelType == 2) chart.getStyler().setLabelType(LabelType.Percentage);
        if (labelType == 3) chart.getStyler().setLabelType(LabelType.Name);
        if (labelType == 4) chart.getStyler().setLabelType(LabelType.NameAndPercentage);
        if (labelType == 5) chart.getStyler().setLabelType(LabelType.NameAndValue);
    }
   
    //the clockwise method does not seem to do what it should do - it breaks the slices into smaller pieces......?
/*    public void setClockwiseDirectionType(int clockwise) {
        if (clockwise == 1) chart.getStyler().setClockwiseDirectionType(ClockwiseDirectionType.CLOCKWISE);
        if (clockwise == 2) chart.getStyler().setClockwiseDirectionType(ClockwiseDirectionType.COUNTER_CLOCKWISE);
    }
*/  
       
     public void setLabelsDistance(double distance) {
        chart.getStyler().setLabelsDistance(distance);
    }
 
     private int style = 1;
     public void setPieSeriesRenderStyle(int style) {
        if (style == 1) chart.getStyler().setDefaultSeriesRenderStyle(PieSeriesRenderStyle.Pie);
        if (style == 2) chart.getStyler().setDefaultSeriesRenderStyle(PieSeriesRenderStyle.Donut);
    }
   
      private double donutThickness = 0.33;
    public void setDonutThickness(double donutThickness) {
        this.donutThickness = donutThickness;
        chart.getStyler().setDonutThickness(donutThickness);
    }
   
    public void setUpdatePieSeries(String seriesName, Number value) {
        chart.updatePieSeries(seriesName, value);
    }
   
    public void setLabelsFontColor(Color labelsFontColor) {
        chart.getStyler().setLabelsFontColorAutomaticEnabled(false);
        chart.getStyler().setLabelsFontColor(labelsFontColor);  
    }
   
    public void setLabelsFont(Font labelFont) {
        chart.getStyler().setLabelsFont(labelFont);
    }
   
    public void setChartTitleFont(Font titleFont) {
        chart.getStyler().setChartTitleFont(titleFont);
       
    }  
   
    public void setChartTitleBoxVisible(boolean visible) {
        chart.getStyler().setChartTitleBoxVisible(visible);
    }
   
    public void setChartFontColor(Color chartFontColor) {
        chart.getStyler().setChartFontColor(chartFontColor);
    }
   
     private boolean labelVisible = true;
     public void setLabelsVisible(boolean labelVisible) {
        this.labelVisible = labelVisible;
        chart.getStyler().setLabelsVisible(labelVisible);
    }
   
    private int pos = 7;
    public void setLegendPosition(int pos) {
        this.pos = pos;
        if (pos == 1) chart.getStyler().setLegendPosition(Styler.LegendPosition.InsideNW);
        if (pos == 2) chart.getStyler().setLegendPosition(Styler.LegendPosition.InsideNE);
        if (pos == 3) chart.getStyler().setLegendPosition(Styler.LegendPosition.InsideSE);
        if (pos == 4) chart.getStyler().setLegendPosition(Styler.LegendPosition.InsideSW);
        if (pos == 5) chart.getStyler().setLegendPosition(Styler.LegendPosition.InsideN);
        if (pos == 6) chart.getStyler().setLegendPosition(Styler.LegendPosition.InsideS);
        if (pos == 7) chart.getStyler().setLegendPosition(Styler.LegendPosition.OutsideS);
       
    }
   
    private int startAngle = 0;
    public void setStartAngle(int startAngle) {
        this.startAngle = startAngle;
        chart.getStyler().setStartAngleInDegrees(startAngle);
    }
   
    private boolean tooltipEnabled = false;
    public void setToolTipEnabled(boolean tooltipEnabled) {
         this.tooltipEnabled = tooltipEnabled;
        chart.getStyler().setToolTipsEnabled(tooltipEnabled);  
    }
   
    public void setLegendBackgroundColor(Color lbgc) {
        chart.getStyler().setLegendBackgroundColor(lbgc);
    }
   
    public void setLegendBorderColor(Color lbc) {
        chart.getStyler().setLegendBorderColor(lbc);
    }
   
    public void setLegendFont(Font legendFont) {
        chart.getStyler().setLegendFont(legendFont);
    }  
   
    public void setChartTitleBoxBackgroundColor(Color ctbbc) {
        chart.getStyler().setChartTitleBoxBackgroundColor(ctbbc);
    }
   
    public void setChartTitleBoxBorderColor(Color ccbbc) {
        chart.getStyler().setChartTitleBoxBorderColor(ccbbc);
    }
   
    public void setToolTipBackgroundColor(Color ttbgc) {
        chart.getStyler().setToolTipBackgroundColor(ttbgc);
    }
   
    public void setToolTipBorderColor(Color ttbc) {
        chart.getStyler().setToolTipBorderColor(ttbc);
    }
   
    public void setToolTipFont(Font toolTipFont) {
        chart.getStyler().setToolTipFont(toolTipFont);
    }      
   
#End If
 

Attachments

  • b4jXchartPie.zip
    9.1 KB · Views: 93
  • JHSxChart.jar
    300.3 KB · Views: 91
Last edited:

Johan Schoeman

Expert
Licensed User
Longtime User
A first dig at Real Time Line Chart. Use the same JAR as in post #9 of this thread.
The project uses a mix of B4J, inline java code, and javaobject

RTLC.gif


B4J Main:
#Region Project Attributes
    #MainFormWidth: 650
    #MainFormHeight: 500

#End Region

#AdditionalJar: JHSxChart
'#VirtualMachineArgs: -Dprism.order=sw

Sub Process_Globals
    
    Private fx As JFX
    Private MainForm As Form
    Private xui As XUI
    Dim llc As Pane            'this is and instance of the pie/donut chart object that will be added to the RootPane of the Mainform

    Dim liveLC As xChartLive
    Dim t As Timer
    
    Dim phase As Double
    Dim xdata(100) As Double
    Dim ydata(100) As Double
    
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    
    phase = 0
    t.Initialize("t", 200)                                                   'just for demo purposes
    MainForm = Form1
    
    MainForm.RootPane.LoadLayout("main")
    MainForm.Show

    For i = 0 To xdata.Length -1
        xdata(i) = (2 * cPI / xdata.length * i)
        ydata(i) = Sin(xdata(i))
    Next
    
    llc = liveLC.Initialize("Real Time Line Chart", "Radians", "Sine", "sine", xdata, ydata)
    
    MainForm.RootPane.AddNode(llc, 10, 10, 350, 350)
    
    'set the font of the chart title using JavaObject -> java.awt.Font
    Dim fontType As JavaObject
    fontType.InitializeStatic("java.awt.Font")
    Dim titleFont As JavaObject
    titleFont.InitializeNewInstance("java.awt.Font", Array(fontType.GetField("SANS_SERIF"), fontType.GetField("BOLD"), 18))           'MONOSPACED, SANS_SERIF, SERIF, DIALOG, DIALOG_INPUT
                                                                                                                                      'BOLD, PLAIN, CENTER_BASELINE, HANGING_BASELINE, ITALIC, TRUETYPE_FONT, TYPE1_FONT, ROMAN_BASELINE
    liveLC.ChartTitleFont = titleFont
    
    
    'set the chart background color
    Dim plotBackGroundColorObject As JavaObject                                             'using JavaObject to pass an instance of java.awt.Color
    plotBackGroundColorObject.InitializeNewInstance("java.awt.Color", Array(255, 255, 255))
    liveLC.PlotBackgroundColor = plotBackGroundColorObject
    
    'set the border colors
    Dim plotBorderColorObject As JavaObject                                                 'using JavaObject to pass an instance of java.awt.Color
    plotBorderColorObject.InitializeNewInstance("java.awt.Color", Array(0,0,0))
    liveLC.PlotBorderColor = plotBorderColorObject


    'set the text color of the labels
    Dim labelsFontColorObject As JavaObject
    labelsFontColorObject.InitializeNewInstance("java.awt.Color", Array(255, 0, 0))                'set the text color of the labels to black
    liveLC.AxisTickLabelsColor = labelsFontColorObject
    
    
    'set the font of the chart axis titles using JavaObject -> java.awt.Font
    Dim fontType As JavaObject
    fontType.InitializeStatic("java.awt.Font")
    Dim titleFont As JavaObject
    titleFont.InitializeNewInstance("java.awt.Font", Array(fontType.GetField("SANS_SERIF"), fontType.GetField("ITALIC"), 15))         'MONOSPACED, SANS_SERIF, SERIF, DIALOG, DIALOG_INPUT
                                                                                                                                      'BOLD, PLAIN, CENTER_BASELINE, HANGING_BASELINE, ITALIC, TRUETYPE_FONT, TYPE1_FONT, ROMAN_BASELINE
    liveLC.AxisTitleFont = titleFont

    'set the font of the chart axis tick labels using JavaObject -> java.awt.Font
    Dim fontType As JavaObject
    fontType.InitializeStatic("java.awt.Font")
    Dim tickLabelsFont As JavaObject
    tickLabelsFont.InitializeNewInstance("java.awt.Font", Array(fontType.GetField("SERIF"), fontType.GetField("HANGING_BASELINE"), 10))              'MONOSPACED, SANS_SERIF, SERIF, DIALOG, DIALOG_INPUT
                                                                                                                                                     'BOLD, PLAIN, CENTER_BASELINE, HANGING_BASELINE, ITALIC, TRUETYPE_FONT, TYPE1_FONT, ROMAN_BASELINE
    liveLC.AxisTickLabelsFont = tickLabelsFont
    
    'set the text color of the labels
    Dim tickColorObject As JavaObject
    tickColorObject.InitializeNewInstance("java.awt.Color", Array(0,255, 255))                'set the text color of the labels to black
    liveLC.AxisTickMarksColor = tickColorObject
    
    liveLC.LegendPosition = liveLC.LPosition_OutsideS
    
    liveLC.LegendLayout = liveLC.LLayout_Horizontal
    liveLC.LegendVisible = True
    
    
    'set the background color of the legend
    Dim legendBackgroundColorObject As JavaObject
    legendBackgroundColorObject.InitializeNewInstance("java.awt.Color", Array(200, 200, 0))
    liveLC.LegendBackgroundColor = legendBackgroundColorObject
    
    'set the border color of the legend
    Dim legendBorderColorObject As JavaObject
    legendBorderColorObject.InitializeNewInstance("java.awt.Color", Array(0, 0, 0))
    liveLC.LegendBorderColor = legendBorderColorObject
    
    
    'set the font of the legend using JavaObject -> java.awt.Font
    Dim fontType As JavaObject
    fontType.InitializeStatic("java.awt.Font")
    Dim legendFont As JavaObject
    legendFont.InitializeNewInstance("java.awt.Font", Array(fontType.GetField("SANS_SERIF"), fontType.GetField("ITALIC"), 15))           'MONOSPACED, SANS_SERIF, SERIF, DIALOG, DIALOG_INPUT
                                                                                                                                         'BOLD, PLAIN, CENTER_BASELINE, HANGING_BASELINE, ITALIC, TRUETYPE_FONT, TYPE1_FONT, ROMAN_BASELINE
    liveLC.LegendFont = legendFont
    
    liveLC.PlotContentSize = 0.99
    
    liveLC.ToolTipsEnabled = True
    
    'set the background color of the title box
    Dim titleBackgroundColorObject As JavaObject
    titleBackgroundColorObject.InitializeNewInstance("java.awt.Color", Array(255, 0, 0))
    liveLC.ChartTitleBoxBackgroundColor = titleBackgroundColorObject
    
    'set the border color of the title box
    Dim titleBoxBorderColorObject As JavaObject
    titleBoxBorderColorObject.InitializeNewInstance("java.awt.Color", Array(0, 0, 0))
    liveLC.ChartTitleBoxBorderColor = titleBoxBorderColorObject
        
    liveLC.ChartTitleBoxVisible = True
    
    Dim tooltipBorderColorObject As JavaObject
    tooltipBorderColorObject.InitializeNewInstance("java.awt.Color", Array(255, 0, 0))
    liveLC.ToolTipBorderColor = tooltipBorderColorObject
    
    Dim tooltipBackgroundColorObject As JavaObject
    tooltipBackgroundColorObject.InitializeNewInstance("java.awt.Color", Array(255, 200, 200))
    liveLC.ToolTipBackgroundColor = tooltipBackgroundColorObject
    
    'set the font of the tooltip
    Dim fontType As JavaObject
    fontType.InitializeStatic("java.awt.Font")
    Dim tooltipFontObject As JavaObject
    tooltipFontObject.InitializeNewInstance("java.awt.Font", Array(fontType.GetField("SANS_SERIF"), fontType.GetField("ITALIC"), 15))           'MONOSPACED, SANS_SERIF, SERIF, DIALOG, DIALOG_INPUT
                                                                                                                                                'BOLD, PLAIN, CENTER_BASELINE, HANGING_BASELINE, ITALIC, TRUETYPE_FONT, TYPE1_FONT, ROMAN_BASELINE
    liveLC.ToolTipFont = tooltipFontObject
    
    'set the text color of the chart title, axes labes, and legend
    Dim chartFontColorObject As JavaObject
    chartFontColorObject.InitializeNewInstance("java.awt.Color", Array(25, 50, 200))
    liveLC.ChartFontColor = chartFontColorObject
    
    liveLC.XAxisDecimalPattern = "###.###"
    
    liveLC.XAxisLabelRotation = 90
    
    liveLC.XAxisTicksVisible = True
    liveLC.YAxisTicksVisible = True
    
    liveLC.AxisTickMarkLength = 5
    
    liveLC.PlotMargin = 0
    
    
    'The below 3 lines of code is for my laptop with Windows 10 else the graph will not display - seems to be working without these lines of code on later versions of Windows....?
    Sleep(300)
    MainForm.WindowLeft = MainForm.WindowLeft - 1
    MainForm.WindowLeft = MainForm.WindowLeft + 1
    
    t.Enabled = True
    
End Sub


'change the value(s) of the series every xxx milliseconds amd repaint the chart to see the updated chart
Sub t_tick
    
    Dim radians As Double
    phase = phase + (2 * cPI * 2 / 20.0)
    
    For i = 0 To xdata.length - 1
        radians = phase + (2 * cPI / xdata.length * i)
        
        xdata(i) = radians
        ydata(i) = Sin(radians)
    Next
    
    liveLC.updateLC (xdata, ydata)
    
End Sub

xChartLive Class:
Sub Class_Globals
    
    Private fx As JFX
    Private nativeMe, lc As JavaObject
    
    Public LPosition_InsideNW As Int
    Public LPosition_InsideNE As Int
    Public LPosition_InsideSE As Int
    Public LPosition_InsideSW As Int
    Public LPosition_InsideN As Int
    Public LPosition_InsideS As Int
    Public LPosition_OutsideS As Int
    
    Public LLayout_Horizontal As Int
    Public LLayout_Vertical As Int
    
End Sub

'Initializes the object
'title = Chart Title
'xAxisTitle = X-Axis Title
'yAxisTitle = Y-Axis Title
'seriesName = name of the series thta will be plotted
'xdata() = array of double with x-axis values to be plotted
'ydata() = array of double with y-axis values to be plotted
Public Sub Initialize (title As String, xAxisTitle As String, yAxisTitle As String, seriesName As String, xdata() As Double, ydata() As Double) As JavaObject

    LPosition_InsideNW = 1
    LPosition_InsideNE = 2
    LPosition_InsideSE = 3
    LPosition_InsideSW = 4
    LPosition_InsideN = 5
    LPosition_InsideS = 6
    LPosition_OutsideS = 7
    
    LLayout_Horizontal = 1
    LLayout_Vertical = 2
    
    nativeMe = Me
    lc = nativeMe.RunMethod("initRTLC", Array(title, xAxisTitle, yAxisTitle, seriesName, xdata, ydata))
    Return lc
    
End Sub


public Sub updateLC(xdata() As Double, ydata() As Double)
    
    nativeMe.RunMethod("updateLC", Array(xdata, ydata))
    
End Sub

'set the chart title font
public Sub setChartTitleFont(ttlfnt As JavaObject)
    
    nativeMe.RunMethod("setChartTitleFont", Array(ttlfnt))
    
End Sub

'set the font of the axis tick labels
public Sub setAxisTickLabelsFont(ttlfnt As JavaObject)
    
    nativeMe.RunMethod("setAxisTickLabelsFont", Array(ttlfnt))
    
End Sub

'set the chart axis title font
public Sub setAxisTitleFont(ttlfnt As JavaObject)
    
    nativeMe.RunMethod("setAxisTitleFont", Array(ttlfnt))
    
End Sub

'set the background color of the plot area
public Sub setPlotBackgroundColor(backColor As JavaObject)
    
    nativeMe.RunMethod("setPlotBackgroundColor", Array(backColor))
    
End Sub

'set the border color of the plot area
public Sub setPlotBorderColor(plotBorderColor As JavaObject)
    
    nativeMe.RunMethod("setPlotBorderColor", Array(plotBorderColor))
    
End Sub

'set the text color of the lables
public Sub setAxisTickLabelsColor(labelFontColor As JavaObject)
    
    nativeMe.RunMethod("setAxisTickLabelsColor", Array(labelFontColor))
    
End Sub

'set the axis tick mark colors
public Sub setAxisTickMarksColor(labelFontColor As JavaObject)
    
    nativeMe.RunMethod("setAxisTickMarksColor", Array(labelFontColor))
    
End Sub

'set the legend position
public Sub setLegendPosition(position As Int)
    
    nativeMe.RunMethod("setLegendPosition", Array(position))
    
End Sub

'set the layout of the Legend to Vertical or Horizontal
public Sub setLegendLayout(layout As Int)
    
    nativeMe.RunMethod("setLegendLayout", Array(layout))
    
End Sub

'make the legend visible / not visible
'default value is true
public Sub setLegendVisible(visible As Boolean)
    
    nativeMe.RunMethod("setLegendVisible", Array(visible))
    
End Sub

'set the background color of the legend
Public Sub setLegendBackgroundColor(lbgc As JavaObject)
    
    nativeMe.RunMethod("setLegendBackgroundColor", Array(lbgc))
    
End Sub

'set the border color of the legend
Public Sub setLegendBorderColor(lbc As JavaObject)
    
    nativeMe.RunMethod("setLegendBorderColor", Array(lbc))
    
End Sub

'set the font of the legend
Public Sub setLegendFont(fnt As JavaObject)
    
    nativeMe.RunMethod("setLegendFont", Array(fnt))
    
End Sub

'Sets the content size of the plot inside the plot area of the chart. To fill the area 100%, use a value of 1.0.
'The default value is 0.9
public Sub setPlotContentSize (size As Double)
    
    nativeMe.RunMethod("setPlotContentSize", Array(size))
    
End Sub

'set the background color of the title box
Public Sub setChartTitleBoxBackgroundColor(ctbbc As JavaObject)
    nativeMe.RunMethod("setChartTitleBoxBackgroundColor", Array(ctbbc))
End Sub

'set the color of the title box    border
Public Sub setChartTitleBoxBorderColor(ccbbc As JavaObject)
    nativeMe.RunMethod("setChartTitleBoxBorderColor", Array(ccbbc))
End Sub


'set the TitleBoxVisible to true / false
Public Sub setChartTitleBoxVisible(visible As Boolean)
    
    nativeMe.RunMethod("setChartTitleBoxVisible", Array(visible))
    
End Sub

'set the tooltip background color
Public Sub setToolTipBackgroundColor (ttbgc As JavaObject)
    
    nativeMe.RunMethod("setToolTipBackgroundColor", Array(ttbgc))
    
End Sub

'set the tooltip border color
Public Sub setToolTipBorderColor (ttbc As JavaObject)
    
    nativeMe.RunMethod("setToolTipBorderColor", Array(ttbc))
    
End Sub

'set the font of the tooltip
Public Sub setToolTipFont(fnt As JavaObject)
    
    nativeMe.RunMethod("setToolTipFont", Array(fnt))
    
End Sub

'set tooltips enabled
'the default value is false
public Sub setToolTipsEnabled (tooltipEnabled As Boolean)
    
    nativeMe.RunMethod("setToolTipEnabled", Array(tooltipEnabled))
    
End Sub

'Set the chart font color. Includes: Chart title, axes label, legend
Public Sub setChartFontColor(chartFontColor As JavaObject)
    
    nativeMe.RunMethod("setChartFontColor", Array(chartFontColor))
    
End Sub

'Set the decimal pattern of the X-Axis values
Public Sub setXAxisDecimalPattern(pattern As String)
    
    nativeMe.RunMethod("setXAxisDecimalPattern", Array(pattern))
    
End Sub

'Set the decimal pattern of the X-Axis values
Public Sub setYAxisDecimalPattern(pattern As String)
    
    nativeMe.RunMethod("setYAxisDecimalPattern", Array(pattern))
    
End Sub

'Set the angle/rotation of the x-axis labels
Public Sub setXAxisLabelRotation(angle As Int)
    
    nativeMe.RunMethod("setXAxisLabelRotation", Array(angle))
    
End Sub

'Set the visibility of the X-Axis ticks
Public Sub setXAxisTicksVisible(visible As Boolean)
    
    nativeMe.RunMethod("setXAxisTicksVisible", Array(visible))
    
End Sub

'Set the visibility of the Y-Axis ticks
Public Sub setYAxisTicksVisible(visible As Boolean)
    
    nativeMe.RunMethod("setYAxisTicksVisible", Array(visible))
    
End Sub

'Set the length of the tick marks in pixels
Public Sub setAxisTickMarkLength(tl As Int)
    
    nativeMe.RunMethod("setAxisTickMarkLength", Array(tl))
    
End Sub


Public Sub setAxisTicksLineVisible(vis As Boolean)
    
    nativeMe.RunMethod("setAxisTicksLineVisible", Array(vis))
    
End Sub

'sets the margin (in pixels) around the plot area
Public Sub setPlotMargin(margin As Int)
    
    nativeMe.RunMethod("setPlotMargin", Array(margin))
    
End Sub


'set visibility of the X-Axis title
Public Sub setXAxisTitleVisible(vis As Boolean)
    
    nativeMe.RunMethod("setXAxisTitleVisible", Array(vis))
    
End Sub


'set visibility of the Y-Axis title
Public Sub setYAxisTitleVisible(vis As Boolean)
    
    nativeMe.RunMethod("setYAxisTitleVisible", Array(vis))
    
End Sub


#if Java


import org.knowm.xchart.QuickChart;
import org.knowm.xchart.SwingWrapper;
import org.knowm.xchart.XYChart;
import javafx.embed.swing.SwingNode;
import org.knowm.xchart.XChartPanel;
import javafx.scene.layout.Pane;

import java.awt.Font;
import java.awt.Color;
import org.knowm.xchart.style.Styler.LegendPosition;
import org.knowm.xchart.style.Styler;


  XChartPanel chartPanel;
  XYChart chart;


  public Object initRTLC(String title, String xAxisTitle, String yAxisTitle, String seriesName, double[] xdata, double[] ydata) {
 
      chart =
        QuickChart.getChart(
            title, xAxisTitle, yAxisTitle, seriesName, xdata, ydata);
            

      SwingNode swingNode = new SwingNode();   
      chartPanel = new XChartPanel(chart);
      swingNode.setContent(chartPanel);   
      Pane pane = new Pane();
      pane.getChildren().add(swingNode);

      return pane;
 
  }

  public void updateLC(double[] xdata, double[] ydata) {

    chart.updateXYSeries("sine", xdata, ydata, null);   
    chartPanel.repaint();

  }   
 
      public void setChartTitleFont(Font titleFont) {
        chart.getStyler().setChartTitleFont(titleFont);
        
    }   
    
      public void setAxisTitleFont(Font titleFont) {
        chart.getStyler().setAxisTitleFont(titleFont);
        
    }       
    
      public void setAxisTickLabelsFont(Font titleFont) {
        chart.getStyler().setAxisTickLabelsFont(titleFont);
        
    }       
    
    public void setPlotBackgroundColor(Color backGroundColor) {
        chart.getStyler().setPlotBackgroundColor(backGroundColor);   
    }   
    
    public void setPlotBorderColor(Color borderColor) {
        chart.getStyler().setPlotBorderColor(borderColor);   
    }
    
    public void setAxisTickMarksColor(Color borderColor) {
        chart.getStyler().setAxisTickMarksColor(borderColor);   
    }   
    
    private int pos = 7;
    public void setLegendPosition(int pos) {
        this.pos = pos;
        if (pos == 1) chart.getStyler().setLegendPosition(Styler.LegendPosition.InsideNW);
        if (pos == 2) chart.getStyler().setLegendPosition(Styler.LegendPosition.InsideNE);
        if (pos == 3) chart.getStyler().setLegendPosition(Styler.LegendPosition.InsideSE);
        if (pos == 4) chart.getStyler().setLegendPosition(Styler.LegendPosition.InsideSW);
        if (pos == 5) chart.getStyler().setLegendPosition(Styler.LegendPosition.InsideN);
        if (pos == 6) chart.getStyler().setLegendPosition(Styler.LegendPosition.InsideS);
        if (pos == 7) chart.getStyler().setLegendPosition(Styler.LegendPosition.OutsideS);
        
    }   
    
    int layout = 1;
    public void setLegendLayout(int layout) {
        this.layout = layout;
        if (layout == 1) chart.getStyler().setLegendLayout(Styler.LegendLayout.Horizontal);
        if (layout == 2) chart.getStyler().setLegendLayout(Styler.LegendLayout.Vertical);
    }
    
    private boolean visible = true;
    public void setLegendVisible (boolean visible) {
        this.visible = visible;
        chart.getStyler().setLegendVisible(visible);
    }   
    
    
    public void setLegendBackgroundColor(Color lbgc) {
        chart.getStyler().setLegendBackgroundColor(lbgc);
    }
    
    public void setLegendBorderColor(Color lbc) {
        chart.getStyler().setLegendBorderColor(lbc);
    }
    
    public void setLegendFont(Font legendFont) {
        chart.getStyler().setLegendFont(legendFont);
    }   
    
    private double plotContentSize = 0.9;
    public void setPlotContentSize(double plotContentSize) {
        this.plotContentSize = plotContentSize;
        chart.getStyler().setPlotContentSize(plotContentSize);   
    }   
    
    public void setChartTitleBoxBackgroundColor(Color ctbbc) {
        chart.getStyler().setChartTitleBoxBackgroundColor(ctbbc);
    }
    
    public void setChartTitleBoxBorderColor(Color ccbbc) {
        chart.getStyler().setChartTitleBoxBorderColor(ccbbc);
    }
    
    public void setChartTitleBoxVisible(boolean visible) {
        chart.getStyler().setChartTitleBoxVisible(visible);
    }   
    
    public void setToolTipBackgroundColor(Color ttbgc) {
        chart.getStyler().setToolTipBackgroundColor(ttbgc);
    }
    
    public void setToolTipBorderColor(Color ttbc) {
        chart.getStyler().setToolTipBorderColor(ttbc);
    }
    
    public void setToolTipFont(Font toolTipFont) {
        chart.getStyler().setToolTipFont(toolTipFont);
    }   
    
    private boolean tooltipEnabled = false;
    public void setToolTipEnabled(boolean tooltipEnabled) {
         this.tooltipEnabled = tooltipEnabled;
        chart.getStyler().setToolTipsEnabled(tooltipEnabled);   
    }   
    
    public void setChartFontColor(Color chartFontColor) {
        chart.getStyler().setChartFontColor(chartFontColor);
    }   
    
    public void setXAxisDecimalPattern(String pattern) {
        chart.getStyler().setXAxisDecimalPattern(pattern);
    }       
    
    public void setYAxisDecimalPattern(String pattern) {
        chart.getStyler().setYAxisDecimalPattern(pattern);
        chartPanel.repaint();
    }   
    
    public void setXAxisLabelRotation(int angle) {
        chart.getStyler().setXAxisLabelRotation(angle);
    }
    
    public void setXAxisTicksVisible(boolean visible) {
        chart.getStyler().setXAxisTicksVisible(visible);
    }
    
    public void setYAxisTicksVisible(boolean visible) {
        chart.getStyler().setYAxisTicksVisible(visible);
    }
    
    public void setAxisTickMarkLength(int tl) {
        chart.getStyler().setAxisTickMarkLength(tl);    //in pixels
    }
    
    public void setAxisTicksLineVisible(boolean vis) {
        chart.getStyler().setAxisTicksLineVisible(vis);
    }
    
    public void setPlotMargin(int margin) {
        chart.getStyler().setPlotMargin(margin);   //in pixels
    }
    
    public void setYAxisTitleVisible(boolean vis) {
        chart.getStyler().setYAxisTitleVisible(true);
        chartPanel.repaint();
    }
    
    public void setXAxisTitleVisible(boolean vis) {
        chart.getStyler().setYAxisTitleVisible(true);
        chartPanel.repaint();
    }   
        
    
    public void setAxisTickLabelsColor(Color labelsFontColor) {
        chart.getStyler().setAxisTickLabelsColor(labelsFontColor);   
    }       

#End If
 

Attachments

  • b4jXchartLineRT.zip
    5.7 KB · Views: 90

Johan Schoeman

Expert
Licensed User
Longtime User
This is a "wrap" for the LineChart of this Github Project. Basically you only need a timer to change between a static and dynamic line chart. Attached a sample of a dynamic chart and a sample of the static chart.

Use the same JAR as in post #9 of this thread (have attached it again - copy it to your B4J additional Libs folder).

All done by using JavaObject - have added as many "methods" as possible to make it useable.

Dynamic Line Chart:
linechartDynamic.gif


Static Line Chart:
1685181483162.png


B4J Code for Dynamic Chart:
#Region Project Attributes
    #MainFormWidth: 650
    #MainFormHeight: 520

#End Region

#AdditionalJar: JHSxChart

Sub Process_Globals
    
    Private fx As JFX
    Private MainForm As Form
    Private xui As XUI
    Dim series As JavaObject
    
    Dim phase As Double
    
    Dim t As Timer
    
    Dim size As Int = 50        'number of points to display for each series
    
    Dim serie1Xdata(size), series1Ydata(size) As Double
    Dim series2Xdata(size), series2Ydata(size) As Double
    Dim chart As JavaObject
    Dim chartPanel As JavaObject

End Sub

Sub AppStart (Form1 As Form, Args() As String)
    
    MainForm = Form1
    
    MainForm.RootPane.LoadLayout("main")
    MainForm.Show
    
    phase = 0
    
    t.Initialize("t", 100)
    
    Dim XYChartBuilder As JavaObject
    XYChartBuilder.InitializeNewInstance("org.knowm.xchart.XYChartBuilder", Null)
    XYChartBuilder.RunMethodJO("width", Array(630)).RunMethodJO("height", Array(500)).RunMethodJO("title", Array("Line Chart - Dynamic")).RunMethodJO("build", Null)
    

    chart.InitializeNewInstance("org.knowm.xchart.XYChart", Array(XYChartBuilder))
    
    chart.RunMethodJO("getStyler", Null).RunMethodJO("setChartTitleVisible", Array(True))
    chart.RunMethodJO("getStyler", Null).RunMethodJO("setLegendVisible", Array(True))
    
    chart.RunMethodJO("getStyler", Null).RunMethod("setLegendPosition", Array(Constants.LegendPosition_OutsideS))
    chart.RunMethodJO("getStyler", Null).RunMethod("setLegendLayout", Array(Constants.LegendLayout_Horizontal))
    
    chart.RunMethodJO("getStyler", Null).RunMethod("setXAxisLabelRotation", Array(45))


    chart.RunMethodJO("getStyler", Null).RunMethod("setDecimalPattern", Array("#0.00"))
    
    chart.RunMethodJO("getStyler", Null).RunMethodJO("setToolTipsEnabled", Array(True))
    
    chart.RunMethodJO("getStyler", Null).RunMethodJO("setToolTipsAlwaysVisible", Array(False))
    
    Dim fontType As JavaObject
    fontType.InitializeStatic("java.awt.Font")
    Dim tooltipFont As JavaObject
    tooltipFont.InitializeNewInstance("java.awt.Font", Array(fontType.GetField("DIALOG"), fontType.GetField("BOLD"), 12))                'MONOSPACED, SANS_SERIF, SERIF, DIALOG, DIALOG_INPUT
                                                                                                                                           'BOLD, PLAIN, CENTER_BASELINE, HANGING_BASELINE, ITALIC, TRUETYPE_FONT, TYPE1_FONT, ROMAN_BASELINE
    chart.RunMethodJO("getStyler", Null).RunMethodJO("setToolTipFont", Array(tooltipFont))
    
    Dim tooltipHighlightColor As JavaObject
    tooltipHighlightColor.InitializeStatic("java.awt.Color")
    chart.RunMethodJO("getStyler", Null).RunMethodJO("setToolTipHighlightColor", Array(tooltipHighlightColor.GetField("CYAN")))

    Dim tooltipBorderColor As JavaObject
    tooltipBorderColor.InitializeStatic("java.awt.Color")
    chart.RunMethodJO("getStyler", Null).RunMethodJO("setToolTipBorderColor", Array(tooltipBorderColor.GetField("BLACK")))
    
    Dim tooltipBackgroundColor As JavaObject
    tooltipBackgroundColor.InitializeStatic("java.awt.Color")
    chart.RunMethodJO("getStyler", Null).RunMethodJO("setToolTipBackgroundColor", Array(tooltipBackgroundColor.GetField("LIGHT_GRAY")))
    
    
    Dim plotBackgroundColor As JavaObject
    plotBackgroundColor.InitializeStatic("java.awt.Color")
    chart.RunMethodJO("getStyler", Null).RunMethodJO("setPlotBackgroundColor", Array(plotBackgroundColor.GetField("LIGHT_GRAY")))
    
    Dim plotGridLinesColor As JavaObject
    plotGridLinesColor.InitializeStatic("java.awt.Color")
    chart.RunMethodJO("getStyler", Null).RunMethodJO("setPlotGridLinesColor", Array(plotGridLinesColor.GetField("GRAY")))
    
    Dim chartBackgroundColor As JavaObject
    chartBackgroundColor.InitializeStatic("java.awt.Color")
    chart.RunMethodJO("getStyler", Null).RunMethodJO("setChartBackgroundColor", Array(chartBackgroundColor.GetField("RED")))
    
    Dim chartLegendBackgroundColor As JavaObject
    chartLegendBackgroundColor.InitializeStatic("java.awt.Color")
    chart.RunMethodJO("getStyler", Null).RunMethodJO("setLegendBackgroundColor", Array(chartLegendBackgroundColor.GetField("YELLOW")))
    
    Dim chartFontColor As JavaObject
    chartFontColor.InitializeStatic("java.awt.Color")
    chart.RunMethodJO("getStyler", Null).RunMethodJO("setChartFontColor", Array(chartFontColor.GetField("BLUE")))
    
    Dim chartTitleBoxBackgroundColor As JavaObject
    chartTitleBoxBackgroundColor.InitializeStatic("java.awt.Color")
    chart.RunMethodJO("getStyler", Null).RunMethodJO("setChartTitleBoxBackgroundColor", Array(chartTitleBoxBackgroundColor.GetField("CYAN")))
    
    chart.RunMethodJO("getStyler", Null).RunMethodJO("setChartTitleBoxVisible", Array(True))
    
    Dim chartTitleBoxBorderColor As JavaObject
    chartTitleBoxBorderColor.InitializeStatic("java.awt.Color")
    chart.RunMethodJO("getStyler", Null).RunMethodJO("setChartTitleBoxBorderColor", Array(chartTitleBoxBorderColor.GetField("BLACK")))
        
    chart.RunMethodJO("getStyler", Null).RunMethodJO("setPlotGridLinesVisible", Array(True))

    chart.RunMethodJO("getStyler", Null).RunMethodJO("setAxisTickPadding", Array(20))
    chart.RunMethodJO("getStyler", Null).RunMethodJO("setAxisTickMarkLength", Array(15))
    chart.RunMethodJO("getStyler", Null).RunMethodJO("setPlotMargin", Array(20))

    Dim fontType As JavaObject
    fontType.InitializeStatic("java.awt.Font")
    Dim chartTitleFont As JavaObject
    chartTitleFont.InitializeNewInstance("java.awt.Font", Array(fontType.GetField("SANS_SERIF"), fontType.GetField("BOLD"), 24))           'MONOSPACED, SANS_SERIF, SERIF, DIALOG, DIALOG_INPUT
                                                                                                                                           'BOLD, PLAIN, CENTER_BASELINE, HANGING_BASELINE, ITALIC, TRUETYPE_FONT, TYPE1_FONT, ROMAN_BASELINE
    chart.RunMethodJO("getStyler", Null).RunMethodJO("setChartTitleFont", Array(chartTitleFont))

    Dim fontType As JavaObject
    fontType.InitializeStatic("java.awt.Font")
    Dim chartLegendFont As JavaObject
    chartLegendFont.InitializeNewInstance("java.awt.Font", Array(fontType.GetField("SERIF"), fontType.GetField("PLAIN"), 18))              'MONOSPACED, SANS_SERIF, SERIF, DIALOG, DIALOG_INPUT
                                                                                                                                           'BOLD, PLAIN, CENTER_BASELINE, HANGING_BASELINE, ITALIC, TRUETYPE_FONT, TYPE1_FONT, ROMAN_BASELINE
    chart.RunMethodJO("getStyler", Null).RunMethodJO("setLegendFont", Array(chartLegendFont))
    
    chart.RunMethodJO("getStyler", Null).RunMethodJO("setLegendSeriesLineLength", Array(50))


    Dim fontType As JavaObject
    fontType.InitializeStatic("java.awt.Font")
    Dim axisTitleFont As JavaObject
    axisTitleFont.InitializeNewInstance("java.awt.Font", Array(fontType.GetField("SANS_SERIF"), fontType.GetField("ITALIC"), 18))               'MONOSPACED, SANS_SERIF, SERIF, DIALOG, DIALOG_INPUT
                                                                                                                                                'BOLD, PLAIN, CENTER_BASELINE, HANGING_BASELINE, ITALIC, TRUETYPE_FONT, TYPE1_FONT, ROMAN_BASELINE
    chart.RunMethodJO("getStyler", Null).RunMethodJO("setAxisTitleFont", Array(axisTitleFont))
    

    Dim fontType As JavaObject
    fontType.InitializeStatic("java.awt.Font")
    Dim axisTickLabelsFont As JavaObject
    axisTickLabelsFont.InitializeNewInstance("java.awt.Font", Array(fontType.GetField("SERIF"), fontType.GetField("PLAIN"), 18))               'MONOSPACED, SANS_SERIF, SERIF, DIALOG, DIALOG_INPUT
                                                                                                                                               'BOLD, PLAIN, CENTER_BASELINE, HANGING_BASELINE, ITALIC, TRUETYPE_FONT, TYPE1_FONT, ROMAN_BASELINE
    chart.RunMethodJO("getStyler", Null).RunMethodJO("setAxisTickLabelsFont", Array(axisTickLabelsFont))
    
    chart.RunMethod("setXAxisTitle", Array("Radians"))
    chart.RunMethod("setYAxisTitle", Array("F(x)"))
    
    chart.RunMethodJO("getStyler", Null).RunMethod("setYAxisLogarithmic", Array(False))
    
    'You can use the below to set the Y-Axis minimum and maximum values
    Dim yMininum As Double = -1.1
    Dim yMaximum As Double = 1.1
    chart.RunMethodJO("getStyler", Null).RunMethod("setYAxisMin", Array(yMininum))
    chart.RunMethodJO("getStyler", Null).RunMethod("setYAxisMax", Array(yMaximum))
    
    'You can use the below to set the X-Axis minimum and maximum values
'    Dim xMininum As Double = 0
'    Dim xMaximum As Double = 2 * cPI
'    chart.RunMethodJO("getStyler", Null).RunMethod("setXAxisMin", Array(xMininum))
'    chart.RunMethodJO("getStyler", Null).RunMethod("setXAxisMax", Array(xMaximum))
    
    
    
    'FOR USE WITH STATIC LINE CHARTS
'    Sleep(0)
'    Try
'        chart.RunMethodJO("getStyler", Null).RunMethodJO("setCursorEnabled", Array(True))
'    Catch
'        LastException
'    End Try   
'   
'    Sleep(0)             
'    Try
'        Dim curColor As JavaObject
'        curColor.InitializeStatic("java.awt.Color")
'        chart.RunMethodJO("getStyler", Null).RunMethodJO("setCursorColor", Array(curColor.GetField("GREEN")))
'    Catch
'        LastException
'    End Try
'   
'    Sleep(0)
'    Try
'        chart.RunMethodJO("getStyler", Null).RunMethodJO("setCursorLineWidth", Array(2.0f))
'    Catch
'        LastException
'    End Try       

'    //    chart.getStyler().setCursorBackgroundColor(Color.BLUE);

    
    '******************* BUILD SERIES 1
    
    For i = 0 To size - 1
        Dim radians As Double = phase + i * 2 * cPI/(size)
        serie1Xdata(i) = radians
        series1Ydata(i) = Sin(radians)
    Next
    
    series = chart.RunMethod("addSeries", Array("y=sin(x)", serie1Xdata, series1Ydata))
    
    
    '******************* BUILD SERIES 2
    
    For i = 0 To size - 1
        Dim radians As Double = phase + i * 2 * cPI/(size)
        series2Xdata(i) = radians
        series2Ydata(i) = Cos(radians)
    Next
    
    series = chart.RunMethod("addSeries", Array("y=cos(x)", series2Xdata, series2Ydata))

    series.RunMethod("setLineColor", Array(Constants.Color_Purple))
    series.RunMethod("setLineStyle", Array(Constants.LineStyle_DashDot))
    series.RunMethod("setMarkerColor", Array(Constants.Color_Orange))
    series.RunMethod("setMarker", Array(Constants.Marker_Circle))
    
'    Dim linewdth As Float = 40                                'does not seem to work
'    series.RunMethod("setLineWidth", Array(linewdth))


    
    '************************** ADDING THE CHART TO A PANE AND DISPLAYING IT STARTS HERE   
    'Add the swingnode to a pane and display the pane containing the chart.
    Dim swingNode As JavaObject
    swingNode.InitializeNewInstance("javafx.embed.swing.SwingNode", Null)
    
    
    chartPanel.InitializeNewInstance("org.knowm.xchart.XChartPanel", Array(chart))
    
    swingNode.RunMethod("setContent", Array(chartPanel))
    
    Dim pn As JavaObject
    pn.InitializeNewInstance("javafx.scene.layout.Pane",  Null)
    
    pn.RunMethodJO("getChildren", Null).RunMethodJO("add", Array((swingNode)))
    
    MainForm.RootPane.AddNode(pn, 10, 10, 350, 350)
    
    'The below 3 lines of code is for my laptop with Windows 10 else the graph will not display - seems to be working without these lines of code on later versions of Windows....?
    Sleep(300)
    MainForm.WindowLeft = MainForm.WindowLeft - 1
    MainForm.WindowLeft = MainForm.WindowLeft + 1
    
    t.Enabled = True
    
End Sub

Sub t_tick
    
    'update Series1 and Series2
    
    phase = phase + 0.08
    
    For i = 0 To size - 1
        Dim radians As Double = phase + i * 2 * cPI/(size)
        serie1Xdata(i) = radians
        series1Ydata(i) = Sin(radians)
    Next
    
    chart.RunMethod("updateXYSeries", Array("y=sin(x)", serie1Xdata, series1Ydata, Null))

    
    For i = 0 To size - 1
        Dim radians As Double = phase + i * 2 * cPI/(size)
        series2Xdata(i) = radians
        series2Ydata(i) = Cos(radians)
    Next
    
    chart.RunMethod("updateXYSeries", Array("y=cos(x)", series2Xdata, series2Ydata, Null))
    
    'repaint the chart after updating the values
    chartPanel.RunMethod("repaint", Null)
    
End Sub

module Constants:
'Static code module
Sub Process_Globals
    Private fx As JFX
End Sub

'************************************************************* PRE DEFINED COLORS START HERE

Public Sub Color_Purple As JavaObject
    
    Dim seriesColor As JavaObject
    seriesColor.InitializeStatic("org.knowm.xchart.style.colors.XChartSeriesColors")
    Return seriesColor.GetField("PURPLE")
    
End Sub

Public Sub Color_Blue As JavaObject
    
    Dim seriesColor As JavaObject
    seriesColor.InitializeStatic("org.knowm.xchart.style.colors.XChartSeriesColors")
    Return seriesColor.GetField("BLUE")
    
End Sub

Public Sub Color_Green As JavaObject
    
    Dim seriesColor As JavaObject
    seriesColor.InitializeStatic("org.knowm.xchart.style.colors.XChartSeriesColors")
    Return seriesColor.GetField("GREEN")
    
End Sub

Public Sub Color_Orange As JavaObject
    
    Dim seriesColor As JavaObject
    seriesColor.InitializeStatic("org.knowm.xchart.style.colors.XChartSeriesColors")
    Return seriesColor.GetField("ORANGE")
    
End Sub

Public Sub Color_Red As JavaObject
    
    Dim seriesColor As JavaObject
    seriesColor.InitializeStatic("org.knowm.xchart.style.colors.XChartSeriesColors")
    Return seriesColor.GetField("RED")
    
End Sub

Public Sub Color_Yellow As JavaObject
    
    Dim seriesColor As JavaObject
    seriesColor.InitializeStatic("org.knowm.xchart.style.colors.XChartSeriesColors")
    Return seriesColor.GetField("YELLOW")
    
End Sub

Public Sub Color_Magenta As JavaObject
    
    Dim seriesColor As JavaObject
    seriesColor.InitializeStatic("org.knowm.xchart.style.colors.XChartSeriesColors")
    Return seriesColor.GetField("MAGENTA")
    
End Sub

Public Sub Color_Pink As JavaObject
    
    Dim seriesColor As JavaObject
    seriesColor.InitializeStatic("org.knowm.xchart.style.colors.XChartSeriesColors")
    Return seriesColor.GetField("PINK")
    
End Sub

Public Sub Color_LightGrey As JavaObject
    
    Dim seriesColor As JavaObject
    seriesColor.InitializeStatic("org.knowm.xchart.style.colors.XChartSeriesColors")
    Return seriesColor.GetField("LIGHT_GREY")
    
End Sub

Public Sub Color_Cyan As JavaObject
    
    Dim seriesColor As JavaObject
    seriesColor.InitializeStatic("org.knowm.xchart.style.colors.XChartSeriesColors")
    Return seriesColor.GetField("CYAN")
    
End Sub

Public Sub Color_Brown As JavaObject
    
    Dim seriesColor As JavaObject
    seriesColor.InitializeStatic("org.knowm.xchart.style.colors.XChartSeriesColors")
    Return seriesColor.GetField("BROWN")
    
End Sub

Public Sub Color_Black As JavaObject
    
    Dim seriesColor As JavaObject
    seriesColor.InitializeStatic("org.knowm.xchart.style.colors.XChartSeriesColors")
    Return seriesColor.GetField("BLACK")
    
End Sub


'************************************************************* PRE DEFINED MARKER SHAPES START HERE

public Sub Marker_None As JavaObject
    
    Dim markerShape As JavaObject
    markerShape.InitializeStatic("org.knowm.xchart.style.markers.SeriesMarkers")
    Return markerShape.GetField("NONE")
End Sub

public Sub Marker_Circle As JavaObject
    
    Dim markerShape As JavaObject
    markerShape.InitializeStatic("org.knowm.xchart.style.markers.SeriesMarkers")
    Return markerShape.GetField("CIRCLE")
End Sub

public Sub Marker_Diamond As JavaObject
    
    Dim markerShape As JavaObject
    markerShape.InitializeStatic("org.knowm.xchart.style.markers.SeriesMarkers")
    Return markerShape.GetField("DIAMOND")
End Sub

public Sub Marker_Square As JavaObject
    
    Dim markerShape As JavaObject
    markerShape.InitializeStatic("org.knowm.xchart.style.markers.SeriesMarkers")
    Return markerShape.GetField("SQUARE")
End Sub

public Sub Marker_TriangleDown As JavaObject
    
    Dim markerShape As JavaObject
    markerShape.InitializeStatic("org.knowm.xchart.style.markers.SeriesMarkers")
    Return markerShape.GetField("TRIANGLE_DOWN")
End Sub

public Sub Marker_TriangleUp As JavaObject
    
    Dim markerShape As JavaObject
    markerShape.InitializeStatic("org.knowm.xchart.style.markers.SeriesMarkers")
    Return markerShape.GetField("TRIANGLE_UP")
End Sub

public Sub Marker_Cross As JavaObject
    
    Dim markerShape As JavaObject
    markerShape.InitializeStatic("org.knowm.xchart.style.markers.SeriesMarkers")
    Return markerShape.GetField("CROSS")
End Sub

public Sub Marker_Plus As JavaObject
    
    Dim markerShape As JavaObject
    markerShape.InitializeStatic("org.knowm.xchart.style.markers.SeriesMarkers")
    Return markerShape.GetField("PLUS")
End Sub

public Sub Marker_Trapezoid As JavaObject
    
    Dim markerShape As JavaObject
    markerShape.InitializeStatic("org.knowm.xchart.style.markers.SeriesMarkers")
    Return markerShape.GetField("TRAPEZOID")
End Sub

public Sub Marker_Oval As JavaObject
    
    Dim markerShape As JavaObject
    markerShape.InitializeStatic("org.knowm.xchart.style.markers.SeriesMarkers")
    Return markerShape.GetField("OVAL")
End Sub

public Sub Marker_Rectangle As JavaObject
    
    Dim markerShape As JavaObject
    markerShape.InitializeStatic("org.knowm.xchart.style.markers.SeriesMarkers")
    Return markerShape.GetField("RECTANGLE")
End Sub

'************************************************************* PRE DEFINED LINE STYLES START HERE


public Sub LineStyle_DashDash As JavaObject
    
    Dim LineStyle As JavaObject
    LineStyle.InitializeStatic("org.knowm.xchart.style.lines.SeriesLines")
    Return LineStyle.GetField("DASH_DASH")
End Sub

public Sub LineStyle_None As JavaObject
    
    Dim LineStyle As JavaObject
    LineStyle.InitializeStatic("org.knowm.xchart.style.lines.SeriesLines")
    Return LineStyle.GetField("NONE")
End Sub

public Sub LineStyle_Solid As JavaObject
    
    Dim LineStyle As JavaObject
    LineStyle.InitializeStatic("org.knowm.xchart.style.lines.SeriesLines")
    Return LineStyle.GetField("SOLID")
End Sub

public Sub LineStyle_DashDot As JavaObject
    
    Dim LineStyle As JavaObject
    LineStyle.InitializeStatic("org.knowm.xchart.style.lines.SeriesLines")
    Return LineStyle.GetField("DASH_DOT")
End Sub

public Sub LineStyle_DotDot As JavaObject
    
    Dim LineStyle As JavaObject
    LineStyle.InitializeStatic("org.knowm.xchart.style.lines.SeriesLines")
    Return LineStyle.GetField("DOT_DOT")
End Sub


'************************************************************* PRE DEFINED LEGEND POSITIONS START HERE


public Sub LegendPosition_InsideNW As JavaObject
    
    Dim legendPos As JavaObject
    legendPos.InitializeStatic("org.knowm.xchart.style.Styler.LegendPosition")
    Return legendPos.GetField("InsideNW")
    
End Sub

public Sub LegendPosition_InsideSE As JavaObject
    
    Dim legendPos As JavaObject
    legendPos.InitializeStatic("org.knowm.xchart.style.Styler.LegendPosition")
    Return legendPos.GetField("InsideSE")
    
End Sub

public Sub LegendPosition_InsideSW As JavaObject
    
    Dim legendPos As JavaObject
    legendPos.InitializeStatic("org.knowm.xchart.style.Styler.LegendPosition")
    Return legendPos.GetField("InsideSW")
    
End Sub

public Sub LegendPosition_InsideN As JavaObject
    
    Dim legendPos As JavaObject
    legendPos.InitializeStatic("org.knowm.xchart.style.Styler.LegendPosition")
    Return legendPos.GetField("InsideN")
    
End Sub

public Sub LegendPosition_InsideS As JavaObject
    
    Dim legendPos As JavaObject
    legendPos.InitializeStatic("org.knowm.xchart.style.Styler.LegendPosition")
    Return legendPos.GetField("InsideS")
    
End Sub

public Sub LegendPosition_OutsideS As JavaObject
    
    Dim legendPos As JavaObject
    legendPos.InitializeStatic("org.knowm.xchart.style.Styler.LegendPosition")
    Return legendPos.GetField("OutsideS")
    
End Sub


'************************************************************* PRE DEFINED LEGEND LAYOUTS START HERE


public Sub LegendLayout_Horizontal As JavaObject
    
    Dim legendPos As JavaObject
    legendPos.InitializeStatic("org.knowm.xchart.style.Styler.LegendLayout")
    Return legendPos.GetField("Horizontal")
    
End Sub

public Sub LegendLayout_Vertical As JavaObject
    
    Dim legendPos As JavaObject
    legendPos.InitializeStatic("org.knowm.xchart.style.Styler.LegendLayout")
    Return legendPos.GetField("Vertical")
    
End Sub
 

Attachments

  • b4jXchartLineChartDynamic.zip
    4.8 KB · Views: 87
  • b4jXchartLineChartStatic.zip
    4.7 KB · Views: 85
  • JHSxChart.jar
    300.3 KB · Views: 83
Last edited:

Johan Schoeman

Expert
Licensed User
Longtime User
If you add the below 3 lines of code to the line chart you can turn it into an Area Chart

B4J - creating an Area Chart:
    Dim xySeriesRenderStyle As JavaObject
    xySeriesRenderStyle.InitializeStatic("org.knowm.xchart.XYSeries.XYSeriesRenderStyle")
    chart.runmethodJO("getStyler", Null).RunMethodJO("setDefaultSeriesRenderStyle", Array(xySeriesRenderStyle.GetField("Area")))

1685184418519.png
 

Attachments

  • b4jXchartLineAreaChart.zip
    4.7 KB · Views: 89

Johan Schoeman

Expert
Licensed User
Longtime User
With some minor changes the Line Chart can be turned into a Bubble Chart....sample project attached.


1685186989502.png


Bubble Chart:
#Region Project Attributes
    #MainFormWidth: 650
    #MainFormHeight: 520

#End Region

#AdditionalJar: JHSxChart

Sub Process_Globals
    
    Private fx As JFX
    Private MainForm As Form
    Private xui As XUI
    Dim series As JavaObject
    
    Dim phase As Double
    
    Dim t As Timer

    
    Dim chart As JavaObject
    Dim chartPanel As JavaObject

End Sub

Sub AppStart (Form1 As Form, Args() As String)
    
    MainForm = Form1
    
    MainForm.RootPane.LoadLayout("main")
    MainForm.Show
    
    phase = 0
    
    t.Initialize("t", 100)
    
    Dim XYChartBuilder As JavaObject
    XYChartBuilder.InitializeNewInstance("org.knowm.xchart.BubbleChartBuilder", Null)
    XYChartBuilder.RunMethodJO("width", Array(630)).RunMethodJO("height", Array(500)).RunMethodJO("title", Array("Bubble Chart")).RunMethodJO("build", Null)
    

    chart.InitializeNewInstance("org.knowm.xchart.BubbleChart", Array(XYChartBuilder))
    
    chart.RunMethodJO("getStyler", Null).RunMethodJO("setChartTitleVisible", Array(True))
    chart.RunMethodJO("getStyler", Null).RunMethodJO("setLegendVisible", Array(True))
    
    chart.RunMethodJO("getStyler", Null).RunMethod("setLegendPosition", Array(Constants.LegendPosition_OutsideS))
    chart.RunMethodJO("getStyler", Null).RunMethod("setLegendLayout", Array(Constants.LegendLayout_Horizontal))
    
    chart.RunMethodJO("getStyler", Null).RunMethod("setXAxisLabelRotation", Array(45))


    chart.RunMethodJO("getStyler", Null).RunMethod("setDecimalPattern", Array("#0.00"))
    
    chart.RunMethodJO("getStyler", Null).RunMethodJO("setToolTipsEnabled", Array(True))
    
    chart.RunMethodJO("getStyler", Null).RunMethodJO("setToolTipsAlwaysVisible", Array(False))
    
    Dim fontType As JavaObject
    fontType.InitializeStatic("java.awt.Font")
    Dim tooltipFont As JavaObject
    tooltipFont.InitializeNewInstance("java.awt.Font", Array(fontType.GetField("DIALOG"), fontType.GetField("BOLD"), 12))                'MONOSPACED, SANS_SERIF, SERIF, DIALOG, DIALOG_INPUT
                                                                                                                                           'BOLD, PLAIN, CENTER_BASELINE, HANGING_BASELINE, ITALIC, TRUETYPE_FONT, TYPE1_FONT, ROMAN_BASELINE
    chart.RunMethodJO("getStyler", Null).RunMethodJO("setToolTipFont", Array(tooltipFont))
    
    Dim tooltipHighlightColor As JavaObject
    tooltipHighlightColor.InitializeStatic("java.awt.Color")
    chart.RunMethodJO("getStyler", Null).RunMethodJO("setToolTipHighlightColor", Array(tooltipHighlightColor.GetField("CYAN")))

    Dim tooltipBorderColor As JavaObject
    tooltipBorderColor.InitializeStatic("java.awt.Color")
    chart.RunMethodJO("getStyler", Null).RunMethodJO("setToolTipBorderColor", Array(tooltipBorderColor.GetField("BLACK")))
    
    Dim tooltipBackgroundColor As JavaObject
    tooltipBackgroundColor.InitializeStatic("java.awt.Color")
    chart.RunMethodJO("getStyler", Null).RunMethodJO("setToolTipBackgroundColor", Array(tooltipBackgroundColor.GetField("LIGHT_GRAY")))
    
    
    Dim plotBackgroundColor As JavaObject
    plotBackgroundColor.InitializeStatic("java.awt.Color")
    chart.RunMethodJO("getStyler", Null).RunMethodJO("setPlotBackgroundColor", Array(plotBackgroundColor.GetField("LIGHT_GRAY")))
    
    Dim plotGridLinesColor As JavaObject
    plotGridLinesColor.InitializeStatic("java.awt.Color")
    chart.RunMethodJO("getStyler", Null).RunMethodJO("setPlotGridLinesColor", Array(plotGridLinesColor.GetField("GRAY")))
    
    Dim chartBackgroundColor As JavaObject
    chartBackgroundColor.InitializeStatic("java.awt.Color")
    chart.RunMethodJO("getStyler", Null).RunMethodJO("setChartBackgroundColor", Array(chartBackgroundColor.GetField("RED")))
    
    Dim chartLegendBackgroundColor As JavaObject
    chartLegendBackgroundColor.InitializeStatic("java.awt.Color")
    chart.RunMethodJO("getStyler", Null).RunMethodJO("setLegendBackgroundColor", Array(chartLegendBackgroundColor.GetField("YELLOW")))
    
    Dim chartFontColor As JavaObject
    chartFontColor.InitializeStatic("java.awt.Color")
    chart.RunMethodJO("getStyler", Null).RunMethodJO("setChartFontColor", Array(chartFontColor.GetField("BLUE")))
    
    Dim chartTitleBoxBackgroundColor As JavaObject
    chartTitleBoxBackgroundColor.InitializeStatic("java.awt.Color")
    chart.RunMethodJO("getStyler", Null).RunMethodJO("setChartTitleBoxBackgroundColor", Array(chartTitleBoxBackgroundColor.GetField("CYAN")))
    
    chart.RunMethodJO("getStyler", Null).RunMethodJO("setChartTitleBoxVisible", Array(True))
    
    Dim chartTitleBoxBorderColor As JavaObject
    chartTitleBoxBorderColor.InitializeStatic("java.awt.Color")
    chart.RunMethodJO("getStyler", Null).RunMethodJO("setChartTitleBoxBorderColor", Array(chartTitleBoxBorderColor.GetField("BLACK")))
        
    chart.RunMethodJO("getStyler", Null).RunMethodJO("setPlotGridLinesVisible", Array(True))

    chart.RunMethodJO("getStyler", Null).RunMethodJO("setAxisTickPadding", Array(20))
    chart.RunMethodJO("getStyler", Null).RunMethodJO("setAxisTickMarkLength", Array(15))
    chart.RunMethodJO("getStyler", Null).RunMethodJO("setPlotMargin", Array(20))

    Dim fontType As JavaObject
    fontType.InitializeStatic("java.awt.Font")
    Dim chartTitleFont As JavaObject
    chartTitleFont.InitializeNewInstance("java.awt.Font", Array(fontType.GetField("SANS_SERIF"), fontType.GetField("BOLD"), 24))           'MONOSPACED, SANS_SERIF, SERIF, DIALOG, DIALOG_INPUT
                                                                                                                                           'BOLD, PLAIN, CENTER_BASELINE, HANGING_BASELINE, ITALIC, TRUETYPE_FONT, TYPE1_FONT, ROMAN_BASELINE
    chart.RunMethodJO("getStyler", Null).RunMethodJO("setChartTitleFont", Array(chartTitleFont))

    Dim fontType As JavaObject
    fontType.InitializeStatic("java.awt.Font")
    Dim chartLegendFont As JavaObject
    chartLegendFont.InitializeNewInstance("java.awt.Font", Array(fontType.GetField("SERIF"), fontType.GetField("PLAIN"), 18))              'MONOSPACED, SANS_SERIF, SERIF, DIALOG, DIALOG_INPUT
                                                                                                                                           'BOLD, PLAIN, CENTER_BASELINE, HANGING_BASELINE, ITALIC, TRUETYPE_FONT, TYPE1_FONT, ROMAN_BASELINE
    chart.RunMethodJO("getStyler", Null).RunMethodJO("setLegendFont", Array(chartLegendFont))
    
    chart.RunMethodJO("getStyler", Null).RunMethodJO("setLegendSeriesLineLength", Array(50))


    Dim fontType As JavaObject
    fontType.InitializeStatic("java.awt.Font")
    Dim axisTitleFont As JavaObject
    axisTitleFont.InitializeNewInstance("java.awt.Font", Array(fontType.GetField("SANS_SERIF"), fontType.GetField("ITALIC"), 18))               'MONOSPACED, SANS_SERIF, SERIF, DIALOG, DIALOG_INPUT
                                                                                                                                                'BOLD, PLAIN, CENTER_BASELINE, HANGING_BASELINE, ITALIC, TRUETYPE_FONT, TYPE1_FONT, ROMAN_BASELINE
    chart.RunMethodJO("getStyler", Null).RunMethodJO("setAxisTitleFont", Array(axisTitleFont))
    

    Dim fontType As JavaObject
    fontType.InitializeStatic("java.awt.Font")
    Dim axisTickLabelsFont As JavaObject
    axisTickLabelsFont.InitializeNewInstance("java.awt.Font", Array(fontType.GetField("SERIF"), fontType.GetField("PLAIN"), 18))               'MONOSPACED, SANS_SERIF, SERIF, DIALOG, DIALOG_INPUT
                                                                                                                                               'BOLD, PLAIN, CENTER_BASELINE, HANGING_BASELINE, ITALIC, TRUETYPE_FONT, TYPE1_FONT, ROMAN_BASELINE
    chart.RunMethodJO("getStyler", Null).RunMethodJO("setAxisTickLabelsFont", Array(axisTickLabelsFont))
    
    chart.RunMethod("setXAxisTitle", Array("X VALUE"))
    chart.RunMethod("setYAxisTitle", Array("Y VALUE"))
    
    
        
    '******************* BUILD SERIES 1
    
    Dim serie1Xdata() As Double = Array As Double(1.5, 2.6, 3.3, 4.9, 5.5, 6.3, 1, 2.0, 3.0, 4.0, 5, 6)
    Dim serie1Ydata() As Double = Array As Double(10, 4, 7, 7.7, 7, 5.5, 10, 4, 7, 1, 7, 9)
    Dim bubble1Data() As Double = Array As Double (17, 40, 50, 51, 26, 20, 66, 35, 80, 27, 29, 44)

    
    series = chart.RunMethod("addSeries", Array("Series 1", serie1Xdata, serie1Ydata, bubble1Data))
    
    
    '******************* BUILD SERIES 2
    
    Dim serie2Xdata() As Double = Array As Double(1, 2.0, 3.0, 4.0, 5, 6, 1.5, 2.6, 3.3, 4.9, 5.5, 6.3)
    Dim serie2Ydata() As Double = Array As Double(1, 2, 3, 4, 5, 6, 10, 8.5, 4, 1, 4.7, 9)
    Dim bubble2Data() As Double = Array As Double (37, 35, 80, 27, 29, 44, 57, 40, 50, 33, 26, 20)
    
    series = chart.RunMethod("addSeries", Array("Series 2", serie2Xdata, serie2Ydata, bubble2Data))

    

    '************************** ADDING THE CHART TO A PANE AND DISPLAYING IT STARTS HERE   
    'Add the swingnode to a pane and display the pane containing the chart.
    Dim swingNode As JavaObject
    swingNode.InitializeNewInstance("javafx.embed.swing.SwingNode", Null)
    
    
    chartPanel.InitializeNewInstance("org.knowm.xchart.XChartPanel", Array(chart))
    
    swingNode.RunMethod("setContent", Array(chartPanel))
    
    Dim pn As JavaObject
    pn.InitializeNewInstance("javafx.scene.layout.Pane",  Null)
    
    pn.RunMethodJO("getChildren", Null).RunMethodJO("add", Array((swingNode)))
    
    MainForm.RootPane.AddNode(pn, 10, 10, 350, 350)
    
    'The below 3 lines of code is for my laptop with Windows 10 else the graph will not display - seems to be working without these lines of code on later versions of Windows....?
    Sleep(300)
    MainForm.WindowLeft = MainForm.WindowLeft - 1
    MainForm.WindowLeft = MainForm.WindowLeft + 1
    
'    t.Enabled = True
    
End Sub
 

Attachments

  • b4jXchartBubbleChart.zip
    4.5 KB · Views: 87

Johan Schoeman

Expert
Licensed User
Longtime User
A Dial Chart from the same lib ("wrapped" with Java Object):

dialChart.gif


B4J Main Code:
#Region Project Attributes
    #MainFormWidth: 650
    #MainFormHeight: 520

#End Region

#AdditionalJar: JHSxChart

Sub Process_Globals
    
    Private fx As JFX
    Private MainForm As Form
    Private xui As XUI
    
    Dim t As Timer

    Dim chart As JavaObject
    Dim chartPanel As JavaObject
    Dim dialSeries As JavaObject

End Sub

Sub AppStart (Form1 As Form, Args() As String)
    
    MainForm = Form1
    
    MainForm.RootPane.LoadLayout("main")
    MainForm.Show
    
    t.Initialize("t", 1000)
    
    Dim DialChartBuilder As JavaObject
    DialChartBuilder.InitializeNewInstance("org.knowm.xchart.DialChartBuilder", Null)
    DialChartBuilder.RunMethodJO("width", Array(630)).RunMethodJO("height", Array(500)).RunMethodJO("title", Array("Dial Chart")).RunMethodJO("build", Null)
    
    chart.InitializeNewInstance("org.knowm.xchart.DialChart", Array(DialChartBuilder))
    
    chart.RunMethodJO("getStyler", Null).RunMethodJO("setChartTitleVisible", Array(True))
    chart.RunMethodJO("getStyler", Null).RunMethodJO("setLegendVisible", Array(True))
    
    chart.RunMethodJO("getStyler", Null).RunMethod("setLegendPosition", Array(Constants.LegendPosition_OutsideS))
    chart.RunMethodJO("getStyler", Null).RunMethod("setLegendLayout", Array(Constants.LegendLayout_Horizontal))
    
    chart.RunMethodJO("getStyler", Null).RunMethod("setDecimalPattern", Array("#0.00"))
    
    chart.RunMethodJO("getStyler", Null).RunMethodJO("setToolTipsEnabled", Array(True))
    
    chart.RunMethodJO("getStyler", Null).RunMethodJO("setToolTipsAlwaysVisible", Array(False))
    
    Dim fontType As JavaObject
    fontType.InitializeStatic("java.awt.Font")
    Dim tooltipFont As JavaObject
    tooltipFont.InitializeNewInstance("java.awt.Font", Array(fontType.GetField("DIALOG"), fontType.GetField("BOLD"), 12))                'MONOSPACED, SANS_SERIF, SERIF, DIALOG, DIALOG_INPUT
                                                                                                                                           'BOLD, PLAIN, CENTER_BASELINE, HANGING_BASELINE, ITALIC, TRUETYPE_FONT, TYPE1_FONT, ROMAN_BASELINE
    chart.RunMethodJO("getStyler", Null).RunMethodJO("setToolTipFont", Array(tooltipFont))
    
    Dim tooltipHighlightColor As JavaObject
    tooltipHighlightColor.InitializeStatic("java.awt.Color")
    chart.RunMethodJO("getStyler", Null).RunMethodJO("setToolTipHighlightColor", Array(tooltipHighlightColor.GetField("CYAN")))

    Dim tooltipBorderColor As JavaObject
    tooltipBorderColor.InitializeStatic("java.awt.Color")
    chart.RunMethodJO("getStyler", Null).RunMethodJO("setToolTipBorderColor", Array(tooltipBorderColor.GetField("BLACK")))
    
    Dim tooltipBackgroundColor As JavaObject
    tooltipBackgroundColor.InitializeStatic("java.awt.Color")
    chart.RunMethodJO("getStyler", Null).RunMethodJO("setToolTipBackgroundColor", Array(tooltipBackgroundColor.GetField("LIGHT_GRAY")))
    
    
    Dim plotBackgroundColor As JavaObject
    plotBackgroundColor.InitializeStatic("java.awt.Color")
    chart.RunMethodJO("getStyler", Null).RunMethodJO("setPlotBackgroundColor", Array(plotBackgroundColor.GetField("PINK")))
    
    
    Dim chartBackgroundColor As JavaObject
    chartBackgroundColor.InitializeStatic("java.awt.Color")
    chart.RunMethodJO("getStyler", Null).RunMethodJO("setChartBackgroundColor", Array(chartBackgroundColor.GetField("RED")))
    
    Dim chartLegendBackgroundColor As JavaObject
    chartLegendBackgroundColor.InitializeStatic("java.awt.Color")
    chart.RunMethodJO("getStyler", Null).RunMethodJO("setLegendBackgroundColor", Array(chartLegendBackgroundColor.GetField("YELLOW")))
    
    Dim chartFontColor As JavaObject
    chartFontColor.InitializeStatic("java.awt.Color")
    chart.RunMethodJO("getStyler", Null).RunMethodJO("setChartFontColor", Array(chartFontColor.GetField("BLUE")))
    
    Dim chartTitleBoxBackgroundColor As JavaObject
    chartTitleBoxBackgroundColor.InitializeStatic("java.awt.Color")
    chart.RunMethodJO("getStyler", Null).RunMethodJO("setChartTitleBoxBackgroundColor", Array(chartTitleBoxBackgroundColor.GetField("CYAN")))
    
    chart.RunMethodJO("getStyler", Null).RunMethodJO("setChartTitleBoxVisible", Array(True))
    
    Dim chartTitleBoxBorderColor As JavaObject
    chartTitleBoxBorderColor.InitializeStatic("java.awt.Color")
    chart.RunMethodJO("getStyler", Null).RunMethodJO("setChartTitleBoxBorderColor", Array(chartTitleBoxBorderColor.GetField("BLACK")))
        
    Dim fontType As JavaObject
    fontType.InitializeStatic("java.awt.Font")
    Dim chartTitleFont As JavaObject
    chartTitleFont.InitializeNewInstance("java.awt.Font", Array(fontType.GetField("SANS_SERIF"), fontType.GetField("BOLD"), 24))           'MONOSPACED, SANS_SERIF, SERIF, DIALOG, DIALOG_INPUT
                                                                                                                                           'BOLD, PLAIN, CENTER_BASELINE, HANGING_BASELINE, ITALIC, TRUETYPE_FONT, TYPE1_FONT, ROMAN_BASELINE
    chart.RunMethodJO("getStyler", Null).RunMethodJO("setChartTitleFont", Array(chartTitleFont))

    Dim fontType As JavaObject
    fontType.InitializeStatic("java.awt.Font")
    Dim chartLegendFont As JavaObject
    chartLegendFont.InitializeNewInstance("java.awt.Font", Array(fontType.GetField("SERIF"), fontType.GetField("PLAIN"), 18))              'MONOSPACED, SANS_SERIF, SERIF, DIALOG, DIALOG_INPUT
                                                                                                                                           'BOLD, PLAIN, CENTER_BASELINE, HANGING_BASELINE, ITALIC, TRUETYPE_FONT, TYPE1_FONT, ROMAN_BASELINE
    chart.RunMethodJO("getStyler", Null).RunMethodJO("setLegendFont", Array(chartLegendFont))
    
    chart.RunMethodJO("getStyler", Null).RunMethodJO("setLegendSeriesLineLength", Array(50))


    Dim fontType As JavaObject
    fontType.InitializeStatic("java.awt.Font")
    Dim axisTitleFont As JavaObject
    axisTitleFont.InitializeNewInstance("java.awt.Font", Array(fontType.GetField("SANS_SERIF"), fontType.GetField("ITALIC"), 18))               'MONOSPACED, SANS_SERIF, SERIF, DIALOG, DIALOG_INPUT
                                                                                                                                                'BOLD, PLAIN, CENTER_BASELINE, HANGING_BASELINE, ITALIC, TRUETYPE_FONT, TYPE1_FONT, ROMAN_BASELINE
    chart.RunMethodJO("getStyler", Null).RunMethodJO("setAxisTitleFont", Array(axisTitleFont))
    
    chart.RunMethodJO("getStyler", Null).RunMethodJO("setLabelVisible", Array(True))
    chart.RunMethodJO("getStyler", Null).RunMethodJO("setLegendVisible", Array(True))
        
    chart.RunMethod("setXAxisTitle", Array("X VALUE"))
    chart.RunMethod("setYAxisTitle", Array("Y VALUE"))
    
    Dim arc As Double = 300
    chart.RunMethodJO("getStyler", Null).RunMethod("setArcAngle", Array(arc))
    
    Dim thk As Double = 0.25
    chart.RunMethodJO("getStyler", Null).RunMethodJO("setDonutThickness", Array(thk))
    
    chart.RunMethodJO("getStyler", Null).RunMethodJO("setCircular", Array(True))                 'else it will be oval
    

    Dim aaa As Double = 40
    chart.RunMethodJO("getStyler", Null).RunMethodJO("setArrowArcAngle", Array(aaa))

    Dim aap As Double = 0.05
    chart.RunMethodJO("getStyler", Null).RunMethodJO("setArrowArcPercentage", Array(aap))
    
    Dim alp As Double = 0.5
    chart.RunMethodJO("getStyler", Null).RunMethodJO("setArrowLengthPercentage", Array(alp))


    Dim arrowColor As JavaObject
    arrowColor.InitializeStatic("java.awt.Color")
    chart.RunMethodJO("getStyler", Null).RunMethodJO("setArrowColor", Array(arrowColor.GetField("RED")))

    Dim lowerFrom As Double = 0
    chart.RunMethodJO("getStyler", Null).RunMethodJO("setLowerFrom", Array(lowerFrom))
    Dim lowerTo As Double = 0.1
    chart.RunMethodJO("getStyler", Null).RunMethodJO("setLowerTo", Array(lowerTo))
    Dim lowerColor As JavaObject
    lowerColor.InitializeStatic("java.awt.Color")
    chart.RunMethodJO("getStyler", Null).RunMethodJO("setLowerColor", Array(lowerColor.GetField("GREEN")))

    
    Dim middleFrom As Double = 0.1
    chart.RunMethodJO("getStyler", Null).RunMethodJO("setMiddleFrom", Array(middleFrom))
    Dim middleTo As Double = 0.8
    chart.RunMethodJO("getStyler", Null).RunMethodJO("setMiddleTo", Array(middleTo))
    Dim middleColor As JavaObject
    middleColor.InitializeStatic("java.awt.Color")
    chart.RunMethodJO("getStyler", Null).RunMethodJO("setMiddleColor", Array(middleColor.GetField("YELLOW")))
    
    Dim upperFrom As Double = 0.8
    chart.RunMethodJO("getStyler", Null).RunMethodJO("setUpperFrom", Array(upperFrom))
    Dim upperTo As Double = 1.0
    chart.RunMethodJO("getStyler", Null).RunMethodJO("setUpperTo", Array(upperTo))
    Dim upperColor As JavaObject
    upperColor.InitializeStatic("java.awt.Color")
    chart.RunMethodJO("getStyler", Null).RunMethodJO("setUpperColor", Array(upperColor.GetField("RED")))

    Dim fontType As JavaObject
    fontType.InitializeStatic("java.awt.Font")
    Dim labelFont As JavaObject
    labelFont.InitializeNewInstance("java.awt.Font", Array(fontType.GetField("MONOSPACED"), fontType.GetField("BOLD"), 20))              'MONOSPACED, SANS_SERIF, SERIF, DIALOG, DIALOG_INPUT
                                                                                                                                         'BOLD, PLAIN, CENTER_BASELINE, HANGING_BASELINE, ITALIC, TRUETYPE_FONT, TYPE1_FONT, ROMAN_BASELINE
    chart.RunMethodJO("getStyler", Null).RunMethodJO("setLabelFont", Array(labelFont))

    chart.RunMethodJO("getStyler", Null).RunMethodJO("setAxisTickLabelsVisible", Array(True))
    chart.RunMethodJO("getStyler", Null).RunMethodJO("setAxisTicksMarksVisible", Array(True))

    Dim tickColor As JavaObject
    tickColor.InitializeStatic("java.awt.Color")
    chart.RunMethodJO("getStyler", Null).RunMethodJO("setAxisTickMarksColor", Array(middleColor.GetField("BLUE")))

    Dim bs As JavaObject
    Dim str As Float = 3.0
    bs.InitializeNewInstance("java.awt.BasicStroke", Array(str))
    chart.RunMethodJO("getStyler", Null).RunMethodJO("setAxisTickMarksStroke", Array(bs))

    chart.RunMethodJO("getStyler", Null).RunMethodJO("setAxisTitleVisible", Array(True))

    Dim pad As Int = 10
    chart.RunMethodJO("getStyler", Null).RunMethodJO("setAxisTitlePadding", Array(pad))

    Dim tickvalarray() As Double = Array As Double(0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1)
    chart.RunMethodJO("getStyler", Null).RunMethodJO("setAxisTickValues", Array(tickvalarray))
    
    Dim ticklablearray() As String = Array As String("0", "10", "20", "30", "40", "50", "60", "70", "80", "90", "100")
    chart.RunMethodJO("getStyler", Null).RunMethodJO("setAxisTickLabels", Array(ticklablearray))

        
    '******************* BUILD SERIES
        
    Dim val As Double = 0.5
    dialSeries = chart.RunMethod("addSeries", Array("kPa", val, "PRESSURE"))
    dialSeries.RunMethod("setEnabled", Array(True))


    '************************** ADDING THE CHART TO A PANE AND DISPLAYING IT STARTS HERE   
    'Add the swingnode to a pane and display the pane containing the chart.
    Dim swingNode As JavaObject
    swingNode.InitializeNewInstance("javafx.embed.swing.SwingNode", Null)
    
    chartPanel.InitializeNewInstance("org.knowm.xchart.XChartPanel", Array(chart))
    
    swingNode.RunMethod("setContent", Array(chartPanel))
    
    Dim pn As JavaObject
    pn.InitializeNewInstance("javafx.scene.layout.Pane",  Null)
    
    pn.RunMethodJO("getChildren", Null).RunMethodJO("add", Array((swingNode)))
    
    MainForm.RootPane.AddNode(pn, 10, 10, 350, 350)
    
    'The below 3 lines of code is for my laptop with Windows 10 else the graph will not display - seems to be working without these lines of code on later versions of Windows....?
    Sleep(300)
    MainForm.WindowLeft = MainForm.WindowLeft - 1
    MainForm.WindowLeft = MainForm.WindowLeft + 1
    
    t.Enabled = True
    

End Sub

Sub t_tick
    
    Dim val As Double = Rnd(0, 101)
    dialSeries.RunMethod("setLabel", Array(NumberFormat((val/100), 2, 2)))
    dialSeries.RunMethod("setValue", Array(val/100))
    
'    'repaint the chart after updating the values
    chartPanel.RunMethod("repaint", Null)
    Log(dialSeries.RunMethod("getValue", Null))
    
End Sub
 

Attachments

  • b4jXchartDialChart.zip
    4.6 KB · Views: 75

walterf25

Expert
Licensed User
Longtime User
A first dig at Real Time Line Chart. Use the same JAR as in post #9 of this thread.
The project uses a mix of B4J, inline java code, and javaobject

View attachment 142125

B4J Main:
#Region Project Attributes
    #MainFormWidth: 650
    #MainFormHeight: 500

#End Region

#AdditionalJar: JHSxChart
'#VirtualMachineArgs: -Dprism.order=sw

Sub Process_Globals
    
    Private fx As JFX
    Private MainForm As Form
    Private xui As XUI
    Dim llc As Pane            'this is and instance of the pie/donut chart object that will be added to the RootPane of the Mainform

    Dim liveLC As xChartLive
    Dim t As Timer
    
    Dim phase As Double
    Dim xdata(100) As Double
    Dim ydata(100) As Double
    
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    
    phase = 0
    t.Initialize("t", 200)                                                   'just for demo purposes
    MainForm = Form1
    
    MainForm.RootPane.LoadLayout("main")
    MainForm.Show

    For i = 0 To xdata.Length -1
        xdata(i) = (2 * cPI / xdata.length * i)
        ydata(i) = Sin(xdata(i))
    Next
    
    llc = liveLC.Initialize("Real Time Line Chart", "Radians", "Sine", "sine", xdata, ydata)
    
    MainForm.RootPane.AddNode(llc, 10, 10, 350, 350)
    
    'set the font of the chart title using JavaObject -> java.awt.Font
    Dim fontType As JavaObject
    fontType.InitializeStatic("java.awt.Font")
    Dim titleFont As JavaObject
    titleFont.InitializeNewInstance("java.awt.Font", Array(fontType.GetField("SANS_SERIF"), fontType.GetField("BOLD"), 18))           'MONOSPACED, SANS_SERIF, SERIF, DIALOG, DIALOG_INPUT
                                                                                                                                      'BOLD, PLAIN, CENTER_BASELINE, HANGING_BASELINE, ITALIC, TRUETYPE_FONT, TYPE1_FONT, ROMAN_BASELINE
    liveLC.ChartTitleFont = titleFont
    
    
    'set the chart background color
    Dim plotBackGroundColorObject As JavaObject                                             'using JavaObject to pass an instance of java.awt.Color
    plotBackGroundColorObject.InitializeNewInstance("java.awt.Color", Array(255, 255, 255))
    liveLC.PlotBackgroundColor = plotBackGroundColorObject
    
    'set the border colors
    Dim plotBorderColorObject As JavaObject                                                 'using JavaObject to pass an instance of java.awt.Color
    plotBorderColorObject.InitializeNewInstance("java.awt.Color", Array(0,0,0))
    liveLC.PlotBorderColor = plotBorderColorObject


    'set the text color of the labels
    Dim labelsFontColorObject As JavaObject
    labelsFontColorObject.InitializeNewInstance("java.awt.Color", Array(255, 0, 0))                'set the text color of the labels to black
    liveLC.AxisTickLabelsColor = labelsFontColorObject
    
    
    'set the font of the chart axis titles using JavaObject -> java.awt.Font
    Dim fontType As JavaObject
    fontType.InitializeStatic("java.awt.Font")
    Dim titleFont As JavaObject
    titleFont.InitializeNewInstance("java.awt.Font", Array(fontType.GetField("SANS_SERIF"), fontType.GetField("ITALIC"), 15))         'MONOSPACED, SANS_SERIF, SERIF, DIALOG, DIALOG_INPUT
                                                                                                                                      'BOLD, PLAIN, CENTER_BASELINE, HANGING_BASELINE, ITALIC, TRUETYPE_FONT, TYPE1_FONT, ROMAN_BASELINE
    liveLC.AxisTitleFont = titleFont

    'set the font of the chart axis tick labels using JavaObject -> java.awt.Font
    Dim fontType As JavaObject
    fontType.InitializeStatic("java.awt.Font")
    Dim tickLabelsFont As JavaObject
    tickLabelsFont.InitializeNewInstance("java.awt.Font", Array(fontType.GetField("SERIF"), fontType.GetField("HANGING_BASELINE"), 10))              'MONOSPACED, SANS_SERIF, SERIF, DIALOG, DIALOG_INPUT
                                                                                                                                                     'BOLD, PLAIN, CENTER_BASELINE, HANGING_BASELINE, ITALIC, TRUETYPE_FONT, TYPE1_FONT, ROMAN_BASELINE
    liveLC.AxisTickLabelsFont = tickLabelsFont
    
    'set the text color of the labels
    Dim tickColorObject As JavaObject
    tickColorObject.InitializeNewInstance("java.awt.Color", Array(0,255, 255))                'set the text color of the labels to black
    liveLC.AxisTickMarksColor = tickColorObject
    
    liveLC.LegendPosition = liveLC.LPosition_OutsideS
    
    liveLC.LegendLayout = liveLC.LLayout_Horizontal
    liveLC.LegendVisible = True
    
    
    'set the background color of the legend
    Dim legendBackgroundColorObject As JavaObject
    legendBackgroundColorObject.InitializeNewInstance("java.awt.Color", Array(200, 200, 0))
    liveLC.LegendBackgroundColor = legendBackgroundColorObject
    
    'set the border color of the legend
    Dim legendBorderColorObject As JavaObject
    legendBorderColorObject.InitializeNewInstance("java.awt.Color", Array(0, 0, 0))
    liveLC.LegendBorderColor = legendBorderColorObject
    
    
    'set the font of the legend using JavaObject -> java.awt.Font
    Dim fontType As JavaObject
    fontType.InitializeStatic("java.awt.Font")
    Dim legendFont As JavaObject
    legendFont.InitializeNewInstance("java.awt.Font", Array(fontType.GetField("SANS_SERIF"), fontType.GetField("ITALIC"), 15))           'MONOSPACED, SANS_SERIF, SERIF, DIALOG, DIALOG_INPUT
                                                                                                                                         'BOLD, PLAIN, CENTER_BASELINE, HANGING_BASELINE, ITALIC, TRUETYPE_FONT, TYPE1_FONT, ROMAN_BASELINE
    liveLC.LegendFont = legendFont
    
    liveLC.PlotContentSize = 0.99
    
    liveLC.ToolTipsEnabled = True
    
    'set the background color of the title box
    Dim titleBackgroundColorObject As JavaObject
    titleBackgroundColorObject.InitializeNewInstance("java.awt.Color", Array(255, 0, 0))
    liveLC.ChartTitleBoxBackgroundColor = titleBackgroundColorObject
    
    'set the border color of the title box
    Dim titleBoxBorderColorObject As JavaObject
    titleBoxBorderColorObject.InitializeNewInstance("java.awt.Color", Array(0, 0, 0))
    liveLC.ChartTitleBoxBorderColor = titleBoxBorderColorObject
        
    liveLC.ChartTitleBoxVisible = True
    
    Dim tooltipBorderColorObject As JavaObject
    tooltipBorderColorObject.InitializeNewInstance("java.awt.Color", Array(255, 0, 0))
    liveLC.ToolTipBorderColor = tooltipBorderColorObject
    
    Dim tooltipBackgroundColorObject As JavaObject
    tooltipBackgroundColorObject.InitializeNewInstance("java.awt.Color", Array(255, 200, 200))
    liveLC.ToolTipBackgroundColor = tooltipBackgroundColorObject
    
    'set the font of the tooltip
    Dim fontType As JavaObject
    fontType.InitializeStatic("java.awt.Font")
    Dim tooltipFontObject As JavaObject
    tooltipFontObject.InitializeNewInstance("java.awt.Font", Array(fontType.GetField("SANS_SERIF"), fontType.GetField("ITALIC"), 15))           'MONOSPACED, SANS_SERIF, SERIF, DIALOG, DIALOG_INPUT
                                                                                                                                                'BOLD, PLAIN, CENTER_BASELINE, HANGING_BASELINE, ITALIC, TRUETYPE_FONT, TYPE1_FONT, ROMAN_BASELINE
    liveLC.ToolTipFont = tooltipFontObject
    
    'set the text color of the chart title, axes labes, and legend
    Dim chartFontColorObject As JavaObject
    chartFontColorObject.InitializeNewInstance("java.awt.Color", Array(25, 50, 200))
    liveLC.ChartFontColor = chartFontColorObject
    
    liveLC.XAxisDecimalPattern = "###.###"
    
    liveLC.XAxisLabelRotation = 90
    
    liveLC.XAxisTicksVisible = True
    liveLC.YAxisTicksVisible = True
    
    liveLC.AxisTickMarkLength = 5
    
    liveLC.PlotMargin = 0
    
    
    'The below 3 lines of code is for my laptop with Windows 10 else the graph will not display - seems to be working without these lines of code on later versions of Windows....?
    Sleep(300)
    MainForm.WindowLeft = MainForm.WindowLeft - 1
    MainForm.WindowLeft = MainForm.WindowLeft + 1
    
    t.Enabled = True
    
End Sub


'change the value(s) of the series every xxx milliseconds amd repaint the chart to see the updated chart
Sub t_tick
    
    Dim radians As Double
    phase = phase + (2 * cPI * 2 / 20.0)
    
    For i = 0 To xdata.length - 1
        radians = phase + (2 * cPI / xdata.length * i)
        
        xdata(i) = radians
        ydata(i) = Sin(radians)
    Next
    
    liveLC.updateLC (xdata, ydata)
    
End Sub

xChartLive Class:
Sub Class_Globals
    
    Private fx As JFX
    Private nativeMe, lc As JavaObject
    
    Public LPosition_InsideNW As Int
    Public LPosition_InsideNE As Int
    Public LPosition_InsideSE As Int
    Public LPosition_InsideSW As Int
    Public LPosition_InsideN As Int
    Public LPosition_InsideS As Int
    Public LPosition_OutsideS As Int
    
    Public LLayout_Horizontal As Int
    Public LLayout_Vertical As Int
    
End Sub

'Initializes the object
'title = Chart Title
'xAxisTitle = X-Axis Title
'yAxisTitle = Y-Axis Title
'seriesName = name of the series thta will be plotted
'xdata() = array of double with x-axis values to be plotted
'ydata() = array of double with y-axis values to be plotted
Public Sub Initialize (title As String, xAxisTitle As String, yAxisTitle As String, seriesName As String, xdata() As Double, ydata() As Double) As JavaObject

    LPosition_InsideNW = 1
    LPosition_InsideNE = 2
    LPosition_InsideSE = 3
    LPosition_InsideSW = 4
    LPosition_InsideN = 5
    LPosition_InsideS = 6
    LPosition_OutsideS = 7
    
    LLayout_Horizontal = 1
    LLayout_Vertical = 2
    
    nativeMe = Me
    lc = nativeMe.RunMethod("initRTLC", Array(title, xAxisTitle, yAxisTitle, seriesName, xdata, ydata))
    Return lc
    
End Sub


public Sub updateLC(xdata() As Double, ydata() As Double)
    
    nativeMe.RunMethod("updateLC", Array(xdata, ydata))
    
End Sub

'set the chart title font
public Sub setChartTitleFont(ttlfnt As JavaObject)
    
    nativeMe.RunMethod("setChartTitleFont", Array(ttlfnt))
    
End Sub

'set the font of the axis tick labels
public Sub setAxisTickLabelsFont(ttlfnt As JavaObject)
    
    nativeMe.RunMethod("setAxisTickLabelsFont", Array(ttlfnt))
    
End Sub

'set the chart axis title font
public Sub setAxisTitleFont(ttlfnt As JavaObject)
    
    nativeMe.RunMethod("setAxisTitleFont", Array(ttlfnt))
    
End Sub

'set the background color of the plot area
public Sub setPlotBackgroundColor(backColor As JavaObject)
    
    nativeMe.RunMethod("setPlotBackgroundColor", Array(backColor))
    
End Sub

'set the border color of the plot area
public Sub setPlotBorderColor(plotBorderColor As JavaObject)
    
    nativeMe.RunMethod("setPlotBorderColor", Array(plotBorderColor))
    
End Sub

'set the text color of the lables
public Sub setAxisTickLabelsColor(labelFontColor As JavaObject)
    
    nativeMe.RunMethod("setAxisTickLabelsColor", Array(labelFontColor))
    
End Sub

'set the axis tick mark colors
public Sub setAxisTickMarksColor(labelFontColor As JavaObject)
    
    nativeMe.RunMethod("setAxisTickMarksColor", Array(labelFontColor))
    
End Sub

'set the legend position
public Sub setLegendPosition(position As Int)
    
    nativeMe.RunMethod("setLegendPosition", Array(position))
    
End Sub

'set the layout of the Legend to Vertical or Horizontal
public Sub setLegendLayout(layout As Int)
    
    nativeMe.RunMethod("setLegendLayout", Array(layout))
    
End Sub

'make the legend visible / not visible
'default value is true
public Sub setLegendVisible(visible As Boolean)
    
    nativeMe.RunMethod("setLegendVisible", Array(visible))
    
End Sub

'set the background color of the legend
Public Sub setLegendBackgroundColor(lbgc As JavaObject)
    
    nativeMe.RunMethod("setLegendBackgroundColor", Array(lbgc))
    
End Sub

'set the border color of the legend
Public Sub setLegendBorderColor(lbc As JavaObject)
    
    nativeMe.RunMethod("setLegendBorderColor", Array(lbc))
    
End Sub

'set the font of the legend
Public Sub setLegendFont(fnt As JavaObject)
    
    nativeMe.RunMethod("setLegendFont", Array(fnt))
    
End Sub

'Sets the content size of the plot inside the plot area of the chart. To fill the area 100%, use a value of 1.0.
'The default value is 0.9
public Sub setPlotContentSize (size As Double)
    
    nativeMe.RunMethod("setPlotContentSize", Array(size))
    
End Sub

'set the background color of the title box
Public Sub setChartTitleBoxBackgroundColor(ctbbc As JavaObject)
    nativeMe.RunMethod("setChartTitleBoxBackgroundColor", Array(ctbbc))
End Sub

'set the color of the title box    border
Public Sub setChartTitleBoxBorderColor(ccbbc As JavaObject)
    nativeMe.RunMethod("setChartTitleBoxBorderColor", Array(ccbbc))
End Sub


'set the TitleBoxVisible to true / false
Public Sub setChartTitleBoxVisible(visible As Boolean)
    
    nativeMe.RunMethod("setChartTitleBoxVisible", Array(visible))
    
End Sub

'set the tooltip background color
Public Sub setToolTipBackgroundColor (ttbgc As JavaObject)
    
    nativeMe.RunMethod("setToolTipBackgroundColor", Array(ttbgc))
    
End Sub

'set the tooltip border color
Public Sub setToolTipBorderColor (ttbc As JavaObject)
    
    nativeMe.RunMethod("setToolTipBorderColor", Array(ttbc))
    
End Sub

'set the font of the tooltip
Public Sub setToolTipFont(fnt As JavaObject)
    
    nativeMe.RunMethod("setToolTipFont", Array(fnt))
    
End Sub

'set tooltips enabled
'the default value is false
public Sub setToolTipsEnabled (tooltipEnabled As Boolean)
    
    nativeMe.RunMethod("setToolTipEnabled", Array(tooltipEnabled))
    
End Sub

'Set the chart font color. Includes: Chart title, axes label, legend
Public Sub setChartFontColor(chartFontColor As JavaObject)
    
    nativeMe.RunMethod("setChartFontColor", Array(chartFontColor))
    
End Sub

'Set the decimal pattern of the X-Axis values
Public Sub setXAxisDecimalPattern(pattern As String)
    
    nativeMe.RunMethod("setXAxisDecimalPattern", Array(pattern))
    
End Sub

'Set the decimal pattern of the X-Axis values
Public Sub setYAxisDecimalPattern(pattern As String)
    
    nativeMe.RunMethod("setYAxisDecimalPattern", Array(pattern))
    
End Sub

'Set the angle/rotation of the x-axis labels
Public Sub setXAxisLabelRotation(angle As Int)
    
    nativeMe.RunMethod("setXAxisLabelRotation", Array(angle))
    
End Sub

'Set the visibility of the X-Axis ticks
Public Sub setXAxisTicksVisible(visible As Boolean)
    
    nativeMe.RunMethod("setXAxisTicksVisible", Array(visible))
    
End Sub

'Set the visibility of the Y-Axis ticks
Public Sub setYAxisTicksVisible(visible As Boolean)
    
    nativeMe.RunMethod("setYAxisTicksVisible", Array(visible))
    
End Sub

'Set the length of the tick marks in pixels
Public Sub setAxisTickMarkLength(tl As Int)
    
    nativeMe.RunMethod("setAxisTickMarkLength", Array(tl))
    
End Sub


Public Sub setAxisTicksLineVisible(vis As Boolean)
    
    nativeMe.RunMethod("setAxisTicksLineVisible", Array(vis))
    
End Sub

'sets the margin (in pixels) around the plot area
Public Sub setPlotMargin(margin As Int)
    
    nativeMe.RunMethod("setPlotMargin", Array(margin))
    
End Sub


'set visibility of the X-Axis title
Public Sub setXAxisTitleVisible(vis As Boolean)
    
    nativeMe.RunMethod("setXAxisTitleVisible", Array(vis))
    
End Sub


'set visibility of the Y-Axis title
Public Sub setYAxisTitleVisible(vis As Boolean)
    
    nativeMe.RunMethod("setYAxisTitleVisible", Array(vis))
    
End Sub


#if Java


import org.knowm.xchart.QuickChart;
import org.knowm.xchart.SwingWrapper;
import org.knowm.xchart.XYChart;
import javafx.embed.swing.SwingNode;
import org.knowm.xchart.XChartPanel;
import javafx.scene.layout.Pane;

import java.awt.Font;
import java.awt.Color;
import org.knowm.xchart.style.Styler.LegendPosition;
import org.knowm.xchart.style.Styler;


  XChartPanel chartPanel;
  XYChart chart;


  public Object initRTLC(String title, String xAxisTitle, String yAxisTitle, String seriesName, double[] xdata, double[] ydata) {
 
      chart =
        QuickChart.getChart(
            title, xAxisTitle, yAxisTitle, seriesName, xdata, ydata);
            

      SwingNode swingNode = new SwingNode();   
      chartPanel = new XChartPanel(chart);
      swingNode.setContent(chartPanel);   
      Pane pane = new Pane();
      pane.getChildren().add(swingNode);

      return pane;
 
  }

  public void updateLC(double[] xdata, double[] ydata) {

    chart.updateXYSeries("sine", xdata, ydata, null);   
    chartPanel.repaint();

  }   
 
      public void setChartTitleFont(Font titleFont) {
        chart.getStyler().setChartTitleFont(titleFont);
        
    }   
    
      public void setAxisTitleFont(Font titleFont) {
        chart.getStyler().setAxisTitleFont(titleFont);
        
    }       
    
      public void setAxisTickLabelsFont(Font titleFont) {
        chart.getStyler().setAxisTickLabelsFont(titleFont);
        
    }       
    
    public void setPlotBackgroundColor(Color backGroundColor) {
        chart.getStyler().setPlotBackgroundColor(backGroundColor);   
    }   
    
    public void setPlotBorderColor(Color borderColor) {
        chart.getStyler().setPlotBorderColor(borderColor);   
    }
    
    public void setAxisTickMarksColor(Color borderColor) {
        chart.getStyler().setAxisTickMarksColor(borderColor);   
    }   
    
    private int pos = 7;
    public void setLegendPosition(int pos) {
        this.pos = pos;
        if (pos == 1) chart.getStyler().setLegendPosition(Styler.LegendPosition.InsideNW);
        if (pos == 2) chart.getStyler().setLegendPosition(Styler.LegendPosition.InsideNE);
        if (pos == 3) chart.getStyler().setLegendPosition(Styler.LegendPosition.InsideSE);
        if (pos == 4) chart.getStyler().setLegendPosition(Styler.LegendPosition.InsideSW);
        if (pos == 5) chart.getStyler().setLegendPosition(Styler.LegendPosition.InsideN);
        if (pos == 6) chart.getStyler().setLegendPosition(Styler.LegendPosition.InsideS);
        if (pos == 7) chart.getStyler().setLegendPosition(Styler.LegendPosition.OutsideS);
        
    }   
    
    int layout = 1;
    public void setLegendLayout(int layout) {
        this.layout = layout;
        if (layout == 1) chart.getStyler().setLegendLayout(Styler.LegendLayout.Horizontal);
        if (layout == 2) chart.getStyler().setLegendLayout(Styler.LegendLayout.Vertical);
    }
    
    private boolean visible = true;
    public void setLegendVisible (boolean visible) {
        this.visible = visible;
        chart.getStyler().setLegendVisible(visible);
    }   
    
    
    public void setLegendBackgroundColor(Color lbgc) {
        chart.getStyler().setLegendBackgroundColor(lbgc);
    }
    
    public void setLegendBorderColor(Color lbc) {
        chart.getStyler().setLegendBorderColor(lbc);
    }
    
    public void setLegendFont(Font legendFont) {
        chart.getStyler().setLegendFont(legendFont);
    }   
    
    private double plotContentSize = 0.9;
    public void setPlotContentSize(double plotContentSize) {
        this.plotContentSize = plotContentSize;
        chart.getStyler().setPlotContentSize(plotContentSize);   
    }   
    
    public void setChartTitleBoxBackgroundColor(Color ctbbc) {
        chart.getStyler().setChartTitleBoxBackgroundColor(ctbbc);
    }
    
    public void setChartTitleBoxBorderColor(Color ccbbc) {
        chart.getStyler().setChartTitleBoxBorderColor(ccbbc);
    }
    
    public void setChartTitleBoxVisible(boolean visible) {
        chart.getStyler().setChartTitleBoxVisible(visible);
    }   
    
    public void setToolTipBackgroundColor(Color ttbgc) {
        chart.getStyler().setToolTipBackgroundColor(ttbgc);
    }
    
    public void setToolTipBorderColor(Color ttbc) {
        chart.getStyler().setToolTipBorderColor(ttbc);
    }
    
    public void setToolTipFont(Font toolTipFont) {
        chart.getStyler().setToolTipFont(toolTipFont);
    }   
    
    private boolean tooltipEnabled = false;
    public void setToolTipEnabled(boolean tooltipEnabled) {
         this.tooltipEnabled = tooltipEnabled;
        chart.getStyler().setToolTipsEnabled(tooltipEnabled);   
    }   
    
    public void setChartFontColor(Color chartFontColor) {
        chart.getStyler().setChartFontColor(chartFontColor);
    }   
    
    public void setXAxisDecimalPattern(String pattern) {
        chart.getStyler().setXAxisDecimalPattern(pattern);
    }       
    
    public void setYAxisDecimalPattern(String pattern) {
        chart.getStyler().setYAxisDecimalPattern(pattern);
        chartPanel.repaint();
    }   
    
    public void setXAxisLabelRotation(int angle) {
        chart.getStyler().setXAxisLabelRotation(angle);
    }
    
    public void setXAxisTicksVisible(boolean visible) {
        chart.getStyler().setXAxisTicksVisible(visible);
    }
    
    public void setYAxisTicksVisible(boolean visible) {
        chart.getStyler().setYAxisTicksVisible(visible);
    }
    
    public void setAxisTickMarkLength(int tl) {
        chart.getStyler().setAxisTickMarkLength(tl);    //in pixels
    }
    
    public void setAxisTicksLineVisible(boolean vis) {
        chart.getStyler().setAxisTicksLineVisible(vis);
    }
    
    public void setPlotMargin(int margin) {
        chart.getStyler().setPlotMargin(margin);   //in pixels
    }
    
    public void setYAxisTitleVisible(boolean vis) {
        chart.getStyler().setYAxisTitleVisible(true);
        chartPanel.repaint();
    }
    
    public void setXAxisTitleVisible(boolean vis) {
        chart.getStyler().setYAxisTitleVisible(true);
        chartPanel.repaint();
    }   
        
    
    public void setAxisTickLabelsColor(Color labelsFontColor) {
        chart.getStyler().setAxisTickLabelsColor(labelsFontColor);   
    }       

#End If
Hey Johan, very great library, thanks for this contribution, I do have a question, I started playing around with your library yesterday and although it works as I expected, I seem to not be able to resize the size of the chart, I have tried setting the chart to cover the entire area of my form which is around 1080 width x 800 height, but for some reason it doesn't resize to that size, it stays at around 350 x 350, I looked at the code in the inline java but wasn't able to spot anything that might give me a hint as to why that is happening, are you able to create a line chart of any size?

Please let me know, thanks.
Walter
 

Johan Schoeman

Expert
Licensed User
Longtime User
MainForm.RootPane.AddNode(llc, 10, 10, 350, 350)
Change this line in the B4J code for left, top, width, height. Please let me know if that is sorting out the problem.
 

walterf25

Expert
Licensed User
Longtime User
Change this line in the B4J code for left, top, width, height. Please let me know if that is sorting out the problem.
I already tried that, but the size doesn't change, the chart size remains the same.

I am using a B4XPages by the way, not sure if that makes a differnce.

Walter
 

Johan Schoeman

Expert
Licensed User
Longtime User
I already tried that, but the size doesn't change, the chart size remains the same.

I am using a B4XPages by the way, not sure if that makes a differnce.

Walter
Which one of the charts are you working with Walter?
 

Johan Schoeman

Expert
Licensed User
Longtime User
XYChartBuilder.RunMethodJO("width", Array(630)).RunMethodJO("height", Array(500)).RunMethodJO("title", Array("Line Chart - Dynamic")).RunMethodJO("build", Null)
And when you change this line in LineChart?
 
Top