Sometimes when pie chart has its nodes assigned to mouse event,
on which selected chart section is highlighted and some data is displayed,
the section selected is not the same which user tended to select.
It occurs when picked for selection section has size smaller then the other.
Most frustrating thing is that this section could not be selected in any way.
...
private DropShadow ds = new DropShadow();
private double totalChartValue = /*total chart value here*/;
...
for (PieChart.Data data : pieChart.getData()) {
Node node = data.getNode();
node.setOnMouseEntered(new OnMouseOverPopupEvent(data, totalChartValue));
}
...
protected class OnMouseOverPopupEvent implements EventHandler<MouseEvent> {
private double totalChartValue;
private PieChart.Data data;
PieChartEvent(PieChart.Data data, double totalChartValue) {
this.totalChartValue = totalChartValue;
this.data = data;
}
public void handle(MouseEvent mouseEvent) {
Node node = (Node) mouseEvent.getSource();
node.setEffect(ds);
node.setCursor(Cursor.HAND);
double x = mouseEvent.getScreenX();
double y = mouseEvent.getScreenY();
DecimalFormat decimalFormat = new DecimalFormat("#.##");
double percentage = data.getPieValue() * 100 / totalChartValue;
String formattedData = String.valueOf(data.getPieValue()) + " of " + totalChartValue + "\n"
+ decimalFormat.format(percentage) + " %";
Text chartValues = new Text(formattedData );
HBox hbChartValues = new HBox();
hbChartValues.getChildren().add(chartValues);
popup = new Popup();
popup.setAutoHide(true);
popup.setX(x);
popup.setY(y);
popup.getContent().addAll(hbChartValues);
popup.show(primaryStage);
}
}
on which selected chart section is highlighted and some data is displayed,
the section selected is not the same which user tended to select.
It occurs when picked for selection section has size smaller then the other.
Most frustrating thing is that this section could not be selected in any way.
...
private DropShadow ds = new DropShadow();
private double totalChartValue = /*total chart value here*/;
...
for (PieChart.Data data : pieChart.getData()) {
Node node = data.getNode();
node.setOnMouseEntered(new OnMouseOverPopupEvent(data, totalChartValue));
}
...
protected class OnMouseOverPopupEvent implements EventHandler<MouseEvent> {
private double totalChartValue;
private PieChart.Data data;
PieChartEvent(PieChart.Data data, double totalChartValue) {
this.totalChartValue = totalChartValue;
this.data = data;
}
public void handle(MouseEvent mouseEvent) {
Node node = (Node) mouseEvent.getSource();
node.setEffect(ds);
node.setCursor(Cursor.HAND);
double x = mouseEvent.getScreenX();
double y = mouseEvent.getScreenY();
DecimalFormat decimalFormat = new DecimalFormat("#.##");
double percentage = data.getPieValue() * 100 / totalChartValue;
String formattedData = String.valueOf(data.getPieValue()) + " of " + totalChartValue + "\n"
+ decimalFormat.format(percentage) + " %";
Text chartValues = new Text(formattedData );
HBox hbChartValues = new HBox();
hbChartValues.getChildren().add(chartValues);
popup = new Popup();
popup.setAutoHide(true);
popup.setX(x);
popup.setY(y);
popup.getContent().addAll(hbChartValues);
popup.show(primaryStage);
}
}
- duplicates
-
JDK-8096331 Region picking is odd with PieChart
- Closed