Results 1 to 7 of 7
Thread: XY LineChart, how to remove?
- 08-24-2012, 09:23 AM #1
XY LineChart, how to remove?
Hi all,
I have a simple serie plotted on a XY Line Chart as below
I would like to remove the line plotted by clicking on the "remove" button and have the empty scene as belowJava 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 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
SusieLast edited by susieferrari; 08-24-2012 at 02:01 PM.
- 08-24-2012, 01:11 PM #2
Re: XY LineChart, how to remove?
Consistent indentation would reveal this:
The call to remove(...) is unconditional. Was that intended?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); } });
Always use braces around if/else/while blocks. That way, if you add a line later you won't mess up the program flow.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 08-24-2012, 02:02 PM #3
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
- 08-24-2012, 02:59 PM #4
Re: XY LineChart, how to remove?
Are you still getting a NPE?
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 08-24-2012, 04:04 PM #5
- 08-24-2012, 05:30 PM #6
Re: XY LineChart, how to remove?
Ok found the problem, question now closed.
Susie
- 08-28-2012, 11:01 AM #7
Re: XY LineChart, how to remove?
All the solved questions posted in this forum are implemented in my project
Advanced Market Forecast
Susie
Similar Threads
-
ArrayList and remove()
By Pragz in forum New To JavaReplies: 1Last Post: 04-21-2011, 01:42 AM -
how to remove milliseconds
By uthpalaw in forum JDBCReplies: 3Last Post: 03-04-2011, 08:39 AM -
Remove .0
By maple_leafs182 in forum New To JavaReplies: 4Last Post: 01-28-2011, 02:47 AM -
how to remove an old version of JDK
By tommy in forum New To JavaReplies: 2Last Post: 07-30-2007, 08:59 AM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks