Skip to content

Compound.Bars

Creates a Bars compound SVG Visual showing a bar chart across an axis

01-Aug-2516-Aug-2531-Aug-2515-Sep-2501-Oct-25020000400006000080000100000

DaxLib.SVG.Compound.Bars( x, y, width, height, axisRef, measureRef, barColor, minMarkColor, maxMarkColor, showAxis, axisFontSize, paddingX, paddingY )
Parameter Type Required Default 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
axisRef ANYREF EXPR The column that the measure will be evaluated against
measureRef NUMERIC EXPR The measure to evaluate
barColor STRING "#01B8AA" The hex color of the bars. Defaults to "#01B8AA"
minMarkColor STRING BLANK() Optional: The hex color for the minimum value bar. Omitted if not specified
maxMarkColor STRING BLANK() Optional: The hex color for the maximum value bar. Omitted if not specified
showAxis BOOLEAN FALSE() Optional: Show axes when TRUE. Defaults to FALSE
axisFontSize INT64 10 Optional: Axis label font size. Defaults to 10
paddingX DECIMAL 0.05 Optional: The horizontal padding percentage (0.0-1.0, e.g., 0.1 = 10% padding). Defaults to 0.05
paddingY DECIMAL 0.02 Optional: The vertical padding percentage (0.0-1.0, e.g., 0.1 = 10% padding). Defaults to 0.02

STRING SVG Bar Chart

DaxLib.SVG.SVG(
    500,
    100,
    BLANK(),
    DaxLib.SVG.Compound.Bars(
        0,                  // x
        0,                  // y
        500,                // width
        100,                // height
        Dates[Date],        // axisRef
        [Total Cost],       // measureRef
        "#EC008C",          // barColor
        BLANK(),            // minMarkColor
        BLANK(),            // maxMarkColor
        FALSE,              // showAxis
        10,                 // axisFontSize
        0.05,               // paddingX
        0.04                // paddingY
    ),
    BLANK()
)
function 'DaxLib.SVG.Compound.Bars' =
        (
            x: INT64,
            y: INT64,
            width: INT64,
            height: INT64,
            axisRef: ANYREF EXPR,
            measureRef: NUMERIC EXPR,
            barColor: STRING = "#01B8AA",
            minMarkColor: STRING = BLANK(),
            maxMarkColor: STRING = BLANK(),
            showAxis: BOOLEAN = FALSE(),
            axisFontSize: INT64 = 10,
            paddingX: DOUBLE = 0.05,
            paddingY: DOUBLE = 0.02
        ) =>

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

            VAR _ShowAxis = IF( ISBLANK( showAxis ), FALSE(), showAxis )
            VAR _AxisFontSize = IF( ISBLANK( axisFontSize ), 10, axisFontSize )
            VAR _AxisIsDate = ISDATETIME( MAX( axisRef ) )
            VAR _BarColor = IF( NOT ISBLANK( barColor ), barColor, "#01B8AA" )

            VAR _Data = DaxLib.SVG.Data.AxisMeasure( axisRef, measureRef, "Auto" )
            VAR _DataNonBlank = FILTER( _Data, NOT ISBLANK( [@Value] ) )
            VAR _RawYMin = MINX( _DataNonBlank, [@Value] )
            VAR _RawYMax = MAXX( _DataNonBlank, [@Value] )
            VAR _Range = DaxLib.SVG.Data.Range( _RawYMin, _RawYMax, TRUE() )

            // Define axis scales
            VAR _XMin = MINX( _DataNonBlank, [@AxisIndex] )
            VAR _XMax = MAXX( _DataNonBlank, [@AxisIndex] )
            VAR _RawYMin2 = MINX( _Range, [@Baseline] )
            VAR _RawYMax2 = MAXX( _Range, [@Max] )

            // Compute nice range for value axis
            VAR _NiceY = DaxLib.SVG.Scale.NiceRange( _RawYMin2, _RawYMax2, 5, TRUE() )
            VAR _YMin = MINX( _NiceY, [@NiceMin] )
            VAR _YMax = MINX( _NiceY, [@NiceMax] )
            VAR _YTickCount = MINX( _NiceY, [@NiceTickCount] )

            // Axis layout
            VAR _MaxTickLabelWidth = DaxLib.SVG.Axes.MaxTickLabelWidth( _YMin, _YMax, _YTickCount, _AxisFontSize )
            VAR _Layout = DaxLib.SVG.Axes.Layout( _X, _Y, _Width, _Height, _MaxTickLabelWidth, _ShowAxis, _AxisFontSize )
            VAR _PlotX = MINX( _Layout, [@PlotX] )
            VAR _PlotY = MINX( _Layout, [@PlotY] )
            VAR _PlotWidth = MINX( _Layout, [@PlotWidth] )
            VAR _PlotHeight = MINX( _Layout, [@PlotHeight] )

            // Bar thickness derived from data grain (smallest gap between consecutive axis values)
            VAR _PointCount = COUNTROWS( _DataNonBlank )
            VAR _Gaps =
                ADDCOLUMNS(
                    _DataNonBlank,
                    "@Gap",
                        VAR _Cur = [@AxisIndex]
                        VAR _Next = MINX( FILTER( _DataNonBlank, [@AxisIndex] > _Cur ), [@AxisIndex] )
                        RETURN IF( NOT ISBLANK( _Next ), _Next - _Cur )
                )
            VAR _MinGap = COALESCE( MINX( FILTER( _Gaps, NOT ISBLANK( [@Gap] ) ), [@Gap] ), 1 )
            VAR _AxisRange = _XMax - _XMin
            VAR _PixelsPerUnit = IF( _AxisRange > 0, DIVIDE( _PlotWidth, _AxisRange ), _PlotWidth )
            VAR _MinBarPx = 2
            VAR _BarThickness = MAX( _MinBarPx, _MinGap * _PixelsPerUnit * 0.8 )

            VAR _Baseline = DaxLib.SVG.Axes.Baseline( "Vertical", _YMin, _YMax, _PlotX, _PlotY, _PlotWidth, _PlotHeight )
            VAR _BaseY = MINX( _Baseline, [@BaseY] )

            // Determine min and max values for bar highlighting
            VAR _VisibleMinValue = MINX( _DataNonBlank, [@Value] )
            VAR _VisibleMaxValue = MAXX( _DataNonBlank, [@Value] )
            VAR _CanHighlight = _PointCount > 2 && _VisibleMinValue <> _VisibleMaxValue

        //Bars
        VAR _Bars =
            CONCATENATEX(
                _DataNonBlank,
                IF(
                    NOT ISBLANK( [@Value] ),
                    VAR _Point = DaxLib.SVG.Axes.Point( "Vertical", [@AxisIndex], [@Value], _XMin, _XMax, _YMin, _YMax, _PlotX, _PlotY, _PlotWidth, _PlotHeight )
                    VAR _CX = MINX( _Point, [@X] )
                    VAR _ValueY = MINX( _Point, [@Y] )
                    VAR _BarCategoryStart = _CX - _BarThickness / 2
                    VAR _ThisBarColor =
                        IF( _CanHighlight && NOT ISBLANK( minMarkColor ) && [@Value] = _VisibleMinValue, minMarkColor,
                        IF( _CanHighlight && NOT ISBLANK( maxMarkColor ) && [@Value] = _VisibleMaxValue, maxMarkColor,
                        _BarColor ) )

                    RETURN
                        DaxLib.SVG.Element.Rect(
                            _BarCategoryStart,
                            MIN( _BaseY, _ValueY ),
                            _BarThickness,
                            ABS( _BaseY - _ValueY ),
                            0,
                            0,
                            DaxLib.SVG.Attr.Shapes(
                                _ThisBarColor,
                                BLANK(),
                                BLANK(),
                                BLANK(),
                                1
                            )
                        )
                ),
                " ",
                [@AxisIndex],
                ASC
            )

            VAR _AxisElements = DaxLib.SVG.Axes.Render( _PlotX, _PlotY, _PlotWidth, _PlotHeight, _XMin, _XMax, _YMin, _YMax, _ShowAxis, _AxisFontSize, _AxisIsDate, FALSE(), 5, _YTickCount )

        RETURN

                IF( NOT ISEMPTY( _DataNonBlank ) && _PlotWidth > 0 && _PlotHeight > 0, _Bars & _AxisElements )