Friday, May 3, 2019

JAVAFX - How to fill LineChart

This LineChart has X String axis and Y Number axis.
@FXML LineChart Chart;
...
/* when is TRUE, is problem with X axis labels */
Chart.setAnimated( false );
  
/* -- get axis */
  
CategoryAxis bottomAxis = (CategoryAxis) Chart.getXAxis();
Axis leftAxis = Chart.getYAxis();
  
/* -- set axis title */
  
bottomAxis.setLabel( "Bottom axis" );
leftAxis.setLabel( "Left axis" );
  
/* -- create series */
  
/* first */
Series serie = new XYChart.Series();
serie.setName( "First" );
  
serie.getData().add( new XYChart.Data( "Jan", 23 ) );
serie.getData().add( new XYChart.Data( "Feb", 32 ) );
serie.getData().add( new XYChart.Data( "Mar", 50 ) );
  
/* second */
Series serie2 = new XYChart.Series();
serie2.setName( "Second" );
  
serie2.getData().add( new XYChart.Data( "Jan", 100 ) );
serie2.getData().add( new XYChart.Data( "Feb", 72 ) );
serie2.getData().add( new XYChart.Data( "Mar", 20 ) );

/* remove point symbols */
Chart.setCreateSymbols(false);
  
/* add series to chart */
Chart.getData().clear();
Chart.getData().add( serie );
Chart.getData().add( serie2 );
Output:

No comments:

Post a Comment