【问题标题】:Draw SVG from json data using javascript使用 javascript 从 json 数据中绘制 SVG
【发布时间】:2021-10-17 06:33:09
【问题描述】:

如何根据返回的json数据在SVG中绘制上下文图?

示例 json 数据:

 "data":{
         "PageType":"Home",
         "CatId":0,
         "Category":"",
         "ObjectId":0,
         "Object":"",
         "Total":1,
         "Prev":[
            {
               "type":"Prev1",
               "Count":1,
               "Time":0
            },
             {
               "type":"Prev2",
               "Count":2,
               "Time":0
            },
             {
               "type":"Prev3",
               "Count":3,
               "Time":0
            },
         ],
         "Next":[
            {
               "type":"Next1",
               "Count":1,
               "Time":1000
            }
              {
               "type":"Next2",
               "Count":2,
               "Time":1000
            },
              {
               "type":"Next3",
               "Count":3,
               "Time":1000
            }
         ]
      }
      

【问题讨论】:

    标签: javascript html jquery json svg


    【解决方案1】:

    好的,这不是问题,这只是实施练习。我没有给你结果,只是一些指示。这些是 SVG 的基本元素。 <g> 是对元素进行分组,可以使用 transform 属性来移动。

    <svg viewBox="0 0 400 300" xmlns="http://www.w3.org/2000/svg">
      <style>
        line, rect {stroke-width: 1; stroke: navy; fill: none;}
        text {fill: navy;}
      </style>
      <g transform="translate(200 150)">
        <rect x="-50" y="-20" width="100" height="40" rx="10" />
        <text dominant-baseline="middle" text-anchor="middle" width="100">Text 1</text>
        <line x1="50" y1="0" x2="100" y2="60" /> 
      </g>
    </svg>

    那么它需要是动态的。您知道每个元素的大小和间距。使用forEach() 循环,您可以遍历列表中的所有元素并将它们“展开”。我只是用...innerHTML += ...“附加”新的SVG元素

    var prev = {
      "Prev": [{
          "type": "Prev1",
          "Count": 1,
          "Time": 0
        },
        {
          "type": "Prev2",
          "Count": 2,
          "Time": 0
        },
        {
          "type": "Prev3",
          "Count": 3,
          "Time": 0
        },
      ]
    };
    
    var drawing = document.getElementById('drawing');
    
    prev.Prev.forEach((p, i, arr) => {
      let x = 55;
      let y = 250/arr.length*i+50;
      drawing.innerHTML += `<g transform="translate(${x} ${y})">
            <rect x="-50" y="-20" width="100" height="40" rx="10" />
            <text dominant-baseline="middle" text-anchor="middle" width="100">${p.type}</text>
            <line x1="50" y1="0" x2="100" y2="${250/arr.length-y+50}" /> 
          </g>`;
    });
    <svg id="drawing" viewBox="0 0 400 300" xmlns="http://www.w3.org/2000/svg">
      <style>
        line, rect {stroke-width: 1; stroke: navy; fill: none;}
        text {fill: navy;}
      </style>
    </svg>

    这对于自己实现整个事情应该没问题:-)

    【讨论】:

    • @chrwal 介意解释一下吗?让 y = 250/arr.length*i+50;顺便说一句,这就是我正在寻找的。谢谢!
    • @mojoe SVG 图像的高度是 300。250 只是小于 300 的任意数字。arr 是迭代的数组(又名 prev.Prev)。它有 3 个项目(arr.length = 3)。 50 是起点,所以第一项的 y 位置将是:250/3*0+50=50,第二项:250/3*1+5=133.33 等等。移动线的 y2 有点像同样,首先:250/3-50+50=83.33。您可以调整 no 250 和 50 以符合您的要求。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-09-03
    • 2023-03-09
    • 1970-01-01
    • 2019-10-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多