Search in sources :

Example 11 with ChartData

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

the class RadarChartTileSkin method initGraphics.

// ******************** Initialization ************************************
@Override
protected void initGraphics() {
    super.initGraphics();
    radarChart = new RadarChart(tile.getChartData());
    radarChart.setMaxValue(tile.getMaxValue());
    radarChart.setUnit(tile.getUnit());
    radarChart.setLegendVisible(true);
    radarChart.setThresholdVisible(tile.isThresholdVisible());
    radarChart.setMode(tile.getRadarChartMode());
    radarChart.setGridColor(tile.getChartGridColor());
    radarChart.setChartTextColor(tile.getTextColor());
    radarChart.setThresholdColor(tile.getThresholdColor());
    radarChart.setGradientStops(tile.getGradientStops());
    chartEventListener = e -> radarChart.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));
            }
        }
        radarChart.redraw();
    };
    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());
    getPane().getChildren().addAll(titleText, radarChart, text);
}
Also used : Tile(eu.hansolo.tilesfx.Tile) Text(javafx.scene.text.Text) ChartData(eu.hansolo.tilesfx.chart.ChartData) Fonts(eu.hansolo.tilesfx.fonts.Fonts) Helper(eu.hansolo.tilesfx.tools.Helper) ListChangeListener(javafx.collections.ListChangeListener) RadarChart(eu.hansolo.tilesfx.chart.RadarChart) Font(javafx.scene.text.Font) ChartDataEventListener(eu.hansolo.tilesfx.events.ChartDataEventListener) RadarChart(eu.hansolo.tilesfx.chart.RadarChart) Text(javafx.scene.text.Text)

Example 12 with ChartData

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

the class RadialChartTileSkin method drawChart.

private void drawChart() {
    double canvasSize = chartCanvas.getWidth();
    double radius = canvasSize * 0.5;
    double innerSpacer = radius * 0.18;
    double barWidth = (radius - innerSpacer) / tile.getChartData().size();
    // List<RadialChartData> sortedDataList = tile.getChartData().stream().sorted(Comparator.comparingDouble(RadialChartData::getValue)).collect(Collectors.toList());
    List<ChartData> dataList = tile.getChartData();
    int noOfItems = dataList.size();
    double max = noOfItems == 0 ? 0 : dataList.stream().max(Comparator.comparingDouble(ChartData::getValue)).get().getValue();
    double nameX = radius * 0.975;
    double nameWidth = radius * 0.95;
    double valueY = radius * 0.94;
    double valueWidth = barWidth * 0.9;
    Color bkgColor = Color.color(tile.getTextColor().getRed(), tile.getTextColor().getGreen(), tile.getTextColor().getBlue(), 0.15);
    chartCtx.clearRect(0, 0, canvasSize, canvasSize);
    chartCtx.setLineCap(StrokeLineCap.BUTT);
    chartCtx.setFill(tile.getTextColor());
    chartCtx.setTextAlign(TextAlignment.RIGHT);
    chartCtx.setTextBaseline(VPos.CENTER);
    chartCtx.setFont(Fonts.latoRegular(barWidth * 0.5));
    chartCtx.setStroke(bkgColor);
    chartCtx.setLineWidth(1);
    chartCtx.strokeLine(radius, 0, radius, radius - barWidth * 0.875);
    chartCtx.strokeLine(0, radius, radius - barWidth * 0.875, radius);
    chartCtx.strokeArc(noOfItems * barWidth, noOfItems * barWidth, canvasSize - (2 * noOfItems * barWidth), canvasSize - (2 * noOfItems * barWidth), 90, -270, ArcType.OPEN);
    for (int i = 0; i < noOfItems; i++) {
        ChartData data = dataList.get(i);
        double value = clamp(0, Double.MAX_VALUE, data.getValue());
        double bkgXY = i * barWidth;
        double bkgWH = canvasSize - (2 * i * barWidth);
        double barXY = barWidth * 0.5 + i * barWidth;
        double barWH = canvasSize - barWidth - (2 * i * barWidth);
        double angle = value / max * 270.0;
        // Background
        chartCtx.setLineWidth(1);
        chartCtx.setStroke(bkgColor);
        chartCtx.strokeArc(bkgXY, bkgXY, bkgWH, bkgWH, 90, -270, ArcType.OPEN);
        // DataBar
        chartCtx.setLineWidth(barWidth);
        chartCtx.setStroke(data.getFillColor());
        chartCtx.strokeArc(barXY, barXY, barWH, barWH, 90, -angle, ArcType.OPEN);
        // Name
        chartCtx.setTextAlign(TextAlignment.RIGHT);
        chartCtx.fillText(data.getName(), nameX, barXY, nameWidth);
        // Value
        chartCtx.setTextAlign(TextAlignment.CENTER);
        chartCtx.fillText(String.format(Locale.US, "%.0f", value), barXY, valueY, valueWidth);
    }
}
Also used : ChartData(eu.hansolo.tilesfx.chart.ChartData) Color(javafx.scene.paint.Color)

Example 13 with ChartData

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

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

the class RadialPercentageTileSkin method initGraphics.

// ******************** Initialization ************************************
@Override
protected void initGraphics() {
    super.initGraphics();
    if (tile.isAutoScale())
        tile.calcAutoScale();
    minValue = tile.getMinValue();
    range = tile.getRange();
    sectionsVisible = tile.getSectionsVisible();
    sections = tile.getSections();
    formatString = new StringBuilder("%.").append(Integer.toString(tile.getDecimals())).append("f").toString();
    locale = tile.getLocale();
    dataList = tile.getChartData();
    sum = dataList.stream().mapToDouble(ChartData::getValue).sum();
    angleStep = ANGLE_RANGE / sum;
    referenceValue = tile.getReferenceValue() < maxValue ? maxValue : tile.getReferenceValue();
    chartEventListener = e -> setProportionBar();
    tile.getChartData().forEach(chartData -> chartData.addChartDataEventListener(chartEventListener));
    chartDataListener = c -> {
        dataList = tile.getChartData();
        sum = dataList.stream().mapToDouble(ChartData::getValue).sum();
        setProportionBar();
    };
    currentValueListener = o -> setBar(tile.getCurrentValue());
    titleText = new Text();
    titleText.setFill(tile.getTitleColor());
    enableNode(titleText, !tile.getTitle().isEmpty());
    text = new Text(tile.getText());
    text.setFill(tile.getTextColor());
    enableNode(text, tile.isTextVisible());
    barBackground = new Arc(PREFERRED_WIDTH * 0.5, PREFERRED_HEIGHT * 0.5, PREFERRED_WIDTH * 0.468, PREFERRED_HEIGHT * 0.468, 90, 360);
    barBackground.setType(ArcType.OPEN);
    barBackground.setStroke(tile.getBarBackgroundColor());
    barBackground.setStrokeWidth(PREFERRED_WIDTH * 0.1);
    barBackground.setStrokeLineCap(StrokeLineCap.BUTT);
    barBackground.setFill(null);
    bar = new Arc(PREFERRED_WIDTH * 0.5, PREFERRED_HEIGHT * 0.5, PREFERRED_WIDTH * 0.468, PREFERRED_HEIGHT * 0.468, 90, 0);
    bar.setType(ArcType.OPEN);
    bar.setStroke(tile.getBarColor());
    bar.setStrokeWidth(PREFERRED_WIDTH * 0.1);
    bar.setStrokeLineCap(StrokeLineCap.BUTT);
    bar.setFill(null);
    proportionBar = new Arc(PREFERRED_WIDTH * 0.5, PREFERRED_HEIGHT * 0.5, PREFERRED_WIDTH * 0.468, PREFERRED_HEIGHT * 0.468, 90, 0);
    proportionBar.setType(ArcType.OPEN);
    proportionBar.setStroke(tile.getBarColor());
    proportionBar.setStrokeWidth(PREFERRED_WIDTH * 0.015);
    proportionBar.setStrokeLineCap(StrokeLineCap.BUTT);
    proportionBar.setFill(null);
    separator = new Line(PREFERRED_WIDTH * 0.5, 1, PREFERRED_WIDTH * 0.5, 0.16667 * PREFERRED_HEIGHT);
    separator.setStroke(tile.getBackgroundColor());
    separator.setFill(Color.TRANSPARENT);
    percentageValueText = new Text(String.format(locale, formatString, tile.getCurrentValue()));
    percentageValueText.setFont(Fonts.latoRegular(PREFERRED_WIDTH * 0.27333));
    percentageValueText.setFill(tile.getValueColor());
    percentageValueText.setTextOrigin(VPos.CENTER);
    percentageUnitText = new Text(tile.getUnit());
    percentageUnitText = new Text("\u0025");
    percentageUnitText.setFont(Fonts.latoLight(PREFERRED_WIDTH * 0.08));
    percentageUnitText.setFill(tile.getUnitColor());
    percentageFlow = new TextFlow(percentageValueText, percentageUnitText);
    percentageFlow.setTextAlignment(TextAlignment.CENTER);
    descriptionText = new Text(String.format(locale, formatString, tile.getCurrentValue()));
    descriptionText.setFont(Fonts.latoRegular(PREFERRED_WIDTH * 0.27333));
    descriptionText.setFill(tile.getValueColor());
    descriptionText.setTextOrigin(VPos.CENTER);
    enableNode(descriptionText, tile.isValueVisible());
    unitText = new Text(tile.getUnit());
    unitText = new Text("\u0025");
    unitText.setFont(Fonts.latoLight(PREFERRED_WIDTH * 0.08));
    unitText.setFill(tile.getUnitColor());
    enableNode(unitText, !tile.getUnit().isEmpty());
    valueUnitFlow = new TextFlow(descriptionText, unitText);
    valueUnitFlow.setTextAlignment(TextAlignment.CENTER);
    getPane().getChildren().addAll(barBackground, proportionBar, bar, separator, titleText, text, percentageFlow, valueUnitFlow);
}
Also used : Line(javafx.scene.shape.Line) ChartData(eu.hansolo.tilesfx.chart.ChartData) Arc(javafx.scene.shape.Arc) Text(javafx.scene.text.Text) TextFlow(javafx.scene.text.TextFlow)

Example 15 with ChartData

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

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