D3.js --- 刻度 ticks()总结

刻度: ticks() 、tickSize() 、tickPadding()、 tickFormat()
  • ticks(10) 设置刻度的个数为10
  • tickSize(12) 设置刻度的长度为12px,默认6px
  • tickPadding(12) 设置刻度与数值之间的间隙为12px
  • tickFormat(d3.format(".0%")) 设置刻度下的数值的格式

图解

ticks.png
    const svgWidth = 400
    const svgHeight = 400
    const padding = {top:20,bottom:20,left:40,right:40}
    const xAxisLength = svgWidth-padding.left-padding.right
    const svg = d3.select(".learn-tick")
                  .append("svg")
                  .attr("width",svgWidth)
                  .attr("height",svgHeight)
    const scale = d3.scaleLinear()
                    .domain([0,1])
                    .range([0,xAxisLength])
    const axis = d3.axisBottom(scale)
                   .ticks(5) //控制坐标轴上的刻度个数
                   .tickSize(10) //控制刻度的大小
                   .tickPadding(5) //设置标签数字与坐标轴的距离
                   .tickFormat(d3.format(".0%")) //设置标签数字的格式
    svg.append("g").attr("transform", "translate(" + padding.left + ", " + padding.bottom + ")")
                    .attr("class","axis")
                    .call(axis)

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • d3 (核心部分)选择集d3.select - 从当前文档中选择一系列元素。d3.selectAll - 从当前文...
    谢大见阅读 3,497评论 1 4
  • 看完该API后,至少会画出一个简单的图形 D3.js 入门 1. d3-selection: 【 选择作图区域...
    Pretty_Boy阅读 1,034评论 0 1
  • D3是用于数据可视化的Javascript库。使用SVG,Canvas和HTML。结合强大的可视化技术和数据驱动的...
    Evelynzzz阅读 7,914评论 7 5
  • 1. d3.brush() //选中或者刷子 d3.brushX() d3.brushY() brush.ex...
    站着瞌睡阅读 1,540评论 0 0
  • Data Visualization with D3 D3: SVG中的jQurey 1. Add Documen...
    王策北阅读 791评论 0 2