google-visualization - Google 表格图表 : how do I change the row background color based on a column value

标签 google-visualization

我正在使用谷歌表格图表将一些数据显示为表格。根据行上的状态值,我必须更改行的背景颜色。所以我必须动态显示多种颜色。我检查了表格图表文档并在线搜索。但是找不到提供这样的功能。请帮忙。

最佳答案

这里有一些选项供您选择...

  • 使用 ColorFormat formatter
  • 通过提供在数据中添加您自己的 html

    v: value
    f: formatted value
    and
    allowHtml: true configuration option

  • 'ready' 事件
  • 上自己修改表

    请参阅以下使用所有三个示例的示例...

    google.charts.load('current', {
      callback: function () {
        var data = new google.visualization.DataTable();
        data.addColumn('string', 'Department');
        data.addColumn('number', 'Revenues');
        data.addRows([
          ['Shoes', 10700],
          ['Sports', -15400],
          ['Toys', 12500],
          ['Electronics', -2100],
          ['Food', 22600],
          ['Art', 1100],
          [
            // add you own html
            {v: 'Web', f: '<div style="background-color: cyan;">Web</div>'},
            {v: 9999, f: '<div style="background-color: cyan;">9999</div>'}
          ]
        ]);
    
        var container = document.getElementById('table_div');
        var table = new google.visualization.Table(container);
        google.visualization.events.addListener(table, 'ready', function () {
          for (var i = 0; i < data.getNumberOfRows(); i++) {
            if (data.getValue(i, 1) === 1100) {
              // modify the table directly on 'ready'
              container.getElementsByTagName('TR')[i+1].style.backgroundColor = 'magenta';
            }
          }
        });
    
        // use formatter
        var formatter = new google.visualization.ColorFormat();
        formatter.addRange(-20000, 0, 'white', 'orange');
        formatter.addRange(20000, null, 'red', '#33ff33');
        formatter.format(data, 1); // Apply formatter to second column
    
        table.draw(data, {
          allowHtml: true
        });
      },
      packages: ['table']
    });
    <script src="https://www.gstatic.com/charts/loader.js"></script>
    <div id="table_div"></div>

    关于google-visualization - Google 表格图表 : how do I change the row background color based on a column value,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36946154/

    相关文章:

    javascript - 谷歌图表不显示

    html - Google 图表仪表板未扩展至 100% 高度?

    javascript - 转换 Google Chart 数据表的 JSON 数据

    php - Google Charts & MySQL Timestamp 处理问题

    html - 我如何从 Google Visualization 中将条形图居中?

    javascript - Google chart api explorer 与 selecthandler

    javascript - 使用 Google Visualization,为什么 DataView 内容显示在 ChartRangeFilter 中,而不显示在其关联的 LineChart 中?

    javascript - Google Chart 表格中的标题行修复不起作用

    javascript - Google 图表 LineGraph 切换数据表和维护状态

    javascript - Google 可视化 - 第 0 行的行类型无效