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

问题描述 投票:0回答:2

我目前正在尝试将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});
google-data-studio data-studio-custom-visuals
2个回答
1
投票

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

我确信这种方法得到了支持,但我没有时间深入研究。

在Github上将链接发布到解决方案后


0
投票

我很乐意在Github完成时看到这个

© www.soinside.com 2019 - 2024. All rights reserved.