简体   繁体   English

PowerBI自定义Visual - 表数据绑定

[英]PowerBI Custom Visual - Table data binding

Also asked this on the PowerBI forum. 也在PowerBI论坛上问过这个问题。

I am trying to change sampleBarChart PowerBI visual to use a "table" data binding instead of current "categorical". 我正在尝试更改sampleBarChart PowerBI visual以使用“表”数据绑定而不是当前“分类”。 First goal is to build a simple table visual, with inputs "X", "Y" and "Value". 第一个目标是构建一个简单的表格视觉,输入“X”,“Y”和“值”。

Both data bindings are described on the official wiki . 两种数据绑定都在官方维基上进行了描述。 This is all I could find: 这是我能找到的全部: 图片

I cannot find any example visuals which use it and are based on the new API. 我找不到任何使用它的示例视觉效果,并且基于新的API。


From the image above, a table object has "rows", "columns", "totals" and "identities". 从上图中,表对象具有“行”,“列”,“总计”和“身份”。 So it looks like rows and columns are my x/y indexes, and totals are my values? 所以看起来行和列是我的x / y索引,总数是我的值?

This is what I tried. 这是我试过的。 (Naming is slightly off as most of it came from existing barchart code) (由于大多数来自现有条形码代码,因此命名略有偏差)

Data roles: 数据角色:

   {   "displayName": "Category1 Data",
        "name": "category1",
        "kind": 0},
   {   "displayName": "Category2 Data",
        "name": "category2",
        "kind": 0},
   {   "displayName": "Measure Data",
        "name": "measure",
        "kind": 1}

Data view mapping: 数据视图映射:

"table": {
    "rows": {"for": {"in": "category1"}},
    "columns": {"for": {"in": "category2"}},
    "totals": {"select": [{"bind": {"to": "measure"}}]}
}

Data Point class: 数据点类:

interface BarChartDataPoint {
    value: number;
    category1: number;
    category2: number;
    color: string;
};

Relevant parts of my visualTransform(): 我的visualTransform()的相关部分:

    ...
let category1 = categorical.rows;
let category2 = categorical.columns;
let dataValue = categorical.totals;
    ...
for (let i = 1, len = category1.length; i <= len; i++) {
for (let j = 1, jlen = category2.length; j <= jlen; j++) {
    {
        barChartDataPoints.push({
            category1: i,
            category2: j,
            value: dataValue[i,j],
            color: "#555555"//for now
    });
}
    ...

Test data looks like this: 测试数据如下所示:

__1_2_3_
1|4 4 3
2|4 5 5
3|3 6 7 (total = 41)

The code above fills barChartDataPoints with just six data points: 上面的代码只用六个数据点填充barChartDataPoints:

(1; 1; 41), (1; 1; 41),
(1; 2; undefined), (1; 2;未定义),
(2; 1; 41), (2; 1; 41),
(2; 2; undefined), (2; 2;未定义),
(3; 1; 41), (3; 1; 41),
(3; 2; undefined). (3; 2;未定义)。

Accessing zero indeces results in nulls. 访问零indeces会导致null。

Q: Is totals not the right measure to access value at (x;y)? 问: totals不是在(x; y)访问价值的正确措施吗? What am I doing wrong? 我究竟做错了什么?


Any help or direction is very appreciated. 非常感谢任何帮助或方向。

User @RichardL shared this link on the PowerBI forum. 用户@RichardL在PowerBI论坛上分享了这个链接 Which helped quite a lot. 这帮助了很多。

"Totals" is not the right measure to access value at (x;y). “总计”不是(x; y)获取价值的正确措施。

It turns out Columns contain column names, and Rows contain value arrays which correspond to those columns. 事实证明, Columns包含列名,而Rows包含与这些列对应的值数组。

From the link above, this is how table structure looks like: 从上面的链接,这是表结构的样子:

{
 "columns":[
  {"displayName": "Year"},
  {"displayName": "Country"},
  {"displayName": "Cost"}
 ],
 "rows":[
  [2014, "Japan", 25],
  [2015, "Japan", 30],
  [2016, "Japan", 18],
  [2015, "North America", 14],
  [2016, "North America", 30],
  [2016, "China", 100]
 ]
}

You can also view the data as your visual receives it by placing this 您也可以通过放置此视图来查看数据

window.alert(JSON.stringify(options.dataViews))

In your update() method. 在您的update()方法中。 Or write it in html contents of your visual. 或者将其写在视觉的html内容中。

This was very helpful but it shows up a few fundamental problems with the data management of PowerBI for a custom visual. 这非常有用,但它显示了PowerBI数据管理的一些基本问题,用于自定义视觉。 There is no documentation and the process from Roles to mapping to visualTransform is horrendous because it takes so much effort to rebuild the data into a format that is usable consistently with D3. 没有文档,从角色到映射到visualTransform的过程是可怕的,因为它需要花费很多精力将数据重建为可与D3一致使用的格式。

Commenting on user5226582's example, for me, columns is presented in a form where I have to look up the Roles property to be able to understand the order of data presented in the rows column array. 在评论user5226582的示例时,对于我来说,列以一种形式呈现,我必须查找Roles属性以便能够理解rows列数组中显示的数据的顺序。 displayName offers no certainty. displayName没有确定性。 For exampe, if a user uses the same field in two different dataRoles then it all gets crazily awry. 例如,如果用户在两个不同的dataRoles中使用相同的字段,那么这一切都会疯狂地出错。

I think the safest approach is to build a new array inside visualTransform using the known well-field names (the "name" property in dataRoles), then iterate columns interrogating the Roles property to establish an index to the rows array items. 我认为最安全的方法是使用已知的井场名称(dataRoles中的“name”属性)在visualTransform中构建一个新数组,然后迭代查询Roles属性的列以建立行数组项的索引。 Then use that index to populate the new array reliably. 然后使用该索引可靠地填充新数组。 D3 then gobbles that up. 然后D3吞噬了它。

I know that's crazy, but at least it means reasonably consistent data and allows for the user selecting the same data field more than once or choosing count instead of column value. 我知道这很疯狂,但至少它意味着合理一致的数据,并允许用户多次选择相同的数据字段或选择计数而不是列值。

All in all, I think this area needs a lot of attention before custom Visuals can really take off. 总而言之,我认为在自定义Visuals真正起飞之前,这个领域需要大量关注。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM