SVG Bar Charts: Alright, here’s my story. On one of my recent projects, I was tasked with creating a table that displayed a ratio. At first, I thought data bars would be the answer. But there was an issue—the text always sat on the right, regardless of whether the value was positive or negative. To make matters worse, sometimes the text would overlap with the bar chart. Not exactly user-friendly, right? Plus, also not what the client needed at that point. I tried to google and adjust the settings, but nothing really helped…Until I stumble uppon SVG images!
Then, inspiration struck when I stumbled upon an SVG video on YouTube from How to PowerBI. Check it out to get a deeper understanding of SVGs in PowerBI. With some trial and error, and some nudges from chat GPT, I finally landed on a solution I was proud of. Admittedly, adjusting the parameters took some time, but I slowly got the hang of it, understanding the nuances of image sizes and other variables. The best part? I’ve documented everything here, making it easy for others to use and adapt.
Scalable Vector Graphics, or SVG, isn’t just another image format. It’s a game-changer for designing visuals in PowerBI. Let’s break down why:
Diving into SVGs might seem technical at first, but the payoff in terms of visual clarity and customization in PowerBI is well worth the effort. When data representation meets visual elegance, that’s where SVG shines!
A great website to find SVG images and the code is svgviewer.com
SVG Bar Chart =
-- Canvas Definitions
VAR SVGWidth = 250 -- Width of the SVG canvas
VAR SVGHeight = 40 -- Height of the SVG canvas
-- Data and Formatting
VAR _Measure = [Total Cost Ratio Ref] -- Retrieve the ratio value from another measure/column
VAR Formatted_Measure = FORMAT(_Measure, "0.0%") -- Format the ratio value for display
-- Color Definitions
VAR PositiveBarColor = "#92D293" -- Color for positive values
VAR NegativeBarColor = "#FAA0A0" -- Color for negative values
VAR BarColor = IF(_Measure >= 0, PositiveBarColor, NegativeBarColor)
-- Bar Dimensions and Positioning
VAR CenterPosition = 100 -- Center of the SVG canvas for bar placement
VAR BarWidth = ABS(_Measure) * (SVGWidth /2) -- Width based on the ratio and canvas size
VAR BarHeight = 40 -- Height of the bar
VAR BarStartX = IF(_Measure >= 0, CenterPosition, CenterPosition - BarWidth) -- X position of the bar
-- SVG Text Positioning and Style
VAR TextPadding = 30 -- Space between bar and text
VAR TextWidth = 50 -- Estimated width of text
VAR TextFontFamily = "Segoe UI" -- Font family for text
VAR TextFontSize = "24" -- Font size for text
VAR TextFill = "black" -- Font color
VAR NumberPositionX = -- Determine text X-position based on bar direction (positive or negative)
IF(
_Measure >= 0,
CenterPosition + BarWidth + TextPadding,
CenterPosition - BarWidth - TextPadding - TextWidth
)
-- SVG Creation
VAR Img =
"data:image/svg+xml;utf8,<svg width='" & SVGWidth & "' height='" & SVGHeight & "' xmlns='http://www.w3.org/2000/svg'>
<rect x='" & BarStartX & "' y='10' width='" & BarWidth & "' height='" & BarHeight & "' fill='" & BarColor & "'></rect>
<text x='" & NumberPositionX & "' y='30' font-family='" & TextFontFamily & "' font-size='" & TextFontSize & "' fill='" & TextFill & "'>" & Formatted_Measure & "</text>
</svg>"
RETURN Img
To be fair, it took me a while to have the SVG adjusted to my liking but it was worth it. My two initial problems are gone. Now the ratio text is on the left or right side, depending on if it’s a positive or negative value. Also, the text is no longer overlapping the bar, which makes it more readable.
Navigating the waters of data visualization might seem daunting, but once you dive in, the possibilities are truly endless. Using SVG Bar Charts in PowerBI doesn’t just make your data look good—it also brings clarity, precision, and innovation to your fingertips. While the journey of crafting the perfect SVG bar chart had its challenges, the result was undeniably worth the effort. It’s a testament to how technology, inspiration, and a touch of creativity can come together to transform raw data into insightful visuals.
Call to Action: Now, it’s your turn! I’d love to see the unique ways you apply this technique. If you’ve found this guide helpful or have any questions, don’t hesitate to drop a comment below. Better yet, share your creations with our community! Let’s learn, innovate, and grow together. And if you’re hungry for more tips and tutorials on PowerBI, follow me on LinkedIn where I post about PowerBI and Data Analytics. Happy visualizing! 📊✨