带日期轴的javafx折线图 [英] javafx line chart with date axis

查看:178
本文介绍了带日期轴的javafx折线图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在JavaFX中创建折线图.该折线图的一个轴(y)带有数字,而另一个轴(x)带有日期.用户应使用两个日期选择器来选择日期范围. 现在这是我的问题: 折线图仅具有类别和数字轴.有什么方法可以将日期整合到图表中?

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?

推荐答案

我遇到了同样的问题,唯一的区别是我的"Date"对象不是SQL日期,而是我自己的混合液,应该足够相似我的经验将适用.

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.

CategoryAxis将轴值视为字符串,听起来不像您想要的那样.您不希望这样,因为会遇到排序问题,如果缺少值或尝试使用多个序列,则会出现不良行为.

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 .

您几乎可以肯定要使用NumberAxis.由于需要一个数字,请使用将日期值转换为序数(整数或浮点数)并将其添加到图表的函数,然后在X轴上使用"setTickLabelFormatter"方法安装自定义刻度格式知道如何将序数转换回代表每个日期的字符串,因此轴刻度标签将正确显示日期.编写自己的自定义格式化程序很简单,因为该类只有两个必须重写的方法.

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.

这篇关于带日期轴的javafx折线图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆