Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javafx get CategoryAxis类别节点_Java_Charts_Java 8_Javafx 8 - Fatal编程技术网

Javafx get CategoryAxis类别节点

Javafx get CategoryAxis类别节点,java,charts,java-8,javafx-8,Java,Charts,Java 8,Javafx 8,是否可以获取由创建类别轴类别的图表(特别是条形图)创建的节点(列表) 目标是向这些节点添加一些鼠标事件 我试图通过xAxis.getChildrenUnmodifiable()访问它们,但这会返回勾号。 还有xAxis.getCategories(),但这会返回类别的标签文本,这些类别是String,要添加鼠标事件,我需要节点 下面是一些代码,它们重现了我想要实现的目标: import java.util.HashMap; import java.util.Map; import javaf

是否可以获取由创建类别轴类别的图表(特别是条形图)创建的节点(列表)

目标是向这些节点添加一些鼠标事件

我试图通过
xAxis.getChildrenUnmodifiable()
访问它们,但这会返回勾号。 还有
xAxis.getCategories()
,但这会返回类别的标签文本,这些类别是
String
,要添加鼠标事件,我需要
节点

下面是一些代码,它们重现了我想要实现的目标:

import java.util.HashMap;
import java.util.Map;

import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.geometry.Point2D;
import javafx.scene.Group;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.chart.Axis;
import javafx.scene.chart.BarChart;
import javafx.scene.chart.CategoryAxis;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart;
import javafx.scene.control.Label;
import javafx.scene.input.MouseEvent;
import javafx.scene.paint.Color;
import javafx.scene.text.Text;
import javafx.scene.text.TextAlignment;
import javafx.scene.text.TextFlow;
import javafx.stage.Stage;

public class Main extends Application {

    final static String austria = "Austria";
    final static String brazil = "Brazil";
    final static String france = "France";
    final static String italy = "Italy";
    final static String usa = "USA";

    @Override
    public void start(Stage stage) {
        Group group = new Group();
        stage.setTitle("Bar Chart Sample");
        final CategoryAxis xAxis = new CategoryAxis();
        final NumberAxis yAxis = new NumberAxis();
        final CustomBarChart<String, Number> bc =
                new CustomBarChart<String, Number>(xAxis, yAxis);
        bc.setTitle("Country Summary");
        bc.setLegendVisible(false);
        xAxis.setLabel("Country");
        xAxis.setTickLabelRotation(90);
        yAxis.setLabel("Value");

        XYChart.Series series1 = new XYChart.Series();
        series1.getData().add(new XYChart.Data(austria, 25601.34));
        series1.getData().add(new XYChart.Data(brazil, 20148.82));
        series1.getData().add(new XYChart.Data(france, 10000));
        series1.getData().add(new XYChart.Data(italy, 35407.15));
        series1.getData().add(new XYChart.Data(usa, 12000));


        final Label tooltipXAxis = new Label("");
        tooltipXAxis.setStyle("-fx-font-size: 12;" +
                "-fx-font-weight: bold;" +
                "-fx-padding: 5px;" +
                "-fx-background-color: white;" +
                "-fx-background-radius: 5;" +
                "-fx-border-radius: 5;" +
                "-fx-border-style: solid;" +
                "-fx-border-width: 2;" +
                "-fx-border-color: black;");
        tooltipXAxis.setVisible(false);

        //xAxis tooltip 
        for (Node n : xAxis.getChildrenUnmodifiable()) {
            n.addEventHandler(MouseEvent.MOUSE_ENTERED,
                    new EventHandler<MouseEvent>() {
                @Override public void handle(MouseEvent e) {
                    tooltipXAxis.setMouseTransparent(true);
                    tooltipXAxis.setText("Tooltip Here");

                    tooltipXAxis.setVisible(true);
                }
            });
            n.addEventHandler(MouseEvent.MOUSE_MOVED,
                    new EventHandler<MouseEvent>() {
                @Override public void handle(MouseEvent e) {
                    Point2D locationInScene = new Point2D(e.getSceneX(), e.getSceneY()-35);
                    Point2D locationInParent = group.sceneToLocal(locationInScene);

                    tooltipXAxis.relocate(locationInParent.getX(), locationInParent.getY());
                }
            });
            n.addEventHandler(MouseEvent.MOUSE_EXITED,
                    new EventHandler<MouseEvent>() {
                @Override public void handle(MouseEvent e) {
                    tooltipXAxis.setVisible(false);
                }
            });
        }

        bc.getData().addAll(series1);

        group.getChildren().addAll(bc, tooltipXAxis);
        Scene scene = new Scene(group);
        stage.setScene(scene);
        stage.show();
    }

    public class CustomBarChart<X, Y> extends BarChart<X, Y> {

        Map<Node, TextFlow> nodeMap = new HashMap<>();

        public CustomBarChart(Axis xAxis, Axis yAxis) {
            super(xAxis, yAxis);
            this.setBarGap(0.0);
        }

        @Override
        protected void seriesAdded(Series<X, Y> series, int seriesIndex) {

            super.seriesAdded(series, seriesIndex);

            for (int j = 0; j < series.getData().size(); j++) {

                Data<X, Y> item = series.getData().get(j);

                Text text = new Text(item.getYValue().toString());
                text.setStyle("-fx-font-size: 10pt;");

                TextFlow textFlow = new TextFlow(text);
                textFlow.setTextAlignment(TextAlignment.CENTER);

                nodeMap.put(item.getNode(), textFlow);
                this.getPlotChildren().add(textFlow);

            }

        }

        @Override
        protected void seriesRemoved(final Series<X, Y> series) {

            for (Node bar : nodeMap.keySet()) {

                Node text = nodeMap.get(bar);
                this.getPlotChildren().remove(text);

            }

            nodeMap.clear();

            super.seriesRemoved(series);
        }

        @Override
        protected void layoutPlotChildren() {

            super.layoutPlotChildren();

            for (Node bar : nodeMap.keySet()) {

                TextFlow textFlow = nodeMap.get(bar);

                if (bar.getBoundsInParent().getHeight() > 30) {
                    ((Text) textFlow.getChildren().get(0)).setFill(Color.WHITE);
                    textFlow.resize(bar.getBoundsInParent().getWidth(), 200);
                    textFlow.relocate(bar.getBoundsInParent().getMinX(), bar.getBoundsInParent().getMinY() + 10);
                } else {
                    ((Text) textFlow.getChildren().get(0)).setFill(Color.GRAY);
                    textFlow.resize(bar.getBoundsInParent().getWidth(), 200);
                    textFlow.relocate(bar.getBoundsInParent().getMinX(), bar.getBoundsInParent().getMinY() - 20);
                }
            }
        }
    }

    public static void main(String[] args) {
        launch(args);
    }
}
import java.util.HashMap;
导入java.util.Map;
导入javafx.application.application;
导入javafx.event.EventHandler;
导入javafx.geometry.Point2D;
导入javafx.scene.Group;
导入javafx.scene.Node;
导入javafx.scene.scene;
导入javafx.scene.chart.Axis;
导入javafx.scene.chart.BarChart;
导入javafx.scene.chart.CategoryAxis;
导入javafx.scene.chart.NumberAxis;
导入javafx.scene.chart.XYChart;
导入javafx.scene.control.Label;
导入javafx.scene.input.MouseEvent;
导入javafx.scene.paint.Color;
导入javafx.scene.text.text;
导入javafx.scene.text.TextAlignment;
导入javafx.scene.text.TextFlow;
导入javafx.stage.stage;
公共类主扩展应用程序{
最终静态字符串austria=“austria”;
最终静态字符串巴西=“巴西”;
最终静态字符串france=“france”;
最终静态字符串意大利=“意大利”;
最终静态字符串usa=“usa”;
@凌驾
公众假期开始(阶段){
组=新组();
阶段。设置标题(“条形图样本”);
最终CategoryAxis xAxis=新CategoryAxis();
最终数字axis yAxis=新数字axis();
最终客户条形图bc=
新客户条形图(xAxis、yAxis);
bc.setTitle(“国家概要”);
bc.setLegendVisible(假);
xAxis.setLabel(“国家”);
xAxis.setticklabel旋转(90);
yAxis.setLabel(“值”);
XYChart.Series系列1=新的XYChart.Series();
series1.getData().add(新的XYChart.Data(奥地利,25601.34));
series1.getData().add(新的XYChart.Data(巴西,20148.82));
series1.getData().add(新的XYChart.Data(法国,10000));
series1.getData().add(新的XYChart.Data(意大利,35407.15));
series1.getData().add(新的XYChart.Data(美国,12000));
最终标签tooltipXAxis=新标签(“”);
tooltipXAxis.setStyle(“-fx字体大小:12;”+
“-fx字体大小:粗体;”+
“-fx填充:5px;”+
“-fx背景色:白色;”+
“-fx背景半径:5;”+
“-fx边框半径:5;”+
“-fx边框样式:实心;”+
“-fx边框宽度:2;”+
“-fx边框颜色:黑色;”;
tooltipXAxis.setVisible(false);
//xAxis工具提示
对于(节点n:xAxis.getChildrenUnmodifiable()){
n、 addEventHandler(MouseEvent.MOUSE_输入,
新的EventHandler(){
@重写公共无效句柄(MouseEvent e){
tooltipXAxis.setMouseTransparent(true);
setText(“此处的工具提示”);
tooltipXAxis.setVisible(true);
}
});
n、 addEventHandler(MouseEvent.MOUSE_移动,
新的EventHandler(){
@重写公共无效句柄(MouseEvent e){
Point2D locationInScene=新的Point2D(e.getSceneX(),e.getSceneY()-35);
Point2D locationInParent=group.sceneToLocal(locationInScene);
重新定位(locationInParent.getX(),locationInParent.getY());
}
});
n、 addEventHandler(MouseEvent.MOUSE_退出,
新的EventHandler(){
@重写公共无效句柄(MouseEvent e){
tooltipXAxis.setVisible(false);
}
});
}
bc.getData().addAll(series1);
group.getChildren().addAll(bc,tooltipXAxis);
场景=新场景(组);
舞台场景;
stage.show();
}
公共类CustomBarChart扩展了BarChart{
Map nodeMap=newhashmap();
公共自定义条形图(轴X轴、轴Y轴){
超级(xAxis,yAxis);
此值为0.0;
}
@凌驾
添加受保护的无效系列(系列、内部系列索引){
超级系列添加(系列,系列索引);
对于(int j=0;j30){
((Text)textFlow.getChildren().get(0)).setFill(Color.WHITE);
textFlow.resize(bar.getBoundsInParent().getWidth(),200);
textFlow.relocate(bar.getBoundsInParent().getMinX(),bar.getBoundsInParent().getMinY()+10);
}否则{