Skip to content

Compound.Bar

Creates a Bar compound SVG Visual

DaxLib.SVG.Compound.Jitter( x, y, width, height, paddingX, paddingY, axisRef, measureRef, pointColor, jitterAmount )
Parameter Type Required Description
x INT64 The x position of the compound
y INT64 The y position of the compound
width INT64 The width of the compound
height INT64 The height of the compound
paddingX DOUBLE The horizontal padding percentage (0.0-1.0, e.g., 0.1 = 10% padding)
paddingY DOUBLE The vertical padding percentage (0.0-1.0, e.g., 0.1 = 10% padding)
axisRef ANYREF EXPR The column that the measure will be evaluated against
measureRef NUMERIC EXPR The measure to evaluate
barColor STRING The Hex color of the bar (e.g., "#01B8AA")

STRING A Bar compound SVG Visual

DaxLib.SVG.SVG(
    500,
    100,
    BLANK(),
    DaxLib.SVG.Compound.Bar(
        0,                  // x
        0,                  // y
        500,                // width
        100,                // height
        0.05,               // paddingX
        0.02,               // paddingY
        Dates[Date],        // xAxis
        [Total Cost],       // measureRef
        DaxLib.SVG.Colour.Theme(
            "Power BI",
            25
        )                   // barColor
    ),
    BLANK()
)
function 'DaxLib.SVG.Compound.Bar' = 
    (
        x: INT64,
        y: INT64,
        width: INT64,
        height: INT64,
        paddingX: DOUBLE,
        paddingY: DOUBLE,
        axisRef: ANYREF EXPR,
        measureRef: NUMERIC EXPR,
        barColor: STRING
    ) =>

        // Apply padding to dimensions
        VAR _X =            x + ( width * COALESCE( paddingX, 0 ) / 2 )
        VAR _Y =            y + ( height * COALESCE( paddingY, 0 ) / 2 )
        VAR _Width =        width * ( 1 - COALESCE( paddingX, 0 ) )
        VAR _Height =       height * ( 1 - COALESCE( paddingY, 0 ) )

        // Check if Axis is numeric
        VAR axisSample =    MAX( axisRef )
        VAR axisIsNumeric = ISNUMERIC( axisSample ) || ISDATETIME( axisSample )

        // For totals
        // Materialize axis + value once (avoid repeated measure evaluation)
        VAR _Values =
            ADDCOLUMNS(
                VALUES( axisRef ),
                "@Value", measureRef
            )

        VAR _DataNonBlank =
            FILTER( _Values, NOT ISBLANK( [@Value] ) )

        VAR _Data =
            ADDCOLUMNS(
                _DataNonBlank,
                "@AxisIndex",
                    IF(
                        axisIsNumeric,
                        axisRef,
                        RANK( DENSE, CALCULATETABLE( VALUES( axisRef ), ALLSELECTED() ) )
                    )
            )

        // Define axis scales       
        VAR _XMin =     MINX( _Data, [@AxisIndex] )
        VAR _XMax =     MAXX( _Data, [@AxisIndex] )
        VAR _RawYMin =  MINX( _Data, [@Value] )
        VAR _YMin =     IF( _RawYMin > 0, 0, _RawYMin )
        VAR _YMax =     MAXX( _Data, [@Value] )
        VAR _XWidth =   _X + _Width
        VAR _YHeight =  _Y + _Height

        VAR _CountBars = COUNTROWS( _Data )
        VAR _BarWidth = DIVIDE( _Width, _CountBars )

        // Bars
        VAR _Bars = 
            CONCATENATEX(
                _Data,
                IF( 
                    NOT ISBLANK( [@Value] ), 
                    VAR _normY = DaxLib.SVG.Scale.Normalize( [@Value], _YMin, _YMax, _YHeight, _Y )
                    RETURN
                    DaxLib.SVG.Element.Rect(
                        DaxLib.SVG.Scale.Normalize( [@AxisIndex], _XMin, _XMax, _X, _XWidth ),
                        _normY,
                        _BarWidth,
                        _YHeight - _normY,
                        0,
                        0,
                        DaxLib.SVG.Attr.Shapes(
                            IF( NOT ISBLANK( barColor ), barColor, "#01B8AA" ),     // fill
                            BLANK(),    // fillOpacity
                            BLANK(),    // fillRule
                            BLANK(),    // stroke
                            1,          // strokeWidth
                            BLANK(),    // strokeOpacity
                            BLANK()     // opacity
                        ),
                        BLANK()
                    )
                ),
                " ",
                [@AxisIndex],
                ASC
            )           

        RETURN

            IF( NOT ISEMPTY( _Data ), _Bars )

Comments