问题
I am trying to create a line chart in JavaFX. This line chart should have one axis (y) with numbers and another axis (x) with dates. The date range should be chosen by the user with two date pickers. Now here is my problem: Line chart only has category and number axis. Is there any way to integrate dates into the chart?
回答1:
I had this same problem, the only difference being that my "Date" object wasn't a SQL date, it was my own concoction that should be similar enough that my experiences will apply.
A CategoryAxis treats axis values as strings and that does not sound like what you want. You don't want that because you'll have sorting issues, and there are undesirable behaviors if you have missing values or try to use multiple series.
https://bugs.openjdk.java.net/browse/JDK-8092134, for example.
You almost certainly want to use a NumberAxis. Since that needs a number, use a function that converts your date value to an ordinal (an integer or a float) and add that value to your chart, then use the "setTickLabelFormatter" method on your X axis to install a custom tick formatter that knows how to convert your ordinal back to a string that represents each date, so the axis tick labels will display dates properly. It's simple to write your own custom formatter because the class only has two methods that you have to override.
来源:https://stackoverflow.com/questions/46987823/javafx-line-chart-with-date-axis