[D3] Create Chart Axes with D3 v4
Most charts aren’t complete without axes to provide context and labeling for the graphical elements being displayed. This lesson introduces D3’s APIs for creating, customizing, and displaying axes while building on topics from previous lessons.
var margin = { top: 10, right: 20, bottom: 60, left: 25 }; var width = 425 - margin.left - margin.right; var height = 625 - margin.top - margin.bottom; var svg = d3.select('.chart') .append('svg') .attr('width', width + margin.left + margin.right) .attr('height', height + margin.top + margin.bottom) .append('g') .attr('transform', `translate(${margin.left}, ${margin.top})`); svg.append('rect') .attr('width', width) .attr('height', height) .style('fill', 'lightblue') .style('stroke', 'green'); /** * Create Y axis */ // Set scale const yScale = d3.scaleLinear() .domain([0, 100]) .range([height, 0]); // add y-axis const yAxis = d3.axisLeft(yScale); // const yAxis = d3.axisLeft(yScale).ticks(10, '.1s'); // If you want to add fine control about the ticks: // const yAxis = d3.axisLeft(yScale).tickValues([5,10,30,50,80,100]); // add to the svg svg.call(yAxis); /** * Create X axis */ const xScale = d3.scaleTime() .domain([new Date(2017, 6, 1), new Date(2017, 7, 1)]) .range([0, width]); //https://github.com/d3/d3-time const xAxis = d3.axisBottom(xScale) .ticks(d3.timeDay.every(4)) .tickSize(10) .tickPadding(15); svg.append('g') .attr('transform', `translate(0, ${height})`) .call(xAxis);
【推荐】国内首个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工具
2016-08-07 [React] Higher Order Components (replaces Mixins)
2014-08-07 [Backbone] Parse JSON on Collection
2014-08-07 [Backbone] Parse not formatted JSON code
2014-08-07 [Backbone]8. Level 7: Router and history