首页 > 代码库 > d3 根据数据绘制svg
d3 根据数据绘制svg
var dataset = [ 5, 10, 15, 20, 25 ]; var circles = svg.selectAll("circle") .data(dataset) .enter() .append("circle"); circles.attr("cx", function(d, i) { return (i * 50) + 25; }) .attr("cy", h/2) .attr("r", function(d) { return d; });
根据dataset里面的数据设置svg circle的坐标以及半径
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>testD3-7-drawSVG.html</title> <script type="text/javascript" src=http://www.mamicode.com/"http://localhost:8080/spring/js/d3.js"></script> <style type="text/css"> </style> </head> <body> <script type="text/javascript"> // SVG尺寸 var w = 500; var h = 50; // 数据 var dataset = [ 5, 10, 15, 20, 25 ]; // 创建SVG容器 var svg = d3.select("body") .append("svg") .attr("width", 500) .attr("height", 50); // 创建圆 var circles = svg.selectAll("circle") .data(dataset) .enter() .append("circle"); // 根据数据设置每个圆的属性 circles.attr("cx", function(d, i) { return (i * 50) + 25; }) .attr("cy", h/2) .attr("r", function(d) { return d; }); </script> </body> </html>
d3 根据数据绘制svg
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。