Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

How to set logarithmic Y scale for a XYChart?

935521Jul 20 2012 — edited Jul 20 2012
Hi all,

I would like to set Y axis as log scale, but it seems JavaFX does not have such possibility: below is a code that plot XY Line Chart as linear mode, how to set Y to plot these data as logarithmic?

Thanks
public class BaseXYChart extends Application {
 
@Override
    public void start(Stage stage) {
       stage.setTitle("Linear plot");
         
       final CategoryAxis xAxis = new CategoryAxis();
       final NumberAxis yAxis = new NumberAxis(1, 22, 0.5);
         
       yAxis.setTickLabelFormatter(new NumberAxis.DefaultFormatter(yAxis){
            @Override
        public String toString(Number object){
            return String.format("%7.2f", object);
        }
    });
final LineChart<String, Number>lineChart = new LineChart<String, Number>(xAxis, yAxis);
 
       lineChart.setCreateSymbols(false);
       lineChart.setAlternativeRowFillVisible(false);
                
       XYChart.Series series1 = new XYChart.Series();
         
        series1.getData().add(new XYChart.Data("Jan", 1));
        series1.getData().add(new XYChart.Data("Feb", 1.5));
        series1.getData().add(new XYChart.Data("Mar", 2));
        series1.getData().add(new XYChart.Data("Apr", 2.5));
        series1.getData().add(new XYChart.Data("May", 3));
        series1.getData().add(new XYChart.Data("Jun", 4));
        series1.getData().add(new XYChart.Data("Jul", 6));
        series1.getData().add(new XYChart.Data("Aug", 9));
        series1.getData().add(new XYChart.Data("Sep", 12));
        series1.getData().add(new XYChart.Data("Oct", 15));
        series1.getData().add(new XYChart.Data("Nov", 20));
        series1.getData().add(new XYChart.Data("Dec", 22));
  
        BorderPane pane = new BorderPane();
        pane.setCenter(lineChart);          
        Scene scene = new Scene(pane, 800, 600);
        lineChart.getData().addAll(series1);
          
        stage.setScene(scene);
        stage.show();
    }
 
public static void main(String[] args) {
        launch(args);
    }    
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Aug 17 2012
Added on Jul 20 2012
3 comments
947 views