【问题标题】:How do I conditionally format Google visualization table cells using chart wrapper function?如何使用图表包装函数有条件地格式化 Google 可视化表格单元格?
【发布时间】:2016-05-12 11:09:38
【问题描述】:

我是 Google Visualization Api 的新手,希望有人可以帮助我有条件地格式化我的 google 可视化表中单元格的颜色。我已经能够更改不同列显示的数字格式,但在颜色格式方面没有这样的运气。我正在使用 arrayToDataTable 和 chartwrapper 函数来显示我从电子表格中查询的一些数据。

在不接受格式的情况下,我需要使用 colorFormat 变量或 chartwrapper 函数进行更改吗?提前谢谢!

function drawDashboard(response) {
  $('#main-heading').addClass("hidden");
  if (response == null) {
    alert('Error: Invalid source data.')
    return;
  } else {

    // Transmogrify spreadsheet contents (array) to a DataTable object
    var responseObjects = JSON.parse(response);
    console.log(responseObjects);
    var testData = [];
    for (var i = 1; i < responseObjects.length; i++) {
      responseObjects[i][0] = new Date(responseObjects[i][0]);
    }
    var data = google.visualization.arrayToDataTable(responseObjects, false);
    console.log(data);
    var dashboard = new google.visualization.Dashboard(document.getElementById('dashboard-div'));

    var percentFormatter = new google.visualization.NumberFormat({
      pattern: '#,###.##%'
    });
    percentFormatter.format(data, 1);
    percentFormatter.format(data, 3);

    var numberFormatter = new google.visualization.NumberFormat({
      pattern: '#.##'
    });
    numberFormatter.format(data, 7);
    numberFormatter.format(data, 8);

    var colorFormatter = new google.visualization.ColorFormat();
    colorFormatter.addRange(0, 5, 'white', 'orange');
    colorFormatter.addRange(20000, 6, 'red', '#33ff33');
    colorFormatter.format(data, 8);
    colorFormatter.format(data, 9);
    colorFormatter.format(data, 10);
    colorFormatter.format(data, 11);

    var table = new google.visualization.ChartWrapper({
      'chartType': 'Table',
      'containerId': 'table-div',
      'view': {
        'columns': [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
      },
    });

    var donutSlider = new google.visualization.ControlWrapper({
      'controlType': 'DateRangeFilter',
      'containerId': 'slider-div',
      'options': {
        'filterColumnLabel': 'Date'
      }
    });

    // Set up dependencies between controls and charts
    dashboard.bind(donutSlider, [table]);
    // Draw all visualization components of the dashboard
    dashboard.draw(data);
  }
}

【问题讨论】:

    标签: javascript google-apps-script google-visualization


    【解决方案1】:

    看起来您正确使用了格式化程序
    但是参数有点偏离

    表格选项中还需要allowHtml: true

    使用链接的电子表格查看以下示例...

    google.charts.load('current', {
      callback: function () {
        var query = new google.visualization.Query(
          'https://docs.google.com/spreadsheets/d/1TBTX_OmNUiq_J0uXEstkxeD6mtImi7BAPWKDBAQIiFA/edit#gid=0'
        );
        query.setQuery("select *");
        query.send(drawDashboard);
      },
      packages: ['controls', 'table']
    });
    
    function drawDashboard(response) {
      if (response.isError()) {
        console.log('Error in query: ' + response.getMessage() + ' ' + response.getDetailedMessage());
        return;
      }
    
      var data = response.getDataTable();
    
      var dashboard = new google.visualization.Dashboard(document.getElementById('dashboard-div'));
    
      var percentFormatter = new google.visualization.NumberFormat({
        pattern: '#,###.##%'
      });
      percentFormatter.format(data, 1);
      percentFormatter.format(data, 3);
    
      var numberFormatter = new google.visualization.NumberFormat({
        pattern: '#.##'
      });
      numberFormatter.format(data, 7);
      numberFormatter.format(data, 8);
    
      var colorFormatter = new google.visualization.ColorFormat();
      colorFormatter.addRange(-20000, 0, 'white', 'orange');
      colorFormatter.addRange(20000, null, 'red', '#33ff33');
      colorFormatter.format(data, 8);
      colorFormatter.format(data, 9);
      colorFormatter.format(data, 10);
      colorFormatter.format(data, 11);
    
      var table = new google.visualization.ChartWrapper({
        chartType: 'Table',
        containerId: 'table-div',
        options: {
          allowHtml: true
        }
      });
    
      var donutSlider = new google.visualization.ControlWrapper({
        controlType: 'DateRangeFilter',
        containerId: 'slider-div',
        options: {
          filterColumnLabel: 'Date'
        }
      });
    
      dashboard.bind(donutSlider, [table]);
      dashboard.draw(data);
    }
    <script src="https://www.gstatic.com/charts/loader.js"></script>
    <div id="dashboard-div">
      <div id="slider-div"></div>
      <div id="table-div"></div>
    </div>

    【讨论】:

    • 希望对您有所帮助,here 是一个工作示例,以及用于添加颜色格式的其他选项...
    • 感谢您的快速回复!当我调整单元格的值时,我仍然没有得到任何颜色格式。即使我从链接的文档中粘贴示例,背景和文本仍保持默认值。 Here 是一个网络应用程序,它显示没有颜色的表格。使用:
    • formatter.addRange(-20000, 0, 'white', 'orange'); formatter.addRange(20000, null, 'red', '#33ff33'); formatter.format(data, 8); formatter.format(data, 9); formatter.format(data, 10);
    • 在表格选项中还需要allowHtml: true,请参阅使用链接电子表格编辑...
    • 太棒了!效果很好。非常感谢您的帮助。
    猜你喜欢
    • 2020-09-09
    • 1970-01-01
    • 2012-11-21
    • 2017-03-18
    • 1970-01-01
    • 2012-03-09
    • 2019-07-28
    • 2014-03-05
    • 1970-01-01
    相关资源
    最近更新 更多