SVG 文件中的 JavaScript

2023-12-05

我有一个特殊请求,因为我想直接在 SVG 文件内生成 SVG ;) 到目前为止,我已经使用生成 SVG 标签编写了以下代码,但屏幕上没有显示任何内容。我可能做错了什么,但我不知道在哪里,因为它不会引发任何错误。

<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 200 200" preserveAspectRatio="xMidYMid meet" id="content">
  <defs>
    <style type="text/css"><![CDATA[
      @font-face {font-family: 'Futura-Heavy'; src: url(../public/css/FuturaStd/FuturaStd-Heavy.otf) format('opentype');}
      @font-face {font-family: 'Futura-Light'; src: url(../public/css/FuturaStd/FuturaStd-Light.otf) format('opentype');}
      @font-face {font-family: 'Bryant-Regular'; src: url(../public/css/Bryant/Bryant_Pro/BryantPro-Regular.otf) format('opentype');}

      .title{
        font-family: 'Futura-Heavy', 'Helvetica Neue', sans-serif, Helvetica;
        font-size: 14pt;
        stroke: none;
        fill: white;
        letter-spacing: 2px;
        text-anchor: middle;
      }

      .subtitle{
        font-family: 'Bryant-Regular', 'Helvetica Neue', sans-serif, Helvetica;
        font-size: 14pt;
        stroke: none;
        fill: grey;
        letter-spacing: 2px;
      }
    ]]></style>
  </defs>
  <script type="text/javascript"><![CDATA[
    var data = {
      "measures" : {
        "avg" : 0.68,
        "value" : 0.2
      },
      "config" : {
        "width" : 150,
        "height" : 180,
      },
      "titles" : {
        "right_eye" : "RIGHT EYE",
        "left_eye" : "LEFT EYE",
        "as_of" : "AS {0}% OF PEOPLE"
      }
    };
    var width = data.config.width || 1500;                          // Longueur totale
    var height = data.config.height || 1000;                        // Hauteur totale
    var widthVis = 330;                                                       // Longueur de la visualisation
    var heightVis = 300;                                                      // hauteur de la visualisation
    var heightBar = 25;                                                       // Hauteur du cadre
    var color = data.measures.color || ["#FFC400", "#18B4DC"];  // couple de couleurs
    var widthBar = widthVis * 0.8;                                      // Longueur du cadre
    data.config.host = data.config.host || '../public';         // Path to resources

    var format_string = function() {
      var mString = arguments[0];
      for (var i=1; i<arguments.length; i++) {
        var regEx = new RegExp("\\{" + (i - 1) + "\\}", "gm");
        mString = mString.replace(regEx, arguments[i]);
      }
      return mString;
    };

    var svg = document.getElementById('content');
    svg.setAttribute('height', height +'px');
    svg.setAttribute('width', width +'px');
    svg.setAttribute('viewBox', '0 0 ' + widthVis + ' ' + heightVis);

    var viz = document.createElement('g');
    viz.setAttribute('id', 'viz');
    viz.setAttribute('transform', 'translate(' + widthVis * 0.1 + ')');
    svg.appendChild(viz);

    // Create gradient
    var defs = document.getElementsByTagName('defs')[0];
    var categoryGradient = document.createElement('linearGradient');
    categoryGradient.setAttribute('id', 'categoryRelativeGradient');
    categoryGradient.setAttribute('x1', '0%');
    categoryGradient.setAttribute('x2', '100%');
    categoryGradient.setAttribute('y1', '0%');
    categoryGradient.setAttribute('y2', '0%');
    defs.appendChild(categoryGradient);
    var stop1 = document.createElement('stop');
    stop1.setAttribute('offset', '0%');
    stop1.setAttribute('stop-color', color[0]);
    stop1.setAttribute('stop-opacity', 1);
    categoryGradient.appendChild(stop1);
    var stop2 = document.createElement('stop');
    stop2.setAttribute('offset', (data.measures.avg*100) + '%');
    stop2.setAttribute('stop-color', color[0]);
    stop2.setAttribute('stop-opacity', 1);
    categoryGradient.appendChild(stop2);
    var stop3 = document.createElement('stop');
    stop3.setAttribute('offset', (data.measures.avg*100) + '%');
    stop3.setAttribute('stop-color', color[1]);
    stop3.setAttribute('stop-opacity', 1);
    categoryGradient.appendChild(stop3);
    var stop4 = document.createElement('stop');
    stop4.setAttribute('offset', '100%');
    stop4.setAttribute('stop-color', color[1]);
    stop4.setAttribute('stop-opacity', 1);
    categoryGradient.appendChild(stop4);

    // Create legend
    var forme = document.createElement('g');
    forme.setAttribute('id', 'path');
    forme.setAttribute('transform', 'translate(0, ' + heightVis * 0.6 + ')');
    viz.appendChild(forme);
    var circle1 = document.createElement('circle'); // Round corners
    circle1.setAttribute('cx', 0);
    circle1.setAttribute('cy', 0);
    circle1.setAttribute('r', heightBar / 2);
    circle1.setAttribute('fill', color[0]);
    forme.appendChild(circle1);
    var circle2 = document.createElement('circle'); // Round corners
    circle2.setAttribute('cx', widthBar);
    circle2.setAttribute('cy', 0);
    circle2.setAttribute('r', heightBar / 2);
    circle2.setAttribute('fill', color[1]);
    forme.appendChild(circle2);
    var rect1 = document.createElement('rect'); // Frame
    rect1.setAttribute('x', 0);
    rect1.setAttribute('y', - heightBar / 2);
    rect1.setAttribute('height', heightBar);
    rect1.setAttribute('width', widthBar);
    rect1.setAttribute('fill', 'url(#categoryRelativeGradient)');
    forme.appendChild(rect1);
    var circle3 = document.createElement('circle'); // Extreme points
    circle3.setAttribute('cx', 0);
    circle3.setAttribute('cy', 0);
    circle3.setAttribute('r', heightBar / 4);
    circle3.setAttribute('fill', 'black');
    forme.appendChild(circle3);
    var circle4 = document.createElement('circle'); // Extreme points
    circle4.setAttribute('cx', widthBar);
    circle4.setAttribute('cy', 0);
    circle4.setAttribute('r', heightBar / 4);
    circle4.setAttribute('fill', 'black');
    forme.appendChild(circle4);
    var dotted_line; // Dotted line
    for(var index = widthBar / 20; index < widthBar; index += widthBar / 20) {
      dotted_line = document.createElement('circle');
      dotted_line.setAttribute('cx', index);
      dotted_line.setAttribute('cy', 0);
      dotted_line.setAttribute('r', heightBar / 12);
      dotted_line.setAttribute('fill', 'black');
      forme.appendChild(dotted_line);
    }

    // Textual information
    var legend = document.createElement('g');
    legend.setAttribute('id', 'legend');
    legend.setAttribute('transform', 'translate('+(data.measures.value < 0.5 ? 0 : widthBar)+', ' + heightVis * 0.15 + ')');
    viz.appendChild(legend);
    var rect_width = data.measures.value < 0.5 ? data.titles.left_eye.length * 15 : data.titles.right_eye.length * 15;
    var legend_title = document.createElement('g');
    legend_title.setAttribute('transform', 'translate('+(data.measures.value < 0.5 ? 0 : -rect_width)+', 0)');
    legend.appendChild(legend_title);
    var rect2 = document.createElement('rect'); // Back rectangle
    rect2.setAttribute('x', 0);
    rect2.setAttribute('y', 0);
    rect2.setAttribute('height', 40);
    rect2.setAttribute('width', rect_width);
    rect2.setAttribute('fill', 'black');
    legend_title.appendChild(rect2);
    var text1 = document.createElement('text'); // Legend text
    text1.setAttribute('class', 'title');
    text1.setAttribute('x', rect_width - 30);
    text1.setAttribute('y', 27);
    text1.setAttribute('height', 40);
    text1.setAttribute('width', rect_width);
    text1.setAttribute('fill', 'white');
    text1.setAttribute('stroke', 'none');
    text1.setAttribute('text-anchor', (data.measures.value < 0.5 ? 'begin' : 'end'));
    text1.appendChild( document.createTextNode(data.measures.value < 0.5 ? data.titles.left_eye : data.titles.right_eye) );
    legend_title.appendChild(text1);

    // Legend sub-text
    var as_of_text = data.titles.as_of.toString();
    var as_of_text_part1 = as_of_text.substr(0, as_of_text.indexOf("%") + 1);
    var as_of_text_part2 = as_of_text.substr(as_of_text.indexOf("%") + 1, as_of_text.length);
    as_of_text_part1 = format_string(as_of_text_part1, (data.measures.value < 0.5 ? data.measures.avg*100 : ((1 - data.measures.avg)*100).toFixed()));
    var subtext1 = document.createElement('text');
    subtext1.setAttribute('class', 'subtitle');
    subtext1.setAttribute('x', (data.measures.value < 0.5 ? 5 : - 5));
    subtext1.setAttribute('y', 65);
    subtext1.setAttribute('fill', 'black');
    subtext1.setAttribute('text-anchor', (data.measures.value < 0.5 ? 'begin' : 'end'));
    subtext1.appendChild( document.createTextNode(as_of_text_part1) );
    legend.appendChild(subtext1);
    var subtext2 = document.createElement('text');
    subtext2.setAttribute('class', 'subtitle');
    subtext2.setAttribute('x', (data.measures.value < 0.5 ? 5 : - 5));
    subtext2.setAttribute('y', 85);
    subtext2.setAttribute('fill', 'black');
    subtext2.setAttribute('text-anchor', (data.measures.value < 0.5 ? 'begin' : 'end'));
    subtext2.appendChild( document.createTextNode(as_of_text_part2) );
    legend.appendChild(subtext2);

    // Vertical line
    var line = document.createElement('line');
    line.setAttribute('class', 'gaugeMark');
    line.setAttribute('x1', 0);
    line.setAttribute('x2', 0);
    line.setAttribute('y1', 0);
    line.setAttribute('y2', heightVis * 0.45);
    line.setAttribute('fill', 'black');
    line.setAttribute('stroke', 'black');
    legend.appendChild(line);
  ]]></script>
</svg>

我得到了带有此代码的白屏,而在 Chrome 开发者控制台中我得到了一些看起来是有效的 SVG 的东西。

你能帮我解决这个问题吗?我几乎可以肯定我距离解决方案并不遥远。

Thanks


SVG 元素必须在 SVG 命名空间中创建,因此您需要使用 createElementNS 而不是 createElement 来创建它们,例如

var viz = document.createElementNS('http://www.w3.org/2000/svg', 'g');
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

SVG 文件中的 JavaScript 的相关文章

  • 将 Javascript 对象的属性从 string 更改为 int

    我有一个对象数组 每个对象具有三个属性 年份 总计 人均 例子 0 Object per capita 125 8 total 1007 2 year 2009 这些属性是字符串 我想创建一个循环来遍历数组并将它们转换为 int 我尝试了以
  • Jquery 悬停卡

    我在用着http designwithpc com Plugins Hovercard http designwithpc com Plugins Hovercard 但我不知道如何在悬停卡上声明 var 每个工作描述都有自己的 ID 当悬
  • 在特定页面上执行 javascript 的正确“Rails”方式

    我试图在特定页面上运行 javascript 而我唯一的解决方案似乎是反模式 我有controller js内部生成的assets javascripts 我在用着gem jquery turbolinks 我的代码类似于以下内容 docu
  • 如何使用有角度的材料创建卡片网格?

    我正在尝试使用 ng repeat 创建每行三张卡片的网格 我有一个普通的 javascript 对象数组附加到范围 下面的代码将为每张卡创建一个新行 div div
  • Number.IsNaN() 比 isNaN() 更糟糕吗

    Soooooo isNaNJavaScript 显然被破坏了 比如 isNaN isNaN isNaN true isNaN false isNaN 0 返回 false 当它们看起来都是 不是数字 在 ECMAScript 6 中 草案包
  • 想要动态处理与分页相关的页码显示:ReactJS

    我有一些分页逻辑工作得很好 唯一的问题是我只能让它显示并固定数量的页面可供选择 现在我已经把它放到了 5 页 但我希望它能够根据总记录动态更改 假设我有 100 条记录 每页限制为 10 条 将有 10 页 现在我只能让它以这种方式显示 第
  • JavaScript 验证和 PHP 验证?

    我正在使用 jquery 验证插件来验证空表单 我还应该在 PHP 中检查一下以确保 100 正确吗 或者用 javascript 验证就可以了 谢谢 您应该始终在服务器上进行验证 如果用户以某种方式不使用 Javascript 提交表单
  • 导航栏下拉菜单(折叠)在 Bootstrap 5 中不起作用

    我在尝试使用以下命令创建响应式菜单或下拉按钮时遇到问题Bootstrap 5一切似乎都正常 导航图标和下拉图标出现 但它不起作用 当我单击nav图标或dropdown按钮 无dropdown menu apears 我想特别提到的是 我还包
  • React autoFocus 将光标设置为输入值的开头

    我有一个受控输入 最初显示一个值 我已将该输入设置为自动聚焦 但当我希望它出现在末尾时 光标出现在输入的开头 我知道这可能是因为自动对焦是在值之前添加的 但我不能 100 确定 在输入字段末尾完成光标初始化的最佳方法是什么 var Test
  • React Router v4 不渲染组件

    React Router v4 渲染组件存在问题 在应用程序初始加载时 它将呈现与 URL 相对应的正确组件 但是 任何后续的组件Link单击不会呈现所需的组件 图书馆 反应路由器 4 2 2 https reacttraining com
  • IE 中的 XPath 查询使用从零开始的索引,但 W3C 规范是从一开始的。我应该如何处理差异?

    问题 我正在转换目前仅适用于 Internet Explorer 的相对较大的 Javascript 代码 以便使其也适用于其他浏览器 由于代码广泛使用 XPath 我们做了一些兼容性功能以使事情变得更容易 function selectN
  • 如何使用 Javascript 设置查询字符串

    有没有办法使用 javascript 设置查询字符串的值 我的页面有一个过滤器列表 单击该列表时 它将更改右侧的页内结果窗格 我正在尝试更新 url 的查询字符串值 因此如果用户离开页面 然后单击 后退 按钮 他们将返回到最后一个过滤器选择
  • mongodb 聚合 - 累积字段的不同组值

    如果我有Player表格文件 name String score Int 我有Group文档 其中组代表玩家列表 groupName String players ObjectID 玩家可以属于多个组 我想做一个聚合Player文档 按以下
  • JavaScript eval("{}") 返回行为?

    根据ECMA 262 规范 http www ecma international org publications files ECMA ST Ecma 262 pdf 以下语句返回1 eval 1 eval 1 eval 1 var a
  • 表单发布请求并存储收到的数据

    我有一个非常简单的表单 在提交时发出发布请求
  • 如何使用 JavaScript 或 jQuery 克隆 HTML 元素的样式对象?

    我正在尝试克隆元素的样式对象 这应该允许我在更改后重置所述元素的样式 例如 el style left 50px curr style left 50px Modify the elements style The cloned style
  • Google Maps API (v3) 添加/更新标记

    编辑 它现在可以工作 但如果用户不允许或没有基于位置的服务 则不会加载 请参阅 jsfiddle 示例接受的答案评论 我已经浏览了一些教程和问题 但我无法安静地理解正在发生的事情 或者在这种情况下 没有发生 当用户单击链接时 我正在加载地图
  • 使用 Enzyme 测试 `React.createRef` api

    我想测试下面的类 它使用React createRef api 不过 快速搜索并没有发现任何这样做的例子 有人成功过吗 我该如何嘲笑裁判 理想情况下我想使用shallow class Main extends React Component
  • 在 iOS 7 Safari 中,如何区分通过边缘滑动与后退/前进按钮的 popstate 事件?

    在 iOS 7 Safari 中 现在有两种后退 前进导航方式 使用底部的传统后退 前进按钮箭头或从屏幕边缘滑动 我正在使用动画在 ajax 应用程序中的页面之间进行转换 但如果用户通过边缘滑动进行导航 我不想触发该转换 因为这本身就是一个
  • 仅当显式选择行时才关闭 ui-bootstrap typeahead

    我创建了这个jsBin http jsbin com livuqafe 2 edit来证明我遇到的问题 如果您转到此处 请尝试输入 五 并继续 你的自然反应是输入 五 然后按 Tab 如果你想要 五百 你可以向下箭头一次 但是 在这种情况下

随机推荐