Looker自定义可视化

3
我想制作一个自定义可视化,将第一行的两个度量值相加并显示出来。为此,我使用了Looker提供的HelloWorld代码,并进行了一些更改(红色文本)。但是我没有得到预期的结果,它只输出 Undefined 。

enter image description here

这是我的代码:

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()
  }
});
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自定义可视化,我强烈推荐它。


0
为什么不制作一个自定义表格计算来进行加法,再使用单值可视化来展示结果呢?或者你是因为其他原因想要进行自定义可视化呢?
第一步应该是使用表格计算来进行加法,并在其他字段上“隐藏不可视”,这样只有一个字段的值被传递到可视化中。然后可视化将会更加直观明了。

他想要一个Looker默认没有提供的可视化。 - Fuad Fouad

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接