简体   繁体   English

使用Power BI Custom Visual的其他情况更改矩形颜色

[英]Change rectangle color using if else for a Power BI Custom Visual

I am new to Power BI Custom Visual and currently working on creating a simple Custom Visual. 我是Power BI Custom Visual的新手,目前正在创建一个简单的Custom Visual。 I actually want a rectangle whose color changes on the basis of a given measure. 我实际上想要一个矩形,其颜色会根据给定的度量而变化。 For Eg: 例如:

  • If value<100 and value >0 then blue 如果值<100且值> 0,则为蓝色
  • Else if Value>100 then green 否则,如果值> 100,则为绿色
  • else red 否则红色

i have used the following code but it is not working as the color of the rectangle is not changing with the value of the measure. 我使用了以下代码,但由于矩形的颜色未随度量值的变化而改变,因此无法正常工作。 Please let me know how this can be achieved 请让我知道如何实现

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"
            }
        })
    }
}

} }

I know this is a very old question but for anyone coming here from Google, I thought I would answer it anyway. 我知道这是一个非常老的问题,但是对于任何来自Google的人来说,我都应该回答。

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"
        }
    })

The first five lines are defining the height and width of the rectangle container, but that is also being defined further down. 前五行定义矩形容器的高度和宽度,但也定义在更下方。

Also, since the code is in the update function, the colour could simply be defined via a variable and then that variable used within the rect.attr function. 同样,由于代码在更新函数中,因此可以简单地通过变量定义颜色,然后在rect.attr函数中使用该变量。 Such as: 如:

let colour = "#EC7769";

if(d.value > 100)
    colour = "#B2EC69";
else if( (d.value < 100) && (d.value > 0) )
    colour = "#69D2EC";

let rect = this.RecContainer.append('rect').classed('rectmain', true);  
rect.attr({
    x: 0,
    y: 0,
    height: height,
    width: width,
    fill: colour
});

If there is a better way, then I am all ears and eager to learn. 如果有更好的方法,那么我将竭尽全力并渴望学习。

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

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