Semi-transparent colors in Google Charts?

后端 未结 2 679
忘掉有多难
忘掉有多难 2020-12-21 01:32

A call like

chart.draw(data, { colors: [\'#e0440e\', \'#e6693e\', \'#ec8f6e\', ...], ... });

creates a chart with colors looking like semi-

2条回答
  •  没有蜡笔的小新
    2020-12-21 02:28

    once the 'ready' event fires on the chart, you can modify the svg

    just need a way to find the chart elements you want to modify

    in the following working snippet, random colors are used to feed the chart

    then when 'ready' fires, those colors are found and replaced with rgba

    google.charts.load('current', {
      callback: drawChart,
      packages: ['corechart']
    });
    
    function drawChart() {
      var data = google.visualization.arrayToDataTable([
        ['Year', 'Y1', 'Y2'],
        ['2010', 10, 14],
        ['2020', 14, 22],
        ['2030', 16, 24],
        ['2040', 22, 30],
        ['2050', 28, 36]
      ]);
    
      var seriesColors = ['#00ffff', '#ff00ff'];
      var rgbaMap = {
        '#00ffff': 'rgba(0,255,0,0.5)',
        '#ff00ff': 'rgba(255,0,0,0.5)'
      };
    
      var options = {
        colors: seriesColors,
      };
    
      var chartDiv = document.getElementById('chart_div');
      var chart = new google.visualization.ColumnChart(chartDiv);
    
      // modify svg
      google.visualization.events.addListener(chart, 'ready', function () {
        Array.prototype.forEach.call(chartDiv.getElementsByTagName('rect'), function(rect) {
          if (seriesColors.indexOf(rect.getAttribute('fill')) > -1) {
            rect.setAttribute('fill', rgbaMap[rect.getAttribute('fill')]);
          }
        });
      });
    
      chart.draw(data, options);
    }
    
    

提交回复
热议问题