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:
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);
}
}
There is no such option in JFX, about labels showing for a bar in barchart.
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.
Code of charts is not hard to understand...
Download rt-repository from http://hg.openjdk.java.net/openjfx/8/master/rt
Find project javafx-ui-charts and open it. Find implementation of bar chart.
Inside a
ChangeListener
for the each data item'snode
property, you can call the following function to add a label to the top of the bar: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 solution for this.