自定义样式 Google 条形图

Custom styling Google bar chart

是否有可能使 Google 条形图看起来像这样?

  1. 带有自定义样式的每个栏的结尾
  2. 注释在线下方(目标 10.3)

您可以使用图表布局方法将图标或任何元素添加到栏的末尾。

  // add icon to bar
  var barBounds = layout.getBoundingBox('bar#0#0');
  var icon = chart.getContainer().appendChild(document.createElement('span'));
  icon.className = 'icon';
  icon.style.top = (barBounds.top + containerBounds.top - 3) + 'px';
  icon.style.left = (barBounds.left + containerBounds.left + (barBounds.width) - 24) + 'px';
  icon.innerHTML = '<i class="fas fa-arrow-alt-circle-right"></i>';

另外,不要绘制注释并试图阻止图表移动它,
我们可以省略它并添加我们自己的自定义注释...

  // add annotation
  var labelCopy = svg.getElementsByTagName('text')[0];
  var annotation = labelCopy.cloneNode(true);
  svg.appendChild(annotation);
  annotation.setAttribute('text-anchor', 'middle');
  annotation.textContent = data.getValue(0, data.getNumberOfColumns() -1);
  annotation.setAttribute('x', xLoc);
  annotation.setAttribute('y',
    layout.getYLocation(0) + (parseInt(annotation.getAttribute('font-size')) * 3)
  );

请参阅以下工作片段...

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

function drawHorizontalChart_portal_name_stella_york_horz_month_points() {
    var data = google.visualization.arrayToDataTable([
        ["", "Goal Achieved", {role: 'style'}, "GOAL 13.1 points", {role: 'style'}, {role: 'annotation'}],
        [1, 1.5, "opacity: .75;", 13.1, "opacity: 0;", "GOAL 13.1 points"]
    ]);

    var view = new google.visualization.DataView(data);
    view.setColumns([0, 1, 3, 4]);

    var options = {
        title: '',
        width: '100%',
        height: 132,
        chartArea: {
          height: '100%',
          width: '100%',
          top: 36,
          left: 18,
          right: 18,
          bottom: 48
        },
        hAxis: {
            title: '',
            minValue: 0,
            gridlines: {
                count: 6
            },
            format: '0'
        },
        bar: {
            groupWidth: "60%"
        },
        legend: {
            position: "top"
        },
        series: {
            0: {
                color: '#70b5c5',
                visibleInLegend: false
            }, // Goal Achieved
            1: {
                color: '#000000',
                type: 'line',
                annotations: {
                    textStyle: {
                        color: '#000000'
                    },
                    stem: {
                        color: 'transparent',
                        length: -128
                    },
                    vertical: true
                }
            } // Target Goal
        },
        vAxis: {
            gridlines: {
                color: 'transparent'
            },
            ticks: [{v: 1, f: ''}]
        }
    };

    var chart = new google.visualization.BarChart(document.getElementById("portal-name-stella-york-horz-month-points"));

    google.visualization.events.addListener(chart, 'click', function(e) {
      console.log(JSON.stringify(e));
    });

    google.visualization.events.addListener(chart, 'ready', function () {
      // init variables
      var layout = chart.getChartLayoutInterface();
      var containerBounds = chart.getContainer().getBoundingClientRect();
      var svg = chart.getContainer().getElementsByTagName('svg')[0];
      var svgNS = svg.namespaceURI;
      var xLoc = drawVAxisLine(chart, layout, data.getValue(0, 3));

      // add image to bar
      var barBounds = layout.getBoundingBox('bar#0#0');
      var icon = chart.getContainer().appendChild(document.createElement('span'));
      icon.className = 'icon';
      icon.style.top = (barBounds.top + containerBounds.top - 3) + 'px';
      icon.style.left = (barBounds.left + containerBounds.left + (barBounds.width) - 24) + 'px';
      icon.innerHTML = '<i class="fas fa-arrow-alt-circle-right"></i>';

      // add annotation
      var labelCopy = svg.getElementsByTagName('text')[0];
      var annotation = labelCopy.cloneNode(true);
      svg.appendChild(annotation);
      annotation.setAttribute('text-anchor', 'middle');
      annotation.textContent = data.getValue(0, data.getNumberOfColumns() -1);
      annotation.setAttribute('x', xLoc);
      annotation.setAttribute('y',
        layout.getYLocation(0) + (parseInt(annotation.getAttribute('font-size')) * 3)
      );
    });

    chart.draw(view, options);
}

jQuery(window).resize(function() {
    drawHorizontalChart_portal_name_stella_york_horz_month_points();
});


function drawVAxisLine(chart, layout, value) {
    var chartArea = layout.getChartAreaBoundingBox();
    var svg = chart.getContainer().getElementsByTagName('svg')[0];
    var xLoc = layout.getXLocation(value)
    svg.appendChild(createLine(xLoc, chartArea.top + chartArea.height, xLoc, chartArea.top, '#000000', 2)); // axis line
    return xLoc;
}

function createLine(x1, y1, x2, y2, color, w) {
    var line = document.createElementNS('http://www.w3.org/2000/svg', 'line');
    line.setAttribute('x1', x1);
    line.setAttribute('y1', y1);
    line.setAttribute('x2', x2);
    line.setAttribute('y2', y2);
    line.setAttribute('stroke', color);
    line.setAttribute('stroke-width', w);
    return line;
}
.icon {
  font-size: 32px;
  position: absolute;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://www.gstatic.com/charts/loader.js"></script>
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.3.1/css/all.css" integrity="sha384-mzrmE5qonljUremFsqc01SB46JvROS7bZs3IO2EmfFsd15uHvIt+Y8vEf7N7fWAU" crossorigin="anonymous">
<div id="portal-name-stella-york-horz-month-points"></div>