[D3] 4. d3.max
how to use d3.max
to normalize your dataset visually within the specific bounds of a variable domain.
<!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(15), function(num) { return Math.random() * 50; }), //reandom generate 15 data from 1 to 50 w = 400, h = 300; var svg = d3.select('#chartArea').append('svg') .attr('width', w) .attr('height', h); //svg deosn't need 'px' var yScale = d3.scale.linear() .domain([0, d3.max(dataset) * 1.1]) //d3.max(dataset), set the max val of database .range([0, h]); svg.selectAll('div') .data(dataset) .enter() .append('rect')// svg doesn't have div, use rect instead .attr('class', "bar") .attr('width', 20) .attr('x', function(each_data, index){ return index*22; }) .attr('y', function(each_data){ return h-yScale(each_data); }) .attr('height', function(each_data, i){ return yScale(each_data); }); </script> <!-- 1. svg should use 'fill' prop instead 'background-color' 2. svg width & height no need 'px' 3. attr(function(data_val, index){}) 4. create svg, d3.select('selector').append('svg').attr('width', xxx).attr('height', xx) 5. svg should use 'rect' instead of 'div' --> </body> </html>
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
2013-06-03 【PHP 】 伪静态 - 2. 使用apahce的rewrite机制(1) 开启rewrite模块,配置虚拟主机,对目录访问权限的修改
2013-06-03 【PHP】伪静态 - 1. 使用正则表达式实现
2013-06-03 【正则表达式 提高】环视,中文字处理