您可以使用 .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('2025-08-04', '2025-08-04')) .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 } }
对 null Y 轴值进行插值?
折线图中缺少或为 null 的 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, } } }