[D3] Create Chart Axes with D3 v4

Posted Answer1215

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[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);   
  

  

 

 

以上是关于[D3] Create Chart Axes with D3 v4的主要内容,如果未能解决你的问题,请参考以下文章

Excel VBA Chart 刻度 Axis.DisplayUnit

[D3] Build a Line Chart with D3 v4

D3 Chart SVG响应条和轴

d3 chart + jQuery DataTables:无法读取嵌套数组

使用 d3.js/chart.js/highcharts 在实际和预测散点图中的 R 平方最佳拟合线

D3.js 中Bubble Chart详解