Skip to content

Instantly share code, notes, and snippets.

@deldersveld
Last active November 26, 2021 18:05
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save deldersveld/dad2419dd87487de160b6481a9680e47 to your computer and use it in GitHub Desktop.
Save deldersveld/dad2419dd87487de160b6481a9680e47 to your computer and use it in GitHub Desktop.
Sparkline Column Categorical Axis =
// Static column color - use %23 instead of # for Firefox compatibility
VAR BarColor = "%2301B8AA"
VAR BarOutlineColor = "%23DDDDDD"
VAR BarOutlineWidth = 2
// Obtain number of columns - width generated based on column count (~20 column maximum for bar chart)
VAR BarCount = DISTINCTCOUNT('Table'[Customer Segment])
VAR BarWidth = INT(DIVIDE(100,BarCount))
// Obtain overall min and overall max measure values when evaluated for each column
VAR YMinValue = MINX(VALUES('Table'[Customer Segment]),CALCULATE([Measure Value]))
VAR YMaxValue = MAXX(VALUES('Table'[Customer Segment]),CALCULATE([Measure Value]))
VAR YRange = SWITCH(TRUE(),
YMinValue >= 0 && YMaxValue >= 0, YMaxValue,
YMinValue < 0 && YMaxValue < 0, ABS(YMinValue),
YMaxValue + ABS(YMinValue)
)
// Build table of X & Y coordinates and fit to 100 x 100 viewbox
VAR SparklineTable = ADDCOLUMNS(
SUMMARIZE('Table','Table'[Customer Segment]),
"X",INT(RANKX('Table','Table'[Customer Segment],'Table'[Customer Segment],ASC,DENSE)),
"Y",SWITCH(TRUE(),
YMinValue >= 0 && YMaxValue >= 0, 100-INT(100 * DIVIDE([Measure Value],YMaxValue)),
YMinValue < 0 && YMaxValue < 0, 0,
YMinValue < 0 && YMaxValue >= 0 && [Measure Value] >= 0, INT(100 * DIVIDE(YMaxValue - [Measure Value],YRange)),
YMinValue < 0 && YMaxValue >= 0 && [Measure Value] < 0, INT(100 * DIVIDE(YMaxValue,YRange))
),
"Height", INT(100 * DIVIDE([Measure Value],YRange))
)
// Concatenate X & Y coordinates to build the sparkline
VAR Cols = CONCATENATEX(SparklineTable,"<rect x='" & [X] * BarWidth - BarWidth &
"' y='" & [Y] &
"' width='" & BarWidth &
// display slim bar if height = 0
"' height='" & IF([Height] = 0, 1, ABS([Height])) &
"' style='fill:" & BarColor &
";stroke-width:" & BarOutlineWidth &
";stroke:" & BarOutlineColor &
"' />","")
// Add to SVG, and verify Data Category is set to Image URL for this measure
VAR SVGImageURL = IF(HASONEVALUE('Table'[Category]),
"data:image/svg+xml;utf8," &
"<svg xmlns='http://www.w3.org/2000/svg' x='0px' y='0px'
viewBox='0 0 100 100'>" &
Cols &
"</svg>",
BLANK())
RETURN SVGImageURL
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment