如何平移JavaFX LineChart [英] How to pan a JavaFX LineChart

查看:367
本文介绍了如何平移JavaFX LineChart的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在JavaFx中,我有一个LineChart lc,我为此创建并添加了一个系列,

In JavaFx I have a LineChart lc for which I create and add a series,

lc.getData().addAll(series1);

它显示得很好,但是似乎对所有个数据点进行了采样,以使整个域适合图表.

It displays just fine, but seems to sample all data points to fit the whole domain into the chart.

是否有一种方法可以强制LineChart仅显示该系列的最后10个数据点?

Is there a way to force the LineChart to just display the last 10 data points, say, of the series?

最终,我想创建一种方法来平移图表内容,以便根据平移位置显示数据窗口.

Ultimately I would like to create a way to pan the chart contents so that a window of the data is displayed, as a function of pan position.

我发现了这篇SO帖子,但是它删除了数据,这意味着我然后需要将其添加回去.我宁愿将所有数据保留在系列中,并尽可能设置该系列的可用范围.

I found this SO post but it removes data, which means I would then need to add it back. I would rather keep all data in the series if possible, and set the usable range of that series if possible.

推荐答案

我自己使用标准的fx库制定了解决方案.

I worked out the solution myself using the standard fx libraries.

只要水平轴是NumberAxis,就可以使用setLowerBound()setUpperBound()函数.

So long as the horizontal axis is a NumberAxis, you can use the setLowerBound() and setUpperBound() functions.

与滚动条结合使用,效果很好.

Combined with a scrollbar this works perfectly.

例如,

    final static int MAX_DISPLAY = 100
    LineChart<Number, Number> lineChart;
    ScrollBar scroller;

    scroller.setMin(0);
    scroller.setMax(1000 - MAX_DISPLAY);

    scroller.valueProperty().addListener(new ChangeListener<Number>() {

        @Override
        public void changed(ObservableValue<? extends Number> observableValue, Number number, Number t1) {

            double pos = scroller.getValue();

            NumberAxis na = (NumberAxis)lineChart.getXAxis();
            na.setLowerBound(pos);
            na.setUpperBound(pos + MAX_DISPLAY);
        }
    });

注意-如果使用Scene Builder,则LineChart x轴将默认为CatergoryAxis,因此请确保编辑.fxml文件并将其手动更改为NumberAxis.

Note - if using Scene Builder, your LineChart x-axis will default to CatergoryAxis, so make sure to edit the .fxml file and change it to NumberAxis manually.

另外,在我的用例中,由于x轴表示Unix时代,您可以使用以下代码(在我的情况下为日期格式的字符串)将Number值转换为其他值,

Additionally, for my use case since the x-axis represents Unix epoch times, you can convert the Number values into something else using the following code (in my case a date formatted string),

        NumberAxis xAxis = (NumberAxis)lineChart.getXAxis();

        xAxis.setTickLabelFormatter(new StringConverter<Number>() {

            @Override
            public String toString(Number object) {

                return convertToDate(object.longValue());
            }

            @Override
            public Number fromString(String string) {
                return 0;
            }
        });

这篇关于如何平移JavaFX LineChart的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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