Annotations with Material Column Chart?

邮差的信 提交于 2019-12-24 15:02:52

问题


I have data in the form of

[[X, Y, {role: "annotation"}], [1,2, "something"], ...,...]

that I pass to

var data = google.visualization.arrayToDataTable(dataPattern); and plot with

var material = new google.charts.Bar(document.getElementById('distribution-over-range'));
material.draw(data, google.charts.Bar.convertOptions(options));

The annotations fail to appear. Any idea why?


回答1:


google.charts.Bar component does not support annotations role but you could utilize google.visualization.BarChart from corechart package for that purpose as demonstrated below:

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

      function drawChart() {
        var data = new google.visualization.arrayToDataTable([
          ['Opening Move', 'Percentage', { role: "style" }, { role: 'annotation' }],
          ["King's pawn (e4)", 44, "#b87333",'43%'],
          ["Queen's pawn (d4)", 31, "silver", '31%'],
          ["Knight to King 3 (Nf3)", 12, "gold", '12%'],
          ["Queen's bishop pawn (c4)", 10, "color: #e5e4e2", '10%'],
          ['Other', 3, "green", '3%']
        ]);

        var options = {
          title: 'Chess opening moves',
          width: 900,
          legend: { position: 'none' },
          chart: { title: 'Chess opening moves',
                   subtitle: 'popularity by percentage' },
          bars: 'horizontal', // Required for Material Bar Charts.
          axes: {
            x: {
              0: { side: 'top', label: 'Percentage'} // Top x-axis.
            }
          },
          bar: { groupWidth: "90%" }
        };

        //var chart = new google.charts.Bar(document.getElementById('top_x_div'));
        var chart = new google.visualization.BarChart(document.getElementById("top_x_div"));
        chart.draw(data, options);
      };
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<div id="top_x_div" style="width: 640px; height: 480px;"></div>


来源:https://stackoverflow.com/questions/31753281/annotations-with-material-column-chart

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!