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