简体   繁体   中英

How do I display a “single bar chart” in a TableColumn of a TableView (javaFX 8)?

Here is a screenshot:

在此输入图像描述

Of interest: the upper right part and the lower part.

In the lower part, there is one rule selected; this rule has: 1080 total invocations, including 274 successful invocations and 84 successful empty invocations. Right now I am displaying the ratio of successful vs total, and empty vs successful.

What I'd like to be able to do is scrap the ratios and instead have a single graphical bar showing the ratios of non empty successes/empty successes/failures, using the same color scheme as the pie chart above...

And one problem that I have is that I "didn't do anything" for that color scheme. Here is the code to fill the pie chart:

@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);
}

Obtaining the data is not the problem, the problem is that I can't find in the JavaFX javadoc how I would display a "single graphic bar" like the above...

Where do I start?

(oh, and I suck at graphics so don't even ask me to "draw" what i want; if more precisions are needed I'll happily give them)


EDIT OK, here is an example of what I mean:

http://www.highcharts.com/demo/bar-stacked

Except that I only need one bar, I don't care about popup menus, I don't want the axes, I don't want the legend, I only want the bare, no frills bar.

Since then I have also modified the output a little:

在此输入图像描述

The goal would be to replace the last column with that darned graphic which I don't know how to generate :(

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;}
    }
}

I just realized there's supposed to be 3 numbers, but I'm sure you get the idea.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM