【问题标题】:Add labels for point in google charts在谷歌图表中为点添加标签
【发布时间】:2019-06-25 15:23:29
【问题描述】:

谷歌图表是否支持向图表添加标签的功能?我需要给图表添加标签,但我不知道怎么做。

我正在使用这样的功能来绘制图表:

function drawChart(node, rows) {
    var data = new google.visualization.DataTable();
    data.addColumn('date', 'Date');
    data.addColumn('number', 'Index');
    data.addRows(rows);

    var options = {
        titleTextStyle: {
            color: '#00FF00',
        },
        height: 350,
        width: $('#' + node).width(),
        pointsVisible: true,
        colors:['#00FF00'],
        backgroundColor: '#1b1b1b',
        legend: {position: 'right', textStyle: {color: '#fff', fontSize: 12}},
        pointShape: 'square',
        annotations: {
            textStyle: {
                fontName: 'Times-Roman',
                fontSize: 18,
                bold: true,
                italic: true,
                color: '#e5d385',
            },
            stem: {
                length: 0
            }
        },
        hAxis: {
            title: "Date",
            titleTextStyle: {
                color: '#fff'
            },
            format: 'dd.MM.yyyy',
            gridlines: {count: 15, color: '#7d7d7d'},
            baselineColor: '#fff',
            slantedTextAngle: 45,
            slantedText: true,
            baselineColor: '#fff',
            minorGridlines:{
                color: "#494949"
            },
            textStyle:{color: '#FFF'},
            ticks: ticksX 
        },
        vAxis: {
            title: "Index",
            titleTextStyle: {
                color: '#fff'
            },
            gridlines: {color: '#7d7d7d'},
            minValue: 0,
            baselineColor: '#fff',
            minorGridlines:{
                color: "#494949"
            },
            textStyle:{color: '#FFF'},
            ticks: ticksY 
        },
    };
    var dataView = new google.visualization.DataView(data);
    dataView.setColumns([
        // reference existing columns by index
        0, 1,
        // add function for line color
        {
            calc: function(data, row) {
                var colorDown = '#FF0000';
                var colorUp = '#00FF00';

                if ((row === 0) && (data.getValue(row, 1) < data.getValue(row + 1, 1))) {
                    return colorDown;
                } else if ((row > 0) && (data.getValue(row - 1, 1) < data.getValue(row, 1))) {
                    return colorDown;
                }
                return colorUp;
            },
            type: 'string',
            role: 'style',
        }
    ]);

    var chart = new google.visualization.LineChart(document.getElementById(node));
    chart.draw(dataView, options);
}

我找不到如何向图表添加标签的信息。所有文档都包含如何将标签添加到图表轴的信息。我需要在折线图点上方添加标签。

你能帮我吗?

【问题讨论】:

    标签: charts google-visualization


    【解决方案1】:

    注释或标签使用annotation column role,
    类似于样式列角色,已经在使用。

    这里,我在数据视图中添加一个注释列角色,
    显示最小值和最大值的标签...

    google.charts.load('current', {
      packages:['corechart']
    }).then(function () {
      var rows = [];
      var oneDay = (24 * 60 * 60 * 1000);
      var currentEndDate = new Date();
      var currentStartDate = new Date(currentEndDate.getTime() - (oneDay * 365.25));
      for (var i = currentStartDate.getTime(); i <= currentEndDate.getTime(); i = i + oneDay) {
          var direction = (i % 2 === 0) ? 1 : -1;
          var rowDate = new Date(i);
          rows.push([rowDate, rowDate.getMonth() + (rowDate.getDate() * direction)]);
      }
      drawChart('chart_div', rows);
    });
    
    
    function drawChart(node, rows) {
        var data = new google.visualization.DataTable();
        data.addColumn('date', 'Date');
        data.addColumn('number', 'Index');
        data.addRows(rows);
    
        var options = {
            titleTextStyle: {
                color: '#00FF00',
            },
            height: 350,
            width: $('#' + node).width(),
            pointsVisible: true,
            colors:['#00FF00'],
            backgroundColor: '#1b1b1b',
            legend: {position: 'right', textStyle: {color: '#fff', fontSize: 12}},
            pointShape: 'square',
            annotations: {
                textStyle: {
                    fontName: 'Times-Roman',
                    fontSize: 18,
                    bold: true,
                    italic: true,
                    color: '#e5d385',
                },
                stem: {
                    length: 0
                }
            },
            hAxis: {
                title: "Date",
                titleTextStyle: {
                    color: '#fff'
                },
                format: 'dd.MM.yyyy',
                gridlines: {count: 15, color: '#7d7d7d'},
                baselineColor: '#fff',
                slantedTextAngle: 45,
                slantedText: true,
                baselineColor: '#fff',
                minorGridlines:{
                    color: "#494949"
                },
                textStyle:{color: '#FFF'}
            },
            vAxis: {
                title: "Index",
                titleTextStyle: {
                    color: '#fff'
                },
                gridlines: {color: '#7d7d7d'},
                minValue: 0,
                baselineColor: '#fff',
                minorGridlines:{
                    color: "#494949"
                },
                textStyle:{color: '#FFF'}
            },
        };
        var range = data.getColumnRange(1);
        var dataView = new google.visualization.DataView(data);
        dataView.setColumns([
            // reference existing columns by index
            0, 1,
            // add function for line color
            {
                calc: function(data, row) {
                    var colorDown = '#FF0000';
                    var colorUp = '#00FF00';
    
                    if ((row === 0) && (data.getValue(row, 1) < data.getValue(row + 1, 1))) {
                        return colorDown;
                    } else if ((row > 0) && (data.getValue(row - 1, 1) < data.getValue(row, 1))) {
                        return colorDown;
                    }
                    return colorUp;
                },
                type: 'string',
                role: 'style',
            },
            // add annotation
            {
                calc: function(data, row) {
                    var value = data.getValue(row, 1);
                    if (value === range.min) {
                      return 'Min: ' + data.getFormattedValue(row, 1);
                    } else if (value === range.max) {
                      return 'Max: ' + data.getFormattedValue(row, 1);
                    }
                    return null;
                },
                type: 'string',
                role: 'annotation',
            }
        ]);
    
        var chart = new google.visualization.LineChart(document.getElementById(node));
        chart.draw(dataView, options);
    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script src='https://www.gstatic.com/charts/loader.js'></script>
    <div id="chart_div"></div>

    【讨论】:

    • 感谢详细解答!
    猜你喜欢
    • 1970-01-01
    • 2017-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多