我如何显示一个";“单柱状图”;在TableView的TableColumn中(JavaFX8)?

我如何显示一个";“单柱状图”;在TableView的TableColumn中(JavaFX8)?,java,charts,javafx-8,Java,Charts,Javafx 8,以下是一个屏幕截图: 感兴趣的部分:右上部分和下部分 在下半部分,选择了一条规则;此规则共有1080次调用,包括274次成功调用和84次成功空调用。现在,我正在显示成功与总、空与成功的比率 我希望能做的是放弃这些比率,而是用一个图形栏显示非空成功/空成功/失败的比率,使用与上面饼图相同的颜色方案 我遇到的一个问题是,我“什么也没做”的配色方案。以下是填充饼图的代码: @Override public void loadPieChart(final int failedMatches, fina

以下是一个屏幕截图:

感兴趣的部分:右上部分和下部分

在下半部分,选择了一条规则;此规则共有1080次调用,包括274次成功调用和84次成功空调用。现在,我正在显示成功与总、空与成功的比率

我希望能做的是放弃这些比率,而是用一个图形栏显示非空成功/空成功/失败的比率,使用与上面饼图相同的颜色方案

我遇到的一个问题是,我“什么也没做”的配色方案。以下是填充饼图的代码:

@Override
public void loadPieChart(final int failedMatches, final int emptyMatches,
    final int nonEmptyMatches)
{
    final int totalInvocations = failedMatches + emptyMatches
        + nonEmptyMatches;
    final List<Data> list = new ArrayList<>(3);

    int nr;
    double percent;
    String fmt;

    /*
     * Failures
     */
    nr = failedMatches;
    percent = 100.0 * nr / totalInvocations;
    fmt = String.format("Failures (%d - %.02f%%)", nr, percent);
    list.add(new Data(fmt, percent));

    /*
     * Empty
     */
    nr = emptyMatches;
    percent = 100.0 * nr / totalInvocations;
    fmt = String.format("Empty matches (%d - %.02f%%)", nr, percent);
    list.add(new Data(fmt, percent));

    /*
     * Non empty
     */
    nr = nonEmptyMatches;
    percent = 100.0 * nr / totalInvocations;
    fmt = String.format("Non empty matches (%d; %.02f%%)", nr, percent);
    list.add(new Data(fmt, percent));

    display.matchChart.getData().setAll(list);

    fmt = String.format("Rule rundown (%d total)", totalInvocations);
    display.matchChart.setTitle(fmt);
}
@覆盖
公共无效载荷图表(最终整型失败匹配、最终整型空匹配、,
最终整数(非空匹配)
{
最终int totalInvocations=失败匹配+空匹配
+非空匹配;
最终列表=新的ArrayList(3);
国际天然气公司;
百分之二;
串fmt;
/*
*失败
*/
nr=失败匹配;
百分比=100.0*nr/totalInvocations;
fmt=String.format(“失败(%d-%.02f%%)”,nr,百分比);
列表。添加(新数据(fmt,百分比));
/*
*空的
*/
nr=空匹配;
百分比=100.0*nr/totalInvocations;
fmt=String.format(“空匹配(%d-%.02f%%)”,nr,percent);
列表。添加(新数据(fmt,百分比));
/*
*非空
*/
nr=非空匹配;
百分比=100.0*nr/totalInvocations;
fmt=String.format(“非空匹配(%d;%.02f%%)”,nr,percent);
列表。添加(新数据(fmt,百分比));
display.matchChart.getData().setAll(列表);
fmt=String.format(“规则汇总(%d total)”,totalInvocations);
display.matchChart.setTitle(fmt);
}
获取数据不是问题,问题是我在JavaFXJavadoc中找不到如何显示如上所示的“单个图形栏”

我从哪里开始

(哦,我对图形很在行,所以甚至不要让我“画”我想要的东西;如果需要更高的精度,我很乐意给他们)


编辑好的,下面是我的意思示例:

除了我只需要一个条,我不关心弹出菜单,我不想要轴,我不想要图例,我只想要裸露的,没有装饰的条

此后,我还对输出进行了一些修改:

目标是用我不知道如何生成的该死的图形替换最后一列:(

import javafx.application.application;
导入javafx.beans.property.ReadOnlyObjectWrapper;
导入javafx.beans.property.SimpleIntegerProperty;
导入javafx.collections.FXCollections;
导入javafx.collections.ObservableList;
导入javafx.scene.scene;
导入javafx.scene.control.TableCell;
导入javafx.scene.control.TableColumn;
导入javafx.scene.control.TableView;
导入javafx.scene.layout.HBox;
导入javafx.scene.shape.Rectangle;
导入javafx.stage.stage;
公共类TableBar扩展了应用程序{
公共静态void main(字符串[]args){launch(args);}
@凌驾
公众假期开始(阶段){
ObservableList data=FXCollections.observableArrayList();
for(inti=0;i{返回p.getValue().num1;});
col2.setCellValueFactory((p)->{return p.getValue().num2;});
//使此列包含整个数据对象,以便我们可以访问所有字段
TableColumn col3=新的TableColumn(“条形”);
col3.setPrefWidth(100);
col3.setCellValueFactory((p)->{返回新的ReadOnlyObjectWrapper(p.getValue());});
col3.setCellFactory((TableColumn参数)->{
返回新的TableCell(){
@凌驾
受保护的void updateItem(数据项,布尔值为空){
super.updateItem(项,空);
if(空)setGraphic(空);
否则{
double-tot=item.num1.get()+item.num2.get();
double ratio1=item.num1.get()/tot;
double ratio2=item.num2.get()/tot;
矩形r1=新矩形();
//参数是列,绑定使矩形随列调整大小
r1.widthProperty().bind(param.widthProperty().multiply(ratio1));
r1.heightProperty().bind(this.getTableRow().heightProperty().multiply(0.5));
r1.设置样式(“-fx填充:#f3622d;”);
矩形r2=新矩形(0,20);
r2.widthProperty().bind(param.widthProperty().multiply(ratio2));
r2.设置样式(“-fx填充:#fba71b;”);
HBox HBox=新的HBox(r1,r2);
hbox.设置校准(位置中心左);
设置图形(hbox);
setText(空);
}
}
};
});
tv.getColumns().addAll(col1、col2、col3);
场景=新场景(电视);
舞台场景;
stage.show();
}
类数据{
私有SimpleIntegerProperty num1=新的SimpleIntegerProperty((int)(Math.random()*1000));
私有SimpleIntegerProperty num2=新的SimpleIntegerProperty((int)(Math.random()*1000));
公共SimpleIntegerProperty num1Property(){return num1;}
公共SimpleIntegerProperty num2Property(){return num2;}
}
}

我刚刚意识到应该有3个数字,但我相信你明白了。

关于默认的饼图颜色,请看一下
modena.css
有一些规则,如
.default-color0.chart pie
或更全局的颜色变量
chart\u color\u 1
。考虑到你的另一个问题:我建议不要显示图表在
表格视图中(虚拟化容器中的重型组件…)
import javafx.application.Application;
import javafx.beans.property.ReadOnlyObjectWrapper;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.layout.HBox;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;

public class TableBar extends Application {
    public static void main(String[] args) { launch(args); }

    @Override
    public void start(Stage stage) {
        ObservableList<Data> data = FXCollections.observableArrayList();
        for (int i = 0; i<10; i++) data.add(new Data());

        TableView<Data> tv = new TableView(data);
        TableColumn<Data, Number> col1 = new TableColumn("num1");
        TableColumn<Data, Number> col2 = new TableColumn("num2");
        col1.setCellValueFactory((p)->{return p.getValue().num1;});
        col2.setCellValueFactory((p)->{return p.getValue().num2;});

        //make this column hold the entire Data object so we can access all fields
        TableColumn<Data, Data> col3 = new TableColumn("bar");
        col3.setPrefWidth(100);
        col3.setCellValueFactory((p)->{return new ReadOnlyObjectWrapper<>(p.getValue());});
        col3.setCellFactory((TableColumn<Data, Data> param) -> {
            return new TableCell<Data, Data>(){

                @Override
                protected void updateItem(Data item, boolean empty) {
                    super.updateItem(item, empty);
                    if (empty) setGraphic (null);
                    else {
                        double tot = item.num1.get() + item.num2.get();
                        double ratio1 = item.num1.get() / tot;
                        double ratio2 = item.num2.get() / tot;

                        Rectangle r1 = new Rectangle();
                        //the param is the column, bind so rects resize with column
                        r1.widthProperty().bind(param.widthProperty().multiply(ratio1));
                        r1.heightProperty().bind(this.getTableRow().heightProperty().multiply(0.5));
                        r1.setStyle("-fx-fill:#f3622d;");
                        Rectangle r2 = new Rectangle(0, 20);
                        r2.widthProperty().bind(param.widthProperty().multiply(ratio2));
                        r2.setStyle("-fx-fill:#fba71b;");

                        HBox hbox = new HBox(r1,r2);
                        hbox.setAlignment(Pos.CENTER_LEFT);
                        setGraphic(hbox);
                        setText(null);
                    }
                }

            };
        });

        tv.getColumns().addAll(col1,col2,col3);
        Scene scene = new Scene(tv);

        stage.setScene(scene);
        stage.show();
    }

    class Data{
        private SimpleIntegerProperty num1 = new SimpleIntegerProperty((int)(Math.random()*1000));
        private SimpleIntegerProperty num2 = new SimpleIntegerProperty((int)(Math.random()*1000));

        public SimpleIntegerProperty num1Property(){return num1;}
        public SimpleIntegerProperty num2Property(){return num2;}
    }
}