1

I am trying to plot charts of NDVI and EVI from MODIS image collection together as the user defines the region of interest (Geometry). It seems like I cannot plot both together using .setSeriesNames function. I am able to plot only one chart at a time in Google Earth Engine (see my comment "// if I set this off, it plots one chart").

Can someone help me plot both (or multiple) charts on the panel?

Here is the script:

//*************************************************
//************************************************* 
// Source: https://developers.google.com/earth-engine/tutorials/community/drawing-tools-region-reduction
// Get the drawing tools widget object; define it as a variable 
// for convenience in recalling it later.
var drawingTools = Map.drawingTools();

// Hide the default drawing tools
drawingTools.setShown(false);

// Setup a while loop to clear all existing geometries
while (drawingTools.layers().length() > 0) {
  var layer = drawingTools.layers().get(0);
  drawingTools.layers().remove(layer);
}

// Initialize a dummy GeometryLayer with null geometry 
// to act as a placeholder for drawn geometries.
var dummyGeometry =
    ui.Map.GeometryLayer({geometries: null, name: 'geometry', color: '0000ff'});

drawingTools.layers().add(dummyGeometry);

// Define the geometry clearing function.
function clearGeometry() {
  var layers = drawingTools.layers();
  layers.get(0).geometries().remove(layers.get(0).geometries().get(0));
}

// Define functions that will be called when 
// each respective drawing button is clicked.

function drawRectangle() {
  clearGeometry();
  drawingTools.setShape('rectangle');
  drawingTools.draw();
}

function drawPolygon() {
  clearGeometry();
  drawingTools.setShape('polygon');
  drawingTools.draw();
}

function drawPoint() {
  clearGeometry();
  drawingTools.setShape('point');
  drawingTools.draw();
}

// Define a panel to hold the time series chart.
var chartPanel = ui.Panel({
  style:
      {height: '235px', width: '600px', position: 'bottom-center', shown: false}
});

// Add the panel to the Map.
Map.add(chartPanel);

// Define a function that gets called on geometry drawing completion and 
// editing events to generate a time series chart for the drawn region.
function chartNdviTimeSeries() {
  // Make the chart panel visible the first time a geometry is drawn.
  if (!chartPanel.style().get('shown')) {
    chartPanel.style().set('shown', true);
  }

  // Get the drawn geometry; it will define the reduction region.
  var aoi = drawingTools.layers().get(0).getEeObject();

  // Set the drawing mode back to null; turns drawing off.
  drawingTools.setShape(null);

  // Reduction scale is based on map scale to avoid memory/timeout errors.
  var mapScale = Map.getScale();
  var scale = mapScale > 5000 ? mapScale * 2 : 5000;

// Image collection.
var Modis = ee.ImageCollection('MODIS/006/MOD13Q1')
            .select(['NDVI', 'EVI']);
print (Modis)

// Set chart style properties.
var chartStyle = {
  title: 'MODIS Average Value by Date',
  titlePostion: 'top-center',
  legend: {position: 'none'},
  hAxis: {title: 'date', format: 'MM-yy', gridlines: {count: 12}},
  vAxis: {title: 'Normalized Indices'}, //  vAxis: {title: 'NDSI', maxValue: 1, minValue: -1},
  series: {
      0: {lineWidth: 2, color: 'E37D05'},
      1: {lineWidth: 2, color: '1D6B99', lineDashStyle: [4, 4]}
      }
}

// Chart NDVI time series for the selected area of interest.
var chart = ui.Chart.image
              .seriesByRegion({
              imageCollection: Modis, // use this for entire timelapse
              regions: aoi,
              reducer: ee.Reducer.mean(),
              //band: ['NDVI', 'EVI'], 
                    //band: ['EVI'],
              scale: scale,
              xProperty: 'system:time_start'
              })
              .setSeriesNames(['NDVI', 'EVI'])// if I set this off, it plots one chart
              .setOptions(chartStyle);

  // Replace the existing chart in the chart panel with the new chart.
  chartPanel.widgets().reset([chart]);
}

// Set the drawing tools widget to listen for geometry drawing and 
// editing events and respond with the chartNdviTimeSeries function.
drawingTools.onDraw(ui.util.debounce(chartNdviTimeSeries, 500));
drawingTools.onEdit(ui.util.debounce(chartNdviTimeSeries, 500));

// User interface
var symbol = {
  rectangle: '⬛',
  polygon: '🔺',
  point: '📍',
};

// Define a ui.Panel to hold app instructions and 
// the geometry drawing buttons. 
var controlPanel = ui.Panel({
  widgets: [
    ui.Label('1. Select a drawing mode.'),
    ui.Button({
      label: symbol.rectangle + ' Rectangle',
      onClick: drawRectangle,
      style: {stretch: 'horizontal'}
    }),
    ui.Button({
      label: symbol.polygon + ' Polygon',
      onClick: drawPolygon,
      style: {stretch: 'horizontal'}
    }),
    ui.Button({
      label: symbol.point + ' Point',
      onClick: drawPoint,
      style: {stretch: 'horizontal'}
    }),
    ui.Label('2. Draw a geometry.'),
    ui.Label('3. Wait for chart to render.'),
    ui.Label(
        '4. Repeat 1-3 or edit/move\ngeometry for a new chart.',
        {whiteSpace: 'pre'})
  ],
  style: {position: 'top-left'},
  layout: null,
});

// Add the panel to the Map.
Map.add(controlPanel);

//*************************************************
//************************************************* 

1 Answer 1

0

If you'd like multiple bands shown in the same chart, you should use ui.Chart.image.series instead of ui.Chart.image.seriesByRegion. I've modified the following sections of your code to show the legend (so you can know what line is what band) and implemented ui.Chart.image.series.

// Set chart style properties.
var chartStyle = {
  title: 'MODIS Average Value by Date',
  titlePostion: 'top-center',
  hAxis: {title: 'date', format: 'MM-yy', gridlines: {count: 12}},
  vAxis: {title: 'Normalized Indices'}, //  vAxis: {title: 'NDSI', maxValue: 1, minValue: -1},
  series: {
      0: {lineWidth: 2, color: 'E37D05'},
      1: {lineWidth: 2, color: '1D6B99', lineDashStyle: [4, 4]}
      }
}

// Chart NDVI time series for the selected area of interest.
var chart = ui.Chart.image
              .series({
              imageCollection: Modis, // use this for entire timelapse
              region: aoi,
              reducer: ee.Reducer.mean(),
              scale: scale,
              xProperty: 'system:time_start'
              })
              .setOptions(chartStyle);

Full Code Editor script

enter image description here

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.