I'm trying to display icons on BarChart
elements on the X-axis. I've yet to figure if this is even a supported feature. Picture of BarChart
I'm trying to make:
In the example below, I tried passing the image in a Label
to the chart. The code runs into an exception below. After many ways of trying to get this to work, I started doubting is this even possible?
JavaFX Application Thread java.lang.ClassCastException:
class javafx.scene.control.Label
cannot be cast to class java.lang.String
javafx.scene.control.Label
is in module javafx.controls
of loader app
; java.lang.String
is in module java.base
of loader bootstrap
.
private BarChart createBarChart() {
Image img = new Image("file:src/images/icon.png");
Label testLabel = new Label("Can this be empty?", new ImageView(img));
//Configuring category and NumberAxis
CategoryAxis xaxis= new CategoryAxis();
NumberAxis yaxis = new NumberAxis(0.1,2,0.1);
xaxis.setLabel("Volume");
yaxis.setLabel("Subject");
//Configuring BarChart
BarChart<Label,Float> bar = new BarChart(xaxis,yaxis);
bar.setTitle("Chart");
//Configuring Series for XY chart
Series<Label, Float> series = new XYChart.Series<>();
series.getData().add(new XYChart.Data(testLabel, 5));
//Adding series to the barchart
bar.getData().add(series);
return bar;
}