3

我想制作一个自定义可视化,它将从第一行添加两个度量的值并显示该值。为此,我一直在使用 Looker 本身提供的 HelloWorld 代码,只需进行少量更改(红色文本)。但我我无法获得预期的结果,它只是将输出作为Undefined

在此处输入图像描述

这是我的代码:-

looker.plugins.visualizations.add({
  // Id and Label are legacy properties that no longer have any function besides documenting
  // what the visualization used to have. The properties are now set via the manifest
  // form within the admin/visualizations page of Looker
  id: "hello_world",
  label: "Hello World",
  options: {
    font_size: {
      type: "string",
      label: "Font Size",
      values: [
        {"Large": "large"},
        {"Small": "small"}
      ],
      display: "radio",
      default: "large"
    }
  },
  // Set up the initial state of the visualization
  create: function(element, config) {

    // Insert a <style> tag with some styles we'll use later.
    element.innerHTML = `
      <style>
        .hello-world-vis {
          /* Vertical centering */
          height: 100%;
          display: flex;
          flex-direction: column;
          justify-content: center;
          text-align: center;
        }
        .hello-world-text-large {
          font-size: 72px;
        }
        .hello-world-text-small {
          font-size: 18px;
        }
      </style>
    `;

    // Create a container element to let us center the text.
    var container = element.appendChild(document.createElement("div"));
    container.className = "hello-world-vis";

    // Create an element to contain the text.
    this._textElement = container.appendChild(document.createElement("div"));

  },
  // Render in response to the data or settings changing
  updateAsync: function(data, element, config, queryResponse, details, done) {

    // Clear any errors from previous updates
    this.clearErrors();

    // Throw some errors and exit if the shape of the data isn't what this chart needs
    if (queryResponse.fields.dimensions.length == 0) {
      this.addError({title: "No Dimensions", message: "This chart requires dimensions."});
      return;
    }

    // Grab the first cell of the data
    var firstRow = data[0];
    var secondRow = data[0];
    var firstCell = firstRow[queryResponse.fields.measures[0].name];
    var secondCell =secondRow[queryResponse.fields.measures[1].name];
    var totalSat = firstCell+secondCell;

    // Insert the data into the page
    this._textElement.innerHTML = LookerCharts.Utils.htmlForCell(totalSat);

    // Set the size to the user-selected size
    if (config.font_size == "small") {
      this._textElement.className = "hello-world-text-small";
    } else {
      this._textElement.className = "hello-world-text-large";
    }

    // We are done rendering! Let Looker know.
    done()
  }
});
4

2 回答 2

2

这几乎是完美的!问题在于以下几行:

    var firstCell = firstRow[queryResponse.fields.measures[0].name];
    var secondCell =secondRow[queryResponse.fields.measures[1].name];
    var totalSat = firstCell+secondCell;

    // Insert the data into the page
    this._textElement.innerHTML = LookerCharts.Utils.htmlForCell(totalSat);

在这里,您尝试获取感兴趣的单元格,然后将它们加在一起以显示它们。

问题是您正在使用LookerCharts.Utils.htmlForCell,它接受单元格名称引用,例如您的示例中的 secondCell 或 firstCell 。当您喂它时 secondCell+firstCell (顺便说一下,在您的情况下可能是 'count_csatcount_dsat',因为您添加的是单元格名称,而不是值),它变得不确定,因为没有单元格称为 ' count_csatcountdsat'。

解决方案(出于示例目的过于冗长,不是最好的)将这些行替换为:

    var firstCell = firstRow[queryResponse.fields.measures[0].name];
    var secondCell =secondRow[queryResponse.fields.measures[1].name];
    var firstCellValue = Number(LookerCharts.Utils.textForCell(firstCell));
    var secondCellValue = Number(LookerCharts.Utils.textForCell(firstCell));

    var totalSat = firstCellValue+secondCellValue;

    // Insert the data into the page
    // This would be unstyled, you'd have to actually construct the HTML string if you wanted it styled
    this._textElement.innerHTML = totalSat;

那有意义吗?很高兴解释更多。这:https ://lookervisbuilder.com/是测试 Looker 自定义可视化的非常好的资源,我强烈推荐它。

于 2021-01-27T00:24:00.850 回答
0

为什么不进行自定义表格计算以进行加法和单值可视化以可视化结果?或者您是否出于其他原因想要进行自定义可视化?

第一步应该是使用表格计算对其他字段进行加法和“从可视化中隐藏”,以便仅将一个字段值传递给可视化。然后可视化将更加直接。

于 2020-12-02T11:59:42.413 回答