我昨天刚刚了解了 JavaFX,现在正在测试图形选项。我在 Oracle 网站 ( http://docs.oracle.com/javafx/2/charts/pie-chart.htm )的饼图示例中注意到了这种行为,但我添加了更多数据点和一些悬停效果来实现它更明显。
当您将鼠标悬停在图表上时,MouseEvent 并不总是由正确的数据段处理。鼠标光标越靠近饼图的中心,这种效果就越明显。据我所知,当靠近边缘时,行为是准确的。
谁能证实这一点?这是一个错误,还是我做错了什么?
尝试移动鼠标,从线段的标记边缘开始,向内移动到中心,查看光标下的线段是否正确指示。
我在玩这张图表时注意到的其他几件事 –</p>
- 笔划效果(此处为白色破折号)在段上不一致。左边缘通常看起来被剪裁了
- 你如何在java中更改工具提示页面角?在 CSS 中是“.page-corner”
- 有没有办法改变饼标签线的形状,或者把圆形端点一起去掉?
编辑:忘记添加代码!
package piechartsample;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.EventHandler;
import javafx.geometry.Side;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.scene.chart.*;
import javafx.scene.control.Label;
import javafx.scene.control.Tooltip;
import javafx.scene.effect.Glow;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.shape.StrokeType;
import javafx.scene.Node;
public class PieChartSample extends Application {
private PieChart.Data selectedData;
private Tooltip tooltip;
@Override
public void start(Stage stage) {
ObservableList<PieChart.Data> pieChartData =
FXCollections.observableArrayList(
new PieChart.Data("Grapefruit", 13),
new PieChart.Data("Orange", 25),
new PieChart.Data("Plum", 10),
new PieChart.Data("Pear", 22),
new PieChart.Data("Banana", 10),
new PieChart.Data("Peach", 5),
new PieChart.Data("Apricot", 1),
new PieChart.Data("Pineapple", 20),
new PieChart.Data("Passion Fruit", 100),
new PieChart.Data("Lychee", 5));
final PieChart chart = new PieChart(pieChartData);
chart.setTitle("Fruits Graph");
chart.setLabelLineLength(10);
chart.setLegendSide(Side.LEFT);
final Label caption = new Label("");
caption.setTextFill(Color.DARKORANGE);
caption.setStyle("-fx-font: 24 arial;");
tooltip = new Tooltip("");
tooltip.setStyle("-fx-font: 14 arial; -fx-font-smoothing-type: lcd;");// -fx-text-fill:black; -fx-background-color: linear-gradient(#e2ecfe, #99bcfd);");
for (final PieChart.Data data : chart.getData()) {
Tooltip.install(data.getNode(),tooltip);
applyMouseEvents(data);
}
BorderPane pane = new BorderPane();
pane.setCenter(chart);
Scene scene = new Scene(pane);
stage.setTitle("Fruits");
stage.setScene(scene);
// scene.getStylesheets().add("piechartsample/Chart.css");
stage.show();
}
public static void main(String[] args) {
launch(args);
}
private void applyMouseEvents(final PieChart.Data data) {
final Node node = data.getNode();
node.setOnMouseEntered(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent arg0) {
node.setEffect(new Glow());
String styleString = "-fx-border-color: white; -fx-border-width: 3; -fx-border-style: dashed;";
node.setStyle(styleString);
tooltip.setText(String.valueOf(data.getName() + "\n" + (int)data.getPieValue()) );
}
});
node.setOnMouseExited(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent arg0) {
node.setEffect(null);
node.setStyle("");
}
});
node.setOnMouseReleased(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent mouseEvent) {
selectedData = data;
System.out.println("Selected data " + selectedData.toString());
}
});
}
}