Power BI中插入动态SVG图像


在上一篇文章[[Power BI新式卡片图性能与美观兼得]]中有介绍新卡片图中可以插入动态的SVG图片,从而丰富卡片图的内容,但其实SVG不止可以插入到新卡片图中。比如最常用的SVG地图,还可以加入到表格或者矩阵中。比如下面这样
image.png

SVG作为一种轻量化的图形,很多时候效果比第三方图表插件要好的多。

华夫饼图

华夫饼图可以用来显示完成的进度。有一个个小单元格网格组成,其中彩色单元格代表数据。所以,如果要用SVG实现,就得先构造小单元风格。
SVG:可缩放矢量图形 | MDN (mozilla.org)

<svg viewBox="0 0 10 10" xmlns="http://www.w3.org/2000/svg">
  <rect width="10" height="10">
    <animate
      attributeName="rx"
      values="0;5;0"
      dur="10s"
      repeatCount="indefinite" />
  </rect>
</svg>

image.png
之后就可以构造SVG了,下面的代码很长,主要是在构造网格,使用时,只需要修改要显示的度量和颜色部分就可以,其他部分可以不做修改

<SVG> Waffle Country = 
VAR __svgPadding = 2
VAR __gridX = 10
VAR __gridY = 10
VAR __gridInterval = 1
VAR __gridTotal = __gridX * __gridY

VAR __gridTable = ADDCOLUMNS(
&nbsp; &nbsp; GENERATESERIES(
&nbsp; &nbsp; &nbsp; &nbsp; 0,
&nbsp; &nbsp; &nbsp; &nbsp; __gridTotal - 1,
&nbsp; &nbsp; &nbsp; &nbsp; __gridInterval
&nbsp; &nbsp; ),
&nbsp; &nbsp; "@Inverse", __gridTotal - [Value] - 1
)

VAR __gridCoords = ADDCOLUMNS(
&nbsp; &nbsp; __gridTable,
&nbsp; &nbsp; "@X", MOD ( [Value], __gridX ),
&nbsp; &nbsp; "@Y", FLOOR( [@Inverse] / __gridX, 1)
)

//需要修改的部分  
VAR __fillEmpty = "#eaeaea"
VAR __fillIncluded = "#666"
VAR __percentage = [% Product Sales of Total] * 100
VAR __percentageSort = FORMAT(__percentage, "000.00")

VAR __svgBase =
&nbsp; &nbsp; "data:image/svg+xml;utf8,
&nbsp; &nbsp; <svg
&nbsp; &nbsp; &nbsp; &nbsp; sort='" & __percentageSort & "'
&nbsp; &nbsp; &nbsp; &nbsp; xmlns='http://www.w3.org/2000/svg'
&nbsp; &nbsp; &nbsp; &nbsp; width='100%'
&nbsp; &nbsp; &nbsp; &nbsp; height='100%'
&nbsp; &nbsp; &nbsp; &nbsp; viewBox='0 0 " & __gridX & " " & __gridY & "'
&nbsp; &nbsp; &nbsp; &nbsp; preserveAspectRatio='none'
&nbsp; &nbsp; >
&nbsp; &nbsp; &nbsp; &nbsp; {waffle}
&nbsp; &nbsp; </svg>"
VAR &nbsp;__waffles = CONCATENATEX(
&nbsp; &nbsp; __gridCoords,
&nbsp; &nbsp; "<rect
&nbsp; &nbsp; &nbsp; &nbsp; value='" & [Value] & "'
&nbsp; &nbsp; &nbsp; &nbsp; height='1'
&nbsp; &nbsp; &nbsp; &nbsp; width='1'
&nbsp; &nbsp; &nbsp; &nbsp; x='" & [@X] & "'
&nbsp; &nbsp; &nbsp; &nbsp; y='" & [@Y] & "'
&nbsp; &nbsp; &nbsp; &nbsp; stroke='#fafafa'
&nbsp; &nbsp; &nbsp; &nbsp; stroke-width='2%'
&nbsp; &nbsp; &nbsp; &nbsp; fill='" &
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; IF(
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ROUND(__percentage, 0 ) > [Value],
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; __fillIncluded,
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; __fillEmpty
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ) &
&nbsp; &nbsp; "'>
&nbsp; &nbsp; &nbsp; &nbsp; <animate
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; attributeName='width'
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; from='0'
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; to='1'
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dur='0.5s'
&nbsp; &nbsp; &nbsp; &nbsp; />
&nbsp; &nbsp; &nbsp; &nbsp; <animate
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; attributeName='height'
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; from='0'
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; to='1'
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dur='1.5s'
&nbsp; &nbsp; &nbsp; &nbsp; />
&nbsp; &nbsp; </rect>", "", [Value], ASC
)
RETURN
&nbsp; &nbsp; IF(
&nbsp; &nbsp; &nbsp; &nbsp; HASONEVALUE( Product[Product] ),
&nbsp; &nbsp; &nbsp; &nbsp; SUBSTITUTE(
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; __svgBase,
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "{waffle}",
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; __waffles
&nbsp; &nbsp; &nbsp; &nbsp; ), 
&nbsp; &nbsp; &nbsp; &nbsp; BLANK()
&nbsp; &nbsp; )
image.png

外部工具制作SVG

上面的代码已经可以做为模板来使用了,我们可以保存在自己的代码库中,供使用的时候调取,有些人可能会觉得还不够方便,当然,也是可以通过外部工具来保管代码库和快速创建度量。这里使用的是企业DNA开发的Quick Measure Pro,该工具需要购买网站的高级会员才能下载,版权原因就不分享了,不过该网站经常有比赛,参加比赛获奖了就能得到高级会员资格。下面以新卡片图中添加迷你图为例,全程只需要动动鼠标,不需要写一行代码。
image.png

Year SVG Sparklines Measure =
// Static line color - use %23 instead of # for Firefox compatibility
&nbsp; &nbsp; VAR LineColor = "%2301B8AA"
&nbsp; &nbsp; // "Date" field used in this example along the X axis
&nbsp; &nbsp; VAR XMinDate = MIN('Date'[Date])
&nbsp; &nbsp; VAR XMaxDate = MAX('Date'[Date])

&nbsp; &nbsp; // Obtain overall min and overall max measure values when evaluated for each date
&nbsp; &nbsp; VAR YMinValue = MINX(VALUES('Date'[Date]),CALCULATE(('Financials'[$ Sales])))
&nbsp; &nbsp; VAR YMaxValue = MAXX(VALUES('Date'[Date]),CALCULATE(('Financials'[$ Sales])))

&nbsp; &nbsp; // Build table of X & Y coordinates and fit to 100 x 100 viewbox
&nbsp; &nbsp; VAR SparklineTable = ADDCOLUMNS(
&nbsp; &nbsp; &nbsp; &nbsp; SUMMARIZE('Date','Date'[Date]),
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "X",INT(100 * DIVIDE('Date'[Date] - XMinDate, XMaxDate - XMinDate)),
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "Y",INT(100 * DIVIDE(('Financials'[$ Sales]) - YMinValue,YMaxValue - YMinValue)))
&nbsp; &nbsp; // Concatenate X & Y coordinates to build the sparkline
&nbsp; &nbsp; VAR Lines = CONCATENATEX(SparklineTable,[X] & "," & 100-[Y]," ", [Date])
&nbsp; &nbsp; // Add to SVG, and verify Data Category is set to Image URL for this measure
&nbsp; &nbsp; VAR SVGImageURL = IF(HASONEVALUE('Date'[Year]),
&nbsp; &nbsp; &nbsp; &nbsp; "data:image/svg+xml;utf8," &
&nbsp; &nbsp; &nbsp; &nbsp; "<svg xmlns='http://www.w3.org/2000/svg' x='0px' y='0px' viewBox='0 0 100 100'>" &
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"<polyline fill='none' stroke='" & LineColor &
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"' stroke-width='3' points='" & Lines &
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"'/></svg>",
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;BLANK())
RETURN
&nbsp; &nbsp; SVGImageURL

效果如下
image.png

总结

SVG的效果很多时候只是为了锦上添花,建模人员还是要花更多的时候在模型优化上,确实必须使用SVG来实现效果时,可求助web前端人员的帮助,毕竟SVG效果中,只是使用DAX变量来代替图表数据部分,其他部分是固定的,专业的事情还是找专业的人帮忙速度更快些。


相关链接

Introduction – SVG: Scalable Vector Graphics | MDN (mozilla.org)
Chart Templates – EXPLORATIONS IN DATA STORYTELLING WITH POWER BI (kerrykolosko.com)
Adding A Power BI SVG Visual On Your Report (enterprisedna.co)
Power BI – 5 Stars SVG – Hat Full of Data — Power BI – 5 星 SVG – 充满数据的帽子
Matrix Revolutions: Putting the “Whoa” into PBI Matrix with dynamic SVG – Daniel Marsh-Patrick – YouTube — 矩阵革命:使用动态 SVG 将“哇”放入 PBI 矩阵 – Daniel Marsh-Patrick – YouTube

类似文章

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注