Power BI Custom Visual:修改日期切片器时的颜色更改

时间:2019-05-22 08:17:31

标签: typescript powerbi powerbi-custom-visuals

我在Power BI中创建了一个自定义视觉对象,该视觉对象允许在一个图中插入不同类型的图表。但是,当我将图表插入仪表板上时,日期切片器出现了一些麻烦。我为ma数据定义了一种颜色,并将其保存为:

let view = dataView[0].categorical;
let categories = view.categories[0];
let objects = categories.objects;
let value = view.values;

for (let i = 0, len = Math.max(value.length); i < len; i++) {
  viewModel.dataPoints.push({
    category: < string > value[i].source.displayName,
    colour: objects && objects[i] && DataViewObjects.getFillColor(objects[i], {
      objectName: "colorSelector",
      propertyName: "fill"
    }, null),
    selectionId: this.host.createSelectionIdBuilder()
      .withCategory(categories, i)
      .createSelectionId(),
  });
}

但是当我更改日期切片器时,对象为null或未定义,并且颜色重置为默认值。

有一种解决方案可以在修改切片器时始终将颜色保存在对象中。

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

enumerateObjectInstances方法:

public enumerateObjectInstances(options: EnumerateVisualObjectInstancesOptions): VisualObjectInstanceEnumeration {
	let objectName = options.objectName;
	let objectEnumeration: VisualObjectInstance[] = [];
	let index = 0;
	switch (objectName) {
		case 'xAxis':
			objectEnumeration.push({
				objectName: objectName,
				properties: this.settings.xAxis,
				selector: null
			});
			break;
		case 'y1Axis':
			objectEnumeration.push({
				objectName: objectName,
				properties: this.settings.y1Axis,
				selector: null
			});
			break;
		case 'y2Axis':
			objectEnumeration.push({
				objectName: objectName,
				properties: this.settings.y2Axis,
				selector: null
			});
			break;
		case 'margin':
			objectEnumeration.push({
				objectName: objectName,
				properties: this.settings.margin,
				selector: null
			});
			break;
		case 'legend':
			objectEnumeration.push({
				objectName: objectName,
				properties: this.settings.legend,
				selector: null
			});
			break;
		case 'colorSelector':
			if (this.viewModel) {
				for (let dp of this.viewModel.dataPoints) {
					objectEnumeration.push({
						objectName: objectName,
						displayName: dp.category,
						properties: {
							fill: {
								solid: {
									color: dp.colour || this.settings.colorSelector[Object.keys(this.settings.colorSelector)[index]]
								}
							}
						},
						selector: dp.selectionId.getSelector()
					});
				}
			}
			break;
		case 'datashape':
			if (this.viewModel) {
				for (let dp of this.viewModel.dataPoints) {
					objectEnumeration.push({
						objectName: objectName,
						displayName: dp.category,
						properties: {
							shape: dp.symbol || this.settings.datashape[Object.keys(this.settings.datashape)[index]]
						},
						selector: dp.selectionId.getSelector()
					});
				}
			}
			break;
		case 'datatrace':
			if (this.viewModel) {
				for (let dp of this.viewModel.dataPoints) {
					objectEnumeration.push({
						objectName: objectName,
						displayName: dp.category,
						properties: {
							charttype: dp.trace || this.settings.datatrace[Object.keys(this.settings.datatrace)[index]]
						},
						selector: dp.selectionId.getSelector()
					});
				}
			}
			break;
	  };
	  return objectEnumeration;
}

Capabilities.json:

{
    "dataRoles": [
        {
            "displayName": "X-Axis",
            "name": "x",
            "kind": "Grouping"
        },
        {
            "displayName": "Y-Axis 1",
            "name": "y1",
            "kind": "Measure"
        },
        {
            "displayName": "Y-Axis 2",
            "name": "y2",
            "kind": "Measure"
        },
        {
            "displayName": "Split by",
            "name": "splitby1",
            "kind": "Grouping"
        }
    ],
    "dataViewMappings": [
        {
            "conditions": [
                {
                    "x": {
                        "max": 1
                    },
                    "legend": {
                        "max": 1
                    },
                    "splitby1": {
                        "max": 1
                    }
                }
            ],
            "categorical": {
                "categories": {
                    "for": {
                        "in": "x"
                    }
                },
                "values": {
                    "group": {
                        "by": "splitby1",
                        "select": [
                            {
                                "for": {
                                    "in": "y1"
                                }
                            },
                            {
                                "for": {
                                    "in": "y2"
                                }
                            }
                        ]
                    }
                }
            }
        }
    ],
    "suppressDefaultTitle": true,
    "objects": {
        "xAxis": {
            "displayName": "X-Axis",
            "properties": {
                "title": {
                    "displayName": "Title",
                    "type": {
                        "text": true
                    }
                },
                "margin": {
                    "displayName": "Margin",
                    "type": {
                        "text": true
                    }
                }
            }
        },
        "y1Axis": {
            "displayName": "Y1-Axis",
            "properties": {
                "title": {
                    "displayName": "Title",
                    "type": {
                        "text": true
                    }
                },
                "margin": {
                    "displayName": "Margin",
                    "type": {
                        "text": true
                    }
                }
            }
        },
        "y2Axis": {
            "displayName": "Y2-Axis",
            "properties": {
                "title": {
                    "displayName": "Title",
                    "type": {
                        "text": true
                    }
                },
                "margin": {
                    "displayName": "Margin",
                    "type": {
                        "text": true
                    }
                }
            }
        },
        "datatrace": {
            "displayName": "Trace Type",
            "properties": {
                "charttype": {
                    "displayName": "Trace",
                    "type": {
                        "enumeration": [
                            {
                                "displayName": "Line Chart",
                                "value": "line"
                            },
                            {
                                "displayName": "Line Marker Chart",
                                "value": "lineMarker"
                            }
                        ]
                    }
                }
            }
        },
        "margin": {
            "displayName": "Margins",
            "properties": {
                "l": {
                    "displayName": "Left",
                    "type": {
                        "numeric": true
                    }
                },
                "r": {
                    "displayName": "Right",
                    "type": {
                        "numeric": true
                    }
                },
                "t": {
                    "displayName": "Top",
                    "type": {
                        "numeric": true
                    }
                },
                "b": {
                    "displayName": "Bottom",
                    "type": {
                        "numeric": true
                    }
                }
            }
        },
        "legend": {
            "displayName": "Legend",
            "properties": {
                "enabled": {
                    "displayName": "Enabled",
                    "type": {
                        "bool": true
                    }
                },
                "orientation": {
                    "displayName": "Orientation",
                    "type": {
                        "enumeration": [
                            {
                                "displayName": "Vertical",
                                "value": "v"
                            },
                            {
                                "displayName": "Horizontal",
                                "value": "h"
                            }
                        ]
                    }
                }
            }
        },
        "colorSelector": {
            "displayName": "Data Colors",
            "properties": {
                "fill": {
                    "displayName": "Color",
                    "type": {
                        "fill": {
                            "solid": {
                                "color": true
                            }
                        }
                    }
                }
            }
        },
        "datashape": {
            "displayName": "Shape Type",
            "properties": {
                "shape": {
                    "displayName": "Shape",
                    "type": {
                        "enumeration": [
                            {
                                "displayName": "Default",
                                "value": "none"
                            },
                            {
                                "displayName": "Circle",
                                "value": "circle"
                            }
                        ]
                    }
                }
            }
        }
    }
}