[英]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.
此规则具有:总共1080次调用,包括274次成功调用和84次成功的空调用。 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... 获取数据不是问题,问题是我在JavaFX javadoc中找不到如何显示如上所示的“单个图形栏”...
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 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. 我刚刚意识到应该有3个数字,但我相信你能得到这个想法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.