图表JavaFX,我的悬停标签被图表的边缘隐藏

问题描述 投票:0回答:2

我再次遇到JavaFX图表的问题:D

上下文:

我的图表上有弹出/标签来显示悬停时的值:JavaFX LineChart Hover Values(Jewelsea答案)

问题:

但是当该点靠近图表边缘时,弹出窗口将被其隐藏。“有问题的图表,我突出显示了图表的边缘。”

有问题的图表,我突出显示了图表的边缘。

这是一个问题,因为我的弹出窗口更大并且显示更多信息(x值,y值和数据系列)

可能的解决方案:

  • 我可以检查边缘在哪里,以及弹出窗口是否隐藏。在这种情况下,我应该移动弹出窗口。但是,当我查看文档时,没有找到正确的方法:

XYChart

XYChart.Data#nodeProperty

  • 我可以将弹出窗口放在图表上方吗?就像CSS中的z-index。

代码:

import javafx.application.Application;
import javafx.collections.*;
import javafx.event.EventHandler;
import javafx.scene.*;
import javafx.scene.chart.*;
import javafx.scene.control.Label;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

/** 
 * Displays a LineChart which displays the value of a plotted Node when you hover over the Node. 
 * @author original, jewelsea https://gist.github.com/jewelsea
 */
public class LineChartWithHover extends Application {
  @SuppressWarnings("unchecked")
  @Override public void start(Stage stage) {
    final LineChart lineChart = new LineChart(
        new NumberAxis(), new NumberAxis(),
        FXCollections.observableArrayList(
            new XYChart.Series(
                "My portfolio",
                FXCollections.observableArrayList(
                    plot(0, 14, 15, 24, 34, 36, 22, 55, 43, 17, 29, 25)
                )
            )
        )
    );
    lineChart.setCursor(Cursor.CROSSHAIR);

    lineChart.setTitle("Stock Monitoring, 2013");

    stage.setScene(new Scene(lineChart, 500, 400));
    stage.show();
      System.out.println("test 1 = "+lineChart.getProperties());
  }

  /** @return plotted y values for monotonically increasing integer x values, starting from x=1 */
  public ObservableList<XYChart.Data<Integer, Integer>> plot(int... y) {
    final ObservableList<XYChart.Data<Integer, Integer>> dataset = FXCollections.observableArrayList();
    int i = 0;
    while (i < y.length) {
      final XYChart.Data<Integer, Integer> data = new XYChart.Data<>(i + 1, y[i]);
      data.setNode(
          new HoveredThresholdNode(
              (i == 0) ? 0 : y[i-1],
              y[i]
          )
      );

      dataset.add(data);
      i++;
    }

    return dataset;
  }

  /** a node which displays a value on hover, but is otherwise empty */
  class HoveredThresholdNode extends StackPane {
    HoveredThresholdNode(int priorValue, int value) {
      setPrefSize(15, 15);

      final Label label = createDataThresholdLabel(priorValue, value);

      setOnMouseEntered(new EventHandler<MouseEvent>() {
        @Override public void handle(MouseEvent mouseEvent) {
          getChildren().setAll(label);
          setCursor(Cursor.NONE);
          toFront();
        }
      });
      setOnMouseExited(new EventHandler<MouseEvent>() {
        @Override public void handle(MouseEvent mouseEvent) {
          getChildren().clear();
          setCursor(Cursor.CROSSHAIR);
        }
      });
    }

    private Label createDataThresholdLabel(int priorValue, int value) {
      final Label label = new Label(value + "");
      label.getStyleClass().addAll("default-color0", "chart-line-symbol", "chart-series-line");
      label.setStyle("-fx-font-size: 20; -fx-font-weight: bold;");

      if (priorValue == 0) {
        label.setTextFill(Color.DARKGRAY);
      } else if (value > priorValue) {
        label.setTextFill(Color.FORESTGREEN);
      } else {
        label.setTextFill(Color.FIREBRICK);
      }

      label.setMinSize(Label.USE_PREF_SIZE, Label.USE_PREF_SIZE);
      return label;
    }
  }

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

先谢谢您!而且我对我的英语表示歉意,还在学习中!

charts javafx popup linechart
2个回答
3
投票

我一直在寻找JavaFX CSS参考指南,但找不到任何可以简单解决您问题的方法。

一种可能的解决方案是根据符号与最大值或最小值的距离来转换符号。

我根据您的代码写了这样的内容:

/**
 * Displays a LineChart which displays the value of a plotted Node when you hover over the Node.
 * @author original, jewelsea https://gist.github.com/jewelsea
 */
public class LineChartWithHover extends Application {


    @SuppressWarnings("unchecked")
    @Override public void start(Stage stage) {
        final LineChart lineChart = new LineChart(
                new NumberAxis(), new NumberAxis(),
                FXCollections.observableArrayList(
                        new XYChart.Series(
                                "My portfolio",
                                FXCollections.observableArrayList(
                                        plot(0, 14, 15, 24, 34, 36, 22, 55, 43, 17, 29, 25)
                                )
                        )
                )
        );
        lineChart.setCursor(Cursor.CROSSHAIR);

        lineChart.setTitle("Stock Monitoring, 2013");

        stage.setScene(new Scene(lineChart, 500, 400));
        stage.show();

        System.out.println("test 1 = "+lineChart.getProperties());
    }

    /** @return plotted y values for monotonically increasing integer x values, starting from x=1 */
    public ObservableList<XYChart.Data<Integer, Integer>> plot(Integer... y) {
        final ObservableList<XYChart.Data<Integer, Integer>> dataset = FXCollections.observableArrayList();
        int i = 0;
        List<Integer> list = Arrays.asList(y);
        int min = Collections.min(list);
        int max = Collections.max(list);
        int minThreshold = 5;
        int maxThreshold = 5;

        while (i < y.length) {
            final XYChart.Data<Integer, Integer> data = new XYChart.Data<>(i + 1, y[i]);
            int topMargin = 0;
            if(y[i] <= min + minThreshold) {
                topMargin = -50;
            } else if (y[i] >= max - maxThreshold) {
                topMargin = 50;
            }
            StackPane stackPane = new HoveredThresholdNode(
                    (i == 0) ? 0 : y[i-1],
                    y[i],
                    topMargin
            );

            data.setNode(stackPane);

            dataset.add(data);
            i++;
        }

        return dataset;
    }

    /** a node which displays a value on hover, but is otherwise empty */
    class HoveredThresholdNode extends StackPane {
        HoveredThresholdNode(int priorValue, int value, int topMargin) {
            setPrefSize(15, 15);

            final Label label = createDataThresholdLabel(priorValue, value);

            setOnMouseEntered(new EventHandler<MouseEvent>() {
                @Override public void handle(MouseEvent mouseEvent) {
                    getChildren().setAll(label);
                    setCursor(Cursor.NONE);
                    toFront();
                    setMargin(label, new Insets(topMargin,0,0,0));
                }
            });
            setOnMouseExited(new EventHandler<MouseEvent>() {
                @Override public void handle(MouseEvent mouseEvent) {
                    getChildren().clear();
                    setCursor(Cursor.CROSSHAIR);
                }
            });

        }

        private Label createDataThresholdLabel(int priorValue, int value) {
            final Label label = new Label(value + "");
            label.getStyleClass().addAll("default-color0", "chart-line-symbol", "chart-series-line");
            label.setStyle("-fx-font-size: 20; -fx-font-weight: bold;");

            if (priorValue == 0) {
                label.setTextFill(Color.DARKGRAY);
            } else if (value > priorValue) {
                label.setTextFill(Color.FORESTGREEN);
            } else {
                label.setTextFill(Color.FIREBRICK);
            }

            label.setMinSize(Label.USE_PREF_SIZE, Label.USE_PREF_SIZE);
            return label;
        }
    }

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

基本上,我只是说必须转换所有<= min + 5和> = max-5的值。

+/- 5是任意的,应该根据刻度线和绘图比例进行计算,以实现完美的重新定位。无论如何,即使不进行任何数学运算,它仍然是令人满意的。


0
投票

基于Kwoinkwoin先生的解决方案,我写了自己的书。我不确定是否有可能对其进行优化或改进。但到目前为止看来似乎对我有用。

import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.Month;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import javafx.application.Application;
import javafx.collections.ObservableList;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Cursor;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.NumberAxis.DefaultFormatter;
import javafx.scene.chart.XYChart.Data;
import javafx.scene.chart.XYChart.Series;
import javafx.scene.control.Label;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.StackPane;
import static javafx.scene.layout.StackPane.setMargin;
import javafx.stage.Stage;
import javafx.util.StringConverter;

public class StockLineChartApp extends Application {

    private LineChart<Number, Number> chart;
    private Series<Number, Number> series;
    private NumberAxis xAxis;

    private ZonedDateTime time;

    public StockLineChartApp() {
        time = ZonedDateTime.of(LocalDateTime.of(LocalDate.of(2000, Month.JANUARY, 1), LocalTime.NOON), ZoneId.systemDefault());

    }

    public Parent createContent() {
        xAxis = new NumberAxis();
        xAxis.setLabel("Date/Time");
        xAxis.setForceZeroInRange(false);
        xAxis.setAutoRanging(true);
        xAxis.setTickLabelFormatter(new StringConverter<Number>() {
            @Override
            public String toString(Number t) {

                long longValue = t.longValue();

                ZonedDateTime zd = convertLongToZonedDateTime(longValue);
                String formatDate = formatDate(zd, "dd/MM/yyyy");

                return formatDate;
            }

            @Override
            public Number fromString(String string) {

                ZonedDateTime dl = ZonedDateTime.parse(string, DateTimeFormatter.ofPattern("dd/MM/yyyy"));
                long toEpochMilli = dl.toEpochSecond();

                //DateTimeFormatter.ofPattern(string).p
                return toEpochMilli;
            }
        });

        final NumberAxis yAxis = new NumberAxis();
        yAxis.setAutoRanging(true);
        chart = new LineChart<>(xAxis, yAxis);
        chart.setCursor(Cursor.CROSSHAIR);
        chart.setAlternativeRowFillVisible(true);
        chart.setAlternativeColumnFillVisible(true);

        // setup chart
        //final String stockLineChartCss= getClass().getResource("StockLineChart.css").toExternalForm();
        //chart.getStylesheets().add(stockLineChartCss);
        chart.setCreateSymbols(true);
        chart.setAnimated(true);
        chart.setLegendVisible(true);
        chart.setTitle("ACME Company Stock");

        yAxis.setLabel("Share Price");
        yAxis.setTickLabelFormatter(new DefaultFormatter(yAxis, "$", null));
        // add starting data
        series = new Series<>();
        series.setName("Data por Peça");

        for (double m = 0; m < (10); m++) {
            long data = nextTime();
            addData(data, (long) (Math.random() * 10));
            System.out.println(data);
        }

        //chart.
        chart.getData().add(series);
        //chart.getData().add(hourDataSeries);

        return chart;
    }

    private void addData(long x, long y) {

        Data<Number, Number> data = new Data<Number, Number>(x, y);
        series.getData().add(data);

        ZonedDateTime zd = convertLongToZonedDateTime(x);
        String formatDate = formatDate(zd, "dd/MM/yyyy");
        //String text = "(" + formatDate + ";" + y + ")";
        String text = y + "";
        if (text.length() > 4) {
            text = text.substring(0, 4);
        }

        String t = formatDate + "\nValor: " + text;
        data.setNode(new HoveredThresholdNode(t, data));
    }

    public static long convertZonedDateTimeToLong(ZonedDateTime zonedDateTime) {

        long e = zonedDateTime.toInstant().toEpochMilli();

        return e;

    }

    private long nextTime() {

        time = time.plusYears(10);
        return convertZonedDateTimeToLong(time);
    }

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

        Parent createContent = createContent();

        //
        final StackPane pane = new StackPane();
        pane.getChildren().add(createContent);
        final Scene scene = new Scene(pane, 500, 400);
        //new ZoomManager(pane, chart, series);

        //
        primaryStage.setScene(scene);
        primaryStage.show();

    }

    public static String formatDate(ZonedDateTime ts, String format) {
        try {

            if (ts == null) {
                return "";
            }

            String format1 = ts.format(DateTimeFormatter.ofPattern(format));
            return format1;

        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return "";

    }

    public static ZonedDateTime convertLongToZonedDateTime(long e) {

        Instant i = Instant.ofEpochMilli(e);
        ZonedDateTime ofInstant = ZonedDateTime.ofInstant(i, ZoneId.systemDefault());

        return ofInstant;
    }

    /**
     * Java main for when running without JavaFX launcher
     */
    public static void main(String[] args) {
        launch(args);
    }

    public class HoveredThresholdNode extends StackPane {

        //Reference
        private Data<Number, Number> data;

        private Label label;
        private String value;

        public HoveredThresholdNode(String value, Data<Number, Number> data) {
            this.data = data;
            this.value = value;
            this.label = new Label(value);
            this.label.getStyleClass().clear();
            this.getStyleClass().clear();

            this.label.setStyle("-fx-font-size: 12; fx-text-fill: black;");
            this.label.getStyleClass().addAll("default-color0", "chart-line-symbol", "chart-series-line");

            this.label.setWrapText(true);
            this.label.setMinSize(Label.USE_PREF_SIZE, Label.USE_PREF_SIZE);

            setOnMouseEntered(new EventHandler<MouseEvent>() {
                @Override
                public void handle(MouseEvent mouseEvent) {

                    getChildren().setAll(label);

                    toFront();

                    boolean close_top = false, close_right = false, close_bottom = false, close_left = false;

                    long min_x = Long.MAX_VALUE;
                    long max_x = Long.MIN_VALUE;

                    long min_y = Long.MAX_VALUE;
                    long max_y = Long.MIN_VALUE;

                    ObservableList<Series<Number, Number>> chartSeries = chart.getData();
                    for (Series<Number, Number> s : chartSeries) {
                        ObservableList<Data<Number, Number>> chartData = s.getData();
                        for (Data<Number, Number> d : chartData) {
                            Number xValue = d.getXValue();
                            Number yValue = d.getYValue();

                            long kx = xValue.longValue();
                            long ky = yValue.longValue();

                            if (kx < min_x) {
                                min_x = kx;
                            }

                            if (kx > max_x) {
                                max_x = kx;
                            }

                            if (ky < min_y) {
                                min_y = ky;
                            }

                            if (ky > max_y) {
                                max_y = ky;
                            }

                        }

                    }

                    if (data.getXValue().longValue() - max_x == 0) {
                        close_right = true;
                    }

                    if (data.getXValue().longValue() - min_x == 0) {
                        close_left = true;
                    }

                    if (data.getYValue().longValue() - min_y == 0) {
                        close_bottom = true;
                    }

                    if (data.getYValue().longValue() - max_y == 0) {
                        close_top = true;
                    }

//                    System.out.println("\n");
//                    System.out.println("  close_right " + close_right);
//                    System.out.println("  close_left " + close_left);
//                    System.out.println("  close_bottom " + close_bottom);
//                    System.out.println("  close_top " + close_top);
                    double top = 0;
                    double right = 0;
                    double bottom = 0;
                    double left = 0;

                    if (close_top) {
                        top = 50;
                    }

                    if (close_bottom) {
                        bottom = 50;
                    }

                    if (close_right) {
                        right = 50;
                    }

                    if (close_left) {
                        left = 50;
                    }

                    setMargin(label, new Insets(top, right, bottom, left));
                }
            });

            setOnMouseExited(new EventHandler<MouseEvent>() {
                @Override
                public void handle(MouseEvent mouseEvent) {

                    getChildren().clear();
                }
            });

        }

        public HoveredThresholdNode copy() {
            HoveredThresholdNode copy = new HoveredThresholdNode(value, data);
            return copy;
        }

    }
}
© www.soinside.com 2019 - 2024. All rights reserved.