我在javafx8中使用LineChart
,我能够在图表中动态绘制系列,并为每个输入绘制符号。但问题是我只想显示当前节点的符号,而不是已经添加的节点。
我试过了:
linechart.setCreateSymbols(false);
但这并不符合我的需要。它停止为输入创建符号。
任何人都可以告诉如何仅显示当前符号而不是添加的oness ????
答案 0 :(得分:0)
只需跟踪添加的最后一个数据点的节点。添加新点时,将前一个“最后一个点”的可见性设置为false。
您可以直接使用
执行此操作import java.util.Random;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart.Data;
import javafx.scene.chart.XYChart.Series;
import javafx.scene.control.Button;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
public class LineChartShowSymbolForLastPointOnly extends Application {
private Node lastPointAdded ;
@Override
public void start(Stage primaryStage) {
LineChart<Number, Number> chart = new LineChart<>(new NumberAxis(), new NumberAxis());
Series<Number, Number> series = new Series<>();
chart.getData().add(series);
chart.setAnimated(false);
Button addDataPointButton = new Button("Add data point");
Random rng = new Random();
addDataPointButton.setOnAction(e -> {
if (lastPointAdded != null) {
lastPointAdded.setVisible(false);
}
Data<Number, Number> data = new Data<>(series.getData().size(), rng.nextDouble());
series.getData().add(data);
lastPointAdded = data.getNode();
});
BorderPane.setAlignment(addDataPointButton, Pos.CENTER);
BorderPane.setMargin(addDataPointButton, new Insets(10));
BorderPane root = new BorderPane(chart, null, null, addDataPointButton, null);
primaryStage.setScene(new Scene(root, 800, 600));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
如果您希望能够更容易地将代码分解出来,可以使用ObjectProperty
并注册一个监听器:
import java.util.Random;
import javafx.application.Application;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart.Data;
import javafx.scene.chart.XYChart.Series;
import javafx.scene.control.Button;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
public class LineChartShowSymbolForLastPointOnly extends Application {
@Override
public void start(Stage primaryStage) {
LineChart<Number, Number> chart = new LineChart<>(new NumberAxis(), new NumberAxis());
Series<Number, Number> series = new Series<>();
chart.getData().add(series);
chart.setAnimated(false);
ObjectProperty<Node> lastPointAdded = new SimpleObjectProperty<>();
lastPointAdded.addListener((obs, oldNode, newNode) -> {
if (oldNode != null) {
oldNode.setVisible(false);
}
});
Button addDataPointButton = new Button("Add data point");
Random rng = new Random();
addDataPointButton.setOnAction(e -> {
Data<Number, Number> data = new Data<>(series.getData().size(), rng.nextDouble());
series.getData().add(data);
lastPointAdded.set(data.getNode());
});
BorderPane.setAlignment(addDataPointButton, Pos.CENTER);
BorderPane.setMargin(addDataPointButton, new Insets(10));
BorderPane root = new BorderPane(chart, null, null, addDataPointButton, null);
Scene scene = new Scene(root, 800, 600);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}