0

Here is my code. Please help merge the two charts into one chart. I am trying to merge the first dataset from the data table with the one in image collection on the same axis. The idea is to merge demand and the supply.

//feature collection for Aoi
var table =ee.Geometry.Polygon(
        [[[37.55303819518035, 0.3541521362992195],
          [37.55303819518035, 0.33389645906159726],
          [37.596554354237966, 0.33389645906159726],
          [37.596554354237966, 0.3541521362992195]]], null, false);

//external dataset
var myTable = {
cols: [
{id: 'name', label: 'month', type: 'string'},
{id: 'name', label: 'demand', type: 'number'},
{id: 'name', label: 'supply', type: 'number'}],
rows: [
{c: [{v: 'Jan'}, {v: 38253956}]},
{c: [{v: 'Feb'}, {v: 10978102}]},
{c: [{v: 'Mar'}, {v: 12030632}]},
{c: [{v: 'Apr'}, {v: 908340}]},
{c: [{v: 'May'},{v: 303074}]}
],
};
//defining the header
var header = ui.Label(' Demand Curve', {fontSize: '30px', color: 'black',fontWeight: 'bold'});
print(header)
var chart = new ui.Chart(myTable, 'LineChart');
chart.setSeriesNames([' Demand'])
        chart.setOptions({
          title: 'Demand Curve',
          hAxis: {title: 'Date', titleTextStyle: {italic: false, bold: true},gridlines: {count: 27}},
          vAxis: {
            title: ' Demand',
            titleTextStyle: {italic: false, bold: true}
          },
          lineWidth: 1,
          colors: [ 'FF0000'],
          curveType: 'line',
          maxPixels:90e9
        });
print(chart);
//---End Of the external data--------

//Getting image collection data fir the ndvi
Map=ui.Map();
var s2a = ee.ImageCollection('COPERNICUS/S2_SR')
                    .filter(ee.Filter.date('2019-01-01', '2019-03-31'))
                    .select('B1','B2','B3','B4','B5','B6','B7','B8','B8A','B9','B11','B12')
                    .filter(ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE', 10))
                    .sort('system:time_start')
                    .map(function(s2a){return s2a.clip(table)})
                    // .set('system:time_start',date_start)
                    // .set('system:time_end',date_end)
                    // .sort('DATE_ACQUIRED');

Map.setCenter (37.577717495842506,0.3597340638009545,5);
    var subndvi = s2a.map(
        function(image) {
        
        
          var ndvi = image.normalizedDifference(['B8', 'B4']).rename('NDVI')
          var ndvi2 =ndvi.gt(0.18).and(ndvi.lte(0.22)).clip(table)
                   .copyProperties(image,['system:time_start','system:time_end','system:index'])
          var ndvi3 = ndvi.gt(0.22).and(ndvi.lte(0.27))
          .copyProperties(image,['system:time_start','system:time_end'])
         
             return ndvi2

})
var ndviChart = ui.Chart.image.series(subndvi, table, ee.Reducer.mean(), 1000);
    ndviChart.setOptions({
  title: 'NDVI(Supply Curve)',
  vAxis: {title: 'NDVI', maxValue: 1},
  hAxis: {title: 'Date', format: 'MM-yy', gridlines: {count: 7}},
  maxPixels:90e9,
});
print(ndviChart)
//-----End Of Ndvi Data--------


The below image shows the two charts from the code above.

enter image description here

2 Answers2

0

The following code will probably do something what you need:

var monthlyDemand = ee.List([
  38253956, // jan
  10978102, // feb 
  12030632, // mar
  908340,   // apr 
  303074,   // may
  0,        // jun
  0,        // jul
  0,        // aug
  0,        // sep
  0,        // oct
  0,        // nov
  0         // dec
])

var s2a = ee.ImageCollection('COPERNICUS/S2_SR')
  .filter(ee.Filter.date('2019-01-01', '2019-03-31'))
  .select('B1','B2','B3','B4','B5','B6','B7','B8','B8A','B9','B11','B12')
  .filter(ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE', 10))

var subndvi = s2a.map(function(image) {
  var ndvi = image.normalizedDifference(['B8', 'B4']).rename('NDVI')
  var ndvi2 = ndvi.gt(0.18).and(ndvi.lte(0.22))

  var demand = monthlyDemand.get(ee.Number(image.date().getRelative('month', 'year')).int())

 return ndvi2.addBands(ee.Image.constant(demand).float().rename('demand'))
  .copyProperties(image, ['system:time_start'])
})

var ndviChart = ui.Chart.image.series(subndvi, aoi, ee.Reducer.mean(), 1000).setOptions({
  title: 'NDVI(Supply Curve)',
  vAxis: {
    title: 'NDVI', 
    maxValue: 1
  },
  hAxis: {
    title: 'Date', 
    format: 'MM-yy', 
    gridlines: { count: 7 }
  },
  series: {
    0: { targetAxisIndex: 0 },
    1: { targetAxisIndex: 1 }
  },
  maxPixels:90e9,
});

print(ndviChart)

https://code.earthengine.google.com/1efd62e93e075cceec00e5f148414681

0

You have two variables "myTable" and "subndvi". The data type for the former is a table whereas for the second you have used ui.Chart.Image.series(). Table chart and image collection charts are different. Either you have to download your image collection point data and merge them manually in MS excel and re-upload to GEE, the new data will have three variables columns supply and demand at their respective dates. This is how data looks from the two charts enter image description here

This is how the data should look like enter image description here

and when you reupload to GEE, use feature by feature chart and it should get you these results. Make sure to normalize your y-axis data. enter image description here

var chart_1 = ui.Chart.feature.byFeature({
  features: ndvi_demand, 
}).setOptions({
  series:{
    0: {color:'red', targetAxisIndex: 0 },
    1: {color:'green', targetAxisIndex: 1 },
    }
})
print(chart_1)

Code Link: https://code.earthengine.google.com/25261edeb9ee44ac1c3ba545e4498d71

And I used a feature collection chart rather than Image Collection one. Lots of approaches Just waned to give you insight on what's happening in background.

best regards Muddasir Shah

Muddasir Shah
  • 15
  • 1
  • 6
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Sumit Sharma May 23 '22 at 06:59