B4J Library jCharts

Here's my first library for B4J. It's a wrapper of javafx.scene.chart with additional functions and classes.

jCharts.jpg
chart2.jpg


To take a snapshot of a chart, set its animated property to False before adding the data or wait until all animations are over.

Changelog:
v1.21:
- I added a workaround for a bug in OpenJDK 11 (thanks to Chris2)

v1.2:
- XAxis and YAxis created by the designer can now raise events with the default event prefix "XAxis" / "YAxis";
- I added the LegendChanged event to all charts;
- I fixed a bug (NullPointerException) in GetChildStyleMap and StyleMap.

v1.11:
- I added the SetChildStyleMap2 function;
- I modified the fourth demo to show a simple way to add the colored bands.

v1.1:
- I added the Plot event to all charts except PieChart;
- I added a demo to show how to use the Plot event (this project requires jGraphicLib);
- I modified the third demo to use a layout made with the designer.

v1.0:
- All charts can be added as custom views in the visual designer;
- I added the Width and Height properties to all charts and axis;
- I added the Series property to XYCoord;
- I added the MouseToData function to all charts and removed the EventPrefixForPlot parameter from Initialize (MouseToData is more convenient than events);
- I modified the second demo to show MouseToData in use;
- I modified the third demo so that the slider value sets the upper bound of the displayed series.

v0.9:
- I added ForceZeroInRange and TickUnit to NumberAxis;
- I added RemoveAllData to PieChart;
- I added FindNearestX to XYSeries;
- I added FindSeries and RemoveAllSeries to all charts using series;
- I added Padding to all charts;
- I added a new example and some documentation.
 

Attachments

  • jCharts_1_21.zip
    74.1 KB · Views: 808
Last edited:

FabioRome

Member
Licensed User
Longtime User
The line chart example provided with the library shows the values on the axis and when you pass the mouse over, so I don't understand the question. Can you give more details?

I would like to show the values not only when passing the mouse
 

Attachments

  • chart3.png
    chart3.png
    159.2 KB · Views: 498

FabioRome

Member
Licensed User
Longtime User
sorry I can not explain.
Along with the symbols (LineChart.Create Symbols = True) I would like to show the value immediately upon loading the LineChart
 

Informatix

Expert
Licensed User
Longtime User
sorry I can not explain.
Along with the symbols (LineChart.Create Symbols = True) I would like to show the value immediately upon loading the LineChart
Take a look at Charts3. Once the chart is drawn, compute the position on screen of all values with XAxis.GetDisplayPosition and YAxis.GetDisplayPosition, then draw the value at the computed position with ChartCanvas.DrawText.
 

tufanv

Expert
Licensed User
Longtime User
is there any way to change the textsize of piechart title and pie labels text color ?
 

Johan Hormaza

Well-Known Member
Licensed User
Longtime User
B4X:
Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Private AreaChart1 As AreaChart
    Private YAxis As NumberAxis
    Private XAxis As CategoryAxis
    Dim series As XYSeries
    Private sql As SQL
End Sub
#AdditionalJar: sqlite-jdbc-3.7.2
Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.RootPane.LoadLayout("main") 'Load the layout file.
    XAxis = AreaChart1.XAxis
    YAxis = AreaChart1.YAxis
    YAxis.LowerBound = 1
    YAxis.UpperBound = 400
    XAxis.TickLabelRotation = 360
    AreaChart1.Animated = False
    AreaChart1.CreateSymbols = False
    AreaChart1.LegendVisible = False
    AreaChart1.Title = "Rendimiento"
    series.Initialize("Rendimiento")
    Button1_Click
    AreaChart1.AddSeries(series)
    MainForm.Show
End Sub

'Return true to allow the default exceptions handler to handle the uncaught exception.
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
    Return True
End Sub

Sub Button1_Click
    Dim query As String
'    Dim listX,listY As List
'    listX.Initialize
'    listY.Initialize
    sql.InitializeSQLite(File.DirData("LuzHor"), "LNOGUERA.db", True)
    query= "SELECT Fecha, COUNT(Latitud) As 'Luminarias',SUM(Potencia) AS 'Potencias W',SUM(Encendida) AS 'Encendidas', SUM(Particular) AS 'Particulares', (SUM(Potencia)*0.36) AS 'Energía kWh', (COUNT(Latitud)/160) AS 'Rendimiento' FROM LNOGUERA GROUP BY Fecha"
Dim cursor As ResultSet
cursor = sql.ExecQuery(query)
Do While cursor.NextRow
        series.Add(cursor.GetString("Fecha"),cursor.GetInt("Luminarias"))
Loop
cursor.Close      

End Sub
This is my code


upload_2018-4-9_17-15-56.png
 

StarinschiAndrei

Active Member
Licensed User
Longtime User
Hi, i modified line chart example ( add line chart from designer) and now it doesn't show anything (attached the test app). The error :
Waiting for debugger to connect...
Program started.
java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Number
at javafx.scene.chart.ValueAxis.invalidateRange(ValueAxis.java:439)
at javafx.scene.chart.LineChart.updateAxisRange(LineChart.java:210)
at javafx.scene.chart.XYChart.layoutChartChildren(XYChart.java:660)
at javafx.scene.chart.Chart$1.layoutChildren(Chart.java:94)
at javafx.scene.Parent.layout(Parent.java:1087)
at javafx.scene.Parent.layout(Parent.java:1093)
at javafx.scene.Parent.layout(Parent.java:1093)
at javafx.scene.Parent.layout(Parent.java:1093)
at javafx.scene.Scene.doLayoutPass(Scene.java:552)
at javafx.scene.Scene$ScenePulseListener.pulse(Scene.java:2397)
at com.sun.javafx.tk.Toolkit.lambda$runPulse$29(Toolkit.java:398)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.Toolkit.runPulse(Toolkit.java:397)
at com.sun.javafx.tk.Toolkit.firePulse(Toolkit.java:424)
at com.sun.javafx.tk.quantum.QuantumToolkit.pulse(QuantumToolkit.java:518)
at com.sun.javafx.tk.quantum.QuantumToolkit.pulse(QuantumToolkit.java:498)
at com.sun.javafx.tk.quantum.QuantumToolkit.pulseFromQueue(QuantumToolkit.java:491)
at com.sun.javafx.tk.quantum.QuantumToolkit.lambda$runToolkit$403(QuantumToolkit.java:319)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$147(WinApplication.java:177)
at java.lang.Thread.run(Thread.java:748)

How can i create dynamic series ? for data like this :
upload_2018-5-6_13-52-33.png
 

Attachments

  • lineChart.zip
    3.1 KB · Views: 446
  • upload_2018-5-6_13-52-28.png
    upload_2018-5-6_13-52-28.png
    3.6 KB · Views: 410
Last edited:

Informatix

Expert
Licensed User
Longtime User
Hi, i modified line chart example ( add line chart from designer) and now it doesn't show anything (attached the test app). The error :
Waiting for debugger to connect...
Program started.
java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Number
at javafx.scene.chart.ValueAxis.invalidateRange(ValueAxis.java:439)
at javafx.scene.chart.LineChart.updateAxisRange(LineChart.java:210)
at javafx.scene.chart.XYChart.layoutChartChildren(XYChart.java:660)
at javafx.scene.chart.Chart$1.layoutChildren(Chart.java:94)
at javafx.scene.Parent.layout(Parent.java:1087)
at javafx.scene.Parent.layout(Parent.java:1093)
at javafx.scene.Parent.layout(Parent.java:1093)
at javafx.scene.Parent.layout(Parent.java:1093)
at javafx.scene.Scene.doLayoutPass(Scene.java:552)
at javafx.scene.Scene$ScenePulseListener.pulse(Scene.java:2397)
at com.sun.javafx.tk.Toolkit.lambda$runPulse$29(Toolkit.java:398)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.Toolkit.runPulse(Toolkit.java:397)
at com.sun.javafx.tk.Toolkit.firePulse(Toolkit.java:424)
at com.sun.javafx.tk.quantum.QuantumToolkit.pulse(QuantumToolkit.java:518)
at com.sun.javafx.tk.quantum.QuantumToolkit.pulse(QuantumToolkit.java:498)
at com.sun.javafx.tk.quantum.QuantumToolkit.pulseFromQueue(QuantumToolkit.java:491)
at com.sun.javafx.tk.quantum.QuantumToolkit.lambda$runToolkit$403(QuantumToolkit.java:319)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$147(WinApplication.java:177)
at java.lang.Thread.run(Thread.java:748)

How can i create dynamic series ? for data like this :
View attachment 67610
LineCharts created by the designer have two numerical axis, incompatible with string values.
I don't understand your question about dynamic series with 3 values.
 

StarinschiAndrei

Active Member
Licensed User
Longtime User
LineCharts created by the designer have two numerical axis, incompatible with string values.
I don't understand your question about dynamic series with 3 values.
Hi i would like to create below code part dynamically, how can i declare Series as an array Dim Series() as XYSeries :
B4X:
Dim Series1 As XYSeries
    Series1.Initialize("Pepito")
    Series1.Add("Feb",44)
    Series1.Add("Mar",42)
    Series1.Add("Apr",33)
    Series1.Add("May",31)
    Series1.Add("Jun",26)
    Series1.Add("Jul",22)
    Series1.Add("Aug",25)
    Series1.Add("Sept",43)
    Series1.Add("Oct",44)
    Series1.Add("Nov",45)
    Series1.Add("Dec",44)
 
    Dim Series2 As XYSeries
    Series2.Initialize("Oreo")
    Series2.Add("Feb",40)
    Series2.Add("Mar",38)
    Series2.Add("Apr",39)
    Series2.Add("May",36)
    Series2.Add("Jun",33)
    Series2.Add("Jul",32)
    Series2.Add("Aug",28)
    Series2.Add("Sept",29)
    Series2.Add("Oct",27)
    Series2.Add("Nov",25)
    Series2.Add("Dec",20)
    LC.AddAllSeries(Array As XYSeries(Series1, Series2))

something like :
B4X:
for i=0 to 10 
Series(i).add("Department A","Value")
Series(i).add("Department B","Value")
Series(i).add("Department C","Value")
Next
 

Informatix

Expert
Licensed User
Longtime User
Hi i would like to create below code part dynamically, how can i declare Series as an array Dim Series() as XYSeries :
B4X:
Dim Series1 As XYSeries
    Series1.Initialize("Pepito")
    Series1.Add("Feb",44)
    Series1.Add("Mar",42)
    Series1.Add("Apr",33)
    Series1.Add("May",31)
    Series1.Add("Jun",26)
    Series1.Add("Jul",22)
    Series1.Add("Aug",25)
    Series1.Add("Sept",43)
    Series1.Add("Oct",44)
    Series1.Add("Nov",45)
    Series1.Add("Dec",44)
 
    Dim Series2 As XYSeries
    Series2.Initialize("Oreo")
    Series2.Add("Feb",40)
    Series2.Add("Mar",38)
    Series2.Add("Apr",39)
    Series2.Add("May",36)
    Series2.Add("Jun",33)
    Series2.Add("Jul",32)
    Series2.Add("Aug",28)
    Series2.Add("Sept",29)
    Series2.Add("Oct",27)
    Series2.Add("Nov",25)
    Series2.Add("Dec",20)
    LC.AddAllSeries(Array As XYSeries(Series1, Series2))

something like :
B4X:
for i=0 to 10
Series(i).add("Department A","Value")
Series(i).add("Department B","Value")
Series(i).add("Department C","Value")
Next
B4X:
Dim Series(11) As XYSeries
for i=0 to 10 
Series(i).Initialize("name")
Series(i).add("Department A","Value")
Series(i).add("Department B","Value")
Series(i).add("Department C","Value")
Next
 

StarinschiAndrei

Active Member
Licensed User
Longtime User
B4X:
Dim Series(11) As XYSeries
for i=0 to 10
Series(i).Initialize("name")
Series(i).add("Department A","Value")
Series(i).add("Department B","Value")
Series(i).add("Department C","Value")
Next
Thank you. Now it's working.
There is no way to use the linechart from designer ?
 

Don Cameron

New Member
Licensed User
Harris

Would you be willing to share some code snippets of how you implemented your -

Zoom and Zoomed in feature... Drag to zoom (left to right - creates visual canvas rect), let go and magic. How nice!
To un-zoom (restore the original view) - drag from right to left...

Thanks

Don
 
Top