JavaFx中的直方图

时间:2015-04-24 16:02:24

标签: java graph javafx histogram

我有一个JavaFX程序,它显示一个直方图,以显示给定英文文本文件中每个字母的出现(不区分大小写)。 它还应该包含2个单选按钮,红色和蓝色,以更改列颜色,它还应包含一个“绘图”按钮。所以这意味着,你的直方图应该先是空的,并且可以用绘图按钮填充。 我做了绘制按钮部分。我创建了我的列,并将我所做的一切添加到HBox,这是我的屏幕。

但是,我不能做单选按钮部分。当我点击“红色”按钮或“蓝色”按钮时,应该改变颜色。但它没有做任何事情。

这是我的代码部分,我遇到了麻烦:

解释:图表是我的图表图表。 ıts类型是BarChart,我也使用XYChartSeries创建每一列;

XYChartSeries Sutun =新的XYChartSeries;

    pane = new Pane(); 
    VBox paneforcolorbutton = new VBox(20);
    paneforcolorbutton.setPadding(new Insets(5 ,5 ,5, 5 ));
    paneforcolorbutton.setStyle("-fx-border-color: green;");
    paneforcolorbutton.setStyle("-fx-border-width: 2px ; -fx-border-color: green;");    

    RadioButton blue = new RadioButton("Blue");
    RadioButton red = new RadioButton ("Red");
    paneforcolorbutton.getChildren().addAll(red, blue);

    ToggleGroup group = new ToggleGroup();
    red.setToggleGroup(group);
    blue.setToggleGroup(group);

    red.setOnAction(e -> {
        if (red.isSelected()){
            Graph.setStyle("-fx-bar-fill-color: red;");
        }
    });

    blue.setOnAction(e -> {
        if (blue.isSelected()){
            Graph.setStyle("-fx-bar-fill-color: blue;");
        }
    });

但它没有出现在我的直方图中。我做错了什么?

1 个答案:

答案 0 :(得分:0)

您好再次感谢您的警告@MikaelOhlson我确实更新了我的问题,我找到了如何解决它。在这里,正确的代码; 但这里的解释很少,Graph的类型是BarChart,并且我创建了这样的每一列; XYChartSeries Sutun =新的XYChartSeries;

    VBox paneforcolorbutton = new VBox(); 
    paneforcolorbutton.setPadding(new Insets(20 ,20 ,20, 20 ));  // setting size on it 


    // creating the radio buttons
    RadioButton blue = new RadioButton("Blue"); 
    RadioButton red = new RadioButton ("Red   ");
    paneforcolorbutton.getChildren().addAll(blue, red); // adding them into the paneforcolorbutton vbox 

    ToggleGroup group = new ToggleGroup(); 
    red.setToggleGroup(group);
    blue.setToggleGroup(group);

    red.setOnAction((event)->{ // if user clicked the red button, all my charts turn the red
        for(Node n:Graph.lookupAll(".default-color0.chart-bar")) {
            n.setStyle("-fx-bar-fill: red;");
        }
    });

    blue.setOnAction((event)->{ // if user clicked the blue button, all my charts turn the blue
        for(Node n:Graph.lookupAll(".default-color0.chart-bar")) {
            n.setStyle("-fx-bar-fill: blue;");
        }
    });