[D3] 8. Margins
2015-06-04 15:40
274 查看
If you want ot add margins, should append graphics container in svg
Define the margin:
var svg = d3.select('#chartArea').append('svg') .attr('width', w + margin.left + margin.right) .attr('height', h + margin.top + margin.bottom) .append('g') //The last step is to add a G element which is a graphics container in SBG. .attr('transform', 'translate(' + margin.left + ', ' + margin.top + ')'); //Then offset that graphic element by our left and top margins.
Define the margin:
var dataset = _.map(_.range(30), function(num) { return Math.random() * 50; }), //reandom generate 15 data from 1 to 50 margin = {top: 10, bottom: 10, left: 10, right: 10}, w = 400 - margin.left - margin.right, h = 300 -margin.top - margin.bottom;
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title></title> <script src="../bower_components/underscore/underscore-min.js"></script> <script src="../ventor/d3.min.js"></script> <style type="text/css"> body { padding-top: 50px; padding-left: 100px; } #chartArea { width: 400px; height: 300px; background-color: #CCC; } .bar { display: inline-block; width: 20px; height: 75px; /* Gets overriden by D3-assigned height below */ margin-right: 2px; /* fill: teal; *//* SVG doesn't have background prop, use fill instead*/ z-index:99; } </style> </head> <body> <section id="chartArea"></section> <script> var dataset = _.map(_.range(30), function(num) { return Math.random() * 50; }), //reandom generate 15 data from 1 to 50 margin = {top: 10, bottom: 10, left: 10, right: 10}, w = 400 - margin.left - margin.right, h = 300 -margin.top - margin.bottom; var svg = d3.select('#chartArea').append('svg') .attr('width', w + margin.left + margin.right) .attr('height', h + margin.top + margin.bottom) .append('g') //The last step is to add a G element which is a graphics container in SBG. .attr('transform', 'translate(' + margin.left + ', ' + margin.top + ')'); //Then offset that graphic element by our left and top margins. var yScale = d3.scale.linear() .domain([0, d3.max(dataset) * 1.1]) //d3.max(dataset), set the max val of database .range([0, h]); var xScale = d3.scale.ordinal() .domain(dataset) .rangeBands([0,w],0.3, 0.1); // var colorScale = d3.scale.category20c(); var colorScale = d3.scale.quantile() .domain([d3.max(dataset) / 4, d3.max(dataset) / 2 , 3*d3.max(dataset) / 4, d3.max(dataset)]) .range(["#9c9ede","#6b6ecf","#5254a3", "#393b79"]); svg.selectAll('div') .data(dataset) .enter() .append('rect')// svg doesn't have div, use rect instead .attr('class', "bar") .attr('width', xScale.rangeBand()) .attr('x', function(each_data, index){ return xScale(each_data); }) .attr('y', function(each_data){ return h-yScale(each_data); }) .attr('height', function(each_data, i){ return yScale(each_data); }) .attr('fill', colorScale); </script> </body> </html>
相关文章推荐
- Unity3D使用SVN进行版本控制
- Swift _basics
- 无外网主机漏洞怎么破
- 数据结构与算法分析-索引
- 5. C#数据结构与算法 -- 非线性结构(树,二叉树,二叉查找树)
- Add Two Numbers
- poj 3254 状压DP
- html引用图片资源无法显示的问题
- C#日期控件datetimepicker保存空值的三种方法
- js日期时间函数(经典+完善+实用)
- iOS的UIButton和UILable
- 堆的基本操作:定义、创建、插入、删除、排序
- BZOJ2730 矿场搭建
- 如何在安装32位Oracle客户端组件的情况下以64位模式运行
- 2015年大一下第13周项目1-小玩文件
- CCNA学习指南 网络互联
- Process.StandardOutput.ReadToEnd()卡死假象(非卡死)
- Java Exlips 操作 Hadoop
- Building a Raspberry Pi 2 WebRTC camera
- 理解SQL Server中的权限体系(下)----安全对象和权限