[Solved] NDVI time series with Sentinel-2

Lucrezia Tosato Asks: NDVI time series with Sentinel-2
I want to calculate the time series of the NDVI with Sentinel-2 of a specific area of Australia. I started from this code:

Time series analysis of NDVI using Google Earth Engine

Substituting Landsat with Sentinel and modifying the data extraction start date.

Code:
  //Importing image and geometry:
var l8 = ee.ImageCollection("COPERNICUS/S2")
var geometry = //Graigzone
    ee.Geometry.Polygon(
        [[[147.232246, -25.695369],
          [147.230186, -25.654525],
          [147.173195, -25.627907],
          [147.353439, -25.648645],
          [147.347946, -25.686707],
          [147.389145, -25.623573],
          [147.398758, -25.622954],
          [147.398758, -25.682994],
          [147.438583, -25.692894],
          [147.431717, -25.702794],
          [147.408371, -25.699082],
          [147.374039, -25.725684],
          [147.298164, -25.747023],
          [147.275848, -25.75228],
          [147.256622, -25.762793],
          [147.259369, -25.772687],
          [147.384338, -25.835123],
          [147.393265, -25.859841],
          [147.345886, -25.934582],
          [147.288208, -25.931495],
          [147.275848, -25.856752],
          [147.240829, -25.789072],
          [147.23774, -25.763103],
          [147.221947, -25.755372],
          [147.232246, -25.695369]]], null, false);
          
  

//Filtering date, polygon, and cloudiness
    var image = l8.filterDate ('2016-01-01', '2021-07-26')
                .filterBounds (geometry)
                //.filterMetadata ('CLOUD_COVER', 'less_than', 20);

    //NDVI calculation:
    var ndvi_func = function (i) {
      var ndvi = i.normalizedDifference (['B8', 'B4']).rename ('NDVI')
      return i.addBands(ndvi);
    }

    var image_ndvi = image.map(ndvi_func);

    //Calculating year wise NDVI
    var year = ee.List.sequence(2015,2021);
    var year_func = function(y){
      var range = ee.Filter.calendarRange (y, y, 'year');
      return image_ndvi.select('NDVI').filter(range).median().set ('Year', y)
    };
    var yearwise_ndvi = ee.ImageCollection(year.map(year_func));
    print (yearwise_ndvi);
    Map.addLayer (yearwise_ndvi)
    
    //Creating time-series chart:
var chart = ui.Chart.image.series ({
  imageCollection: yearwise_ndvi,
  region: geometry,
  reducer: ee.Reducer.median(),
  scale: 10,
  xProperty: 'Year'
}).setOptions ({title: "NDVI over time",
  hAxis: {title: 'Time of the year', format: 'year'}
});

print(chart);

The Error I get is : Error generating chart: Invalid JSON:

What is wrong with the code?

Ten-tools.com may not be responsible for the answers or solutions given to any question asked by the users. All Answers or responses are user generated answers and we do not have proof of its validity or correctness. Please vote for the answer that helped you in order to help others find out which is the most helpful answer. Questions labeled as solved may be solved or may not be solved depending on the type of question and the date posted for some posts may be scheduled to be deleted periodically. Do not hesitate to share your response here to help other visitors like you. Thank you, Ten-tools.