仅在 google 个图表中显示最大值和最小值的注释文本

Display annotation text in google charts for max and min values only

如何在 google 图表中仅显示最大值和最小值的注释文本。我不想显示其他值的注释消息,请参阅图片了解信息

注意:只想显示带圆圈的点的文字

可以使用 google.visualization.DataView.setColumns 函数实现,下面的示例显示了如何在 google 图表中仅显示最大值和最小值的注释文本:

//add calculated column to print annotation only for min/max values
view.setColumns([0,1,
    {
        role: "annotation",
        type: "string",
        calc: function (dt, row) { 
            var range = dt.getColumnRange(1);
            var curVal = dt.getFormattedValue(row, 1);
            if(curVal == range.min || curVal == range.max)
                return curVal; 
            return null; 
        }
    }
]);

完整示例

google.load("visualization", "1", { packages: ["corechart"] });
google.setOnLoadCallback(drawChart);

function drawChart() {
   
    var data = new google.visualization.DataTable();
    data.addColumn('string', 'Month'); // Implicit domain label col.
    data.addColumn('number', 'Sales'); // Implicit series 1 data col.
   
    data.addRows([
        ['April', 1000],
        ['May', 1170],
        ['June', 660],
        ['July', 1030]
    ]);

    var view = new google.visualization.DataView(data);

    var options = {
        title: 'Company Sales',
    };

    //add calculated column to print annotation only for min/max values
    view.setColumns([0,1,
        {
            role: "annotation",
            type: "string",
            calc: function (dt, row) { 
                var range = dt.getColumnRange(1);
                var curVal = dt.getFormattedValue(row, 1);
                if(curVal == range.min || curVal == range.max)
                    return curVal; 
                return null; 
            }
        }
    ]);

    var chart = new google.visualization.LineChart(document.getElementById('curve_chart'));
    chart.draw(view, options);
}
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<div id="curve_chart" style="width: 900px; height: 500px"></div>