This discussion is archived
2 Replies Latest reply: Apr 21, 2013 4:23 PM by KonradZuse RSS

Setting Axis.AutoRange

998450 Newbie
Currently Being Moderated
Say I have manually set the range of my X and Y axis. How can I tell the axis/chart to use AutoRange to zoom out and show the entire dataset (i.e. zoom out)?

Axis.setAutoRanging(true) doesn't seem to trigger the chart to redraw the data.

I could manually determine the range, but that doesn't seem like the best solution.

Thanks
  • 1. Re: Setting Axis.AutoRange
    998450 Newbie
    Currently Being Moderated
    I found a solution that works, but it seems rather ineligant. Still searching for something better.

    Unfortunately, I need to extract all the data in the x/y axis and pass it to the invalidate range function so the axis can recalculate the min/max.

    public static void fitToData(XYChart chart) {
              
              NumberAxis xAxis = (NumberAxis) chart.getXAxis();
              NumberAxis yAxis = (NumberAxis) chart.getYAxis();
              xAxis.setAutoRanging(true);
              yAxis.setAutoRanging(true);
              xAxis.invalidateRange(getXData(chart));
              yAxis.invalidateRange(getYData(chart));
              xAxis.setForceZeroInRange(false);
              yAxis.setForceZeroInRange(false);
              xAxis.requestAxisLayout();
              yAxis.requestAxisLayout();


         }
    public static List<Number> getXData(XYChart<Number, Number> chart) {
              List<Number> list = new ArrayList<Number>();
              
              for (Series<Number, Number> series : chart.getData()) {
                   for (Data<Number, Number> data : series.getData()) {
                        list.add(data.getXValue());
                   }
              }
              return list;
         }
         
         public static List<Number> getYData(XYChart<Number, Number> chart) {
              List<Number> list = new ArrayList<Number>();
              
              for (Series<Number, Number> series : chart.getData()) {
                   for (Data<Number, Number> data : series.getData()) {
                        list.add(data.getYValue());
                   }
              }
              return list;
         }
  • 2. Re: Setting Axis.AutoRange
    KonradZuse Explorer
    Currently Being Moderated
    I made a lineChart that would autoRange when adding new data. Every time I pressed a button it would grab the data from the textfields then populate the chart.

    I grabbed parts of the code, so this should be all of it. Also if it looks ugly it's because I did this a few months ago when I was new to FX since I needed the chart in a Swing app.

    I'm not too sure why I put the autoranging in the AWT.Invoke and not the FXThread but it worked soo :P.

        NumberAxis xAxis = new NumberAxis("Number saved", 1, 10.1, 1);
        
        NumberAxis yAxis = new NumberAxis("Calculated Value", 0, 1000, 1);
        LineChart chart = new LineChart(xAxis, yAxis);      
    
    XYChart.Series<Double,Double> volts = new XYChart.Series<>();
    XYChart.Series<Double,Double> ress = new XYChart.Series<>();
    XYChart.Series<Double,Double> curs = new XYChart.Series<>();
    XYChart.Series<Double,Double> pows = new XYChart.Series<>();
    
    
           try
          {
                java.awt.EventQueue.invokeLater(new Runnable()
                {
                    @Override
                    public void run()
                    {                      
                yAxis.setAutoRanging(true);
                        initComponents();
                            initial();
    
                   }
    } 
    }
    
     public void initial() {
       
    
            Platform.runLater(new Runnable() {
                @Override
                public void run() {
                    
                    if(start == false)
                    {
                        
                    root.getChildren().add(chart);
                    
                   
                        
               chart.getData().add(volts);
               chart.getData().add(ress);
               chart.getData().add(curs);
               chart.getData().add(pows);
               
               volts.setName("Voltage");
               ress.setName("Resistance");
               curs.setName("Current");
               pows.setName("Power");
               
               Scene scene = new Scene(root);
               scene.getStylesheets().add("calculator/chart.css");
               
              chart.getStyleClass().add("lineChart");
                           fxContainer.setScene(scene);
                           graph.add(fxContainer);
                           start = true;
    
                           
                    }
                    else
                    {
                           chart();
                    }
                }
            });
        }
        /**
         *
         */
    
    public void chart()     
    {
        
        if(add == true)
        {
            volts.getData().add(new XYChart.Data<>((double) c, Double.parseDouble(V1.getText())));
            ress.getData().add(new XYChart.Data<>((double) c, Double.parseDouble(R1.getText())));
            curs.getData().add(new XYChart.Data<>((double) c, Double.parseDouble(C1.getText())));
            pows.getData().add(new XYChart.Data<>((double) c, Double.parseDouble(P1.getText())));
            add = false;
        }
    }
    Edited by: KonradZuse on Apr 21, 2013 4:21 PM

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points