I am trying to "wrap" the Pie Chart of this project via inline Java Code (in a B4J class). Funny thing is that the graph will not be displayed until such time as what I just click on the outer border of the Main Form. When running the B4J project it shows the outline of the Pane but not the graph - I need to click on the outer border of the Form to make the graph appear. Why is it behaving like this?
Sample B4J project is attached - you will also need to download the attached Jar (JHSxChart.jar) and copy it to your B4J additional library folder to run the project. Once the project has been started the Pane border should be visible but not the graph that should be displayed inside the Pane. Click on the border of the Form and then the graph appears out of thin air...
Anyone that can shed some light on this problem? Comment the last 6 jars in the B4J project - see the code uploaded in this post.
Code of Main:
Code of B4J Class with inline Java Code (xPieChart):
Sample B4J project is attached - you will also need to download the attached Jar (JHSxChart.jar) and copy it to your B4J additional library folder to run the project. Once the project has been started the Pane border should be visible but not the graph that should be displayed inside the Pane. Click on the border of the Form and then the graph appears out of thin air...
Anyone that can shed some light on this problem? Comment the last 6 jars in the B4J project - see the code uploaded in this post.
Code of Main:
B4J Main:
#Region Project Attributes
#MainFormWidth: 1000
#MainFormHeight: 800
#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 pieObject As JavaObject
Private ComboBox1 As ComboBox
Private Pane1 As Pane
End Sub
Sub AppStart (Form1 As Form, Args() As String)
MainForm = Form1
MainForm.RootPane.LoadLayout("Layout1")
MainForm.Show
Sleep(1000)
Dim piechart As xPieChart
piechart.Initialize
piechart.GraphWidth = Pane1.Width
piechart.GraphHeight = Pane1.Height
piechart.GraphTitle = "B4J Pie Chart Title"
piechart.GraphCircular = True
pieObject = piechart.drawPieChart
Sleep(0)
Pane1.AddNode(pieObject, 0, 0, Pane1.Width, Pane1.Height)
Sleep(0)
MainForm.RootPane.SetSize(MainForm.Width, MainForm.Height)
End Sub
Code of B4J Class with inline Java Code (xPieChart):
B4J class xPieChart:
Sub Class_Globals
Private fx As JFX
Private nativeMe As JavaObject
End Sub
'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize
nativeMe = Me
End Sub
'draw the pie chart
public Sub drawPieChart As JavaObject
Return nativeMe.RunMethod("drawChart", Null)
End Sub
'set the width of the chart
public Sub setGraphWidth(wdth As Int)
nativeMe.RunMethod("setGraphWidth", Array(wdth))
End Sub
'set the height of the chart
Public Sub setGraphHeight(hght As Int)
nativeMe.RunMethod("setGraphHeight", Array(hght))
End Sub
'set the title of the chart
public Sub setGraphTitle(title As String)
nativeMe.RunMethod("setGraphTitle", Array(title))
End Sub
'set the graph to be circular or to follow the width/height of the pane
public Sub setGraphCircular(circ As Boolean)
nativeMe.RunMethod("setCircular", Array(circ))
End Sub
#If Java
import org.knowm.xchart.PieChart;
import org.knowm.xchart.PieChartBuilder;
//import org.knowm.xchart.SwingWrapper;
import org.knowm.xchart.style.Styler;
import javafx.scene.Node;
import javax.swing.JPanel;
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 javafx.embed.swing.SwingNode;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.Pane;
private int wdth = 400;
public void setGraphWidth(int wdth) {
this.wdth = wdth;
}
private int hght = 400;
public void setGraphHeight(int hght) {
this.hght = hght;
}
private String graphTitle = "";
public void setGraphTitle(String graphTitle) {
this.graphTitle = graphTitle;
}
private boolean circ = false;
public void setCircular(boolean circ) {
this.circ = circ;
}
public Pane drawChart() {
PieChart chart =
new PieChartBuilder().width(wdth).height(hght).title(graphTitle).build();
// Customize Chart
chart.getStyler().setCircular(circ);
chart.getStyler().setLegendPosition(Styler.LegendPosition.OutsideS);
chart.getStyler().setLegendLayout(Styler.LegendLayout.Horizontal);
// Series
chart.addSeries("Pennies", 100);
chart.addSeries("Nickels", 50);
chart.addSeries("Dimes", 70);
chart.addSeries("Quarters", 170);
SwingNode swingNode = new SwingNode();
JPanel chartPanel = new XChartPanel(chart);
swingNode.setContent(chartPanel);
Pane pane = new Pane(swingNode);
return pane;
}
#End If
Attachments
Last edited: