Annotation Google Chart API

北慕城南 提交于 2019-11-28 02:20:15

just so happens, i ran into the same problem this week

so I added my own annotations, during the 'animationfinish' event

see following working snippet...

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

function drawChart() {
  var dataChart = new google.visualization.DataTable({"cols":[{"label":"Category","type":"string"},{"label":"Bottom 1","type":"number"},{"label":"Bottom 2","type":"number"},{"label":"Top 1","type":"number"},{"label":"Top 2","type":"number"},{"role":"style","type":"string","p":{"role":"style"}}],"rows":[{"c":[{"v":"Budget"},{"v":0},{"v":0},{"v":22707893.613},{"v":22707893.613},{"v":"#007fff"}]},{"c":[{"v":"Contract Labor"},{"v":22707893.613},{"v":22707893.613},{"v":22534350.429},{"v":22534350.429},{"v":"#1e8449"}]},{"c":[{"v":"Contract Non Labor"},{"v":22534350.429},{"v":22534350.429},{"v":22930956.493},{"v":22930956.493},{"v":"#922b21"}]},{"c":[{"v":"Materials and Equipment"},{"v":22930956.493},{"v":22930956.493},{"v":22800059.612},{"v":22800059.612},{"v":"#1e8449"}]},{"c":[{"v":"Other"},{"v":22800059.612},{"v":22800059.612},{"v":21993391.103},{"v":21993391.103},{"v":"#1e8449"}]},{"c":[{"v":"Labor"},{"v":21993391.103},{"v":21993391.103},{"v":21546003.177999996},{"v":21546003.177999996},{"v":"#1e8449"}]},{"c":[{"v":"Travel"},{"v":21546003.177999996},{"v":21546003.177999996},{"v":21533258.930999994},{"v":21533258.930999994},{"v":"#1e8449"}]},{"c":[{"v":"Training"},{"v":21533258.930999994},{"v":21533258.930999994},{"v":21550964.529999994},{"v":21550964.529999994},{"v":"#922b21"}]},{"c":[{"v":"Actual"},{"v":0},{"v":0},{"v":21550964.52999999},{"v":21550964.52999999},{"v":"#007fff"}]}]});

  var waterFallChart = new google.visualization.ChartWrapper({
    chartType: 'CandlestickChart',
    containerId: 'chart_div',
    dataTable: dataChart,
    options: {
      animation: {
        duration: 1500,
        easing: 'inAndOut',
        startup: true
      },
      backgroundColor: 'transparent',
      bar: {
        groupWidth: '85%'
      },
      chartArea: {
        backgroundColor: 'transparent',
        height: 210,
        left: 60,
        top: 24,
        width: '100%'
      },
      hAxis: {
        slantedText: false,
        textStyle: {
          color: '#616161',
          fontSize: 9
        }
      },
      height: 272,
      legend: 'none',
      tooltip: {
        isHtml: true,
        trigger: 'both'
      },
      vAxis: {
        format: 'short',
        gridlines: {
          count: -1
        },
        textStyle: {
          color: '#616161'
        },
        viewWindow: {
          max: 24000000,
          min: 16000000
        }
      },
      width: '100%'
    }
  });

  google.visualization.events.addOneTimeListener(waterFallChart, 'ready', function () {
    google.visualization.events.addListener(waterFallChart.getChart(), 'animationfinish', function () {
      var annotation;
      var chartLayout;
      var container;
      var numberFormatShort;
      var positionY;
      var positionX;
      var rowBalance;
      var rowBottom;
      var rowFormattedValue;
      var rowIndex;
      var rowTop;
      var rowValue;
      var rowWidth;

      container = document.getElementById(waterFallChart.getContainerId());
      chartLayout = waterFallChart.getChart().getChartLayoutInterface();
      numberFormatShort = new google.visualization.NumberFormat({
        pattern: 'short'
      });
      rowIndex = 0;
      Array.prototype.forEach.call(container.getElementsByTagName('rect'), function(rect) {
        switch (rect.getAttribute('fill')) {
          // use colors to identify bars
          case '#922b21':
          case '#1e8449':
          case '#007fff':
            rowWidth = parseFloat(rect.getAttribute('width'));
            if (rowWidth > 2) {
              rowBottom = waterFallChart.getDataTable().getValue(rowIndex, 1);
              rowTop = waterFallChart.getDataTable().getValue(rowIndex, 3);
              rowValue = rowTop - rowBottom;
              rowBalance = Math.max(rowBottom, rowTop);
              positionY = chartLayout.getYLocation(rowBalance) - 6;
              positionX = parseFloat(rect.getAttribute('x'));
              rowFormattedValue = numberFormatShort.formatValue(rowValue);
              if (rowValue < 0) {
                rowFormattedValue = rowFormattedValue.replace('-', '');
                rowFormattedValue = '(' + rowFormattedValue + ')';
              }
              annotation = container.getElementsByTagName('svg')[0].appendChild(container.getElementsByTagName('text')[0].cloneNode(true));
              $(annotation).text(rowFormattedValue);
              annotation.setAttribute('x', (positionX + (rowWidth / 2)));
              annotation.setAttribute('y', positionY);
              annotation.setAttribute('font-weight', 'bold');
              rowIndex++;
            }
            break;
        }
      });
    });
  });

  $(window).resize(function() {
    waterFallChart.draw();
  });
  waterFallChart.draw();
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="chart_div"></div>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!