Change position of Annotation in google Chart

Asked

Viewed 1,133 times

1

The idea is to make the text of the line type Annotation stand over the column.

I tried to create another Annotation for the same column, but did not succeed.

JS:

    google.charts.load("current", {
    packages: ['corechart']
});
google.charts.setOnLoadCallback(drawChart);
function drawChart() {
    var data = new google.visualization.DataTable();
    data.addColumn('string', 'x');
    data.addColumn({type: 'string', role: 'annotation'});
    data.addColumn('number', 'Faixas');
    data.addColumn({type: 'string', role: 'annotation'});
                data.addRow(["até 4", null, 0, '0']);
                    data.addRow(["4.5", null, 0, '0']);
                    data.addRow(["5", null, 0, '0']);
                    data.addRow(["5.5", null, 0, '0']);
                    data.addRow(["6", null, 0, '0']);
                    data.addRow(["6.5", null, 0, '0']);
                    data.addRow(["7", null, 1, '1']);
                    data.addRow(["7.5", null, 2, '2']);
                    data.addRow(["8", 'Você', 5, '5']);
                    data.addRow(["8.5", null, 6, '6']);
                    data.addRow(["9", null, 2, '2']);
                    data.addRow(["9.5", null, 0, '0']);
                    data.addRow(["10", null, 3, '3']);

    var view = new google.visualization.DataView(data);
    var chart = new google.visualization.ColumnChart(document.getElementById("grafico"));
    chart.draw(view, {
        legend: {position: 'bottom'},
        curveType: 'function',
        vAxis: {
            maxValue: 10,
            format: 0
        },
        annotation: {
            1: {
                style: 'line'
            }
        }
    });
    $(document).ready(function () {
        $(window).resize(function () {
            drawChart();
        });
    });
}

HTML:

<div id="grafico"></div>

EXPECTED RESULT:

][1]

1 answer

0

You have placed Annotation in the wrong column. Replace:

data.addColumn('string', 'x');
data.addColumn({type: 'string', role: 'annotation'});
data.addColumn('number', 'Faixas');
data.addColumn({type: 'string', role: 'annotation'});

for :

data.addColumn('string', 'x');
data.addColumn('number', 'Faixas');
data.addColumn({type: 'string', role: 'annotation'});

And in the lines you ride as follows:

data.addRow(["valor eixo X", 0, 'annotation']);

In the first position is what will be in the x axis, the second the value in the y axis and the third the Annotation. See:

google.charts.load("current", {
  packages: ['corechart']
});
google.charts.setOnLoadCallback(drawChart);

function drawChart() {
  var data = new google.visualization.DataTable();
  data.addColumn('string', 'x');
  data.addColumn('number', 'Faixas');
  data.addColumn({type: 'string', role: 'annotation'});

  data.addRow(["até 4", 0, '0']);
  data.addRow(["4.5", 0, '0']);
  data.addRow(["5", 0, '0']);
  data.addRow(["5.5", 0, '0']);
  data.addRow(["6", 0, '0']);
  data.addRow(["6.5", 0, '0']);
  data.addRow(["7", 1, '1']);
  data.addRow(["7.5", 2, '2']);
  data.addRow(["8", 5, 'Você']);
  data.addRow(["8.5", 6, '6']);
  data.addRow(["9", 2, '2']);
  data.addRow(["9.5", 0, '0']);
  data.addRow(["10", 3, '3']);


  var view = new google.visualization.DataView(data);
  var chart = new google.visualization.ColumnChart(document.getElementById("grafico"));
  chart.draw(view, {
    legend: {
      position: 'bottom'
    },
    curveType: 'function',
    vAxis: {
      maxValue: 10,
      format: 0
    },
    annotation: {
      1: {
        style: 'line'
      }
    }
  });
  $(document).ready(function() {
    $(window).resize(function() {
      drawChart();
    });
  });
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<div id="grafico"></div>

  • Maybe my explanation got confused. My idea is that, in the column in which the text "you" will appear, also appear Annotation with quantity. That is, two annotations for the same column, a string and a row - and I don’t know if that’s possible -.

  • @Thiagobarros, I don’t know if this is possible only with google functions Charts. Maybe if you overwrite SVG it will work. You can try to replace the Annotations, see: https://jsfiddle.net/7m7gbbsb/1/ Only one detail when you use style: 'line' in Annotations is to leave it upright.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.