Search in sources :

Example 6 with TileEvent

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

the class SmoothAreaChartTileSkin method select.

private void select(final MouseEvent EVT) {
    final double EVENT_X = EVT.getX();
    final double CHART_X = 0;
    final double CHART_WIDTH = width;
    if (Double.compare(EVENT_X, CHART_X) < 0 || Double.compare(EVENT_X, CHART_WIDTH) > 0) {
        return;
    }
    double upperBound = tile.getChartData().stream().max(Comparator.comparing(ChartData::getValue)).get().getValue();
    double range = upperBound - tile.getMinValue();
    double factor = range / (height * 0.5);
    List<PathElement> elements = strokePath.getElements();
    int noOfElements = elements.size();
    PathElement lastElement = elements.get(0);
    if (tile.isSnapToTicks()) {
        double reverseFactor = (height * 0.5) / range;
        int noOfDataElements = tile.getChartData().size();
        double interval = width / (double) (noOfDataElements - 1);
        int selectedIndex = Helper.roundDoubleToInt(EVENT_X / interval);
        ChartData selectedData = tile.getChartData().get(selectedIndex);
        double selectedValue = selectedData.getValue();
        selector.setCenterX(interval * selectedIndex);
        selector.setCenterY(height - selectedValue * reverseFactor);
        selector.setVisible(true);
        fadeInFadeOut.playFrom(Duration.millis(0));
        String tooltipText = new StringBuilder(selectedData.getName()).append("\n").append(String.format(locale, formatString, selectedValue)).toString();
        Point2D popupLocation = tile.localToScreen(selector.getCenterX() - selectorTooltip.getWidth() * 0.5, selector.getCenterY() - size * 0.025 - selectorTooltip.getHeight());
        selectorTooltip.setText(tooltipText);
        selectorTooltip.setX(popupLocation.getX());
        selectorTooltip.setY(popupLocation.getY());
        selectorTooltip.show(tile.getScene().getWindow());
        tile.fireTileEvent(new TileEvent(EventType.SELECTED_CHART_DATA, selectedData));
    } else {
        for (int i = 1; i < noOfElements; i++) {
            PathElement element = elements.get(i);
            double[] xy = getXYFromPathElement(lastElement);
            double[] xy1 = getXYFromPathElement(element);
            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 = upperBound - (y - (height * 0.5)) * factor;
                selector.setCenterX(EVT.getX());
                selector.setCenterY(y);
                selector.setVisible(true);
                fadeInFadeOut.playFrom(Duration.millis(0));
                Point2D popupLocation = tile.localToScreen(EVT.getX() - selectorTooltip.getWidth() * 0.5, selector.getCenterY() - size * 0.025 - selectorTooltip.getHeight());
                selectorTooltip.setText(String.format(locale, formatString, selectedValue));
                selectorTooltip.setX(popupLocation.getX());
                selectorTooltip.setY(popupLocation.getY());
                selectorTooltip.show(tile.getScene().getWindow());
                tile.fireTileEvent(new TileEvent(EventType.SELECTED_CHART_DATA, new ChartData(selectedValue)));
                break;
            }
            lastElement = element;
        }
    }
}
Also used : TileEvent(eu.hansolo.tilesfx.events.TileEvent) ChartData(eu.hansolo.tilesfx.chart.ChartData) PathElement(javafx.scene.shape.PathElement) Point2D(javafx.geometry.Point2D) Point(eu.hansolo.tilesfx.tools.Point)

Example 7 with TileEvent

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

the class WorldMapTileSkin method registerListeners.

@Override
protected void registerListeners() {
    super.registerListeners();
    countryPaths.forEach((name, pathList) -> {
        Country country = Country.valueOf(name);
        EventHandler<MouseEvent> clickHandler = e -> tile.fireTileEvent(new TileEvent(EventType.SELECTED_CHART_DATA, new ChartData(country.getName(), country.getValue(), country.getColor())));
        pathList.forEach(path -> {
            handlerMap.put(path, clickHandler);
            path.addEventHandler(MouseEvent.MOUSE_PRESSED, clickHandler);
        });
    });
    tile.getPoiList().addListener(poiListener);
    tile.getChartData().addListener(chartDataListener);
}
Also used : EventHandler(javafx.event.EventHandler) Fonts(eu.hansolo.tilesfx.fonts.Fonts) Location(eu.hansolo.tilesfx.tools.Location) CacheHint(javafx.scene.CacheHint) MouseEvent(javafx.scene.input.MouseEvent) EventType(eu.hansolo.tilesfx.events.TileEvent.EventType) FXCollections(javafx.collections.FXCollections) HashMap(java.util.HashMap) CountryPath(eu.hansolo.tilesfx.tools.CountryPath) ChartData(eu.hansolo.tilesfx.chart.ChartData) Helper(eu.hansolo.tilesfx.tools.Helper) ListChangeListener(javafx.collections.ListChangeListener) Locale(java.util.Locale) Country(eu.hansolo.tilesfx.tools.Country) Map(java.util.Map) Helper.clamp(eu.hansolo.tilesfx.tools.Helper.clamp) Circle(javafx.scene.shape.Circle) Tooltip(javafx.scene.control.Tooltip) Pane(javafx.scene.layout.Pane) Tile(eu.hansolo.tilesfx.Tile) Color(javafx.scene.paint.Color) TextSize(eu.hansolo.tilesfx.Tile.TextSize) Font(javafx.scene.text.Font) ObservableMap(javafx.collections.ObservableMap) Group(javafx.scene.Group) Text(javafx.scene.text.Text) List(java.util.List) TileEvent(eu.hansolo.tilesfx.events.TileEvent) LocationEvent(eu.hansolo.tilesfx.events.LocationEvent) WeakListChangeListener(javafx.collections.WeakListChangeListener) TileEvent(eu.hansolo.tilesfx.events.TileEvent) MouseEvent(javafx.scene.input.MouseEvent) ChartData(eu.hansolo.tilesfx.chart.ChartData) Country(eu.hansolo.tilesfx.tools.Country)

Example 8 with TileEvent

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

the class RadialChartTileSkin method initGraphics.

// ******************** Initialization ************************************
@Override
protected void initGraphics() {
    super.initGraphics();
    chartEventListener = e -> drawChart();
    tile.getChartData().forEach(chartData -> chartData.addChartDataEventListener(chartEventListener));
    chartDataListener = c -> {
        while (c.next()) {
            if (c.wasAdded()) {
                c.getAddedSubList().forEach(addedItem -> addedItem.addChartDataEventListener(chartEventListener));
            } else if (c.wasRemoved()) {
                c.getRemoved().forEach(removedItem -> removedItem.removeChartDataEventListener(chartEventListener));
            }
        }
        drawChart();
    };
    clickHandler = e -> {
        double x = e.getX();
        double y = e.getY();
        double startAngle = 90;
        List<ChartData> dataList = tile.getChartData();
        int noOfItems = dataList.size();
        double canvasSize = chartCanvas.getWidth();
        double barWidth = canvasSize * 0.1;
        double max = noOfItems == 0 ? 0 : dataList.stream().max(Comparator.comparingDouble(ChartData::getValue)).get().getValue();
        for (int i = 0; i < noOfItems; i++) {
            ChartData data = dataList.get(i);
            double value = clamp(0, Double.MAX_VALUE, data.getValue());
            double barXY = barWidth * 0.5 + i * barWidth;
            double barWH = canvasSize - barWidth - (2 * i * barWidth);
            double angle = value / max * 270.0;
            double centerX = barXY + barWH * 0.5;
            double centerY = centerX;
            boolean hit = Helper.isInRingSegment(x, y, centerX, centerY, (barWH + barWidth) * 0.5, (barWH - barWidth) * 0.5, startAngle, angle);
            if (hit) {
                tile.fireTileEvent(new TileEvent(EventType.SELECTED_CHART_DATA, data));
                break;
            }
        }
    };
    titleText = new Text();
    titleText.setFill(tile.getTitleColor());
    Helper.enableNode(titleText, !tile.getTitle().isEmpty());
    text = new Text(tile.getText());
    text.setFill(tile.getTextColor());
    Helper.enableNode(text, tile.isTextVisible());
    chartCanvas = new Canvas(size * 0.9, tile.isTextVisible() ? size * 0.72 : size * 0.795);
    chartCtx = chartCanvas.getGraphicsContext2D();
    getPane().getChildren().addAll(titleText, chartCanvas, text);
}
Also used : Tile(eu.hansolo.tilesfx.Tile) EventHandler(javafx.event.EventHandler) Color(javafx.scene.paint.Color) Fonts(eu.hansolo.tilesfx.fonts.Fonts) GraphicsContext(javafx.scene.canvas.GraphicsContext) MouseEvent(javafx.scene.input.MouseEvent) Font(javafx.scene.text.Font) ChartDataEventListener(eu.hansolo.tilesfx.events.ChartDataEventListener) EventType(eu.hansolo.tilesfx.events.TileEvent.EventType) Canvas(javafx.scene.canvas.Canvas) ArcType(javafx.scene.shape.ArcType) Text(javafx.scene.text.Text) ChartData(eu.hansolo.tilesfx.chart.ChartData) List(java.util.List) Helper(eu.hansolo.tilesfx.tools.Helper) ListChangeListener(javafx.collections.ListChangeListener) TileEvent(eu.hansolo.tilesfx.events.TileEvent) VPos(javafx.geometry.VPos) Locale(java.util.Locale) StrokeLineCap(javafx.scene.shape.StrokeLineCap) TextAlignment(javafx.scene.text.TextAlignment) Helper.clamp(eu.hansolo.tilesfx.tools.Helper.clamp) Comparator(java.util.Comparator) TileEvent(eu.hansolo.tilesfx.events.TileEvent) ChartData(eu.hansolo.tilesfx.chart.ChartData) Canvas(javafx.scene.canvas.Canvas) Text(javafx.scene.text.Text)

Example 9 with TileEvent

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

the class SmoothedChartTileSkin method initGraphics.

// ******************** Initialization ************************************
@Override
protected void initGraphics() {
    super.initGraphics();
    chartEventEventHandler = e -> tile.fireTileEvent(new TileEvent(EventType.SELECTED_CHART_DATA, new ChartData(e.getValue())));
    titleText = new Text();
    titleText.setFill(tile.getTitleColor());
    Helper.enableNode(titleText, !tile.getTitle().isEmpty());
    xAxis = tile.getXAxis();
    yAxis = tile.getYAxis();
    chart = new SmoothedChart<>(xAxis, yAxis);
    chart.setSmoothed(tile.isSmoothing());
    chart.setAnimated(tile.isAnimated());
    chart.setLegendSide(Side.TOP);
    chart.setVerticalZeroLineVisible(false);
    chart.setCreateSymbols(false);
    chart.setSnapToTicks(tile.isSnapToTicks());
    chart.setSymbolsVisible(tile.getDataPointsVisible());
    switch(tile.getChartType()) {
        case AREA:
            chart.setChartType(SmoothedChart.ChartType.AREA);
            break;
        default:
            chart.setChartType(SmoothedChart.ChartType.LINE);
            break;
    }
    getPane().getChildren().addAll(titleText, chart);
    // Add series not before chart is part of scene
    chart.getData().setAll(tile.getTilesFXSeries().stream().map(tilesFxSeries -> tilesFxSeries.getSeries()).collect(Collectors.toList()));
    // Adjust colors according to series settings
    tile.getTilesFXSeries().stream().forEach(series -> chart.setSeriesColor(series.getSeries(), series.getStroke(), series.getFill(), series.getSymbolBackground(), series.getLegendSymbolFill()));
/*
        Scene scene = chart.getScene();
        if (scene != null) {
            Window stage = scene.getWindow();
            if (stage != null) {
                if (stage.isShowing()) {
                    chart.getData().setAll(tile.getTilesFXSeries().stream().map(tilesFxSeries -> tilesFxSeries.getSeries()).collect(Collectors.toList()));
                }
            }
        }
        */
}
Also used : TileEvent(eu.hansolo.tilesfx.events.TileEvent) ChartData(eu.hansolo.tilesfx.chart.ChartData) Text(javafx.scene.text.Text)

Example 10 with TileEvent

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

the class CalendarTileSkin method checkClick.

private void checkClick(final MouseEvent EVENT) {
    Label selectedLabel = ((Label) EVENT.getSource());
    String selectedText = selectedLabel.getText();
    if (null == selectedText || selectedText.isEmpty() || !Character.isDigit(selectedText.charAt(0))) {
        return;
    }
    if (selectedLabel.getBorder() != null && selectedLabel.getBorder().equals(weekBorder)) {
        return;
    }
    int selectedNo = Integer.parseInt(selectedText);
    if (selectedNo > 31) {
        return;
    }
    List<ChartData> dataList = tile.getChartData();
    ZonedDateTime time = tile.getTime();
    LocalDate selectedDate = LocalDate.of(time.getYear(), time.getMonth(), selectedNo);
    Optional<ChartData> selectedChartData = dataList.stream().filter(data -> data.getTimestampAsLocalDate().isEqual(selectedDate)).findAny();
    if (selectedChartData.isPresent()) {
        tile.fireTileEvent(new TileEvent(EventType.SELECTED_CHART_DATA, selectedChartData.get()));
    }
}
Also used : EventHandler(javafx.event.EventHandler) Pos(javafx.geometry.Pos) Fonts(eu.hansolo.tilesfx.fonts.Fonts) MouseEvent(javafx.scene.input.MouseEvent) ZonedDateTime(java.time.ZonedDateTime) EventType(eu.hansolo.tilesfx.events.TileEvent.EventType) ArrayList(java.util.ArrayList) ChartData(eu.hansolo.tilesfx.chart.ChartData) Insets(javafx.geometry.Insets) Helper(eu.hansolo.tilesfx.tools.Helper) BorderWidths(javafx.scene.layout.BorderWidths) BackgroundFill(javafx.scene.layout.BackgroundFill) Locale(java.util.Locale) Tile(eu.hansolo.tilesfx.Tile) Color(javafx.scene.paint.Color) Label(javafx.scene.control.Label) TextStyle(java.time.format.TextStyle) Border(javafx.scene.layout.Border) Font(javafx.scene.text.Font) Background(javafx.scene.layout.Background) DAY_OF_MONTH(java.time.temporal.ChronoField.DAY_OF_MONTH) BorderStrokeStyle(javafx.scene.layout.BorderStrokeStyle) BorderStroke(javafx.scene.layout.BorderStroke) Text(javafx.scene.text.Text) List(java.util.List) TileEvent(eu.hansolo.tilesfx.events.TileEvent) IsoFields(java.time.temporal.IsoFields) DayOfWeek(java.time.DayOfWeek) LocalDate(java.time.LocalDate) DateTimeFormatter(java.time.format.DateTimeFormatter) Optional(java.util.Optional) CornerRadii(javafx.scene.layout.CornerRadii) TileEvent(eu.hansolo.tilesfx.events.TileEvent) ChartData(eu.hansolo.tilesfx.chart.ChartData) ZonedDateTime(java.time.ZonedDateTime) Label(javafx.scene.control.Label) LocalDate(java.time.LocalDate)

Aggregations

TileEvent (eu.hansolo.tilesfx.events.TileEvent)12 Text (javafx.scene.text.Text)10 ChartData (eu.hansolo.tilesfx.chart.ChartData)9 Tile (eu.hansolo.tilesfx.Tile)8 Fonts (eu.hansolo.tilesfx.fonts.Fonts)8 Helper (eu.hansolo.tilesfx.tools.Helper)8 List (java.util.List)8 EventHandler (javafx.event.EventHandler)8 MouseEvent (javafx.scene.input.MouseEvent)8 Font (javafx.scene.text.Font)8 EventType (eu.hansolo.tilesfx.events.TileEvent.EventType)7 ListChangeListener (javafx.collections.ListChangeListener)6 Color (javafx.scene.paint.Color)6 ChartDataEventListener (eu.hansolo.tilesfx.events.ChartDataEventListener)5 Locale (java.util.Locale)5 Comparator (java.util.Comparator)4 HashMap (java.util.HashMap)4 Map (java.util.Map)4 WeakListChangeListener (javafx.collections.WeakListChangeListener)4 Pane (javafx.scene.layout.Pane)4