2 Replies Latest reply: Jul 9, 2012 1:43 PM by 947710 RSS

    How to remove symbols from an AreaChart in JavaFX

    947710
      Hello -

      Is there a way to remove the symbols from an AreaChart? LineChart and ScatterChart has that functionality, but nothing available in AreaChart.

      Alternately, if I can "fill" paint in the area under, in a LineChart (like in AreaChart), I could go with that as well.

      My goal is to build an AreaChart without symbols - whether its by using a lineChart (fill the area under it somehow) or AreaChart - doesnt matter...

      Thanks
        • 1. Re: How to remove symbols from an AreaChart in JavaFX
          jsmith
          Some sample methods to prevent rendering of chart symbols.

          METHOD 1: load a stylesheet which styles makes the chart symbols invisible.
          METHOD 2: set the node for a data point to something invisible (an empty rectangle).
          METHOD 3: ookup the symbols node via css and make them invisible.
          import javafx.application.Application;
          import javafx.collections.FXCollections;
          import javafx.scene.*;
          import javafx.scene.chart.*;
          import javafx.scene.chart.XYChart.Data;
          import javafx.stage.Stage;
          
          public class AreaChartWithoutSymbols extends Application {
            public static void main(String[] args) { launch(args); }
            @Override public void start(Stage stage) {
              // create a sample chart.
              final AreaChart chart = new AreaChart(
                new NumberAxis(), new NumberAxis(),
                FXCollections.observableArrayList(
                   new XYChart.Series("April", FXCollections.observableArrayList(
                     createData(0, 4), createData(1, 10), createData(2, 18)
                   )),
                   new XYChart.Series("May", FXCollections.observableArrayList(
                     createData(0, 20), createData(1, 15), createData(2, 12)
                   ))
                )      
              );
              chart.setLegendVisible(false);
          
              // show the chart.
              stage.setScene(new Scene(chart, 800, 600));
          // METHOD 1: for making a chart symbol invisible.    
          // load a stylesheet which styles makes the chart symbols invisible (comment out to try other methods).
              stage.getScene().getStylesheets().add(
                AreaChartWithoutSymbols.class.getResource("nosymbols.css").toExternalForm()
              );
              stage.show();
              
          // METHOD 2: for making a chart symbol invisible.    
          // uncomment to lookup the symbols node via css and make them invisible.    
          //    for (Node symbol: chart.lookupAll(".chart-area-symbol")) {
          //      symbol.setVisible(false);
          //      symbol.setManaged(false);
          //    }
            }
          
            // helper method to create a data point for a chart.
            private Data createData(int x, int y) {
              Data data =  new Data(x, y);
          // METHOD 3: for making a chart symbol invisible.    
          // uncomment to set the node for a data point to something invisible (e.g. an empty rectangle).    
          //    data.setNode(new Rectangle(0,0)); 
              return data;
            }
          }
          /** nosymbols.css - place in same directory as AreaChartWithoutSymbols.java 
              and ensure build system copies the css file to the class output directory. */
          .chart-area-symbol { visibility: hidden; }
          Edited by: jsmith on Jul 10, 2012 2:24 AM

          METHOD 4: Subclass the chart and override the chart's dataItemAdded method to remove symbols on each data point
          @Override protected void dataItemAdded(Series<Number, Number> series, int itemIndex, Data<Number, Number> item) {}
          http://docs.oracle.com/javafx/2/api/javafx/scene/chart/XYChart.html#dataItemAdded%28javafx.scene.chart.XYChart.Series,%20int,%20javafx.scene.chart.XYChart.Data%29

          Method 4 is courtesy of Jasper Potts' answer in the following thread:
          How to remove symbols from an AreaChart in JavaFX
          • 2. Re: How to remove symbols from an AreaChart in JavaFX
            947710
            Thanks a lot - Appreciate it.