Results 1 to 7 of 7
Like Tree1Likes
  • 1 Post By susieferrari

Thread: XY LineChart, how to remove?

  1. #1
    Join Date
    Mar 2011
    Location
    Parma Italy
    Posts
    88
    Rep Power
    0

    Default XY LineChart, how to remove?

    Hi all,

    I have a simple serie plotted on a XY Line Chart as below

    Java FX Code:
    public class SimpleXYLineChart extends Application {
    
    @Override
    public void start(Stage stage) {        
       stage.setTitle("Line 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);
        lineChart.setLegendVisible(false);
        lineChart.setTitle("LineChart");
    
        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));
    
        lineChart.getData().addAll(series1);        
    
        Scene scene = new Scene(new Group(), 800, 600);
        final VBox vbox = new VBox();
        final HBox hbox = new HBox();
    
        final Button remove = new Button("Remove Series");
        remove.setOnAction(new EventHandler<ActionEvent>() {
            @Override public void handle(ActionEvent e) {
        if (!lineChart.getData().isEmpty()){
            System.out.println("Remove Series");
            lineChart.getData().remove((lineChart.getData().size()-1),0);
            }
    }
        });                
    
        hbox.setSpacing(10);
        hbox.getChildren().addAll(remove);
    
        vbox.getChildren().addAll(lineChart, hbox);
        hbox.setPadding(new Insets(10, 10, 10, 50));
    
        ((Group)scene.getRoot()).getChildren().add(vbox);
        stage.setScene(scene);
        stage.show();
    }
    
    public static void main(String[] args) {
        launch(args);
    }   
    }
    I would like to remove the line plotted by clicking on the "remove" button and have the empty scene as below

    XY LineChart, how to remove?-a02116.png

    I have found this example for scatter plot

    docs.oracle.com/javafx/2/charts/ScatterChartSample.java.html

    and it works, I do not know why my example does not remove line plotted while the scatter plot does.

    When I click on remove button I get Null Pointer Exception

    Any help really appreciated

    Susie
    Last edited by susieferrari; 08-24-2012 at 02:01 PM.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,236
    Rep Power
    19

    Default Re: XY LineChart, how to remove?

    Consistent indentation would reveal this:
    Java FX Code:
        remove.setOnAction(new EventHandler<ActionEvent>() {
    
            @Override
            public void handle(ActionEvent e) {
                if (!lineChart.getData().isEmpty()) 
                        System.out.println("Remove Series");
                lineChart.getData().remove(/*(lineChart.getData().size()-1)*/0);
            }
        });
    The call to remove(...) is unconditional. Was that intended?

    Always use braces around if/else/while blocks. That way, if you add a line later you won't mess up the program flow.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    Join Date
    Mar 2011
    Location
    Parma Italy
    Posts
    88
    Rep Power
    0

    Default Re: XY LineChart, how to remove?

    Hi Darryl,

    I have correct the code above, but still remain my question: how to remove a Line in a XY Chart once plotted by clicking on a button?

    Thanks

    Susie

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,236
    Rep Power
    19

    Default Re: XY LineChart, how to remove?

    Are you still getting a NPE?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    Join Date
    Mar 2011
    Location
    Parma Italy
    Posts
    88
    Rep Power
    0

    Default Re: XY LineChart, how to remove?

    Quote Originally Posted by DarrylBurke View Post
    Are you still getting a NPE?

    db
    Having removed comment I do not have Null Pointer Exc but it does not remove the serie: I wonder why the scatter chart example works perfectly while my example do not.

    Where am I wrong?

    Susie

  6. #6
    Join Date
    Mar 2011
    Location
    Parma Italy
    Posts
    88
    Rep Power
    0

    Default Re: XY LineChart, how to remove?

    Ok found the problem, question now closed.

    Susie

  7. #7
    Join Date
    Mar 2011
    Location
    Parma Italy
    Posts
    88
    Rep Power
    0

    Default Re: XY LineChart, how to remove?

    All the solved questions posted in this forum are implemented in my project

    Advanced Market Forecast

    Susie
    DarrylBurke likes this.

Similar Threads

  1. ArrayList and remove()
    By Pragz in forum New To Java
    Replies: 1
    Last Post: 04-21-2011, 01:42 AM
  2. how to remove milliseconds
    By uthpalaw in forum JDBC
    Replies: 3
    Last Post: 03-04-2011, 08:39 AM
  3. Remove .0
    By maple_leafs182 in forum New To Java
    Replies: 4
    Last Post: 01-28-2011, 02:47 AM
  4. how to remove an old version of JDK
    By tommy in forum New To Java
    Replies: 2
    Last Post: 07-30-2007, 08:59 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •