Search in sources :

Example 1 with SmoothedChartEvent

use of eu.hansolo.tilesfx.events.SmoothedChartEvent in project tilesfx by HanSolo.

the class SmoothedChart method select.

private void select(final MouseEvent EVT) {
    if (!isInteractive()) {
        return;
    }
    final double EVENT_X = EVT.getX();
    final double EVENT_Y = EVT.getY();
    final double CHART_X = chartPlotBackground.getBoundsInParent().getMinX();
    final double CHART_MIN_Y = chartPlotBackground.getBoundsInParent().getMinY();
    final double CHART_HEIGHT = chartPlotBackground.getBoundsInParent().getHeight();
    if (!(getYAxis() instanceof NumberAxis)) {
        return;
    }
    double upperBound = ((NumberAxis) getYAxis()).getUpperBound();
    double lowerBound = ((NumberAxis) getYAxis()).getLowerBound();
    double range = upperBound - lowerBound;
    double factor = range / getYAxis().getLayoutBounds().getHeight();
    List<PathElement> elements = null;
    int noOfElements = 0;
    Bounds pathBounds = null;
    double pathMinX = 0;
    double pathWidth = 0;
    PathElement lastElement = null;
    Series<X, Y> series = null;
    for (Series<X, Y> s : getData()) {
        Path[] paths = getPaths(s);
        // AREA == 0, LINE == 1 in ChartType enum
        int type = getChartType().ordinal();
        if (paths[type].contains(EVENT_X, EVENT_Y)) {
            series = s;
            elements = paths[type].getElements();
            noOfElements = elements.size();
            lastElement = elements.get(0);
            pathBounds = paths[1].getLayoutBounds();
            pathMinX = pathBounds.getMinX();
            pathWidth = pathBounds.getWidth();
            break;
        }
    }
    if (null == series || series.getData().isEmpty()) {
        return;
    }
    if (isSnapToTicks()) {
        double reverseFactor = CHART_HEIGHT / range;
        int noOfDataElements = series.getData().size();
        double interval = pathWidth / (double) (noOfDataElements - 1);
        int selectedIndex = Helper.roundDoubleToInt((EVENT_X - pathMinX) / interval);
        Data<X, Y> selectedData = series.getData().get(selectedIndex);
        Y selectedYValue = selectedData.getYValue();
        if (!(selectedYValue instanceof Number)) {
            return;
        }
        double selectedValue = ((Number) selectedYValue).doubleValue();
        selector.setCenterX(pathMinX + CHART_X + interval * selectedIndex);
        selector.setCenterY((CHART_MIN_Y + CHART_HEIGHT) - (selectedValue * reverseFactor));
        selector.setVisible(true);
        fadeInFadeOut.playFrom(Duration.millis(0));
        Point2D tooltipLocation = selector.localToScreen(selector.getCenterX(), selector.getCenterY());
        String tooltipText = new StringBuilder(selectedData.getXValue().toString()).append("\n").append(selectedData.getYValue()).toString();
        selectorTooltip.setText(tooltipText);
        selectorTooltip.setX(tooltipLocation.getX());
        selectorTooltip.setY(tooltipLocation.getY());
        selectorTooltip.show(getScene().getWindow());
        fireEvent(new SmoothedChartEvent(SmoothedChart.this, null, SmoothedChartEvent.DATA_SELECTED, selectedValue));
    } else {
        for (int i = 1; i < noOfElements; i++) {
            PathElement element = elements.get(i);
            double[] xy = getXYFromPathElement(lastElement);
            double[] xy1 = getXYFromPathElement(element);
            if (xy[0] < 0 || xy[1] < 0 || xy1[0] < 0 || xy1[1] < 0) {
                continue;
            }
            if (EVENT_X > xy[0] && EVENT_X < xy1[0]) {
                double deltaX = xy1[0] - xy[0];
                double deltaY = xy1[1] - xy[1];
                double m = deltaY / deltaX;
                double y = m * (EVT.getX() - xy[0]) + xy[1];
                double selectedValue = ((getYAxis().getLayoutBounds().getHeight() - y) * factor + lowerBound);
                selector.setCenterX(CHART_X + EVT.getX());
                selector.setCenterY(CHART_MIN_Y + y);
                selector.setVisible(true);
                fadeInFadeOut.playFrom(Duration.millis(0));
                Point2D tooltipLocation = selector.localToScreen(selector.getCenterX(), selector.getCenterY());
                String tooltipText = new StringBuilder(String.format(Locale.US, formatString, selectedValue)).toString();
                selectorTooltip.setText(tooltipText);
                selectorTooltip.setX(tooltipLocation.getX());
                selectorTooltip.setY(tooltipLocation.getY());
                selectorTooltip.show(getScene().getWindow());
                fireEvent(new SmoothedChartEvent(SmoothedChart.this, null, SmoothedChartEvent.DATA_SELECTED, selectedValue));
                break;
            }
            lastElement = element;
        }
    }
}
Also used : ClosePath(javafx.scene.shape.ClosePath) Path(javafx.scene.shape.Path) NumberAxis(javafx.scene.chart.NumberAxis) Bounds(javafx.geometry.Bounds) Paint(javafx.scene.paint.Paint) Point(eu.hansolo.tilesfx.tools.Point) SmoothedChartEvent(eu.hansolo.tilesfx.events.SmoothedChartEvent) PathElement(javafx.scene.shape.PathElement) Point2D(javafx.geometry.Point2D)

Aggregations

SmoothedChartEvent (eu.hansolo.tilesfx.events.SmoothedChartEvent)1 Point (eu.hansolo.tilesfx.tools.Point)1 Bounds (javafx.geometry.Bounds)1 Point2D (javafx.geometry.Point2D)1 NumberAxis (javafx.scene.chart.NumberAxis)1 Paint (javafx.scene.paint.Paint)1 ClosePath (javafx.scene.shape.ClosePath)1 Path (javafx.scene.shape.Path)1 PathElement (javafx.scene.shape.PathElement)1