Skip to content

Instantly share code, notes, and snippets.

@angelicalleite
Last active August 29, 2015 14:16
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save angelicalleite/0b67fb27b118433f84ff to your computer and use it in GitHub Desktop.
Save angelicalleite/0b67fb27b118433f84ff to your computer and use it in GitHub Desktop.
JavaFX - Info valores gráficos de barrra
package br.com.fapce.ugb.util;
import javafx.application.Platform;
import javafx.beans.value.ObservableValue;
import javafx.geometry.Bounds;
import javafx.scene.Group;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.chart.XYChart;
import javafx.scene.text.Text;
/**
* Auxilia na exibição dos valores no gráficos de barras
*
* @author Angelica
*/
public class GraficoBarra {
private GraficoBarra() {
}
/**
* Informar a barra do grafico no qual deseja exibir os valores
*/
public static void infoBar(XYChart.Data< String, Number> data) {
Node node = data.getNode();
Text texto = new Text(data.getYValue() + "");
texto.setStyle("-fx-fill: #888; -fx-font-size: 11px;");
node.parentProperty().addListener((ObservableValue<? extends Parent> obs, Parent old, Parent parent) -> {
Platform.runLater(() -> {
if (parent != null) {
Group parentGroup = (Group) parent;
parentGroup.getChildren().add(texto);
}
});
});
node.boundsInParentProperty().addListener((ObservableValue<? extends Bounds> obs, Bounds old, Bounds bounds) -> {
texto.setLayoutX(Math.round(bounds.getMinX() + bounds.getWidth() / 2 - texto.prefWidth(- 1) / 2));
texto.setLayoutY(Math.round(bounds.getMinY() - texto.prefHeight(- 1) * 0.5));
});
}
}
@Heverton
Copy link

Muito interessante.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment