java 如何在条形javafx顶部显示条形值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15237192/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to display bar value on top of bar javafx
提问by H4SN
from this code i can generate bar chart of 10 bars now i want to know how to display value of each bar on top of bar like the attached image:
从这段代码中,我可以生成 10 个条形图,现在我想知道如何在条形图顶部显示每个条形的值,如附加图像:
here is code:
这是代码:
public class BarChartSample extends Application {
@Override public void start(Stage stage) {
stage.setTitle("Bar Chart Sample");
final CategoryAxis xAxis = new CategoryAxis();
final NumberAxis yAxis = new NumberAxis();
final BarChart<String,Number> bc =
new BarChart<String,Number>(xAxis,yAxis);
bc.setTitle("Country Summary");
xAxis.setLabel("bars");
yAxis.setLabel("Value");
XYChart.Series series1 = new XYChart.Series();
series1.setName("...");
for(int i=0;i<10;i++)
{
//here i want to change color of bar if value of i is >5 than red if i>8 than blue
series1.getData().add(new XYChart.Data("Value", i));
}
}
public static void main(String[] args) {
launch(args);
}
}
回答by jewelsea
Inside a ChangeListener
for the each data item's node
property, you can call the following function to add a label to the top of the bar:
在ChangeListener
每个数据项的node
属性中,您可以调用以下函数在栏的顶部添加标签:
private void displayLabelForData(XYChart.Data<String, Number> data) {
final Node node = data.getNode();
final Text dataText = new Text(data.getYValue() + "");
node.parentProperty().addListener(new ChangeListener<Parent>() {
@Override public void changed(ObservableValue<? extends Parent> ov, Parent oldParent, Parent parent) {
Group parentGroup = (Group) parent;
parentGroup.getChildren().add(dataText);
}
});
node.boundsInParentProperty().addListener(new ChangeListener<Bounds>() {
@Override public void changed(ObservableValue<? extends Bounds> ov, Bounds oldBounds, Bounds bounds) {
dataText.setLayoutX(
Math.round(
bounds.getMinX() + bounds.getWidth() / 2 - dataText.prefWidth(-1) / 2
)
);
dataText.setLayoutY(
Math.round(
bounds.getMinY() - dataText.prefHeight(-1) * 0.5
)
);
}
});
}
The code works by adding a text label to the parent of each bar node, then dynamically positioning the text label based on the bar and text's bounds each time the bar is resized.
该代码的工作原理是向每个条形节点的父节点添加一个文本标签,然后在每次调整条形时根据条形和文本的边界动态定位文本标签。
I created a sample solutionfor this.
我为此创建了一个示例解决方案。
回答by Alexander Kirov
There is no such option in JFX, about labels showing for a bar in barchart.
JFX 中没有这样的选项,关于在条形图中为条形显示的标签。
What you can do is to extend a bar chart class, and override methods about content creation and allocation.
您可以做的是扩展条形图类,并覆盖有关内容创建和分配的方法。
You can observe code of charts in javafx-ui-charts project, which you can download from OpenJFX repository.
您可以在 javafx-ui-charts 项目中观察图表代码,您可以从 OpenJFX 存储库下载。
Code of charts is not hard to understand...
图表代码不难理解...
Download rt-repository from http://hg.openjdk.java.net/openjfx/8/master/rt
从http://hg.openjdk.java.net/openjfx/8/master/rt下载 rt-repository
Find project javafx-ui-charts and open it. Find implementation of bar chart.
找到项目 javafx-ui-charts 并打开它。查找条形图的实现。