Search in sources :

Example 6 with ChartData

use of eu.hansolo.tilesfx.chart.ChartData in project tilesfx by HanSolo.

the class DonutChartTileSkin method initGraphics.

// ******************** Initialization ************************************
@Override
protected void initGraphics() {
    super.initGraphics();
    chartEventListener = e -> redraw();
    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();
        drawLegend();
    };
    clickHandler = e -> {
        double x = e.getX();
        double y = e.getY();
        double startAngle = 90;
        double angle = 0;
        List<ChartData> dataList = tile.isSortedData() ? tile.getChartData().stream().sorted(Comparator.comparingDouble(ChartData::getValue)).collect(Collectors.toList()) : tile.getChartData();
        int noOfItems = dataList.size();
        double sum = dataList.stream().mapToDouble(ChartData::getValue).sum();
        double stepSize = 360.0 / sum;
        double barWidth = chartCanvas.getWidth() * 0.1;
        double ri = outerRadius - barWidth * 0.5;
        double ro = outerRadius + barWidth * 0.5;
        for (int i = 0; i < noOfItems; i++) {
            ChartData data = dataList.get(i);
            double value = data.getValue();
            startAngle -= angle;
            angle = value * stepSize;
            boolean hit = Helper.isInRingSegment(x, y, centerX, centerY, ro, ri, startAngle, angle);
            if (hit) {
                String tooltipText = new StringBuilder(data.getName()).append("\n").append(String.format(locale, formatString, value)).toString();
                Point2D popupLocation = new Point2D(e.getScreenX() - selectionTooltip.getWidth() * 0.5, e.getScreenY() - size * 0.025 - selectionTooltip.getHeight());
                selectionTooltip.setText(tooltipText);
                selectionTooltip.setX(popupLocation.getX());
                selectionTooltip.setY(popupLocation.getY());
                selectionTooltip.show(tile.getScene().getWindow());
                tile.fireTileEvent(new TileEvent(EventType.SELECTED_CHART_DATA, data));
                break;
            }
        }
    };
    moveHandler = e -> selectionTooltip.hide();
    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() ? height - size * 0.28 : height - size * 0.205);
    chartCtx = chartCanvas.getGraphicsContext2D();
    legendCanvas = new Canvas(size * 0.225, tile.isTextVisible() ? height - size * 0.28 : height - size * 0.205);
    legendCtx = legendCanvas.getGraphicsContext2D();
    selectionTooltip = new Tooltip("");
    selectionTooltip.setWidth(60);
    selectionTooltip.setHeight(48);
    Tooltip.install(chartCanvas, selectionTooltip);
    getPane().getChildren().addAll(titleText, legendCanvas, chartCanvas, text);
}
Also used : EventHandler(javafx.event.EventHandler) Fonts(eu.hansolo.tilesfx.fonts.Fonts) MouseEvent(javafx.scene.input.MouseEvent) ChartDataEventListener(eu.hansolo.tilesfx.events.ChartDataEventListener) EventType(eu.hansolo.tilesfx.events.TileEvent.EventType) ChartData(eu.hansolo.tilesfx.chart.ChartData) Helper(eu.hansolo.tilesfx.tools.Helper) ListChangeListener(javafx.collections.ListChangeListener) VPos(javafx.geometry.VPos) Locale(java.util.Locale) Point2D(javafx.geometry.Point2D) TextAlignment(javafx.scene.text.TextAlignment) Tooltip(javafx.scene.control.Tooltip) Tile(eu.hansolo.tilesfx.Tile) Color(javafx.scene.paint.Color) GraphicsContext(javafx.scene.canvas.GraphicsContext) Font(javafx.scene.text.Font) Canvas(javafx.scene.canvas.Canvas) Collectors(java.util.stream.Collectors) ArcType(javafx.scene.shape.ArcType) Text(javafx.scene.text.Text) List(java.util.List) TileEvent(eu.hansolo.tilesfx.events.TileEvent) StrokeLineCap(javafx.scene.shape.StrokeLineCap) Comparator(java.util.Comparator) TileEvent(eu.hansolo.tilesfx.events.TileEvent) ChartData(eu.hansolo.tilesfx.chart.ChartData) Canvas(javafx.scene.canvas.Canvas) Tooltip(javafx.scene.control.Tooltip) Text(javafx.scene.text.Text) Point2D(javafx.geometry.Point2D)

Example 7 with ChartData

use of eu.hansolo.tilesfx.chart.ChartData in project tilesfx by HanSolo.

the class MatrixTileSkin method initGraphics.

// ******************** Initialization ************************************
@Override
protected void initGraphics() {
    super.initGraphics();
    matrix = PixelMatrixBuilder.create().pixelShape(PixelShape.SQUARE).useSpacer(true).squarePixels(false).colsAndRows(tile.getMatrixSize()).pixelOnColor(tile.getBarColor()).pixelOffColor(Helper.isDark(tile.getBackgroundColor()) ? tile.getBackgroundColor().brighter() : tile.getBackgroundColor().darker()).build();
    if (!tile.getChartData().isEmpty() && tile.getChartData().size() > 2) {
        matrix.setColsAndRows(tile.getChartData().size(), matrix.getRows());
    }
    chartEventListener = e -> updateMatrixWithChartData();
    tile.getChartData().forEach(chartData -> chartData.addChartDataEventListener(chartEventListener));
    chartDataListener = c -> {
        while (c.next()) {
            if (c.wasAdded()) {
                c.getAddedSubList().forEach(addedItem -> addedItem.addChartDataEventListener(chartEventListener));
                if (!tile.getChartData().isEmpty() && tile.getChartData().size() > 2) {
                    matrix.setColsAndRows(tile.getChartData().size(), matrix.getRows());
                }
            } else if (c.wasRemoved()) {
                c.getRemoved().forEach(removedItem -> removedItem.removeChartDataEventListener(chartEventListener));
                if (!tile.getChartData().isEmpty() && tile.getChartData().size() > 2) {
                    matrix.setColsAndRows(tile.getChartData().size(), matrix.getRows());
                }
            }
        }
        updateMatrixWithChartData();
    };
    matrixListener = e -> {
        if (tile.getChartData().isEmpty()) {
            return;
        }
        int column = e.getX();
        ChartData data = tile.getChartData().get(column);
        String tooltipText = new StringBuilder(data.getName()).append("\n").append(String.format(locale, formatString, data.getValue())).toString();
        Point2D popupLocation = new Point2D(e.getMouseScreenX() - selectionTooltip.getWidth() * 0.5, e.getMouseScreenY() - size * 0.025 - selectionTooltip.getHeight());
        selectionTooltip.setText(tooltipText);
        selectionTooltip.setX(popupLocation.getX());
        selectionTooltip.setY(popupLocation.getY());
        selectionTooltip.show(tile.getScene().getWindow());
        tile.fireTileEvent(new TileEvent(EventType.SELECTED_CHART_DATA, data));
    };
    mouseHandler = e -> {
        final javafx.event.EventType<? extends MouseEvent> TYPE = e.getEventType();
        if (MouseEvent.MOUSE_CLICKED.equals(TYPE)) {
            matrix.checkForClick(e);
        } else if (MouseEvent.MOUSE_MOVED.equals(TYPE)) {
            selectionTooltip.hide();
        } else if (MouseEvent.MOUSE_EXITED.equals(TYPE)) {
            selectionTooltip.hide();
        }
    };
    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());
    selectionTooltip = new Tooltip("");
    selectionTooltip.setWidth(60);
    selectionTooltip.setHeight(48);
    Tooltip.install(matrix, selectionTooltip);
    getPane().getChildren().addAll(titleText, matrix, text);
}
Also used : Tile(eu.hansolo.tilesfx.Tile) EventHandler(javafx.event.EventHandler) Color(javafx.scene.paint.Color) Fonts(eu.hansolo.tilesfx.fonts.Fonts) PixelShape(eu.hansolo.tilesfx.chart.PixelMatrix.PixelShape) MouseEvent(javafx.scene.input.MouseEvent) PixelMatrixEventListener(eu.hansolo.tilesfx.events.PixelMatrixEventListener) Font(javafx.scene.text.Font) ChartDataEventListener(eu.hansolo.tilesfx.events.ChartDataEventListener) EventType(eu.hansolo.tilesfx.events.TileEvent.EventType) Text(javafx.scene.text.Text) ChartData(eu.hansolo.tilesfx.chart.ChartData) List(java.util.List) PixelMatrixBuilder(eu.hansolo.tilesfx.chart.PixelMatrixBuilder) Helper(eu.hansolo.tilesfx.tools.Helper) ListChangeListener(javafx.collections.ListChangeListener) TileEvent(eu.hansolo.tilesfx.events.TileEvent) Point2D(javafx.geometry.Point2D) Tooltip(javafx.scene.control.Tooltip) PixelMatrix(eu.hansolo.tilesfx.chart.PixelMatrix) TileEvent(eu.hansolo.tilesfx.events.TileEvent) ChartData(eu.hansolo.tilesfx.chart.ChartData) Point2D(javafx.geometry.Point2D) Tooltip(javafx.scene.control.Tooltip) Text(javafx.scene.text.Text)

Example 8 with ChartData

use of eu.hansolo.tilesfx.chart.ChartData 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 9 with ChartData

use of eu.hansolo.tilesfx.chart.ChartData in project tilesfx by HanSolo.

the class SmoothAreaChartTileSkin method handleData.

private void handleData() {
    selectorTooltip.hide();
    selector.setVisible(false);
    List<ChartData> data = tile.getChartData();
    if (null == data || data.isEmpty()) {
        return;
    }
    Optional<ChartData> lastDataEntry = data.stream().reduce((first, second) -> second);
    if (lastDataEntry.isPresent()) {
        valueText.setText(String.format(locale, formatString, lastDataEntry.get().getValue()));
        tile.setValue(lastDataEntry.get().getValue());
        resizeDynamicText();
    }
    dataSize = data.size();
    maxValue = data.stream().max(Comparator.comparing(c -> c.getValue())).get().getValue();
    hStepSize = width / (dataSize - 1);
    vStepSize = (height * 0.5) / maxValue;
    points.clear();
    for (int i = 0; i < dataSize; i++) {
        points.add(new Point((i) * hStepSize, height - data.get(i).getValue() * vStepSize));
    }
    drawChart(points);
}
Also used : ChartData(eu.hansolo.tilesfx.chart.ChartData) Point(eu.hansolo.tilesfx.tools.Point) Point(eu.hansolo.tilesfx.tools.Point)

Example 10 with ChartData

use of eu.hansolo.tilesfx.chart.ChartData 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)

Aggregations

ChartData (eu.hansolo.tilesfx.chart.ChartData)18 Color (javafx.scene.paint.Color)12 Text (javafx.scene.text.Text)11 TileEvent (eu.hansolo.tilesfx.events.TileEvent)10 Helper (eu.hansolo.tilesfx.tools.Helper)9 Tile (eu.hansolo.tilesfx.Tile)8 Fonts (eu.hansolo.tilesfx.fonts.Fonts)8 List (java.util.List)8 Font (javafx.scene.text.Font)8 EventType (eu.hansolo.tilesfx.events.TileEvent.EventType)7 Locale (java.util.Locale)7 EventHandler (javafx.event.EventHandler)7 MouseEvent (javafx.scene.input.MouseEvent)7 ListChangeListener (javafx.collections.ListChangeListener)6 ChartDataEventListener (eu.hansolo.tilesfx.events.ChartDataEventListener)4 Country (eu.hansolo.tilesfx.tools.Country)3 Location (eu.hansolo.tilesfx.tools.Location)3 ZonedDateTime (java.time.ZonedDateTime)3 ArrayList (java.util.ArrayList)3 Insets (javafx.geometry.Insets)3