How to set the origin baseline of a barchart?












0















I'd using JavaFX to draw barcharts which represents a sound power. The range of my values are from -60 to +3 dbBF (decibel full scale).



When JavaFX draw the "bars", the baseline is always set to 0 so, my bars are drawn from 0 to +X (above the zero line) or from 0 to -X (below the zero line).



My aim is to get a bar from -60 to -X. Is there a way to set the baseline origin to -60 ? I looked for tips on google and tried few things but nothing worked.



Thank you for your answers.










share|improve this question





























    0















    I'd using JavaFX to draw barcharts which represents a sound power. The range of my values are from -60 to +3 dbBF (decibel full scale).



    When JavaFX draw the "bars", the baseline is always set to 0 so, my bars are drawn from 0 to +X (above the zero line) or from 0 to -X (below the zero line).



    My aim is to get a bar from -60 to -X. Is there a way to set the baseline origin to -60 ? I looked for tips on google and tried few things but nothing worked.



    Thank you for your answers.










    share|improve this question



























      0












      0








      0








      I'd using JavaFX to draw barcharts which represents a sound power. The range of my values are from -60 to +3 dbBF (decibel full scale).



      When JavaFX draw the "bars", the baseline is always set to 0 so, my bars are drawn from 0 to +X (above the zero line) or from 0 to -X (below the zero line).



      My aim is to get a bar from -60 to -X. Is there a way to set the baseline origin to -60 ? I looked for tips on google and tried few things but nothing worked.



      Thank you for your answers.










      share|improve this question
















      I'd using JavaFX to draw barcharts which represents a sound power. The range of my values are from -60 to +3 dbBF (decibel full scale).



      When JavaFX draw the "bars", the baseline is always set to 0 so, my bars are drawn from 0 to +X (above the zero line) or from 0 to -X (below the zero line).



      My aim is to get a bar from -60 to -X. Is there a way to set the baseline origin to -60 ? I looked for tips on google and tried few things but nothing worked.



      Thank you for your answers.







      javafx bar-chart






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Dec 31 '18 at 7:45









      marc_s

      572k12811061253




      572k12811061253










      asked Dec 28 '18 at 13:02









      Dr_ClickDr_Click

      1099




      1099
























          1 Answer
          1






          active

          oldest

          votes


















          1














          I'm not aware of a way to change the baseline of a JavaFX chart. However, you can make it look like it's starting from -60. The ValueAxis (and by extension, NumberAxis) class has a property for formatting the tick labels: tickLabelFormatter. You can supply your own StringFormatter that makes the labels "off by 60". Here's an example:



          import javafx.application.Application;
          import javafx.collections.FXCollections;
          import javafx.scene.Scene;
          import javafx.scene.chart.BarChart;
          import javafx.scene.chart.CategoryAxis;
          import javafx.scene.chart.NumberAxis;
          import javafx.scene.chart.XYChart.Data;
          import javafx.scene.chart.XYChart.Series;
          import javafx.stage.Stage;
          import javafx.util.StringConverter;

          public class App extends Application {

          @Override
          public void start(Stage primaryStage) throws Exception {
          var scene = new Scene(createChart() 600, 400);
          primaryStage.setScene(scene);
          primaryStage.setTitle("SO-53959031");
          primaryStage.show();
          }

          @SuppressWarnings("unchecked")
          private Series<String, Number> createData() {
          return new Series<>(FXCollections.observableArrayList(
          new Data<>("Test #0", 40),
          new Data<>("Test #1", 20),
          new Data<>("Test #2", 63),
          new Data<>("Test #3", 50),
          new Data<>("Test #4", 30)
          ));
          }

          private BarChart<String, Number> createChart() {
          var yAxis = new NumberAxis("dbBF", 0, 63, 1);
          yAxis.setTickLabelFormatter(new StringConverter<>() {
          @Override
          public String toString(Number object) {
          return Double.toString(object.doubleValue() - 60);
          }

          @Override
          public Number fromString(String string) {
          return Double.valueOf(string) + 60;
          }
          });

          var chart = new BarChart<>(new CategoryAxis(), yAxis);
          chart.getData().add(createData());
          chart.setLegendVisible(false);
          return chart;
          }

          }


          There's a caveat: You have to translate your data to be zero-based. This only has to be done for the chart's data. In other words, your model can keep the data between -60 and 3 but it has to be translated to be in the range 0 to 63 for the chart.





          Screenshot of example:



          screenshot of chart






          share|improve this answer


























          • Thank you Slaw. It works perfectly. It is very clever ! :-D

            – Dr_Click
            Dec 28 '18 at 21:26











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53959031%2fhow-to-set-the-origin-baseline-of-a-barchart%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          I'm not aware of a way to change the baseline of a JavaFX chart. However, you can make it look like it's starting from -60. The ValueAxis (and by extension, NumberAxis) class has a property for formatting the tick labels: tickLabelFormatter. You can supply your own StringFormatter that makes the labels "off by 60". Here's an example:



          import javafx.application.Application;
          import javafx.collections.FXCollections;
          import javafx.scene.Scene;
          import javafx.scene.chart.BarChart;
          import javafx.scene.chart.CategoryAxis;
          import javafx.scene.chart.NumberAxis;
          import javafx.scene.chart.XYChart.Data;
          import javafx.scene.chart.XYChart.Series;
          import javafx.stage.Stage;
          import javafx.util.StringConverter;

          public class App extends Application {

          @Override
          public void start(Stage primaryStage) throws Exception {
          var scene = new Scene(createChart() 600, 400);
          primaryStage.setScene(scene);
          primaryStage.setTitle("SO-53959031");
          primaryStage.show();
          }

          @SuppressWarnings("unchecked")
          private Series<String, Number> createData() {
          return new Series<>(FXCollections.observableArrayList(
          new Data<>("Test #0", 40),
          new Data<>("Test #1", 20),
          new Data<>("Test #2", 63),
          new Data<>("Test #3", 50),
          new Data<>("Test #4", 30)
          ));
          }

          private BarChart<String, Number> createChart() {
          var yAxis = new NumberAxis("dbBF", 0, 63, 1);
          yAxis.setTickLabelFormatter(new StringConverter<>() {
          @Override
          public String toString(Number object) {
          return Double.toString(object.doubleValue() - 60);
          }

          @Override
          public Number fromString(String string) {
          return Double.valueOf(string) + 60;
          }
          });

          var chart = new BarChart<>(new CategoryAxis(), yAxis);
          chart.getData().add(createData());
          chart.setLegendVisible(false);
          return chart;
          }

          }


          There's a caveat: You have to translate your data to be zero-based. This only has to be done for the chart's data. In other words, your model can keep the data between -60 and 3 but it has to be translated to be in the range 0 to 63 for the chart.





          Screenshot of example:



          screenshot of chart






          share|improve this answer


























          • Thank you Slaw. It works perfectly. It is very clever ! :-D

            – Dr_Click
            Dec 28 '18 at 21:26
















          1














          I'm not aware of a way to change the baseline of a JavaFX chart. However, you can make it look like it's starting from -60. The ValueAxis (and by extension, NumberAxis) class has a property for formatting the tick labels: tickLabelFormatter. You can supply your own StringFormatter that makes the labels "off by 60". Here's an example:



          import javafx.application.Application;
          import javafx.collections.FXCollections;
          import javafx.scene.Scene;
          import javafx.scene.chart.BarChart;
          import javafx.scene.chart.CategoryAxis;
          import javafx.scene.chart.NumberAxis;
          import javafx.scene.chart.XYChart.Data;
          import javafx.scene.chart.XYChart.Series;
          import javafx.stage.Stage;
          import javafx.util.StringConverter;

          public class App extends Application {

          @Override
          public void start(Stage primaryStage) throws Exception {
          var scene = new Scene(createChart() 600, 400);
          primaryStage.setScene(scene);
          primaryStage.setTitle("SO-53959031");
          primaryStage.show();
          }

          @SuppressWarnings("unchecked")
          private Series<String, Number> createData() {
          return new Series<>(FXCollections.observableArrayList(
          new Data<>("Test #0", 40),
          new Data<>("Test #1", 20),
          new Data<>("Test #2", 63),
          new Data<>("Test #3", 50),
          new Data<>("Test #4", 30)
          ));
          }

          private BarChart<String, Number> createChart() {
          var yAxis = new NumberAxis("dbBF", 0, 63, 1);
          yAxis.setTickLabelFormatter(new StringConverter<>() {
          @Override
          public String toString(Number object) {
          return Double.toString(object.doubleValue() - 60);
          }

          @Override
          public Number fromString(String string) {
          return Double.valueOf(string) + 60;
          }
          });

          var chart = new BarChart<>(new CategoryAxis(), yAxis);
          chart.getData().add(createData());
          chart.setLegendVisible(false);
          return chart;
          }

          }


          There's a caveat: You have to translate your data to be zero-based. This only has to be done for the chart's data. In other words, your model can keep the data between -60 and 3 but it has to be translated to be in the range 0 to 63 for the chart.





          Screenshot of example:



          screenshot of chart






          share|improve this answer


























          • Thank you Slaw. It works perfectly. It is very clever ! :-D

            – Dr_Click
            Dec 28 '18 at 21:26














          1












          1








          1







          I'm not aware of a way to change the baseline of a JavaFX chart. However, you can make it look like it's starting from -60. The ValueAxis (and by extension, NumberAxis) class has a property for formatting the tick labels: tickLabelFormatter. You can supply your own StringFormatter that makes the labels "off by 60". Here's an example:



          import javafx.application.Application;
          import javafx.collections.FXCollections;
          import javafx.scene.Scene;
          import javafx.scene.chart.BarChart;
          import javafx.scene.chart.CategoryAxis;
          import javafx.scene.chart.NumberAxis;
          import javafx.scene.chart.XYChart.Data;
          import javafx.scene.chart.XYChart.Series;
          import javafx.stage.Stage;
          import javafx.util.StringConverter;

          public class App extends Application {

          @Override
          public void start(Stage primaryStage) throws Exception {
          var scene = new Scene(createChart() 600, 400);
          primaryStage.setScene(scene);
          primaryStage.setTitle("SO-53959031");
          primaryStage.show();
          }

          @SuppressWarnings("unchecked")
          private Series<String, Number> createData() {
          return new Series<>(FXCollections.observableArrayList(
          new Data<>("Test #0", 40),
          new Data<>("Test #1", 20),
          new Data<>("Test #2", 63),
          new Data<>("Test #3", 50),
          new Data<>("Test #4", 30)
          ));
          }

          private BarChart<String, Number> createChart() {
          var yAxis = new NumberAxis("dbBF", 0, 63, 1);
          yAxis.setTickLabelFormatter(new StringConverter<>() {
          @Override
          public String toString(Number object) {
          return Double.toString(object.doubleValue() - 60);
          }

          @Override
          public Number fromString(String string) {
          return Double.valueOf(string) + 60;
          }
          });

          var chart = new BarChart<>(new CategoryAxis(), yAxis);
          chart.getData().add(createData());
          chart.setLegendVisible(false);
          return chart;
          }

          }


          There's a caveat: You have to translate your data to be zero-based. This only has to be done for the chart's data. In other words, your model can keep the data between -60 and 3 but it has to be translated to be in the range 0 to 63 for the chart.





          Screenshot of example:



          screenshot of chart






          share|improve this answer















          I'm not aware of a way to change the baseline of a JavaFX chart. However, you can make it look like it's starting from -60. The ValueAxis (and by extension, NumberAxis) class has a property for formatting the tick labels: tickLabelFormatter. You can supply your own StringFormatter that makes the labels "off by 60". Here's an example:



          import javafx.application.Application;
          import javafx.collections.FXCollections;
          import javafx.scene.Scene;
          import javafx.scene.chart.BarChart;
          import javafx.scene.chart.CategoryAxis;
          import javafx.scene.chart.NumberAxis;
          import javafx.scene.chart.XYChart.Data;
          import javafx.scene.chart.XYChart.Series;
          import javafx.stage.Stage;
          import javafx.util.StringConverter;

          public class App extends Application {

          @Override
          public void start(Stage primaryStage) throws Exception {
          var scene = new Scene(createChart() 600, 400);
          primaryStage.setScene(scene);
          primaryStage.setTitle("SO-53959031");
          primaryStage.show();
          }

          @SuppressWarnings("unchecked")
          private Series<String, Number> createData() {
          return new Series<>(FXCollections.observableArrayList(
          new Data<>("Test #0", 40),
          new Data<>("Test #1", 20),
          new Data<>("Test #2", 63),
          new Data<>("Test #3", 50),
          new Data<>("Test #4", 30)
          ));
          }

          private BarChart<String, Number> createChart() {
          var yAxis = new NumberAxis("dbBF", 0, 63, 1);
          yAxis.setTickLabelFormatter(new StringConverter<>() {
          @Override
          public String toString(Number object) {
          return Double.toString(object.doubleValue() - 60);
          }

          @Override
          public Number fromString(String string) {
          return Double.valueOf(string) + 60;
          }
          });

          var chart = new BarChart<>(new CategoryAxis(), yAxis);
          chart.getData().add(createData());
          chart.setLegendVisible(false);
          return chart;
          }

          }


          There's a caveat: You have to translate your data to be zero-based. This only has to be done for the chart's data. In other words, your model can keep the data between -60 and 3 but it has to be translated to be in the range 0 to 63 for the chart.





          Screenshot of example:



          screenshot of chart







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Dec 28 '18 at 19:52

























          answered Dec 28 '18 at 19:40









          SlawSlaw

          7,1282932




          7,1282932













          • Thank you Slaw. It works perfectly. It is very clever ! :-D

            – Dr_Click
            Dec 28 '18 at 21:26



















          • Thank you Slaw. It works perfectly. It is very clever ! :-D

            – Dr_Click
            Dec 28 '18 at 21:26

















          Thank you Slaw. It works perfectly. It is very clever ! :-D

          – Dr_Click
          Dec 28 '18 at 21:26





          Thank you Slaw. It works perfectly. It is very clever ! :-D

          – Dr_Click
          Dec 28 '18 at 21:26


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53959031%2fhow-to-set-the-origin-baseline-of-a-barchart%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Angular Downloading a file using contenturl with Basic Authentication

          Olmecas

          Can't read property showImagePicker of undefined in react native iOS