3

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:

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;

}
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
griffith97
  • 53
  • 4
  • Maybe a [chart legend image](https://stackoverflow.com/search?tab=votes&q=%5bjavafx%5d%20chart%20legend%20image) instead? – trashgod Oct 03 '21 at 18:24
  • 2
    This is a difficult problem because [tick mark labels](https://openjfx.io/javadoc/12/javafx.controls/javafx/scene/chart/Axis.html#getTickMarkLabel(T)) are plain strings, not standard `Label`s which can have a graphic – jewelsea Oct 04 '21 at 02:27
  • 2
    I know this is not what you want exactly, but check this out: [Adding an Image Inside Each Bar on a BarChart JavaFX](https://stackoverflow.com/a/61940149/6028807). – Miss Chanandler Bong Oct 04 '21 at 08:10
  • 1
    not supported. And as @jewelsea already noted: the nodes that are representing the tick text are of type plain Text, so don't see any way to hack it into showing an image, astonished .. You might consider filing an RFE in the javafx issuetracker, maybe somebody is interested to implement it :) Or maybe third party chart libs have something along the lines you need (f.i. JFXFreeChart), don't know – kleopatra Oct 04 '21 at 10:48
  • You can try to take advantage of [setTickLabelFont(Font)](https://openjfx.io/javadoc/12/javafx.controls/javafx/scene/chart/Axis.html#setTickLabelFont(javafx.scene.text.Font)). Create a custom font ([.ttf file](https://fontstruct.com/)) and draw icons to represent a letter. For example, b can be represented by a banana icon and a by an apple icon. Loat this fonts using `setTickLabelFont`. It is not what you wanted but I think it is the closest you can get. See some ttf icon examples: [1](https://fontstruct.com/fontstructions/show/25757/old_school_1) – c0der Oct 04 '21 at 16:07
  • [2](https://fontstruct.com/fontstructions/show/14327/juggrbudz), [3](https://fontstruct.com/fontstructions/show/116849/pointersoft) – c0der Oct 04 '21 at 16:07

0 Answers0