About adding images to SVG, use JS under rails and use parsefloat to adjust the position of Force.on

Source: Internet
Author: User

Note the path in code/represents ... File in Windows is the \

1, <image xlink:href=<%= asset_path ' vnet/virtual_switch.png '%> x= "0" y= "0" height= "30px" width= "30px"/>

(1) in the HTML CAs directly in the embedded way to get pictures in rails. For example, here's the picture in \app\assets\images\vnet\virtual_switch.png, directly with rails Asset_path can get the URL

(2) Of course, the use of direct path can also be <image xlink:href= "\assets\vnet\virtual_switch.png" x= "" "y=" 0 "height=" 30px "width=" 30px "/>

2, var image_vswitch = "/assets/vnet/virtual_switch.png";

In JS, you can not use the embedded method, only the direct reference, note the path information here

3, with JS in SVG to add the picture, here is the first bound G element, and then add, of course, if there is no other, only with the image element is also possible

4, D3js in the last Force.on time slightly adjust the position of the picture

parsefloat (d.x)-;                                         var dy = parsefloat (d.y)-;                                         Return "translate (" + DX + "," + dy + ")";   } ";

  

Take a good look at the total code.

<%= render partial: ' Vnet_nav_bar '%><meta charset= ' utf-8 ' ><style>/*.node {stroke: #fff; stroke-width:1.5px;}.  Link {stroke: #999; Stroke-opacity:. 6;} */</style><body> <svg width= "height=" > <image xlink:href=<%= asset_path ' Vnet/virtua L_switch.png '%> x= "0" y= "0" height= "30px" width= "30px"/><image xlink:href=<%= asset_path ' Vnet/virtual_ Machine.png '%> x= "y=" 0 "height=" 30px "width=" 30px "/><image xlink:href=<%= asset_path ' Vnet/virtual_ Machine_container.png '%> x= "" y= "0" height= "30px" width= "30px"/> <text x= "" "y=" "" Fill= "Black" >virtu Al switch</text> <text x= "233" y= "Up" fill= "Black" >virtual machine</text> <text x= "433" y= "18" Fill= "BLACK" >virtual machine container</text> </svg><script src= "Http://d3js.org/d3.v3.min.js" ></script><script>/* <circle cx= "Ten" cy= "r=" "fill=" #FF3420 "/> <circle cx=" 12"Cy=" "r=" "fill=" #7FFF00 "/><circle cx=" [cy=] "r=" fill= "#1E90FF"/> <!--rect x= "2" y= "wi" Dth= "height=" style= "Fill: #1E90FF"/--><image xlink:href= "http://photo.kaibei.com/Upfiles/BeyondPic/ Zixun/shumajishu/2009-01/20090110011644496.gif "x=" "y=" "height=" 50px "width=" 50px "/>*/var image_vswitch ="/ Assets/vnet/virtual_switch.png "; var image_vm ="/assets/vnet/virtual_machine.png "; var image_vmc ="/assets/vnet/ Virtual_machine_container.png "; var width = 960, height = 500;//var color = D3.scale.category20 (); var force = d3.layout. Force (). Charge ( -1000). Linkdistance (. Size ([width, height]). Gravity (0.25); var svg = D3.select ("Body"). Append ( "SVG"). attr ("width", width). attr ("height", height);d 3.json ("Http://localhost:3000/vnets/topodata.json", function (  Error, Graph) {//Do you want to change this address after you put it in Apach?  Force.nodes (Graph.nodes). Links (graph.links). Start ();      var link = svg.selectall (". Link"). Data (Graph.links). Enter (). Append ("line"). Style ("Stroke", "#C6E2FF"). Style ("stroke-opacity", "ten"). Style ("Stroke-width", "3"). attr (      "Class", "link");  . Style ("Stroke-width", function (d) {return math.sqrt (d.value);});   var node = svg.selectall (". Node"). Data (Graph.nodes);var nodeenter = Node.enter (). Append ("G"). att  R ("Class", "Node"). Call (Force.drag);  Nodeenter.append ("title"). Text (function (d) {return d.name;}); /*nodeenter.append ("Circle"). attr ("R", 8). Style ("Fill", function (d) {if (D.group = = 1) return "#FF342                                   0 ";   else if (D.group = = 2) return "#7FFF00"; else if (D.group = = 3) return "#1E90FF";});  */Nodeenter.append ("image"). attr ("width", "28px"). attr ("height", "28px"). attr ("Xlink:href", function (d) {                                   if (D.group = = 1) return image_vswitch;   else if (D.group = = 2) return IMAGE_VM; else if (D.group = = 3) return IMAGE_VMC;}); Nodeenter.append ("text"). attr ("dy", ". 85em"). Text (function (d) {return d.name;})  . attr ("Font-size", "13px")//font size. Style ("Fill", "#0A0A0A");        Force.on ("tick", function () {link.attr ("x1", function (d) {return d.source.x;})        . attr ("Y1", function (d) {return d.source.y;})        . attr ("X2", function (d) {return d.target.x;}) . attr ("Y2", function (d) {return d.target.y;}); /node.attr ("Transform", function (d) {return "translate (" + D.x + "," + D.y + ")";} ";                                         Node.attr ("Transform", function (d) {var dx = parsefloat (d.x)-13;                                         var dy = parsefloat (d.y)-12;   Return "translate (" + DX + "," + dy + ")";                               }); });}); </script>

  

About adding images to SVG, use JS under rails and use parsefloat to adjust the position of Force.on

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.