使用注释移动Google Chart列注释位置:line

时间:2018-01-26 10:38:17

标签: javascript charts google-visualization

使用下面的示例,是否可以移动注释,以便在使用style: 'line'(使注释垂直读取)时,它们都出现在图表底部的相同(静态)位置?

google.charts.load('current', {
  callback: function () {
    drawChart();
    window.addEventListener('resize', drawChart, false);
  },
  packages:['corechart']
});

function drawChart() {
    var data = google.visualization.arrayToDataTable([
        ['Type', 'Completed', 'Outstanding'],
        ['', 75, 25],
        ['', 50, 40],
        ['', 80, 15]
    ]);

    var view = new google.visualization.DataView(data);
    view.setColumns([0, 1,
        {
            calc: "stringify",
            sourceColumn: 1,
            type: "string",
            role: "annotation"
        },
        2,
        {
            calc: "stringify",
            sourceColumn: 2,
            type: "string",
            role: "annotation"
        }]);

    var options = {
        legend: 'none',
        height: 270,
        chartArea: { 'width': '80%', 'height': '80%' },
        bar: { groupWidth: '80%' },
            annotations: {
              style: 'line',
                textStyle: {
                fontSize: 8,
                color: 'black',
                strokeSize: 0,
                auraColor: 'transparent'
              },
              alwaysOutside: true,  
              stem:{
                            color: 'transparent',
                          },   
            },        
        vAxis: {
            textPosition: 'none',
            gridlines: {
                color: 'transparent'
            }
        },
      series: {
          0: { color: '#00A887' },
          1: { color: '#F6323E' },
        }

    };

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

    // move annotations
    var observer = new MutationObserver(function () {
      Array.prototype.forEach.call(container.getElementsByTagName('text'), function(annotation) {
        if ((annotation.getAttribute('text-anchor') === 'middle') &&
            (annotation.getAttribute('fill') === '#ffffff')) {
          var chartLayout = chart.getChartLayoutInterface();
          annotation.setAttribute('y',
            chartLayout.getYLocation(0) - (parseInt(annotation.getAttribute('font-size')) / 2)
          );
        }
      });
    });
    observer.observe(container, {
      childList: true,
      subtree: true
    });

    chart.draw(view, options);
}

这是一个JSFiddle示例:http://jsfiddle.net/tVCv9/340/

这就是我想要实现的目标: -

enter image description here

1 个答案:

答案 0 :(得分:1)

看起来你找到了一个移动注释的例子,
但是在这种情况下,由于正在使用style: 'line', 您必须同时调整'y'属性以及'rotation'

请参阅以下工作代码段...

google.charts.load('current', {
  packages:['corechart']
}).then(function () {
  var data = google.visualization.arrayToDataTable([
    ['Type', 'Completed', 'Outstanding'],
    ['', 75, 25],
    ['', 50, 40],
    ['', 80, 15]
  ]);

  var view = new google.visualization.DataView(data);
  view.setColumns([0, 1, {
    calc: 'stringify',
    sourceColumn: 1,
    type: 'string',
    role: 'annotation'
  }, 2, {
    calc: 'stringify',
    sourceColumn: 2,
    type: 'string',
    role: 'annotation'
  }]);

  var options = {
    annotations: {
      style: 'line',
      textStyle: {
        fontSize: 8,
        color: 'black',
        strokeSize: 0,
        auraColor: 'transparent'
      },
      alwaysOutside: true,
      stem:{
        color: 'transparent',
      },
    },
    legend: 'none',
    height: 270,
    chartArea: {
      width: '80%',
      height: '80%'
    },
    bar: {
      groupWidth: '80%'
    },
    vAxis: {
      textPosition: 'none',
      gridlines: {
        color: 'transparent'
      }
    },
    series: {
      0: {color: '#00A887'},
      1: {color: '#F6323E'},
    }
  };

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

  // move annotations
  google.visualization.events.addListener(chart, 'ready', function () {
    var chartLayout = chart.getChartLayoutInterface();
    var yLocation = chartLayout.getYLocation(0) + options.annotations.textStyle.fontSize;
    var observer = new MutationObserver(function () {
      Array.prototype.forEach.call(container.getElementsByTagName('text'), function(annotation) {
        if ((annotation.getAttribute('text-anchor') === 'middle') &&
            (parseInt(annotation.getAttribute('font-size')) === options.annotations.textStyle.fontSize)) {
          var rotate = 'rotate(270 ' + annotation.getAttribute('x') + ' ' + yLocation + ')';
          annotation.setAttribute('y', yLocation);
          annotation.setAttribute('transform', rotate);
        }
      });
    });
    observer.observe(container, {
      childList: true,
      subtree: true
    });
  });

  chart.draw(view, options);
});
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="northPMChart"></div>