月と値のJavaFXlineチャートがあります。グラフをクリックするたびに、月と折れ線グラフの値をコンソールに出力しようとしています。問題はgetValueForDisplay
、CategoryAxis
オブジェクトでメソッドを呼び出すと、が返されることnull
です。
(x、y):jan、3をクリックすると
、印刷したいのです
X value = jan Y value: 3
が、プログラムは次のように印刷しています。
X value = **null** Y value: 2.7796610169491527
別の質問;
2)座標を印刷しようとすると、キャリブレーションされていないように見えるのはなぜですか(3をクリックすると、プログラムの印刷2.779661016949152
ですか?コンソールに印刷された値を確認すると、この問題に気付くことがあります。
これが私が使用しているコードです:
public class LineChartTest extends Application {
String xValue;
Number yValue;
private void init(Stage primaryStage) {
Group root = new Group();
primaryStage.setScene(new Scene(root));
final CategoryAxis xAxis = new CategoryAxis();
final NumberAxis yAxis = new NumberAxis("Values for Y-Axis", 0, 3, 1);
yAxis.setUpperBound(5);
ObservableList<XYChart.Series<String,Double>> lineChartData = FXCollections.observableArrayList(
new LineChart.Series<String,Double>("en", FXCollections.observableArrayList(
new XYChart.Data<String,Double>("jan", 1.0),
new XYChart.Data<String,Double>("feb", 2.0),
new XYChart.Data<String,Double>("mar", 3.0),
new XYChart.Data<String,Double>("apr", 4.0),
new XYChart.Data<String,Double>("may", 0.5)
)),
new LineChart.Series<String,Double>("to", FXCollections.observableArrayList(
new XYChart.Data<String,Double>("jan", 1.6),
new XYChart.Data<String,Double>("feb", 0.4),
new XYChart.Data<String,Double>("mar", 2.9),
new XYChart.Data<String,Double>("apr", 1.3),
new XYChart.Data<String,Double>("may", 0.9)
))
);
//define a new line
final Series<String,Double> otherSeries = new Series<String,Double>();
//set points
otherSeries.getData().add(new XYChart.Data("jan",3));
otherSeries.getData().add(new XYChart.Data("feb",2));
otherSeries.getData().add(new XYChart.Data("mar",4));
//set name to the line
otherSeries.setName("tre");
//add the new line to the graph
lineChartData.add(otherSeries);
final LineChart chart = new LineChart(xAxis, yAxis, lineChartData);
//set title to the figure
chart.setTitle("Line Chart Test");
root.getChildren().add(chart);
chart.setOnMouseClicked(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent t) {
//Why this seems to be not working properly?
double sceneX = t.getSceneX();
double sceneY = t.getSceneY();
//gives the point of the graph
xValue = xAxis.getValueForDisplay(sceneX);
yValue = yAxis.getValueForDisplay(sceneY);
System.out.println("sceneX-25: "+ sceneX);
System.out.println("getDisplayPosition: " + xAxis.getValueForDisplay(sceneX));
//Test
System.out.println(" X value = " +
xValue + " Y value: " + yValue );
}
});
}
@Override public void start(Stage primaryStage) throws Exception {
init(primaryStage);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
答え:
SergeyGrinevとUlukBiyに感謝します
前のコードでは、変更する必要があります。
折れ線グラフコード:
chart.setOnMouseClicked(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent e) {
Node chartPlotBackground = chart.lookup(".chart-plot-background");
final double shiftX = xSceneShift(chartPlotBackground);
final double shiftY = ySceneShift(chartPlotBackground);
double x = e.getSceneX() - shiftX;
double y = e.getSceneY() - shiftY;
xValue = xAxis.getValueForDisplay(x);
yValue = yAxis.getValueForDisplay(y );
System.out.println("shiftX = " + shiftX + " shiftY: " + shiftY);
System.out.println("X value = "
+ xValue + " \nY value: " + yValue);
}
});
}
//recursive calls
private double xSceneShift(Node node) {
return node.getParent() == null ? 0 : node.getBoundsInParent().getMinX() + xSceneShift(node.getParent());
}
private double ySceneShift(Node node) {
return node.getParent() == null ? 0 : node.getBoundsInParent().getMinY() + ySceneShift(node.getParent());
}
@Override
public void start(Stage primaryStage) throws Exception {
init(primaryStage);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
ソースコードでCategoryAxis
変更する必要があります(コメントを見てください!):
@Override public String getValueForDisplay(double displayPosition) {
if (getSide().equals(Side.TOP) || getSide().equals(Side.BOTTOM)) { // HORIZONTAL
if (displayPosition < 0 || displayPosition > getHeight()) return null; // <-------------- WRONG SHOULD BE displayPosition > getWidth()
double d = (displayPosition - firstCategoryPos.get()) / categorySpacing.get();
return toRealValue(d);
} else { // VERTICAL
if (displayPosition < 0 || displayPosition > getWidth()) return null; // <-------------- WRONG SHOULD BE displayPosition > getHeight()
double d = (displayPosition - firstCategoryPos.get()) / (categorySpacing.get() * -1);
return toRealValue(d);
}
}
ここでソースコードを見つけることができます: