javascript - 是否可以为 Google 条形图中的不同数据行设置不同的图例背景颜色,而不破坏图表?

标签 javascript charts google-visualization bar-chart data-visualization

查看示例代码:

var data = new google.visualization.DataTable();
    data.addColumn('string', 'Mac');
    data.addColumn('number', 'Score');
    data.addColumn({ type: 'string', role: 'style' });
    data.addRows([
       ['Mac model 12', 200, 'color: #8bba30; opacity: 0.75;'],
       ['Another Mac Model', 110, 'color: #ffcc33; opacity: 0.75;'],
    ]);
    var options = {
        title: '',
        width: 500,
        height: data.getNumberOfRows() * 40 + 100,
        hAxis: {
            minValue: 0,
            maxValue: 255,
            ticks: [0, 75, 150, 255],
            textPosition: 'out',
            side: 'top'
        },
        series: {
            0: { axis: 'Mac' },
            1: { axis: 'Score' }
        },
        chartArea: {
            top: 0,
            bottom: 50,
            right: 50,
            left: 150
        },
        legend: { position: 'none' },
        fontSize: 12,
        bar: {groupWidth: '75%'},
    };
    var chart = new google.visualization.BarChart(document.getElementById('apple_div'));
    chart.draw(data, options);
}

这是输出:

enter image description here

看,不同的栏有不同的颜色。但我想要左侧不同图例的不同颜色和/或背景颜色。

有人可以帮我解决这个问题吗?

我找到了以下答案,Is it possible to show each legend in different color in google pie chart .

但它建议分解图表(即为每一行绘制单独的图表),这是不可取的,因为有大量的行。

最佳答案

不确定破坏图表是什么意思,但是...

一旦 'ready' 事件触发,您就可以修改图表 svg。

此示例更改图例文本的颜色以匹配条形颜色。

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

function drawChart() {
  var colors = ['#8bba30', '#ffcc33'];
  var data = new google.visualization.DataTable();
  data.addColumn('string', 'Mac');
  data.addColumn('number', 'Score');
  data.addColumn({ type: 'string', role: 'style' });
  data.addRows([
     ['Mac model 12', 200, 'color: ' + colors[0] + '; opacity: 0.75;'],
     ['Another Mac Model', 110, 'color: ' + colors[1] + '; opacity: 0.75;'],
  ]);
  var options = {
      title: '',
      width: 500,
      height: data.getNumberOfRows() * 40 + 100,
      hAxis: {
          minValue: 0,
          maxValue: 255,
          ticks: [0, 75, 150, 255],
          textPosition: 'out',
          side: 'top'
      },
      series: {
          0: { axis: 'Mac' },
          1: { axis: 'Score' }
      },
      chartArea: {
          top: 0,
          bottom: 50,
          right: 50,
          left: 150
      },
      legend: { position: 'none' },
      fontSize: 12,
      bar: {groupWidth: '75%'},
  };

  var chartContainer = document.getElementById('apple_div');
  var chart = new google.visualization.BarChart(chartContainer);
  google.visualization.events.addListener(chart, 'ready', function () {
    var labels = chartContainer.getElementsByTagName('text');
    var colorIndex = 0;
    for (var i = 0; i < labels.length; i++) {
      if (labels[i].getAttribute('text-anchor') === 'end') {
        labels[i].setAttribute('fill', colors[colorIndex]);
        colorIndex++;
      }
    }
  });
  chart.draw(data, options);
}
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="apple_div"></div>

至于背景颜色,SVG elements do not have background
所以你必须为此绘制自己的矩形...

关于javascript - 是否可以为 Google 条形图中的不同数据行设置不同的图例背景颜色,而不破坏图表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36766839/

相关文章:

javascript - 尝试通过 (jQuery)ajax 调用加载 Google 图表

javascript - 如何使用脚本禁用当前行的输入文本值

javascript - 在鼠标悬停时显示(悬停)图表并在鼠标悬停时隐藏图表

c# - .net图表清除并重新添加

javascript - 如何防止最后一个刻度渲染?使用 'recharts' 库

javascript - 在 h 轴上仅显示部分文本

javascript - 如何将 ajax post 连接到 PHP 文件中的特定条件?

javascript - 我可以将类字段链接到orientdb中的多个类吗?

javascript - 我如何知道 `AnalyserNode` 的 FFT 分析数据中哪一个对应于哪个频率?

javascript - Google Visualization Table Chart - 更改列名称