简体   繁体   English

JavaFX 8如何在JavaFX折线图上布置Node对象

[英]JavaFX 8 How to dispose a Node object on a JavaFX Line chart

I've a moving line chart (live chart where the data moves each second ie, the lower bound and the upper bound as well as the series that displays the line are updated each second). 我有一个移动的折线图(实时图表,其中数据每秒移动一次,即上限和下限以及显示折线的序列每秒更新一次)。 Once every minute, I also display a text object anchored to a particular xy value on the chart. 每分钟一次,我还会在图表上显示锚定到特定xy值的文本对象。

文字对象

Each second the entire chart gets updated and so does this text object (the text object moves each second) 整个图表每秒钟更新一次,此文本对象也会更新(文本对象每秒移动一次)

Issue: 问题:

The text object is not moving past the lower bound 文本对象没有移过下限

不会越过下限在此处输入图片说明

I'm creating the text object and then adding it to a XYChart.Data point using the setNode() API, then setting this data point to a XYChart.Series and setting the style on it. 我正在创建文本对象,然后使用setNode()API将其添加到XYChart.Data点,然后将此数据点设置为XYChart.Series并设置其样式。

I'm not able to figure out a way to dispose this text object. 我无法找出一种处理此文本对象的方法。

Code: Creating Text Object and setting it to the XYChart.Data object 代码:创建文本对象并将其设置为XYChart.Data对象

Text moaLabel = new Text("MHRext\nUS1\nUnknown");

Data<Date, Integer> moaPlot = new Data<>();
moaPlot.setNode(moaLabel);
moaPlot.setXValue(xPosition);
moaPlot.setYValue(yPosition);

Adding the Data object to the series object 将Data对象添加到series对象

textSeries.getData().add(moaPlot);

CSS 的CSS

.chart-series-line-text-mode {    
    -fx-stroke-width: 0px;
    -fx-stroke: #FFFFFF;    
    -fx-effect: null;
}

I found a solution to my problem. 我找到了解决问题的方法。

The approach is to check if the moaPlot.getXValue().equals(lowerBound) then remove it from the textSeries list. 方法是检查moaPlot.getXValue().equals(lowerBound)然后将其从textSeries列表中删除。

In my case since the chart moves every second, I've the following code in an scheduler that runs every second. 就我而言,由于图表每秒移动一次,因此我在调度程序中每秒运行以下代码。

Executors.newSingleThreadScheduledExecutor().scheduleAtFixedRate(()->{
    textSeries.getData().removeIf(moa -> (moa.getXValue().before(lowerBound))); 
}, 0, 1, TimeUnit.SECONDS);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM