グラフのスタイル設定

Earth Engine コードエディタの ui.Chart モジュールによって生成されたグラフは、.setOptions() メソッドを使用してスタイル設定できます。このメソッドは、構成オプションのクライアントサイド JavaScript オブジェクトを入力として受け取ります。各グラフタイプの構成オプションについては、それぞれの Google グラフのドキュメントの [構成オプション] セクションをご覧ください(例: 折れ線グラフ)。

構成オプションの例

ここでは、カスタム グラフのスタイル設定が ui.Chart.image.doySeries の例に適用されています。構成オプション オブジェクトをフォーマットして ee.Chart に適用する方法について説明します。

// Import the example feature collection and subset the glassland feature.
var grassland = ee.FeatureCollection('projects/google/charts_feature_example')
                    .filter(ee.Filter.eq('label', 'Grassland'));

// Load MODIS vegetation indices data and subset a decade of images.
var vegIndices = ee.ImageCollection('MODIS/006/MOD13A1')
                     .filter(ee.Filter.date('2010-01-01', '2020-01-01'))
                     .select(['NDVI', 'EVI']);

// Set chart style properties.
var chartStyle = {
  title: 'Average Vegetation Index Value by Day of Year for Grassland',
  hAxis: {
    title: 'Day of year',
    titleTextStyle: {italic: false, bold: true},
    gridlines: {color: 'FFFFFF'}
  },
  vAxis: {
    title: 'Vegetation index (x1e4)',
    titleTextStyle: {italic: false, bold: true},
    gridlines: {color: 'FFFFFF'},
    format: 'short',
    baselineColor: 'FFFFFF'
  },
  series: {
    0: {lineWidth: 3, color: 'E37D05', pointSize: 7},
    1: {lineWidth: 7, color: '1D6B99', lineDashStyle: [4, 4]}
  },
  chartArea: {backgroundColor: 'EBEBEB'}
};

// Define the chart.
var chart =
    ui.Chart.image
        .doySeries({
          imageCollection: vegIndices,
          region: grassland,
          regionReducer: ee.Reducer.mean(),
          scale: 500,
          yearReducer: ee.Reducer.mean(),
          startDay: 1,
          endDay: 365
        })
        .setSeriesNames(['EVI', 'NDVI']);

// Apply custom style properties to the chart.
chart.setOptions(chartStyle);

// Print the chart to the console.
print(chart);

課題

次の例は、質問に回答するために関連する構成オプションのみを定義する JavaScript オブジェクトを示しています。必要に応じてオブジェクトに追加オプションを追加し、結果を ee.Chart.setOptions() メソッドに渡します。

グラフのタイトルを設定できますか?

{
  title: 'The Chart Title'
}

グラフのタイトルを非表示にできますか?

{
  titlePosition: 'none'
}

凡例を非表示にできますか?

{
  legend: {position: 'none'}
}

軸の範囲を定義できますか?

{
  hAxis: {  // x-axis
    viewWindow: {min: 10, max: 100}
  },
  vAxis: {  // y-axis
    viewWindow: {min: -10, max: 50}
  }
}

シンボルのサイズと色を設定できますか?

最上位のプロパティを使用して、すべてのシリーズのシンボル プロパティを設定できます。次に例を示します。

{
  colors: ['blue'],
  pointSize: 10,
  lineWidth: 5,
  lineDashStyle: [4, 4],
  pointShape: 'diamond'  // 'circle', 'triangle', 'square', 'star', or 'polygon'
}

または、選択したシリーズのプロパティを設定します。

{
  series: {
    0: {lineWidth: 3, color: 'yellow', pointSize: 7},
    2: {lineWidth: 7, color: '1D6D99', lineDashStyle: [4, 4]}
  }
}

系列の長さと順序に対応する色配列を指定して、個々の系列の色を設定することもできます。

{
  colors: ['blue', 'yellow', 'red']
}

凡例からシリーズを非表示にするにはどうすればよいですか?

{
  series: {
    0: {visibleInLegend: false},  // hides the 1st series in the legend
    2: {visibleInLegend: false}  // hides the 3rd series in the legend
  }
}

折れ線グラフにポイントを表示する

すべてのシリーズのポイントを表示する:

{
  pointSize: 10
}

または、個々のシリーズの場合:

{
  series: {
    0: {pointSize: 10},  // shows size 10 points for the 1st line series
    2: {pointSize: 10}  // shows size 10 points for the 3rd line series
  }
}

散布図に線を表示する

すべてのシリーズの線を表示する:

{
  lineWidth: 10
}

または、個々のシリーズの場合:

{
  series: {
    0: {lineWidth: 10},  // shows size 10 lines for the 1st point series
    2: {lineWidth: 10}  // shows size 10 lines for the 3rd point series
  }
}

軸に対数目盛を適用する

{
  hAxis: {logScale: true},  // x-axis
  vAxis: {logScale: true}  // y-axis
}

線にスムージング関数を適用する

すべての折れ線グラフにスムージング関数を適用します。

{
  curveType: 'function'
}

または個々のシリーズ:

{
  series: {
    0: {curveType: 'function'},  // apply smoothing function to 1st line series
    2: {curveType: 'function'}  // apply smoothing function to 3rd line series
  }
}

グラフの軸をズームおよびパンできますか?

それぞれの Google グラフの種類の explorer オプションをご覧ください。次のコードでは、両方の軸でズームとパンが可能になります。

{
  explorer: {}
}

パンとズームを 1 つの軸に制限する:

{
  explorer: {axis: 'vertical'}  // or 'horizontal'
}

設定ポイントの記号の不透明度を設定できますか?

{
  dataOpacity: 0.5
}

軸を回転する

{
  orientation: 'vertical'  // or 'horizontal'
}

テキスト スタイルを設定できますか?

テキストのスタイル設定オプションは、次の JavaScript オブジェクトに従って指定します。

var textStyle = {
  color: 'grey',
  fontName: 'arial',
  fontSize: 14,
  bold: true,
  italic: false
}

X 軸のテキスト スタイルを設定します。

{
  hAxis: {
    textStyle: textStyle,  // tick label text style
    titleTextStyle: textStyle  // axis title text style
  }
}

Y 軸のテキスト スタイルを設定します。

{
  vAxis: {
    textStyle: textStyle,  // tick label text style
    titleTextStyle: textStyle  // axis title text style
  }
}

凡例のテキスト スタイルを設定します。

{
  legend: {textStyle: textStyle}
}

すべてのテキスト要素にフォント名とサイズを設定することもできます。

{
  fontName: 'arial',
  fontSize: 14
}

グラフの背景色を設定できますか?

{
  chartArea: {backgroundColor: 'EBEBEB'}
}

グラフのグリッド線の色を設定するにはどうすればよいですか?

{
  hAxis: {  // x-axis
    gridlines: {color: 'FFFFFF'}
  },
  vAxis: {  // y-axis
    gridlines: {color: 'FFFFFF'}
  }
}

グリッド線を削除する

{
  hAxis: {  // x-axis
    gridlines: {count: 0}
  },
  vAxis: {  // y-axis
    gridlines: {count: 0}
  }
}

軸値のラベルをフォーマットできますか?

軸値ラベルの形式オプションの一覧については、こちらのガイドをご覧ください。

{
  hAxis: {  // x-axis
    format: 'short'  // applies the 'short' format option
  },
  vAxis: {  // y-axis
    format: 'scientific'  // applies the 'scientific' format option
  }
}

null の Y 軸の値を補間する

折れ線グラフの Y 軸の値が欠落している場合や null 値の場合、改行が発生することがあります。interpolateNulls: true を使用して連続した線を描画します。

{
  interpolateNulls: true
}

トレンドラインを追加できますか?

散布図、棒グラフ、縦棒グラフ、折れ線グラフには、トレンドラインを自動的に生成できます。詳しくは、こちらのページをご覧ください。

{
  trendlines: {
    0: {  // add a trend line to the 1st series
      type: 'linear',  // or 'polynomial', 'exponential'
      color: 'green',
      lineWidth: 5,
      opacity: 0.2,
      visibleInLegend: true,
    }
  }
}