LoginSignup
1
3

More than 5 years have passed since last update.

JavaFXのLineChartでクリックしたところのx座標を取ってくる

Posted at

環境

  • Java SE 8u20

コード一部抜粋

...
TakeXYAction action = new TakeXYAction(chart);
chart.lookup(".chart-plot-background").setOnMouseClicked(action);
...
TakeXYAction.java
final class TakeXYAction implements EventHandler<MouseEvent> {
    private final LineChart<Number, Number> chart;
    public TakeXYAction(LineChart<Number, Number> _chart) {
        this.chart = _chart;
    }

    @Override
    public void handle(MouseEvent event) {
        Axis<Number> xAxis  = this.chart.getXAxis();
        double       x      = xAxis.getValueForDisplay(event.getX()).doubleValue();
...
    }
}

要点

  • Chartに対してsetOnMouseClicked(action)をしない
  • lookup(".chart-plot-bakground")でとってこれるNode(Region)に対してsetOnMouseClicked(action)をする
  • event.getX()をAxis#getValueForDisplayに突っ込んでとる
1
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
3