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;
}
}
}
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);
}
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);
}
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()));
}
}
}
*/
}
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()));
}
}
Aggregations