【问题标题】:Looker Custom VisualizationLooker 自定义可视化
【发布时间】:2021-03-09 03:29:08
【问题描述】:

我想制作一个自定义可视化,它将从第一行添加两个度量的值并显示该值。为此,我一直在使用 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()
  }
});

【问题讨论】:

    标签: javascript visualization dashboard looker


    【解决方案1】:

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

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

    【讨论】:

    • 他想要一个 Looker 默认不提供的可视化
    【解决方案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 自定义可视化的非常好的资源,我强烈推荐它。

    【讨论】:

      猜你喜欢
      • 2022-08-22
      • 2022-10-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多