使用SVG在Google Data Studio中创建自定义可视化效果

时间:2019-03-24 13:23:28

标签: google-data-studio data-studio-custom-visuals

我目前正在尝试将SVG径向记分卡库移植到Google Data Studio-但我一直在获得空白的可视化效果。

这是在html中工作的代码 https://codepen.io/grant-kemp/pen/NJoYLB

<div class="flex-wrapper">

  <div class="single-chart">
    <svg viewBox="0 0 36 36" class="circular-chart blue">
      <path class="circle-bg"
        d="M18 2.0845
          a 15.9155 15.9155 0 0 1 0 31.831
          a 15.9155 15.9155 0 0 1 0 -31.831"
      />
      <path class="circle"
        stroke-dasharray="90, 100"
        d="M18 2.0845
          a 15.9155 15.9155 0 0 1 0 31.831
          a 15.9155 15.9155 0 0 1 0 -31.831"
      />
      <text x="18" y="20.35" class="percentage">90%</text>
      <text x="18" y="12" class="percentageLabel">Mobile</text>
    </svg>
  </div>
</div>

CSS:

.flex-wrapper {
  display: flex;
  flex-flow: row nowrap;
}

.single-chart {
  width: 33%;
  justify-content: space-around ;
}

.circular-chart {
  display: block;
  margin: 10px auto;
  max-width: 80%;
  max-height: 250px;
}

.circle-bg {
  fill: none;
  stroke: #eee;
  stroke-width: 3.8;
}

.circle {
  fill: none;
  stroke-width: 2.8;
  stroke-linecap: round;
  animation: progress 1s ease-out forwards;
}

@keyframes progress {
  0% {
    stroke-dasharray: 0 100;
  }
}

.circular-chart.orange .circle {
  stroke: #ff9f00;
}

.circular-chart.green .circle {
  stroke: #4CC790;
}

.circular-chart.blue .circle {
  stroke: #3c9ee5;
}

.percentage {
  fill: #667 ;
  font-family: sans-serif;
  font-size: 0.5em;
  text-anchor: middle;
}
.percentageLabel {
  fill: #667 ;
  font-family: sans-serif;
  font-size: 0.2em;
  text-anchor: middle;
}

当我尝试将其移植到JS以在Data Studio中工作时,我使用了它,似乎给了我一个空白的屏幕。(而且没有错误)

Data Studio代码

function drawViz(data) {
  var height = dscc.getHeight();
  var width = dscc.getWidth();
  console.log("drawing")
  console.log("data is "+data);

  var newDiv = document.createElement('div')
newDiv.class = "flex-wrapper"
var chartDiv = document.createElement('single-chart');
chartDiv.innerText = "test"

var chartSVG = document .createElementNS("http://www.w3.org/2000/svg", "svg");
chartSVG.setAttribute("viewBox","0 0 36 36")
chartSVG.setAttributeNS(null, "class",  "circular-chart orange");
var newPath1 = document.createElementNS("http://www.w3.org/2000/svg","path");
newPath1.setAttributeNS(null, "class", "circle-bg");
newPath1.setAttributeNS(null, "d", "M18 2.0845 a 15.9155 15.9155 0 0 1 0 31.831 a 15.9155 15.9155 0 0 1 0 -31.831");
chartSVG.appendChild(newPath1);
var newPath2 = document.createElementNS("http://www.w3.org/2000/svg","path");
newPath2.setAttributeNS(null, "class", "circle");
newPath2.setAttributeNS(null, "stroke-dasharray", "30, 100");
newPath2.setAttributeNS(null, "d", "M18 2.0845 a 15.9155 15.9155 0 0 1 0 31.831 a 15.9155 15.9155 0 0 1 0 -31.831");
chartSVG.appendChild(newPath2);

var text = document.createElement("text")
text.setAttribute("x","18")
text.setAttribute("y","20.35")
text.setAttribute("class","percentage")
text.innerText = "30%"
chartSVG.appendChild(text)

chartDiv.appendChild.chartSVG
  newDiv.appendChild(chartDiv)
document.body.appendChild(newDiv)
}

// subscribe to data and style changes.
dscc.subscribeToData(drawViz, {transform: dscc.objectTransform});

2 个答案:

答案 0 :(得分:1)

我最终使用了一个不依赖SVG的替代库。

我确定支持此方法,但是我没有时间进行深入研究。

一旦我在Github上将其链接发布到解决方案

答案 1 :(得分:0)

I'd love to see this on Github when it's finished