您可以使用 .setOptions()
方法為 Earth Engine 程式碼編輯器中 ui.Chart
模組產生的圖表設定樣式。這個方法會將用戶端 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: {} }
將平移和縮放功能限制在單一軸:
{ 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 } }
內插空白的 y 軸值嗎?
線型圖中的 y 軸值如果遺漏或為空值,可能會導致換行。使用 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, } } }