ui.Chart
関数は、Google グラフの DataTable
クラスと同じ構造のクライアントサイド JSON オブジェクトからグラフをレンダリングしますが、DataTable
メソッドと変更可能性がありません。基本的には、観測値を表す行と観測属性を表す列を持つ 2 次元テーブルです。Earth Engine でのグラフ作成用の柔軟なベース インターフェースを提供します。高度なグラフのカスタマイズが必要な場合に適しています。
DataTable
個のスキーマ
Earth Engine で疑似 DataTable
を定義する方法は、JavaScript 2 次元配列と JavaScript リテラル オブジェクトの 2 つがあります。ほとんどのアプリケーションでは、2 次元配列を作成するのが最も簡単な方法です。どちらの場合も、ui.Chart
に渡されるテーブルはクライアントサイド オブジェクトである必要があります。手動でコード化されたテーブルは本来クライアントサイドですが、計算されたオブジェクトは evaluate
を使用してクライアントサイドに転送する必要があります。サーバーサイド オブジェクトとクライアントサイド オブジェクトの違いについて詳しくは、クライアントとサーバーのページをご覧ください。
JavaScript 配列
2 次元の DataTable
は、行と列の配列で構成されます。行は観測値で、列は属性です。最初の列は X 軸の値を定義し、追加の列は Y 軸の系列の値を定義します。最初の行は列ヘッダーである必要があります。最も単純なヘッダーは、一連の列ラベルです。次の配列 DataTable
は、選択した州の人口を示しています。
var dataTable = [ ['State', 'Population'], ['CA', 37253956], ['NY', 19378102], ['IL', 12830632], ['MI', 9883640], ['OR', 3831074], ];
必要に応じて、列に、ドメイン(X 軸)とデータ(Y 軸の系列)の定義以外のロール(アノテーション、区間、ツールチップ、スタイルなど)を指定できます。次の例では、ヘッダー配列が一連のオブジェクトとして示されています。各列のロールが明示的に定義されています。各 Google グラフの種類で使用できる列のロールについては、それぞれのドキュメント(縦棒グラフのデータ形式など)をご覧ください。
var dataTable = [ [{role: 'domain'}, {role: 'data'}, {role: 'annotation'}], ['CA', 37253956, '37.2e6'], ['NY', 19378102, '19.3e6'], ['IL', 12830632, '12.8e6'], ['MI', 9883640, '9.8e6'], ['OR', 3831074, '3.8e6'], ];
列のプロパティは次のように指定します。
パラメータ | タイプ | 定義 |
---|---|---|
type |
文字列(推奨) | 列のデータ型: 'string' 、'number' 、'boolean' 、'date' 、'datetime' 、'timeofday' 。 |
label |
文字列(推奨) | 列のラベル、グラフの凡例の系列ラベル。 |
role |
文字列(推奨) | 列のロール(縦棒グラフのロールなど)。 |
pattern |
文字列、省略可 | 列値の表示方法を指定する数値(または日付)形式文字列。 |
JavaScript オブジェクト
DataTable
は、行オブジェクトと列オブジェクトの配列が指定された JavaScript リテラル オブジェクトとしてフォーマットできます。列パラメータと行パラメータを指定する手順については、こちらのガイドをご覧ください。
var dataTable = { cols: [{id: 'name', label: 'State', type: 'string'}, {id: 'pop', label: 'Population', type: 'number'}], rows: [{c: [{v: 'CA'}, {v: 37253956}]}, {c: [{v: 'NY'}, {v: 19378102}]}, {c: [{v: 'IL'}, {v: 12830632}]}, {c: [{v: 'MI'}, {v: 9883640}]}, {c: [{v: 'OR'}, {v: 3831074}]}] };
手動 DataTable
のグラフ
グラフに表示する少量の静的データがあるとします。JavaScript の配列またはオブジェクト仕様を使用して、ui.Chart
関数に渡す入力を作成します。ここでは、2010 年の米国国勢調査から選択した州の人口が、列プロパティを定義する列見出しオブジェクトを含む JavaScript 配列としてエンコードされています。3 番目の列は 'annotation'
のロールに指定されています。この列には、グラフ内の各観測に人口がアノテーションとして追加されます。
// Define a DataTable using a JavaScript array with a column property header. var dataTable = [ [ {label: 'State', role: 'domain', type: 'string'}, {label: 'Population', role: 'data', type: 'number'}, {label: 'Pop. annotation', role: 'annotation', type: 'string'} ], ['CA', 37253956, '37.2e6'], ['NY', 19378102, '19.3e6'], ['IL', 12830632, '12.8e6'], ['MI', 9883640, '9.8e6'], ['OR', 3831074, '3.8e6'] ]; // Define the chart and print it to the console. var chart = ui.Chart(dataTable).setChartType('ColumnChart').setOptions({ title: 'State Population (US census, 2010)', legend: {position: 'none'}, hAxis: {title: 'State', titleTextStyle: {italic: false, bold: true}}, vAxis: {title: 'Population', titleTextStyle: {italic: false, bold: true}}, colors: ['1d6b99'] }); print(chart);
計算された DataTable
グラフ
DataTable
配列は、evaluate
を介してサーバーからクライアントに渡される 2 次元 ee.List
から作成できます。一般的なシナリオは、ee.FeatureCollection
または ee.ImageCollection
のプロパティ、またはそれらの要素ごとの減算を DataTable
に変換することです。次の例で適用される戦略では、指定された要素を減らす ee.ImageCollection
に関数をマッピングし、減算結果から ee.List
を組み立て、リストを 'row'
というプロパティとして返された要素に接続します。新しいコレクションの各要素には、DataTable
内の行を表す 1 次元の ee.List
があります。aggregate_array()
関数は、すべての 'row'
プロパティを親 ee.List
に集約し、DataTable
に必要な形状の 2D サーバーサイド ee.List
を作成するために使用されます。カスタム列ヘッダーがテーブルに連結され、結果が evaluate
でクライアントサイドに転送され、ui.Chart
関数を使用してレンダリングされます。
リージョン別の時系列
この例は、森林生態系地域の MODIS 由来の NDVI と EVI 植生指数の時系列を示しています。シリーズ内の各画像はエコリージョンごとに縮小され、その結果は 'row'
プロパティとして組み立てられ、DataTable
に集約されてクライアントに渡され、ui.Chart
でグラフ化されます。このスニペットでは、ui.Chart.image.series
グラフの例で生成されたものと同じグラフが生成されます。
// Import the example feature collection and subset the forest feature. var forest = ee.FeatureCollection('projects/google/charts_feature_example') .filter(ee.Filter.eq('label', 'Forest')); // Load MODIS vegetation indices data and subset a decade of images. var vegIndices = ee.ImageCollection('MODIS/061/MOD13A1') .filter(ee.Filter.date('2010-01-01', '2020-01-01')) .select(['NDVI', 'EVI']); // Define a function to format an image timestamp as a JavaScript Date string. function formatDate(img) { var millis = img.date().millis().format(); return ee.String('Date(').cat(millis).cat(')'); } // Build a feature collection where each feature has a property that represents // a DataFrame row. var reductionTable = vegIndices.map(function(img) { // Reduce the image to the mean of pixels intersecting the forest ecoregion. var stat = img.reduceRegion( {reducer: ee.Reducer.mean(), geometry: forest, scale: 500}); // Extract the reduction results along with the image date. var date = formatDate(img); // x-axis values. var evi = stat.get('EVI'); // y-axis series 1 values. var ndvi = stat.get('NDVI'); // y-axis series 2 values. // Make a list of observation attributes to define a row in the DataTable. var row = ee.List([date, evi, ndvi]); // Return the row as a property of an ee.Feature. return ee.Feature(null, {'row': row}); }); // Aggregate the 'row' property from all features in the new feature collection // to make a server-side 2-D list (DataTable). var dataTableServer = reductionTable.aggregate_array('row'); // Define column names and properties for the DataTable. The order should // correspond to the order in the construction of the 'row' property above. var columnHeader = ee.List([[ {label: 'Date', role: 'domain', type: 'date'}, {label: 'EVI', role: 'data', type: 'number'}, {label: 'NDVI', role: 'data', type: 'number'} ]]); // Concatenate the column header to the table. dataTableServer = columnHeader.cat(dataTableServer); // Use 'evaluate' to transfer the server-side table to the client, define the // chart and print it to the console. dataTableServer.evaluate(function(dataTableClient) { var chart = ui.Chart(dataTableClient).setOptions({ title: 'Average Vegetation Index Value by Date for Forest', hAxis: { title: 'Date', titleTextStyle: {italic: false, bold: true}, }, vAxis: { title: 'Vegetation index (x1e4)', titleTextStyle: {italic: false, bold: true} }, lineWidth: 5, colors: ['e37d05', '1d6b99'], curveType: 'function' }); print(chart); });
区間グラフ
このグラフは、DataTable
列の 'role'
プロパティを利用して、区間グラフを生成します。このグラフは、カリフォルニア州モントレー近郊のピクセルの年間 NDVI プロファイルと年次差異を示しています。年ごとの中央値は線で示され、絶対範囲と四分位範囲は帯で示されます。各区間を表すテーブル列は、'role'
列プロパティを 'interval'
として設定することで、そのように割り当てられます。intervals.style
グラフ プロパティを 'area'
に設定すると、中央線の周りに帯が描画されます。
// Define a point to extract an NDVI time series for. var geometry = ee.Geometry.Point([-121.679, 36.479]); // Define a band of interest (NDVI), import the MODIS vegetation index dataset, // and select the band. var band = 'NDVI'; var ndviCol = ee.ImageCollection('MODIS/006/MOD13Q1').select(band); // Map over the collection to add a day of year (doy) property to each image. ndviCol = ndviCol.map(function(img) { var doy = ee.Date(img.get('system:time_start')).getRelative('day', 'year'); // Add 8 to day of year number so that the doy label represents the middle of // the 16-day MODIS NDVI composite. return img.set('doy', ee.Number(doy).add(8)); }); // Join all coincident day of year observations into a set of image collections. var distinctDOY = ndviCol.filterDate('2013-01-01', '2014-01-01'); var filter = ee.Filter.equals({leftField: 'doy', rightField: 'doy'}); var join = ee.Join.saveAll('doy_matches'); var joinCol = ee.ImageCollection(join.apply(distinctDOY, ndviCol, filter)); // Calculate the absolute range, interquartile range, and median for the set // of images composing each coincident doy observation group. The result is // an image collection with an image representative per unique doy observation // with bands that describe the 0, 25, 50, 75, 100 percentiles for the set of // coincident doy images. var comp = ee.ImageCollection(joinCol.map(function(img) { var doyCol = ee.ImageCollection.fromImages(img.get('doy_matches')); return doyCol .reduce(ee.Reducer.percentile( [0, 25, 50, 75, 100], ['p0', 'p25', 'p50', 'p75', 'p100'])) .set({'doy': img.get('doy')}); })); // Extract the inter-annual NDVI doy percentile statistics for the // point of interest per unique doy representative. The result is // is a feature collection where each feature is a doy representative that // contains a property (row) describing the respective inter-annual NDVI // variance, formatted as a list of values. var reductionTable = comp.map(function(img) { var stats = ee.Dictionary(img.reduceRegion( {reducer: ee.Reducer.first(), geometry: geometry, scale: 250})); // Order the percentile reduction elements according to how you want columns // in the DataTable arranged (x-axis values need to be first). var row = ee.List([ img.get('doy'), // x-axis, day of year. stats.get(band + '_p50'), // y-axis, median. stats.get(band + '_p0'), // y-axis, min interval. stats.get(band + '_p25'), // y-axis, 1st quartile interval. stats.get(band + '_p75'), // y-axis, 3rd quartile interval. stats.get(band + '_p100') // y-axis, max interval. ]); // Return the row as a property of an ee.Feature. return ee.Feature(null, {row: row}); }); // Aggregate the 'row' properties to make a server-side 2-D array (DataTable). var dataTableServer = reductionTable.aggregate_array('row'); // Define column names and properties for the DataTable. The order should // correspond to the order in the construction of the 'row' property above. var columnHeader = ee.List([[ {label: 'Day of year', role: 'domain'}, {label: 'Median', role: 'data'}, {label: 'p0', role: 'interval'}, {label: 'p25', role: 'interval'}, {label: 'p75', role: 'interval'}, {label: 'p100', role: 'interval'} ]]); // Concatenate the column header to the table. dataTableServer = columnHeader.cat(dataTableServer); // Use 'evaluate' to transfer the server-side table to the client, define the // chart and print it to the console. dataTableServer.evaluate(function(dataTableClient) { var chart = ui.Chart(dataTableClient).setChartType('LineChart').setOptions({ title: 'Annual NDVI Time Series with Inter-Annual Variance', intervals: {style: 'area'}, hAxis: { title: 'Day of year', titleTextStyle: {italic: false, bold: true}, }, vAxis: {title: 'NDVI (x1e4)', titleTextStyle: {italic: false, bold: true}}, colors: ['0f8755'], legend: {position: 'none'} }); print(chart); });
区間の表現方法はさまざまです。次の例では、intervals.style
プロパティを 'boxes'
に変更し、対応するボックス スタイルを使用して、バンドの代わりにボックスを使用しています。
dataTableServer.evaluate(function(dataTableClient) { var chart = ui.Chart(dataTableClient).setChartType('LineChart').setOptions({ title: 'Annual NDVI Time Series with Inter-Annual Variance', intervals: {style: 'boxes', barWidth: 1, boxWidth: 1, lineWidth: 0}, hAxis: { title: 'Day of year', titleTextStyle: {italic: false, bold: true}, }, vAxis: {title: 'NDVI (x1e4)', titleTextStyle: {italic: false, bold: true}}, colors: ['0f8755'], legend: {position: 'none'} }); print(chart); });