4 Replies Latest reply: Mar 27, 2013 2:45 AM by jsmith RSS

    Display value of each bar on a bar chart

    915980
      I'm currently experimenting with JavaFX graphing capabilities, embedding JavaFX in Swing.
      I've been using the bar chart and have had no problems creating graphs and styling them with CSS.
      However my data is quite fine grained and its not very easy to determine the value of a bar from just the chart scale. Is there a way to display the value of each bar next to it?
        • 1. Re: Display value of each bar on a bar chart
          David Grieve
          Check out the "advanced candlestick" demo in Ensemble, which is in the samples.
          • 2. Re: Display value of each bar on a bar chart
            915980
            To help others here is the code that I used to enable tooltips that show the value of the bars as you hover over them.
            final CategoryAxis xAxis = new CategoryAxis();
            final NumberAxis yAxis = new NumberAxis();
            final BarChart<String, Number> bc = new BarChart<String, Number>(xAxis, yAxis);
            bc.setVerticalGridLinesVisible(true);
            bc.setHorizontalGridLinesVisible(true);
            bc.setTitle("Test");
            xAxis.setLabel("X");
            yAxis.setLabel("Y");
            
            for (final Series<String, Number> series : bc.getData()) {
                 for (final Data<String, Number> data : series.getData()) {
                      Tooltip tooltip = new Tooltip();
                      tooltip.setText(data.getYValue().toString());
                      Tooltip.install(data.getNode(), tooltip);                    
                 }
            }
            • 3. Re: Display value of each bar on a bar chart
              978147
              Does anybody managed to display bar values? Tooltips is not an option for me.
              I tried to put labels on top of the chart, but I`m not able to find position and size of bars. When I get bar node, methods to get location on scene and parent return 0.
              • 4. Re: Display value of each bar on a bar chart
                jsmith
                This Stackoverflow sample displays values with bars in a barchart.

                http://stackoverflow.com/questions/15237192/how-to-display-bar-value-on-top-of-bar-javafx
                https://gist.github.com/jewelsea/5094893