【发布时间】:2018-05-17 19:58:23
【问题描述】:
我是 Power BI 自定义视觉对象的新手,目前正在创建一个简单的自定义视觉对象。我实际上想要一个矩形,它的颜色会根据给定的度量而变化。 例如:
- 如果值0,则为蓝色
- Else if Value>100 then green
- 其他红色
我使用了以下代码,但它不起作用,因为矩形的颜色不会随度量值而变化。请告诉我如何实现这一目标
module powerbi.extensibility.visual {
interface RaggedViewModel{
dataPoints: RaggedDataPoint[];
};
interface RaggedDataPoint{
value:number;
}
export class Visual implements IVisual {
private svg:D3.Selection;
private RecContainer:D3.Selection;
constructor(options: VisualConstructorOptions) {
this.svg=d3.select(options.element)
.append('svg')
.classed("circs",true);
this.RecContainer = this.svg.append("g")
.classed("RecContainer",true);
}
public convertData(options: VisualUpdateOptions): RaggedViewModel
{
let dataViews =options.dataViews[0];
let viewModel:RaggedViewModel;
let categorical = dataViews.categorical;
let category = categorical.categories[0];
let dataValues = categorical.values[0];
let objects = dataViews.metadata.objects;
let cirDataPoint:RaggedDataPoint[]=[];
for (let i = 0, len = Math.max(category.values.length, dataValues.values.length); i < len; i++) {
cirDataPoint.push({
value: dataValues.values[i],
});
return {
dataPoints: cirDataPoint,
};
}
}
public update(options: VisualUpdateOptions)
{
this.RecContainer.selectAll('rect').remove();
let height =options.viewport.height;
let width = options.viewport.width;
this.svg.attr(
{
height:height,
width:width
});
this.RecContainer.attr(
{
height:height,
width:width
});
let rect=this.RecContainer.append('rect').classed('rectmain', true);
rect.attr({
x:0,
y:0,
height:height,
width:width,
fill:function(d){
if(d.value>100)
return "#B2EC69"
else if(d.value<100 && d.value>0 )
return "#69D2EC"
else
return "#EC7769"
}
})
}
}
}
【问题讨论】:
-
您对您的代码是否有具体问题,或者这是代码审查请求?
-
此代码不起作用,因为矩形的颜色没有改变。我想知道如何做到这一点。
标签: javascript node.js typescript d3.js powerbi