【问题标题】:google charts move annotation position to center of stacked chart谷歌图表将注释位置移动到堆叠图表的中心
【发布时间】:2023-03-25 18:53:02
【问题描述】:

我正在使用 Google Chart 的柱形图。该图表是一个堆积柱形图,为堆积柱形的每个数据点添加了注释。注释位于条形内部的顶部,但我希望它们位于条形内部的中心。我找到的最接近的解决方案是将非堆叠柱状谷歌图表的注释移动到底部here

这个解决方案的问题是代码会寻找白色的注释并将它们移动到底部。我正在处理的图表有多种颜色的注释,并且该颜色的所有注释都移动到顶部而不是堆叠列子集的中心。

 google.charts.setOnLoadCallback(drawChart);
    function drawChart() {

    var data = google.visualization.arrayToDataTable([
      ['Range', 'score range A',{role: 'style'},{ role: 'annotation' }, 'score range B',{role: 'style'},{ role: 'annotation' }, 'score range C', {role: 'style'},{ role: 'annotation' },'score range D', {role: 'style'},{ role: 'annotation' }],
      ['Range', 4,'#D7F0B4','0-4',6,'#00B050','5-10',9,'#92D050','11-19',6, '#AAE182','20-25']
    ]);

    var options = {
      vAxis:{ ticks: [0,4,10,19], viewWindow: { max: 25} },
      isStacked: true,
      title: 'Score',
      legend: {position: 'none'}
    };

    var chart = new google.visualization.ColumnChart(document.getElementById('idealchartA'));

    chart.draw(data, options);
  }

@Whitehat 解决了上述问题,但是当我将另一列的某些注释值设为 null 时,定位被抛出。

这是我尝试添加的新数据系列 ['Range', 4,'#D7F0B4','0-4',6,'#00B050','5-10',9,'#92D050','11-19',6, '#AAE182','20-25'], ['Yours', 4,'white; fill-opacity: 0',null,6, '#00B050',10, 9,'white; fill-opacity: 0',null,6,'white; fill-opacity: 0',null].

我们的想法是制作如下图所示的图表,但注释居中。

【问题讨论】:

    标签: charts annotations position google-visualization stacked


    【解决方案1】:

    与其他问题类似,只需要一种查找注释的方法。
    在这里,我们使用text-anchor 属性,假设它不是x 轴标签。

    然后我们可以使用图表方法getChartLayoutInterface 来计算新的位置。

    请参阅以下工作 sn-p...

    google.charts.load('current', {
      packages:['corechart']
    }).then(function () {
      var data = google.visualization.arrayToDataTable([
        ['Range', 'score range A',{role: 'style'},{ role: 'annotation' }, 'score range B',{role: 'style'},{ role: 'annotation' }, 'score range C', {role: 'style'},{ role: 'annotation' },'score range D', {role: 'style'},{ role: 'annotation' }],
        ['Range', 4,'#D7F0B4','0-4',6,'#00B050','5-10',9,'#92D050','11-19',6, '#AAE182','20-25']
      ]);
    
      var options = {
        vAxis:{ ticks: [0,4,10,19], viewWindow: { max: 25} },
        isStacked: true,
        title: 'Score',
        legend: {position: 'none'}
      };
    
      var container = document.getElementById('idealchartA');
      var chart = new google.visualization.ColumnChart(container);
    
      google.visualization.events.addListener(chart, 'ready', function () {
        var observer = new MutationObserver(moveAnnotations);
        observer.observe(container, {
          childList: true,
          subtree: true
        });
      });
    
      function moveAnnotations() {
        var chartLayout = chart.getChartLayoutInterface();
        var barBounds;
        var labelBounds;
        var yAdj;
    
        var labels = container.getElementsByTagName('text');
        var index = 0;
        Array.prototype.forEach.call(labels, function(label) {
          if ((label.getAttribute('text-anchor') === 'middle') && (label.textContent !== data.getValue(0, 0))) {
            barBounds = chartLayout.getBoundingBox('bar#' + index + '#0');
            labelBounds = chartLayout.getBoundingBox('annotationtext#' + index + '#0#0');
            yAdj = (barBounds.height / 2) + (parseFloat(label.getAttribute('font-size')) / 2) - 2;
            label.setAttribute('y', barBounds.top + yAdj);
            index++;
          }
        });
      }
    
      chart.draw(data, options);
    });
    <script src="https://www.gstatic.com/charts/loader.js"></script>
    <div id="idealchartA"></div>

    【讨论】:

    • 我添加了一个新的堆叠列,其中包含空值作为注释,但这会使 javascript 关闭,因为它可以计算空值的距离。 ['范围', 4,'#D7F0B4','0-4',6,'#00B050','5-10',9,'#92D050','11-19',6, '#AAE182' ,'20-25'], ['你的', 4,'白色;填充不透明度:0',null,6, '#00B050',10, 9,'white;填充不透明度:0',null,6,'white;填充不透明度:0',null]
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-27
    相关资源
    最近更新 更多