• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
  • Knute Snortum
Sheriffs:
  • Liutauras Vilda
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Joe Ess
  • salvin francis
  • fred rosenberger

Can I add a label to each individual bar in a bar graph?

 
Bartender
Posts: 1741
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looking at various JavaFX examples for a bar graph, I don't see any examples of how to add descriptive (label) text to each bar in the graph.

All the examples I've seen are well-behaved 2-D with static values for x-y.

But what if you have data like this:

Year,Amount, User
----------------------------
"2017","200","Tammy"
"2016","1500","Fred"
"2016","1123","Ethel"

You could graph any two of these, but the third value? I'm guessing to display that, you'd need to "decorate" each bar with a label showing that value. I've experimented with creating a label and going through the series collection but nothing has worked.

For example, you might have a graph showing year and Amount where each bar is the "error count", but you need to then add the "user" (name) to the bar.

Not sure how to label each bar with the third value in FX or if it's even possible. No examples I could find.

(Is this actually just a 3D chart? Hope that's not my only option.)

TIA

- mike

 
Marshal
Posts: 6670
178
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's hard for me to visualize what you want your chart to look like without being 3D.
 
Mike London
Bartender
Posts: 1741
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Knute Snortum wrote:It's hard for me to visualize what you want your chart to look like without being 3D.



I was thinking about a bar graph where each bar had a label with the user name, for example.

Thanks for your reply.

- mike
 
Rancher
Posts: 387
30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is difficult to answer this without a mock-up of what you would like the final graph to look like.

Possible answer might be on StackOverflow:
 http://stackoverflow.com/questions/15237192/how-to-display-bar-value-on-top-of-bar-javafx
Though I am not sure if that really addresses your question.

In general the Barchart is usually a CategoryAxis on the X Axis and a NumberAxis on the Y Axis.

As you can see from this example by Oracle:
 https://docs.oracle.com/javase/8/javafx/user-interface-tutorial/bar-chart.htm
The categories are just strings, so you can set them to whatever you want.

So instead of setting the category axis to just 2017, you could set it to 2017 + " "  + userName, for example:
That will set the category name to year + user.  However what you probably want is to have multiple series of data with the username written directly next to the bar.  In which case the following wouldn't work:
As the categories wouldn't match as you would wish.

So what you might want to do is to have just
Then use a similar trick to that from the StackOverflow answer to add an additional text item into the chart to label each individual bar:

The above code is adding in an extra label which states the value of the bar, but you can replace the text in the example with a username as per your requirements.
 
Mike London
Bartender
Posts: 1741
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your reply.

Since there is no built-in way to just add a simple label to a bar, I "solved" it by adding the label information as part of the axis label.

Not what I really wanted, but don't have days to work on something so basic. Can't believe I'm the only one who might want to add an explanatory label next to a bar, but whatever...

Thanks again for your great reply.

- mike
 
John Damien Smith
Rancher
Posts: 387
30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's not very tricky.  You just use the setNode function of XYChart.Data:
 https://docs.oracle.com/javase/8/javafx/api/javafx/scene/chart/XYChart.Data.html#setNode-javafx.scene.Node-

Here is an example based upon minor modifications to the Oracle documentation supplied sample code for a BarChart.

 
Mike London
Bartender
Posts: 1741
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks.

While I agree the code is relatively simple, that's still a lot of code to just add a label.

Appreciate your reply.

- mike
 
I promise I will be the best, most loyal friend ever! All for this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!