8

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:enter image description here

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);
    }
}
gontard
  • 28,720
  • 11
  • 94
  • 117
H4SN
  • 1,482
  • 3
  • 24
  • 43

2 Answers2

19

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:

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 solution for this.

labeledchartwithlegend

jewelsea
  • 150,031
  • 14
  • 366
  • 406
  • while compiling your code it gives error '<>' operator is not allowed for source level below 1.7 at final BarChart bc = new BarChart<>(xAxis, yAxis); – H4SN Mar 14 '13 at 00:10
  • The code is written for Java 7 using the [diamond operator](http://docs.oracle.com/javase/7/docs/technotes/guides/language/type-inference-generic-instance-creation.html). You can either set your compiler to accept Java 7 source as a minimum ([javac](http://docs.oracle.com/javase/7/docs/technotes/tools/windows/javac.html) option `-source 7`) or change the statement to `BarChart bc = new BarChart(xAxis, yAxis);`, which is source compatible with Java 6. Note that even if you are using jdk7+, you may be compiling for a source level below 7. – jewelsea Mar 14 '13 at 00:26
  • @jewelsea What about adding a text label on top of StackedBarChart – Khaled Lela Jul 22 '13 at 12:02
  • @khaled I'm guessing that doing this with a StackedBarChart is the same as a standard BarChart, if not, then log a new question specific to StackedBarCharts which explains what you tried and what didn't work. – jewelsea Jul 22 '13 at 18:10
  • @jewelsea http://stackoverflow.com/questions/17838490/javafx-adding-a-text-label-on-top-of-stackedbarchart/17838883?noredirect=1#comment26039264_17838883 – Khaled Lela Jul 25 '13 at 01:17
  • @jewelsea I tried the same code with data refresh. The text seems to stay once the data is refreshed. I tried to remove the text once added, but received no success. If you can go through the following link, I have modified your code and a kept a `refresh` button at the bottom. On pressing it, the chart gets new values, but the old Text stays. Is there a way to remove it before new values are supplied to the chart. `LiNK` - https://gist.github.com/abhinayagarwal/9474756 – ItachiUchiha Mar 10 '14 at 21:31
  • @jewelsea your solution works perfectly except when data is updated: http://stackoverflow.com/questions/34286062/how-to-clear-text-added-in-a-javafx-barchart can't find a solution ! – Jérémy Halin Dec 16 '15 at 09:42
0

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.

Alexander Kirov
  • 3,624
  • 1
  • 19
  • 23
  • i have searched allot and yet no luck can you provide an example or if you can edit my code.. thanks for your intrest – H4SN Mar 10 '13 at 06:22