gpt4 book ai didi

java - 如何根据鼠标位置在折线图上绘制十字准线

转载 作者:搜寻专家 更新时间:2023-11-01 02:19:02 24 4
gpt4 key购买 nike

我正在尝试绘制一条垂直线和一条水平线,指向我鼠标所指的位置,我希望看到我的“光标”随着我的鼠标移动。

因此,我尝试向我的图表添加一条折线,但我无法添加任何内容,因为折线图没有 getChildren() 函数。因此,我想要这样的东西:

enter image description here

我也在想,如果这些“光标”可以是一个对象,当我称我的类为“光标”时,它会创建 2 条线,一条垂直线和一条水平线?

我的代码:

我尝试使用 LineBuilder 来画一条线,但现在我想要怎样了。现在,我成功获取了我的鼠标位置,我现在怎么画2条线呢?

    NumberAxis x = new NumberAxis();
NumberAxis y = new NumberAxis();
LineChart<Number,Number> chart = new LineChart<Number,Number>(x,y);
XYChart.Series series1 = ...;
// creating a series then giving this series 100 random values
chart.getData().add(series1);
chartPane.getChildren().add(chart);

chart.setOnMousePressed((MouseEvent event) -> {

Point2D mouseSceneCoords = new Point2D(event.getSceneX(), event.getSceneY());
double x = xAxis.sceneToLocal(mouseSceneCoords).getX();
double y = yAxis.sceneToLocal(mouseSceneCoords).getY();

primaryStage.setTitle("" +
xAxis.getValueForDisplay(x) + ", " +
yAxis.getValueForDisplay(y)
);
});

现在我已成功将您的代码应用到我的应用程序中,但我遇到了一个问题: enter image description here我的鼠标指向点 (800,10) 但我的光标完全远离鼠标的位置。

最佳答案

关键是使用 StackPane。这样你就可以添加一个 Pane 来处理这两行。这是一个粗略的例子:

import javafx.animation.AnimationTimer;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.ValueAxis;
import javafx.scene.chart.XYChart;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.scene.shape.Line;
import javafx.stage.Stage;

public class LineChartExperiments extends Application
{

double mouseX = 0;
double mouseY = 0;

@Override
public void start(Stage primaryStage) throws Exception
{

primaryStage.setTitle("LineChart Experiments");

ValueAxis xAxis = new NumberAxis();
xAxis.setLabel("No of employees");

ValueAxis yAxis = new NumberAxis();
yAxis.setLabel("Revenue per employee");

LineChart lineChart = new LineChart(xAxis, yAxis);

XYChart.Series dataSeries1 = new XYChart.Series();
dataSeries1.setName("2014");

dataSeries1.getData().add(new XYChart.Data(1, 567));
dataSeries1.getData().add(new XYChart.Data(5, 612));
dataSeries1.getData().add(new XYChart.Data(10, 800));
dataSeries1.getData().add(new XYChart.Data(20, 780));
dataSeries1.getData().add(new XYChart.Data(40, 810));
dataSeries1.getData().add(new XYChart.Data(80, 850));

lineChart.getData().add(dataSeries1);

Line verticleLine = new Line();
verticleLine.setStrokeWidth(3);
Line horizontalLine = new Line();
horizontalLine.setStrokeWidth(3);

Pane pane = new Pane(verticleLine, horizontalLine);

AnimationTimer loop = new AnimationTimer()
{
@Override
public void handle(long now)
{
verticleLine.setStartY(0);
verticleLine.setEndY(pane.getHeight());
verticleLine.setEndX(mouseX);
verticleLine.setStartX(mouseX);

horizontalLine.setStartX(0);
horizontalLine.setEndX(pane.getWidth());
horizontalLine.setEndY(mouseY);
horizontalLine.setStartY(mouseY);
}
};

pane.addEventFilter(MouseEvent.ANY, event -> {
mouseX = event.getSceneX();
mouseY = event.getSceneY();

loop.start();
});

StackPane stackPane = new StackPane(lineChart, pane);

Scene scene = new Scene(stackPane, 400, 200);

primaryStage.setScene(scene);
primaryStage.setHeight(500);
primaryStage.setWidth(700);

primaryStage.show();

}

public static void main(String[] args)
{
Application.launch(args);
}
}

**此代码有很多缺陷,只能用作概念验证。

关于java - 如何根据鼠标位置在折线图上绘制十字准线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56151077/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com